aboutsummaryrefslogtreecommitdiff
path: root/Hook/Hook.cpp
blob: 201bede7d7e5008524d0e63abe4db4306293911c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#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_3.3.5.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;
}