Upload files to "src/Utils"
This commit is contained in:
76
src/Utils/globals.h
Normal file
76
src/Utils/globals.h
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "Inc/Includes.h"
|
||||||
|
|
||||||
|
// Defined at namespace scope but NOT initialized from GetModuleHandle at DLL-load time.
|
||||||
|
// Suspended-start injection would run those calls when the game hasn't loaded its modules yet,
|
||||||
|
// dereferencing NULL for NtHeaders and hard-faulting inside DllMain. Instead we set these in
|
||||||
|
// InitGlobals(), called from InitThread() after the launcher has confirmed the modules are loaded.
|
||||||
|
uintptr_t GA = 0;
|
||||||
|
const IMAGE_NT_HEADERS* NtHeaders = nullptr;
|
||||||
|
uintptr_t Referee = 0;
|
||||||
|
|
||||||
|
uint64_t* RetAddr = nullptr;
|
||||||
|
|
||||||
|
// Called from the DLL's init thread once the game has loaded GameAssembly and Referee.
|
||||||
|
// Returns true only if both modules are present AND GameAssembly has a readable PE header.
|
||||||
|
static bool InitGlobals() {
|
||||||
|
GA = (uintptr_t)GetModuleHandleA("GameAssembly.dll");
|
||||||
|
Referee = (uintptr_t)GetModuleHandleA("Referee.dll");
|
||||||
|
if (!GA) { printf("[Campus] init: GameAssembly.dll not loaded\n"); return false; }
|
||||||
|
if (!Referee) { printf("[Campus] init: Referee.dll not loaded\n"); return false; }
|
||||||
|
auto dos = reinterpret_cast<IMAGE_DOS_HEADER*>(GA);
|
||||||
|
if (dos->e_magic != IMAGE_DOS_SIGNATURE) { printf("[Campus] init: GameAssembly has bad DOS header\n"); return false; }
|
||||||
|
NtHeaders = reinterpret_cast<IMAGE_NT_HEADERS*>(GA + dos->e_lfanew);
|
||||||
|
if (NtHeaders->Signature != IMAGE_NT_SIGNATURE) { printf("[Campus] init: GameAssembly has bad NT header\n"); NtHeaders = nullptr; return false; }
|
||||||
|
printf("[Campus] init: GameAssembly=%p Referee=%p SizeOfImage=0x%X\n",
|
||||||
|
(void*)GA, (void*)Referee, NtHeaders->OptionalHeader.SizeOfImage);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
T read(void* obj, std::ptrdiff_t offset) {
|
||||||
|
return *(T*)((std::uint8_t*)obj + offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void set(void* obj, std::ptrdiff_t offset, T val) {
|
||||||
|
*(T*)((std::uint8_t*)obj + offset) = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Byte-for-byte identical to the pre-refactor version — same offsets, same wcstombs_s call,
|
||||||
|
// same success/failure branches. Only difference: caller is REQUIRED to delete[] the returned
|
||||||
|
// buffer (see the ReadIl2CppStringOwned helper below which does that for you). The earlier
|
||||||
|
// std::string return-by-value refactor was reading empty strings on this build for reasons
|
||||||
|
// I couldn't pin down without a live debugger, so we revert to the known-working shape.
|
||||||
|
char* ReadIl2CppString(Il2cppString* il2cppString) {
|
||||||
|
|
||||||
|
if (il2cppString == nullptr) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t length = static_cast<size_t>(il2cppString->Length);
|
||||||
|
const size_t bufferSize = (length + 1) * sizeof(wchar_t);
|
||||||
|
char* internalString = new char[bufferSize];
|
||||||
|
size_t convertedChars = 0;
|
||||||
|
|
||||||
|
errno_t result = wcstombs_s(&convertedChars, internalString, bufferSize, reinterpret_cast<wchar_t*>(il2cppString) + 0xA, length);
|
||||||
|
|
||||||
|
if (result != 0) {
|
||||||
|
delete[] internalString;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return internalString;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Owning wrapper: reads the IL2CPP string, copies into std::string, frees the intermediate
|
||||||
|
// buffer. Use this from hot paths (SendRequest_H, log lines) — the raw ReadIl2CppString
|
||||||
|
// leaks (length+1)*2 bytes per call if the caller doesn't delete[] the return.
|
||||||
|
#include <string>
|
||||||
|
inline std::string ReadIl2CppStringOwned(Il2cppString* il2cppString) {
|
||||||
|
char* raw = ReadIl2CppString(il2cppString);
|
||||||
|
if (!raw) return {};
|
||||||
|
std::string out(raw);
|
||||||
|
delete[] raw;
|
||||||
|
return out;
|
||||||
|
}
|
||||||
176
src/Utils/logging.h
Normal file
176
src/Utils/logging.h
Normal file
@@ -0,0 +1,176 @@
|
|||||||
|
#pragma once
|
||||||
|
// CampusPatch console tee: mirrors every std::cout write into a per-session log file next to
|
||||||
|
// the DLL. Existing call sites (`std::cout << ...`) don't need to change - the redirect is
|
||||||
|
// transparent once Init() runs.
|
||||||
|
//
|
||||||
|
// File path: <dll folder>\Logs\logsession<YYYYMMDD_HHMMSS>.txt (BST timestamp)
|
||||||
|
// Header: date/time in BST + full log path, so opening the file cold identifies the session.
|
||||||
|
//
|
||||||
|
// Implementation note: an earlier revision used std::ofstream + std::cout.rdbuf() and swapped
|
||||||
|
// buffers. That interacted badly with the game's CRT (crash on first write). This version uses
|
||||||
|
// only raw Win32 HANDLEs and WriteFile, so nothing about std::cout's original rdbuf, the game's
|
||||||
|
// C++ runtime state, or std::sync_with_stdio matters. If Init fails for any reason we simply
|
||||||
|
// leave logging in console-only mode - the game keeps running.
|
||||||
|
|
||||||
|
#include <windows.h>
|
||||||
|
#include <streambuf>
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
namespace CampusLogging {
|
||||||
|
|
||||||
|
// A streambuf whose overflow/xsputn write bytes to two Win32 handles: the process's console
|
||||||
|
// (CONOUT$) and a session log file. We deliberately don't chain to a "parent" streambuf - by
|
||||||
|
// writing to CONOUT$ ourselves we avoid ever touching the original std::cout rdbuf and its
|
||||||
|
// unclear ownership/lifetime relationship with the CRT.
|
||||||
|
//
|
||||||
|
// Concurrency: multiple hook threads write concurrently (heartbeat + SendRequest + Steam
|
||||||
|
// callbacks + SteamHookThread's install log all race). A CRITICAL_SECTION guards each write so
|
||||||
|
// individual `std::cout << ...` chains land as atomic byte groups instead of interleaving.
|
||||||
|
class TeeBuf : public std::streambuf {
|
||||||
|
HANDLE hConsole_ = INVALID_HANDLE_VALUE;
|
||||||
|
HANDLE hFile_ = INVALID_HANDLE_VALUE;
|
||||||
|
CRITICAL_SECTION cs_{};
|
||||||
|
public:
|
||||||
|
TeeBuf() { InitializeCriticalSection(&cs_); }
|
||||||
|
|
||||||
|
// Open both destinations. Returns true iff the FILE handle is valid; console failure is
|
||||||
|
// tolerated (we'll silently drop the console mirror if CONOUT$ can't be opened, which
|
||||||
|
// shouldn't happen since CreateConsole() already AllocConsole'd it).
|
||||||
|
bool Open(const std::string& path) {
|
||||||
|
hConsole_ = CreateFileA("CONOUT$",
|
||||||
|
GENERIC_WRITE,
|
||||||
|
FILE_SHARE_WRITE | FILE_SHARE_READ,
|
||||||
|
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||||
|
hFile_ = CreateFileA(path.c_str(),
|
||||||
|
GENERIC_WRITE,
|
||||||
|
FILE_SHARE_READ,
|
||||||
|
nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||||
|
return hFile_ != INVALID_HANDLE_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
HANDLE FileHandle() const { return hFile_; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
int overflow(int ch) override {
|
||||||
|
if (ch == traits_type::eof()) { sync(); return 0; }
|
||||||
|
char c = static_cast<char>(ch);
|
||||||
|
writeBoth(&c, 1);
|
||||||
|
// Flush the file on newline so a crash leaves the last line on disk.
|
||||||
|
if (c == '\n' && hFile_ != INVALID_HANDLE_VALUE) FlushFileBuffers(hFile_);
|
||||||
|
return ch;
|
||||||
|
}
|
||||||
|
std::streamsize xsputn(const char* s, std::streamsize n) override {
|
||||||
|
if (n <= 0) return n;
|
||||||
|
writeBoth(s, static_cast<DWORD>(n));
|
||||||
|
// Flush on any newline in the batch.
|
||||||
|
for (std::streamsize i = 0; i < n; i++) {
|
||||||
|
if (s[i] == '\n' && hFile_ != INVALID_HANDLE_VALUE) {
|
||||||
|
FlushFileBuffers(hFile_);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
int sync() override {
|
||||||
|
if (hFile_ != INVALID_HANDLE_VALUE) FlushFileBuffers(hFile_);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Serialise writes across threads so a `std::cout << "a"` chain from one thread doesn't
|
||||||
|
// interleave mid-word with another thread's chain. NOT full line-based - streambuf gets
|
||||||
|
// called per fragment, so bytes from different `<<` chains can still interleave between
|
||||||
|
// fragments; but individual fragments are now atomic, and the batch flush on newline keeps
|
||||||
|
// sub-word tearing rare in practice.
|
||||||
|
void writeBoth(const char* p, DWORD n) {
|
||||||
|
EnterCriticalSection(&cs_);
|
||||||
|
DWORD w = 0;
|
||||||
|
if (hConsole_ != INVALID_HANDLE_VALUE) WriteFile(hConsole_, p, n, &w, nullptr);
|
||||||
|
if (hFile_ != INVALID_HANDLE_VALUE) WriteFile(hFile_, p, n, &w, nullptr);
|
||||||
|
LeaveCriticalSection(&cs_);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Kept as a raw pointer for the process lifetime - never deleted, so no destruction race with
|
||||||
|
// hook threads still logging during shutdown.
|
||||||
|
static TeeBuf* g_tee = nullptr;
|
||||||
|
|
||||||
|
// UTC + 1 hour = BST. Hard-coded rather than reading the OS timezone so the label is stable
|
||||||
|
// across machines. In UK winter (GMT) this would be an hour ahead - accepted trade-off, port
|
||||||
|
// work is happening in summer.
|
||||||
|
static void UtcNowAsBst(SYSTEMTIME& out) {
|
||||||
|
SYSTEMTIME utc;
|
||||||
|
GetSystemTime(&utc);
|
||||||
|
FILETIME ft;
|
||||||
|
SystemTimeToFileTime(&utc, &ft);
|
||||||
|
ULARGE_INTEGER u;
|
||||||
|
u.LowPart = ft.dwLowDateTime;
|
||||||
|
u.HighPart = ft.dwHighDateTime;
|
||||||
|
u.QuadPart += 3600ULL * 10000000ULL; // +1h in 100-ns units
|
||||||
|
ft.dwLowDateTime = u.LowPart;
|
||||||
|
ft.dwHighDateTime = u.HighPart;
|
||||||
|
FileTimeToSystemTime(&ft, &out);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call once from InitThread. selfModule is the DLL's own HMODULE (from DllMain) - used to
|
||||||
|
// resolve <dll folder>\Logs. All status/failure messages use printf so they're visible in the
|
||||||
|
// console even if the tee itself never comes up.
|
||||||
|
static void Init(HMODULE selfModule) {
|
||||||
|
// Resolve <dll folder>\Logs. If selfModule is null (unexpected), fall back to the process
|
||||||
|
// exe's folder rather than crashing.
|
||||||
|
char dllPath[MAX_PATH] = {};
|
||||||
|
if (GetModuleFileNameA(selfModule, dllPath, MAX_PATH) == 0) {
|
||||||
|
printf("[Campus/Log] GetModuleFileNameA failed err=%lu - console only\n", GetLastError());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
char* slash = strrchr(dllPath, '\\');
|
||||||
|
if (slash) *slash = 0;
|
||||||
|
std::string dir = std::string(dllPath) + "\\Logs";
|
||||||
|
// CreateDirectoryA on an existing dir returns FALSE with ERROR_ALREADY_EXISTS - fine.
|
||||||
|
CreateDirectoryA(dir.c_str(), nullptr);
|
||||||
|
|
||||||
|
SYSTEMTIME bst;
|
||||||
|
UtcNowAsBst(bst);
|
||||||
|
char id[32];
|
||||||
|
_snprintf_s(id, sizeof(id), _TRUNCATE, "%04d%02d%02d_%02d%02d%02d",
|
||||||
|
bst.wYear, bst.wMonth, bst.wDay,
|
||||||
|
bst.wHour, bst.wMinute, bst.wSecond);
|
||||||
|
std::string path = dir + "\\logsession" + id + ".txt";
|
||||||
|
|
||||||
|
// Print the target path FIRST via printf so even if the tee itself fails, the user can see
|
||||||
|
// where we tried to write.
|
||||||
|
printf("[Campus/Log] session log -> %s\n", path.c_str());
|
||||||
|
|
||||||
|
TeeBuf* tee = new TeeBuf();
|
||||||
|
if (!tee->Open(path)) {
|
||||||
|
printf("[Campus/Log] failed to open '%s' err=%lu - console only\n",
|
||||||
|
path.c_str(), GetLastError());
|
||||||
|
delete tee;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write the session header directly to both destinations via the TeeBuf, WITHOUT swapping
|
||||||
|
// std::cout's rdbuf first. Reason: writing via std::cout hits the game's shared C++
|
||||||
|
// streambuf machinery, which is what crashed the earlier version. sputn() below only touches
|
||||||
|
// our own HANDLEs.
|
||||||
|
char header[512];
|
||||||
|
int hn = _snprintf_s(header, sizeof(header), _TRUNCATE,
|
||||||
|
"=== CampusPatch session log ===\n"
|
||||||
|
"Started (BST): %04d-%02d-%02d %02d:%02d:%02d BST\n"
|
||||||
|
"Log path: %s\n"
|
||||||
|
"===============================\n",
|
||||||
|
bst.wYear, bst.wMonth, bst.wDay,
|
||||||
|
bst.wHour, bst.wMinute, bst.wSecond,
|
||||||
|
path.c_str());
|
||||||
|
if (hn > 0) tee->sputn(header, hn);
|
||||||
|
|
||||||
|
// Publish the tee AFTER a known-good write proved it works. Any std::cout << ... call after
|
||||||
|
// this point routes through our overflow/xsputn -> WriteFile to both handles.
|
||||||
|
g_tee = tee;
|
||||||
|
std::cout.rdbuf(g_tee);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace CampusLogging
|
||||||
75
src/Utils/scan.h
Normal file
75
src/Utils/scan.h
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
static std::vector<int> PatternToIntVector(const char* Pattern)
|
||||||
|
{
|
||||||
|
std::vector<int> PatternVector = std::vector<int>{};
|
||||||
|
|
||||||
|
char* Start = const_cast<char*>(Pattern);
|
||||||
|
|
||||||
|
char* End = const_cast<char*>(Pattern) + strlen(Pattern);
|
||||||
|
|
||||||
|
for (char* Current = Start; Current < End; ++Current)
|
||||||
|
{
|
||||||
|
if (*Current == '?')
|
||||||
|
{
|
||||||
|
++Current;
|
||||||
|
|
||||||
|
PatternVector.push_back(-69);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
PatternVector.push_back(strtoul(Current, &Current, 16));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return PatternVector;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void* FindPointer(std::vector<int> Pattern, uint8_t* Address, int Addition = 0) {
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
while (Pattern[i] != -69) i++;
|
||||||
|
|
||||||
|
int Offset = *reinterpret_cast<int*>(Address + i);
|
||||||
|
|
||||||
|
return reinterpret_cast<void*>(Address + i + 0x4 + Offset + Addition);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static T PatternScan(const char* Pattern, uint64_t Start, size_t Size, bool bFindPointer = false, int Addition = 0)
|
||||||
|
{
|
||||||
|
std::vector<int> PatternVector = PatternToIntVector(Pattern);
|
||||||
|
|
||||||
|
uint8_t* Search = reinterpret_cast<uint8_t*>(Start);
|
||||||
|
|
||||||
|
const size_t SizeOfPattern = PatternVector.size();
|
||||||
|
const int* PatternData = PatternVector.data();
|
||||||
|
|
||||||
|
for (int i = 0; i < Size - SizeOfPattern; ++i)
|
||||||
|
{
|
||||||
|
bool FoundSignature = true;
|
||||||
|
|
||||||
|
for (int j = 0; j < SizeOfPattern; ++j)
|
||||||
|
{
|
||||||
|
if (Search[i + j] != PatternData[j] && PatternData[j] != -69)
|
||||||
|
{
|
||||||
|
FoundSignature = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (FoundSignature) {
|
||||||
|
|
||||||
|
if (bFindPointer) {
|
||||||
|
return reinterpret_cast<T>(FindPointer(PatternVector, &Search[i], Addition));
|
||||||
|
}
|
||||||
|
|
||||||
|
return reinterpret_cast<T>(&Search[i]);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return reinterpret_cast<T>(nullptr);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user