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>
This commit is contained in:
2026-06-18 22:48:31 +02:00
commit cf058aecfa
23 changed files with 1129 additions and 0 deletions

View File

@@ -0,0 +1,118 @@
// IPC contract shared between the host (coop_host.exe) and the injected hook
// DLL (coop_hook.dll). Both sides compile this identical header, so the memory
// layout must stay POD and version-locked.
#pragma once
#include <atomic>
#include <cstdint>
namespace coop
{
// Bump whenever the layout of SharedBlock or CoopPadState changes. The hook
// refuses to attach to a host with a mismatched version.
inline constexpr std::uint32_t kProtocolVersion = 1;
// 'COOP' little-endian, used to sanity-check the mapping before trusting it.
inline constexpr std::uint32_t kProtocolMagic = 0x504F4F43u;
// XInput exposes four controller slots; we mirror that fixed count.
inline constexpr std::uint32_t kMaxPads = 4;
// The shared-memory section is named per host process id so multiple sessions
// can coexist. Format with the target game's pid: coop_ipc_<pid>.
inline constexpr wchar_t kSharedMemoryPrefix[] = L"Local\\coop_ipc_";
// One controller's state, laid out to map 1:1 onto XINPUT_GAMEPAD plus the
// metadata the hook needs. Field names/types match XINPUT_GAMEPAD so the hook
// can memcpy the trailing region straight into an XINPUT_STATE.
struct CoopPadState
{
std::uint8_t connected; // 1 if a guest/host pad is mapped to this slot
std::uint8_t reserved[3];
std::uint32_t packet; // bumps on change -> XINPUT_STATE::dwPacketNumber
std::uint16_t buttons; // XINPUT_GAMEPAD_* bitmask
std::uint8_t left_trigger;
std::uint8_t right_trigger;
std::int16_t thumb_lx;
std::int16_t thumb_ly;
std::int16_t thumb_rx;
std::int16_t thumb_ry;
};
static_assert(sizeof(CoopPadState) == 20, "CoopPadState layout must stay stable across both modules");
// Top-level shared block. The host is the sole writer of pad state; the hook is
// the sole reader. A seqlock (even = stable, odd = write in progress) lets the
// reader grab a torn-free snapshot without a kernel lock on the hot path.
struct SharedBlock
{
std::uint32_t magic;
std::uint32_t version;
std::uint32_t pad_count; // number of populated slots, <= kMaxPads
std::atomic<std::uint32_t> sequence;
CoopPadState pads[kMaxPads];
// Phase 2 appends the shared-texture handle/dimensions control fields here;
// keep new members at the end so existing offsets never shift.
};
static_assert(std::atomic<std::uint32_t>::is_always_lock_free,
"seqlock requires a lock-free 32-bit atomic for cross-process use");
// --- Seqlock helpers -------------------------------------------------------
// Writer side: publish a fresh set of pad states. Called from the host.
inline void publish_pads(SharedBlock& block, const CoopPadState* pads, std::uint32_t count)
{
if (count > kMaxPads)
{
count = kMaxPads;
}
const std::uint32_t seq = block.sequence.load(std::memory_order_relaxed);
block.sequence.store(seq + 1, std::memory_order_release); // -> odd: write begins
std::atomic_thread_fence(std::memory_order_release);
block.pad_count = count;
for (std::uint32_t i = 0; i < count; ++i)
{
block.pads[i] = pads[i];
}
for (std::uint32_t i = count; i < kMaxPads; ++i)
{
block.pads[i] = CoopPadState{};
}
block.sequence.store(seq + 2, std::memory_order_release); // -> even: write done
}
// Reader side: copy a consistent snapshot. Called from the hook. Spins briefly
// if a write is in flight; bounded so a crashed writer can't hang the game.
inline bool read_pads(const SharedBlock& block, CoopPadState (&out)[kMaxPads], std::uint32_t& out_count)
{
for (int attempt = 0; attempt < 64; ++attempt)
{
const std::uint32_t before = block.sequence.load(std::memory_order_acquire);
if (before & 1u)
{
continue; // writer mid-update, retry
}
std::uint32_t count = block.pad_count;
if (count > kMaxPads)
{
count = kMaxPads;
}
for (std::uint32_t i = 0; i < kMaxPads; ++i)
{
out[i] = block.pads[i];
}
std::atomic_thread_fence(std::memory_order_acquire);
const std::uint32_t after = block.sequence.load(std::memory_order_acquire);
if (before == after)
{
out_count = count;
return true;
}
}
return false;
}
} // namespace coop

View File

@@ -0,0 +1,131 @@
// 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