CVarRegisterSpreadsheet/Hook/Hook.cpp

53 lines
2.1 KiB
C++
Raw Permalink Normal View History

#include "pch.h"
#include <Windows.h>
#include <detours.h>
#include <string>
#include <fstream>
#include <intrin.h>
int cvarCallIndex = 0;
FILE* csvFile;
std::string handleString(char* input)
{
if (input == 0) return ""; // return empty string if it was a nullptr
std::string replacement = input;
std::size_t found = replacement.find("\"");
while (found != std::string::npos) { // escape double quotes by doubling them
replacement.replace(found, 1, "\"\"");
found = replacement.find("\"", found + 2);
}
return replacement;
}
void* CVar__Register = (void*)0x00767fc0;
UINT32 __cdecl CVar__Register__Hook(char* name, char* description, UINT32 flags, char* defaultValue, PVOID callback, UINT32 category, BYTE saveToWTF, UINT32 param_8, BYTE param_9)
{
void* callAddress = (void*)((uintptr_t)_ReturnAddress() - 5); // subtracting the length of a CALL instruction to find out where we have been called from
fprintf_s(csvFile, R"(%s"%i", "%p", "%s", "%s", "%i", "%s", "%p", "%i", "%hhu", "%i", "%hhu")", "\n", cvarCallIndex, callAddress, handleString(name).c_str(), handleString(description).c_str(), flags, defaultValue, callback, category, saveToWTF, param_8, param_9);
cvarCallIndex++;
return ((decltype(&CVar__Register__Hook))CVar__Register)(name, description, flags, defaultValue, callback, category, saveToWTF, param_8, param_9);
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
if (DetourIsHelperProcess()) return TRUE;
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
fopen_s(&csvFile, "CVar__Register_calls_335a_12340.csv", "w");
if (csvFile == 0) return -1;
fprintf_s(csvFile, R"("Index", "Call Address", "Name", "Description", "Flags", "Default Value", "Callback Address", "Category", "Save To WTF", "param_8", "param_9")");
DetourRestoreAfterWith();
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&CVar__Register, CVar__Register__Hook);
if (DetourTransactionCommit() != NO_ERROR) MessageBoxA(NULL, "DetourAttach failed", "error", 0);
break;
case DLL_PROCESS_DETACH:
fclose(csvFile);
break;
}
return TRUE;
}