Apply clang-format across the whole tree

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.
This commit is contained in:
2026-07-12 11:52:53 +02:00
parent c684a15fb9
commit 30eccf749d
155 changed files with 3333 additions and 6171 deletions

View File

@@ -23,8 +23,7 @@
#include "coop/shared_memory.hpp"
#include "coop/tool_paths.hpp"
namespace
{
namespace {
// coop_hook.dll ships in the deployable bin/<config>/ root; this probe runs from
// bin/<config>/tools/, so resolve next-to-self first, then one level up.
@@ -44,9 +43,8 @@ bool inject_via_helper(unsigned long pid, const std::wstring& dll_path)
{
const std::wstring helper = sibling_of(dll_path, L"coop_inject_x86.exe");
const std::wstring x86_dll = sibling_of(dll_path, L"coop_hook_x86.dll");
if (GetFileAttributesW(helper.c_str()) == INVALID_FILE_ATTRIBUTES ||
GetFileAttributesW(x86_dll.c_str()) == INVALID_FILE_ATTRIBUTES)
{
if (GetFileAttributesW(helper.c_str()) == INVALID_FILE_ATTRIBUTES
|| GetFileAttributesW(x86_dll.c_str()) == INVALID_FILE_ATTRIBUTES) {
std::printf("ERROR: x86 helper/dll missing next to the probe.\n");
return false;
}
@@ -55,8 +53,7 @@ bool inject_via_helper(unsigned long pid, const std::wstring& dll_path)
STARTUPINFOW si{};
si.cb = sizeof(si);
PROCESS_INFORMATION pi{};
if (!CreateProcessW(helper.c_str(), cmd.data(), nullptr, nullptr, FALSE, 0, nullptr, nullptr, &si, &pi))
{
if (!CreateProcessW(helper.c_str(), cmd.data(), nullptr, nullptr, FALSE, 0, nullptr, nullptr, &si, &pi)) {
std::printf("ERROR: CreateProcess(coop_inject_x86) failed (%lu).\n", GetLastError());
return false;
}
@@ -65,8 +62,7 @@ bool inject_via_helper(unsigned long pid, const std::wstring& dll_path)
GetExitCodeProcess(pi.hProcess, &code);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
if (code != 0)
{
if (code != 0) {
std::printf("ERROR: coop_inject_x86 reported failure (exit %lu).\n", code);
return false;
}
@@ -75,36 +71,31 @@ bool inject_via_helper(unsigned long pid, const std::wstring& dll_path)
bool inject(unsigned long pid, const std::wstring& dll_path)
{
if (GetFileAttributesW(dll_path.c_str()) == INVALID_FILE_ATTRIBUTES)
{
if (GetFileAttributesW(dll_path.c_str()) == INVALID_FILE_ATTRIBUTES) {
std::printf("ERROR: coop_hook.dll not found at the probe's directory.\n");
return false;
}
const DWORD access = PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION |
PROCESS_VM_WRITE | PROCESS_VM_READ;
const DWORD access =
PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ;
HANDLE process = OpenProcess(access, FALSE, pid);
if (process == nullptr)
{
if (process == nullptr) {
std::printf("ERROR: OpenProcess(%lu) failed (%lu). Run as administrator?\n", pid, GetLastError());
return false;
}
USHORT proc_machine = IMAGE_FILE_MACHINE_UNKNOWN, native_machine = IMAGE_FILE_MACHINE_UNKNOWN;
if (IsWow64Process2(process, &proc_machine, &native_machine) && proc_machine != IMAGE_FILE_MACHINE_UNKNOWN)
{
if (IsWow64Process2(process, &proc_machine, &native_machine) && proc_machine != IMAGE_FILE_MACHINE_UNKNOWN) {
CloseHandle(process);
return inject_via_helper(pid, dll_path);
}
const SIZE_T bytes = (dll_path.size() + 1) * sizeof(wchar_t);
void* remote = VirtualAllocEx(process, nullptr, bytes, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
bool ok = false;
if (remote != nullptr && WriteProcessMemory(process, remote, dll_path.c_str(), bytes, nullptr))
{
auto load_library = reinterpret_cast<LPTHREAD_START_ROUTINE>(
GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "LoadLibraryW"));
if (remote != nullptr && WriteProcessMemory(process, remote, dll_path.c_str(), bytes, nullptr)) {
auto load_library =
reinterpret_cast<LPTHREAD_START_ROUTINE>(GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "LoadLibraryW"));
HANDLE thread = CreateRemoteThread(process, nullptr, 0, load_library, remote, 0, nullptr);
if (thread != nullptr)
{
if (thread != nullptr) {
WaitForSingleObject(thread, INFINITE);
DWORD exit_code = 0;
GetExitCodeThread(thread, &exit_code);
@@ -112,13 +103,11 @@ bool inject(unsigned long pid, const std::wstring& dll_path)
ok = (exit_code != 0);
}
}
if (remote != nullptr)
{
if (remote != nullptr) {
VirtualFreeEx(process, remote, 0, MEM_RELEASE);
}
CloseHandle(process);
if (!ok)
{
if (!ok) {
std::printf("ERROR: injection failed (%lu).\n", GetLastError());
}
return ok;
@@ -128,8 +117,7 @@ bool inject(unsigned long pid, const std::wstring& dll_path)
int wmain(int argc, wchar_t** argv)
{
if (argc < 2)
{
if (argc < 2) {
std::printf("usage: coop_input_probe <pid> [seconds] [disable_mask]\n"
" Injects coop_hook.dll, reports one connected pad, and toggles a\n"
" button every second so the game processes a real state change.\n"
@@ -140,8 +128,7 @@ int wmain(int argc, wchar_t** argv)
const unsigned long pid = std::wcstoul(argv[1], nullptr, 10);
const int seconds = (argc >= 3) ? std::max(1, _wtoi(argv[2])) : 30;
const unsigned disable_mask = (argc >= 4) ? std::wcstoul(argv[3], nullptr, 0) : 0u;
if (pid == 0)
{
if (pid == 0) {
std::printf("ERROR: invalid pid.\n");
return 1;
}
@@ -149,8 +136,7 @@ int wmain(int argc, wchar_t** argv)
// 1) Input SharedBlock: report one connected pad up front (buttons still zero),
// so the game sees a controller arrive before we start pressing anything.
coop::SharedMemory ipc;
if (!ipc.create(coop::shared_memory_name(pid), sizeof(coop::SharedBlock)))
{
if (!ipc.create(coop::shared_memory_name(pid), sizeof(coop::SharedBlock))) {
std::printf("ERROR: create input mapping failed (%lu).\n", GetLastError());
return 1;
}
@@ -162,8 +148,7 @@ int wmain(int argc, wchar_t** argv)
// (0x1=input 0x2=focus 0x4=audio 0x8=video). Lets us bisect which injected
// subsystem freezes a given game.
static const char* kSubsysNames[] = {"input", "focus", "audio", "video", "mkb"};
for (std::uint32_t i = 0; i < coop::HookSubsys_Count; ++i)
{
for (std::uint32_t i = 0; i < coop::HookSubsys_Count; ++i) {
// MKB forwarding needs the host to stream events, which this probe doesn't, so
// keep it off here regardless of the mask (avoids confounding crash bisection).
const bool disabled = (i == coop::HookSubsys_Mkb) || (disable_mask & (1u << i)) != 0;
@@ -181,8 +166,7 @@ int wmain(int argc, wchar_t** argv)
coop::SharedMemory log_shm;
coop::LogRing* log_ring = nullptr;
std::uint64_t log_cursor = 0;
if (log_shm.create(coop::log_ring_name(pid), coop::log_ring_total_size(coop::kLogCapacity)))
{
if (log_shm.create(coop::log_ring_name(pid), coop::log_ring_total_size(coop::kLogCapacity))) {
log_ring = log_shm.as<coop::LogRing>();
coop::log_ring_init(*log_ring, coop::kLogCapacity);
}
@@ -190,29 +174,25 @@ int wmain(int argc, wchar_t** argv)
// Enable the hook's file trace for this session.
{
wchar_t dir[MAX_PATH] = {};
if (GetTempPathW(MAX_PATH, dir) != 0)
{
if (GetTempPathW(MAX_PATH, dir) != 0) {
const std::wstring sentinel = std::wstring(dir) + L"coop_hook.log.on";
HANDLE h = CreateFileW(sentinel.c_str(), GENERIC_WRITE, FILE_SHARE_READ, nullptr, OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL, nullptr);
if (h != INVALID_HANDLE_VALUE)
{
if (h != INVALID_HANDLE_VALUE) {
CloseHandle(h);
}
}
}
std::printf("Injecting coop_hook.dll into pid %lu ...\n", pid);
if (!inject(pid, dll_path_next_to_self()))
{
if (!inject(pid, dll_path_next_to_self())) {
return 1;
}
std::printf("Injected. Reporting pad 0 connected; toggling button A each second for %d s.\n", seconds);
std::printf("Hook trace: %%TEMP%%\\coop_hook.log\n\n");
const coop::HookStatus& status = block->status;
for (int t = 0; t < seconds; ++t)
{
for (int t = 0; t < seconds; ++t) {
// Toggle A (0x1000) every other second so the game's input layer sees a real
// edge -- this is the "press a button" event the crash report points at.
const bool press = (t % 2) == 1;
@@ -232,8 +212,7 @@ int wmain(int argc, wchar_t** argv)
static_cast<unsigned long long>(gc0), pads[0].buttons);
// Surface the hook's log lines as they arrive (shows where it got to).
if (log_ring != nullptr)
{
if (log_ring != nullptr) {
coop::log_ring_drain(*log_ring, log_cursor,
[](const coop::LogRecord& rec) { std::printf(" | %s\n", rec.text); });
}
@@ -241,8 +220,7 @@ int wmain(int argc, wchar_t** argv)
std::printf("\nInstalled hooks (%u):\n", status.hook_entry_count);
static const char* kSubsys[] = {"Input", "Focus", "Audio", "Video", "MKB"};
for (std::uint32_t i = 0; i < status.hook_entry_count && i < coop::kMaxHookEntries; ++i)
{
for (std::uint32_t i = 0; i < status.hook_entry_count && i < coop::kMaxHookEntries; ++i) {
const coop::HookEntry& e = status.hook_entries[i];
std::printf(" [%-5s] %-34s %s calls=%llu\n", e.subsystem < 5 ? kSubsys[e.subsystem] : "?", e.name,
e.installed ? "ON " : "off", static_cast<unsigned long long>(e.calls));