LuaFunctionRegisterSpreadsheet/Hook/Hook.cpp

53 lines
1.7 KiB
C++
Raw Normal View History

#include "pch.h"
#include <Windows.h>
#include <detours.h>
#include <string>
#include <fstream>
#include <intrin.h>
int luaCallIndex = 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*)0x00817F90;
UINT32 __cdecl FrameScript_RegisterFunction__Hook(char* name, void* param_2)
{
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", "%p")", "\n", luaCallIndex, callAddress, handleString(name).c_str(), param_2);
luaCallIndex++;
return ((decltype(&FrameScript_RegisterFunction__Hook))CVar__Register)(name, param_2);
}
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, "FrameScript_RegisterFunction_calls_335a_12340.csv", "w");
if (csvFile == 0) return -1;
fprintf_s(csvFile, R"("Index", "Register Address", "Name", "Function Address")");
DetourRestoreAfterWith();
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&CVar__Register, FrameScript_RegisterFunction__Hook);
if (DetourTransactionCommit() != NO_ERROR) MessageBoxA(NULL, "DetourAttach failed", "error", 0);
break;
case DLL_PROCESS_DETACH:
fclose(csvFile);
break;
}
return TRUE;
}