From 369d3c2b6516a0d39a8d2bffa2260bea0610c613 Mon Sep 17 00:00:00 2001
From: FunkyFr3sh <cc.red.alert.1@googlemail.com>
Date: Fri, 14 May 2021 01:24:46 +0200
Subject: [PATCH] move dinput hooks into hook.c

---
 inc/directinput.h |  2 --
 inc/hook.h        |  1 +
 src/directinput.c | 12 ------------
 src/dllmain.c     |  4 +---
 src/hook.c        |  9 +++++++++
 5 files changed, 11 insertions(+), 17 deletions(-)

diff --git a/inc/directinput.h b/inc/directinput.h
index c23ff7f..0940fb1 100644
--- a/inc/directinput.h
+++ b/inc/directinput.h
@@ -13,8 +13,6 @@ typedef HRESULT(WINAPI* DIDGETDEVICEDATAPROC)(IDirectInputDeviceA*, DWORD, LPDID
 extern DIRECTINPUTCREATEAPROC real_DirectInputCreateA;
 extern DIRECTINPUT8CREATEPROC real_DirectInput8Create;
 
-void dinput_hook();
-void dinput_unhook();
 HRESULT WINAPI fake_DirectInputCreateA(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUTA* lplpDirectInput, LPUNKNOWN punkOuter);
 HRESULT WINAPI fake_DirectInput8Create(HINSTANCE hinst, DWORD dwVersion, REFIID riidltf, LPDIRECTINPUT8* ppvOut, LPUNKNOWN punkOuter);
 
diff --git a/inc/hook.h b/inc/hook.h
index 08b837d..58aa2c7 100644
--- a/inc/hook.h
+++ b/inc/hook.h
@@ -64,6 +64,7 @@ extern int g_hook_method;
 extern BOOL g_hook_active;
 
 void hook_init();
+void hook_early_init();
 void hook_exit();
 void hook_patch_iat(HMODULE hmod, BOOL unhook, char* module_name, char* function_name, PROC new_function);
 void hook_patch_iat_list(HMODULE hmod, BOOL unhook, hook_list* hooks);
diff --git a/src/directinput.c b/src/directinput.c
index 6eff8e1..6a62fa5 100644
--- a/src/directinput.c
+++ b/src/directinput.c
@@ -111,15 +111,3 @@ HRESULT WINAPI fake_DirectInput8Create(HINSTANCE hinst, DWORD dwVersion, REFIID
 
     return result;
 }
-
-void dinput_hook()
-{
-    hook_patch_iat(GetModuleHandle(NULL), FALSE, "dinput.dll", "DirectInputCreateA", (PROC)fake_DirectInputCreateA);
-    hook_patch_iat(GetModuleHandle(NULL), FALSE, "dinput8.dll", "DirectInput8Create", (PROC)fake_DirectInput8Create);
-}
-
-void dinput_unhook()
-{
-    hook_patch_iat(GetModuleHandle(NULL), TRUE, "dinput.dll", "DirectInputCreateA", (PROC)fake_DirectInputCreateA);
-    hook_patch_iat(GetModuleHandle(NULL), TRUE, "dinput8.dll", "DirectInput8Create", (PROC)fake_DirectInput8Create);
-}
diff --git a/src/dllmain.c b/src/dllmain.c
index 1fa1575..170c624 100644
--- a/src/dllmain.c
+++ b/src/dllmain.c
@@ -7,7 +7,6 @@
 #include "ddclipper.h"
 #include "debug.h"
 #include "config.h"
-#include "directinput.h"
 #include "hook.h"
 
 
@@ -96,7 +95,7 @@ BOOL WINAPI DllMain(HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
         }
 
         timeBeginPeriod(1);
-        dinput_hook();
+        hook_early_init();
         break;
     }
     case DLL_PROCESS_DETACH:
@@ -107,7 +106,6 @@ BOOL WINAPI DllMain(HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
 
         timeEndPeriod(1);
         hook_exit();
-        dinput_unhook();
         break;
     }
     }
diff --git a/src/hook.c b/src/hook.c
index 14483f4..e944f2d 100644
--- a/src/hook.c
+++ b/src/hook.c
@@ -407,6 +407,12 @@ void hook_init()
     }
 }
 
+void hook_early_init()
+{
+    hook_patch_iat(GetModuleHandle(NULL), FALSE, "dinput.dll", "DirectInputCreateA", (PROC)fake_DirectInputCreateA);
+    hook_patch_iat(GetModuleHandle(NULL), FALSE, "dinput8.dll", "DirectInput8Create", (PROC)fake_DirectInput8Create);
+}
+
 void hook_exit()
 {
     if (g_hook_active)
@@ -436,4 +442,7 @@ void hook_exit()
 
         hook_revert((hook_list*)&g_hooks);
     }
+
+    hook_patch_iat(GetModuleHandle(NULL), TRUE, "dinput.dll", "DirectInputCreateA", (PROC)fake_DirectInputCreateA);
+    hook_patch_iat(GetModuleHandle(NULL), TRUE, "dinput8.dll", "DirectInput8Create", (PROC)fake_DirectInput8Create);
 }