aboutsummaryrefslogtreecommitdiff
path: root/primedev/core
diff options
context:
space:
mode:
authorJack <66967891+ASpoonPlaysGames@users.noreply.github.com>2024-08-18 11:21:06 +0100
committerGitHub <noreply@github.com>2024-08-18 12:21:06 +0200
commit5c730b0bb3ffcb5f6da9b15e05f54f778d86c23f (patch)
treee49ff21e6fccdadc7f122313756d350159d09cf0 /primedev/core
parenta28c1cb10cc805907d22b656ccc300b39b720a88 (diff)
downloadNorthstarLauncher-5c730b0bb3ffcb5f6da9b15e05f54f778d86c23f.tar.gz
NorthstarLauncher-5c730b0bb3ffcb5f6da9b15e05f54f778d86c23f.zip
Refactor DLL callbacks (#665)
Cherry-picked from primedev and slightly modified Co-authored-by: F1F7Y <filip.bartos07@proton.me>
Diffstat (limited to 'primedev/core')
-rw-r--r--primedev/core/hooks.cpp85
-rw-r--r--primedev/core/hooks.h28
2 files changed, 31 insertions, 82 deletions
diff --git a/primedev/core/hooks.cpp b/primedev/core/hooks.cpp
index fef8bbcf..5026f837 100644
--- a/primedev/core/hooks.cpp
+++ b/primedev/core/hooks.cpp
@@ -10,8 +10,6 @@
#include <filesystem>
#include <Psapi.h>
-#define XINPUT1_3_DLL "XInput1_3.dll"
-
namespace fs = std::filesystem;
AUTOHOOK_INIT()
@@ -392,87 +390,12 @@ void CallAllPendingDLLLoadCallbacks()
}
}
-// clang-format off
-AUTOHOOK_ABSOLUTEADDR(_LoadLibraryExA, (LPVOID)LoadLibraryExA,
-HMODULE, WINAPI, (LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags))
-// clang-format on
-{
- HMODULE moduleAddress;
-
- LPCSTR lpLibFileNameEnd = lpLibFileName + strlen(lpLibFileName);
- LPCSTR lpLibName = lpLibFileNameEnd - strlen(XINPUT1_3_DLL);
-
- // replace xinput dll with one that has ASLR
- if (lpLibFileName <= lpLibName && !strncmp(lpLibName, XINPUT1_3_DLL, strlen(XINPUT1_3_DLL) + 1))
- {
- moduleAddress = _LoadLibraryExA("XInput9_1_0.dll", hFile, dwFlags);
-
- if (!moduleAddress)
- {
- MessageBoxA(0, "Could not find XInput9_1_0.dll", "Northstar", MB_ICONERROR);
- exit(EXIT_FAILURE);
-
- return nullptr;
- }
- }
- else
- moduleAddress = _LoadLibraryExA(lpLibFileName, hFile, dwFlags);
-
- if (moduleAddress)
- {
- CallLoadLibraryACallbacks(lpLibFileName, moduleAddress);
- g_pPluginManager->InformDllLoad(moduleAddress, fs::path(lpLibFileName));
- }
-
- return moduleAddress;
-}
-
-// clang-format off
-AUTOHOOK_ABSOLUTEADDR(_LoadLibraryA, (LPVOID)LoadLibraryA,
-HMODULE, WINAPI, (LPCSTR lpLibFileName))
-// clang-format on
-{
- HMODULE moduleAddress = _LoadLibraryA(lpLibFileName);
-
- if (moduleAddress)
- CallLoadLibraryACallbacks(lpLibFileName, moduleAddress);
-
- return moduleAddress;
-}
-
-// clang-format off
-AUTOHOOK_ABSOLUTEADDR(_LoadLibraryExW, (LPVOID)LoadLibraryExW,
-HMODULE, WINAPI, (LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags))
-// clang-format on
-{
- HMODULE moduleAddress = _LoadLibraryExW(lpLibFileName, hFile, dwFlags);
-
- if (moduleAddress)
- CallLoadLibraryWCallbacks(lpLibFileName, moduleAddress);
-
- return moduleAddress;
-}
-
-// clang-format off
-AUTOHOOK_ABSOLUTEADDR(_LoadLibraryW, (LPVOID)LoadLibraryW,
-HMODULE, WINAPI, (LPCWSTR lpLibFileName))
-// clang-format on
-{
- HMODULE moduleAddress = _LoadLibraryW(lpLibFileName);
-
- if (moduleAddress)
- {
- CallLoadLibraryWCallbacks(lpLibFileName, moduleAddress);
- g_pPluginManager->InformDllLoad(moduleAddress, fs::path(lpLibFileName));
- }
-
- return moduleAddress;
-}
-
-void InstallInitialHooks()
+void HookSys_Init()
{
if (MH_Initialize() != MH_OK)
+ {
spdlog::error("MH_Initialize (minhook initialization) failed");
-
+ }
+ // todo: remove remaining instances of autohook in this file
AUTOHOOK_DISPATCH()
}
diff --git a/primedev/core/hooks.h b/primedev/core/hooks.h
index e5a65354..facf51bf 100644
--- a/primedev/core/hooks.h
+++ b/primedev/core/hooks.h
@@ -3,7 +3,33 @@
#include <string>
#include <iostream>
-void InstallInitialHooks();
+//-----------------------------------------------------------------------------
+// Purpose: Init minhook
+//-----------------------------------------------------------------------------
+void HookSys_Init();
+
+//-----------------------------------------------------------------------------
+// Purpose: MH_MakeHook wrapper
+// Input : *ppOriginal - Original function being detoured
+// pDetour - Detour function
+//-----------------------------------------------------------------------------
+inline void HookAttach(PVOID* ppOriginal, PVOID pDetour)
+{
+ PVOID pAddr = *ppOriginal;
+ if (MH_CreateHook(pAddr, pDetour, ppOriginal) == MH_OK)
+ {
+ if (MH_EnableHook(pAddr) != MH_OK)
+ {
+ spdlog::error("Failed enabling a function hook!");
+ }
+ }
+ else
+ {
+ spdlog::error("Failed creating a function hook!");
+ }
+}
+
+void CallLoadLibraryACallbacks(LPCSTR lpLibFileName, HMODULE moduleAddress);
typedef void (*DllLoadCallbackFuncType)(CModule moduleAddress);
void AddDllLoadCallback(std::string dll, DllLoadCallbackFuncType callback, std::string tag = "", std::vector<std::string> reliesOn = {});