This commit is contained in:
2024-01-24 10:45:25 +01:00
commit bcdbe48523
267 changed files with 39510 additions and 0 deletions

95
src/runner/debugger.cpp Normal file
View File

@ -0,0 +1,95 @@
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include "debugger.hpp"
namespace
{
bool acquire_debug_privilege()
{
TOKEN_PRIVILEGES token_privileges;
ZeroMemory(&token_privileges, sizeof(token_privileges));
token_privileges.PrivilegeCount = 1;
HANDLE token;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &token))
{
return false;
}
if (!LookupPrivilegeValue(nullptr, SE_DEBUG_NAME, &token_privileges.Privileges[0].Luid))
{
CloseHandle(token);
return false;
}
token_privileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
DWORD size;
if (!AdjustTokenPrivileges(token, FALSE, &token_privileges, 0, nullptr, &size))
{
CloseHandle(token);
return false;
}
return CloseHandle(token) != FALSE;
}
}
debugger::debugger(const unsigned long process_id, const bool start)
{
if (!start)
{
return;
}
this->runner_ = std::thread([this, process_id]()
{
this->terminate_ = false;
this->run(process_id);
});
}
debugger::~debugger()
{
this->terminate_ = true;
if (this->runner_.joinable())
{
this->runner_.join();
}
}
void debugger::run(const unsigned long process_id) const
{
acquire_debug_privilege();
if (!DebugActiveProcess(process_id))
{
return;
}
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
DEBUG_EVENT event;
while (!this->terminate_ && WaitForDebugEvent(&event,INFINITE))
{
if (event.dwDebugEventCode == EXCEPTION_DEBUG_EVENT)
{
ContinueDebugEvent(event.dwProcessId, event.dwThreadId, DBG_EXCEPTION_NOT_HANDLED);
continue;
}
if (event.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT)
{
ContinueDebugEvent(event.dwProcessId, event.dwThreadId, DBG_CONTINUE);
break;
}
#ifdef DEV_BUILD
if (event.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT)
{
OutputDebugStringA("Debugger attached!\n");
}
#endif
ContinueDebugEvent(event.dwProcessId, event.dwThreadId, DBG_CONTINUE);
}
}

15
src/runner/debugger.hpp Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include <thread>
class debugger
{
public:
debugger(unsigned long process_id, bool start);
~debugger();
private:
volatile bool terminate_ = false;
std::thread runner_;
void run(unsigned long process_id) const;
};

100
src/runner/resource.rc Normal file
View File

@ -0,0 +1,100 @@
// Microsoft Visual C++ generated resource script.
//
#pragma code_page(65001)
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "windows.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// English (United States) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE
BEGIN
"#include ""windows.h""\r\n"
"\0"
END
2 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Version
//
VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,0,0,0
PRODUCTVERSION 1,0,0,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
#else
FILEFLAGS 0x0L
#endif
FILEOS 0x40004L
FILETYPE VFT_DLL
FILESUBTYPE 0x0L
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904b0"
BEGIN
VALUE "CompanyName", "AlterWare"
VALUE "FileDescription", "Steam mod runner"
VALUE "FileVersion", "1.0.0.0"
VALUE "InternalName", "Runner"
VALUE "LegalCopyright", "All rights reserved."
VALUE "OriginalFilename", "runner.exe"
VALUE "ProductName", "runner"
VALUE "ProductVersion", "1.0.0.0"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x409, 1200
END
END
/////////////////////////////////////////////////////////////////////////////
//
// Binary Data
//
102 ICON "../client/resources/icon.ico"
#endif // English (United States) resources
/////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED

26
src/runner/runner.cpp Normal file
View File

@ -0,0 +1,26 @@
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <cstdlib>
#include "debugger.hpp"
int __stdcall WinMain(HINSTANCE, HINSTANCE, PSTR, int)
{
const auto* const command = "-proc ";
const char* parent_proc = strstr(GetCommandLineA(), command);
if (parent_proc)
{
const auto pid = DWORD(atoi(parent_proc + strlen(command)));
auto* const process_handle = OpenProcess(SYNCHRONIZE, FALSE, pid);
if (process_handle)
{
//debugger _(pid, strstr(GetCommandLineA(), "-debug ") != nullptr);
WaitForSingleObject(process_handle, INFINITE);
CloseHandle(process_handle);
return 0;
}
}
return 1;
}