Run clang-format (the repo's .clang-format: LLVM base, 120 cols, tabs, Allman functions) over every source file so the tree is formatter-clean. Whitespace only -- no behavior change; full x64 + x86 suites pass. Also set SortIncludes: false in .clang-format. Windows include order is load-bearing (windows.h must precede tlhelp32.h / mmreg.h / xinput.h / dinput.h; winsock2.h must precede windows.h), and the default alphabetical sort reorders tlhelp32.h ahead of windows.h -- a build break. Leaving order alone keeps the manual, correct grouping.
116 lines
2.7 KiB
C++
116 lines
2.7 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);
|
|
}
|
|
|
|
// Name of the Present-hook shared backbuffer texture (created by the hook with
|
|
// CreateSharedHandle, opened by the host with OpenSharedResourceByName).
|
|
inline std::wstring video_share_name(unsigned long target_pid)
|
|
{
|
|
return std::wstring(kVideoSharePrefix) + std::to_wstring(target_pid);
|
|
}
|
|
|
|
} // namespace coop
|