Files
CoopAllTheThings/common/include/coop/shared_memory.hpp
BlackMark cf058aecfa Phase 0: donor-launch spike foundation
Scaffold CoopAllTheThings: Remote Play Together for any XInput game via a
mirror app under a donor appid (real game keeps its own appid, so DRM,
achievements, and playtime stay intact).

- Build: CMake skeleton, ImGui + SafetyHook submodules (no vcpkg)
- common/: host<->hook IPC contract (seqlock pad state, shared-memory RAII)
- host/: borderless D3D11 window + ImGui overlay listing visible XInput pads,
  behind an InputSource interface (Steam Input slots in later)
- README documents the Phase 0 donor-launch validation procedure, anti-cheat
  limitation, and XInput/bitness constraints

Phase 0 validates the riskiest assumption (Steam RPT streams an arbitrary
window under a donor appid and routes guest input to it) before capture and
injection are built.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-18 22:48:31 +02:00

132 lines
2.4 KiB
C++

// Thin RAII wrapper over a Win32 file-mapping section used as the host<->hook
// IPC transport. Header-only so both modules share one implementation.
#pragma once
#include <string>
#include <utility>
#include <windows.h>
#include "coop/protocol.hpp"
namespace coop
{
class SharedMemory
{
public:
SharedMemory() = default;
SharedMemory(const SharedMemory&) = delete;
SharedMemory& operator=(const SharedMemory&) = delete;
SharedMemory(SharedMemory&& other) noexcept
{
*this = std::move(other);
}
SharedMemory& operator=(SharedMemory&& other) noexcept
{
if (this != &other)
{
reset();
mapping_ = std::exchange(other.mapping_, nullptr);
view_ = std::exchange(other.view_, nullptr);
size_ = std::exchange(other.size_, 0);
}
return *this;
}
~SharedMemory()
{
reset();
}
// Host side: create (or open if it already exists) the named section.
bool create(const std::wstring& name, std::size_t size)
{
reset();
mapping_ = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0,
static_cast<DWORD>(size), name.c_str());
if (mapping_ == nullptr)
{
return false;
}
return map(size);
}
// Hook side: open an existing section created by the host.
bool open(const std::wstring& name, std::size_t size)
{
reset();
mapping_ = OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, name.c_str());
if (mapping_ == nullptr)
{
return false;
}
return map(size);
}
void reset()
{
if (view_ != nullptr)
{
UnmapViewOfFile(view_);
view_ = nullptr;
}
if (mapping_ != nullptr)
{
CloseHandle(mapping_);
mapping_ = nullptr;
}
size_ = 0;
}
[[nodiscard]] bool valid() const
{
return view_ != nullptr;
}
template <typename T>
[[nodiscard]] T* as() const
{
return static_cast<T*>(view_);
}
[[nodiscard]] void* data() const
{
return view_;
}
[[nodiscard]] std::size_t size() const
{
return size_;
}
private:
bool map(std::size_t size)
{
view_ = MapViewOfFile(mapping_, FILE_MAP_ALL_ACCESS, 0, 0, size);
if (view_ == nullptr)
{
CloseHandle(mapping_);
mapping_ = nullptr;
return false;
}
size_ = size;
return true;
}
HANDLE mapping_ = nullptr;
void* view_ = nullptr;
std::size_t size_ = 0;
};
// Build the per-pid section name both sides agree on.
inline std::wstring shared_memory_name(unsigned long target_pid)
{
return std::wstring(kSharedMemoryPrefix) + std::to_wstring(target_pid);
}
} // namespace coop