aboutsummaryrefslogtreecommitdiff
path: root/NorthstarDedicatedTest/hooks.cpp
diff options
context:
space:
mode:
authorBobTheBob <32057864+BobTheBob9@users.noreply.github.com>2021-07-08 15:33:31 +0100
committerBobTheBob <32057864+BobTheBob9@users.noreply.github.com>2021-07-08 15:33:31 +0100
commit4c4d605d10109e02708984755405bbe0947ef5c4 (patch)
tree1d5ffe7909b24b6c79fd444b420741e85f6c2b57 /NorthstarDedicatedTest/hooks.cpp
parent8dfb8e866119f653802609b24165b0458149c4cc (diff)
downloadNorthstarLauncher-4c4d605d10109e02708984755405bbe0947ef5c4.tar.gz
NorthstarLauncher-4c4d605d10109e02708984755405bbe0947ef5c4.zip
initial commit
Diffstat (limited to 'NorthstarDedicatedTest/hooks.cpp')
-rw-r--r--NorthstarDedicatedTest/hooks.cpp90
1 files changed, 90 insertions, 0 deletions
diff --git a/NorthstarDedicatedTest/hooks.cpp b/NorthstarDedicatedTest/hooks.cpp
new file mode 100644
index 00000000..a3be154d
--- /dev/null
+++ b/NorthstarDedicatedTest/hooks.cpp
@@ -0,0 +1,90 @@
+#include "pch.h"
+#include "hooks.h"
+#include "hookutils.h"
+#include "dedicated.h"
+#include "tier0.h"
+
+#include <wchar.h>
+#include <iostream>
+#include <vector>
+
+typedef HMODULE(*LoadLibraryExAType)(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags);
+HMODULE LoadLibraryExAHook(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags);
+
+typedef HMODULE(*LoadLibraryExWType)(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags);
+HMODULE LoadLibraryExWHook(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags);
+
+LoadLibraryExAType LoadLibraryExAOriginal;
+LoadLibraryExWType LoadLibraryExWOriginal;
+
+void InstallInitialHooks()
+{
+ //AllocConsole();
+
+ if (MH_Initialize() != MH_OK)
+ Error("MH_Initialize failed");
+
+ HookEnabler hook;
+ ENABLER_CREATEHOOK(hook, &LoadLibraryExA, &LoadLibraryExAHook, reinterpret_cast<LPVOID*>(&LoadLibraryExAOriginal));
+ ENABLER_CREATEHOOK(hook, &LoadLibraryExW, &LoadLibraryExWHook, reinterpret_cast<LPVOID*>(&LoadLibraryExWOriginal));
+}
+
+// dll load callback stuff
+// this allows for code to register callbacks to be run as soon as a dll is loaded, mainly to allow for patches to be made on dll load
+struct DllLoadCallback
+{
+ std::string dll;
+ DllLoadCallbackFuncType callback;
+ bool called;
+};
+
+std::vector<DllLoadCallback*> dllLoadCallbacks;
+
+void AddDllLoadCallback(std::string dll, DllLoadCallbackFuncType callback)
+{
+ DllLoadCallback* callbackStruct = new DllLoadCallback;
+ callbackStruct->dll = dll;
+ callbackStruct->callback = callback;
+ callbackStruct->called = false;
+
+ dllLoadCallbacks.push_back(callbackStruct);
+}
+
+HMODULE LoadLibraryExAHook(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
+{
+ HMODULE moduleAddress = LoadLibraryExAOriginal(lpLibFileName, hFile, dwFlags);
+
+ if (moduleAddress)
+ {
+ for (auto& callbackStruct : dllLoadCallbacks)
+ {
+ if (!callbackStruct->called && strstr(lpLibFileName + (strlen(lpLibFileName) - strlen(callbackStruct->dll.c_str())), callbackStruct->dll.c_str()) != nullptr)
+ {
+ callbackStruct->callback(moduleAddress);
+ callbackStruct->called = true;
+ }
+ }
+ }
+
+ return moduleAddress;
+}
+
+HMODULE LoadLibraryExWHook(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
+{
+ HMODULE moduleAddress = LoadLibraryExWOriginal(lpLibFileName, hFile, dwFlags);
+
+ if (moduleAddress)
+ {
+ for (auto& callbackStruct : dllLoadCallbacks)
+ {
+ const wchar_t* callbackDll = std::wstring(callbackStruct->dll.begin(), callbackStruct->dll.end()).c_str();
+ if (!callbackStruct->called && wcsstr(lpLibFileName + (wcslen(lpLibFileName) - wcslen(callbackDll)), callbackDll) != nullptr)
+ {
+ callbackStruct->callback(moduleAddress);
+ callbackStruct->called = true;
+ }
+ }
+ }
+
+ return moduleAddress;
+} \ No newline at end of file