From afda4d59aa506dcb0e036be6e035ecba637fce76 Mon Sep 17 00:00:00 2001 From: Hagb Date: Wed, 12 Jun 2024 06:56:21 +0800 Subject: [PATCH] Fix compatibility with new mods which use the C runtime of soku This commit lets mods be loaded after the C runtime is initialized but before other static objects of soku are constructed. - Soku2 probably hooks constructors of static objects of soku. So it is too late to load Soku2 after these constructors are called. - Some new mods depend on the C runtime of soku. So they should be loaded after the C runtime of soku is initialized. `SetUnhandledExceptionFilter` is now called before mods are loaded, so that mods can easily get the unhandled exception filter of SWRSToys and set their own ones when being initialized. --- swrstoys/dummy.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/swrstoys/dummy.cpp b/swrstoys/dummy.cpp index e81d3d1..2e1aae8 100644 --- a/swrstoys/dummy.cpp +++ b/swrstoys/dummy.cpp @@ -25,7 +25,8 @@ FARPROC p_Direct3DCreate9Ex = NULL; HMODULE orig_module = NULL; HMODULE this_module = NULL; -int (*ogSecurityInitCookie)(); +typedef int (*PIFV)(void); +int (*ogInittermE)(PIFV* start, PIFV* end); int (*ogSokuMain)(int a, int b, int c, int d); LONG WINAPI UnhandledExFilter(PEXCEPTION_POINTERS ExPtr) @@ -82,14 +83,18 @@ LONG WINAPI UnhandledExFilter(PEXCEPTION_POINTERS ExPtr) int SokuMain(int a, int b, int c, int d) { - SetUnhandledExceptionFilter(UnhandledExFilter); return ogSokuMain(a, b, c, d); } -int mySecurityInitCookie() +int myInittermE(PIFV* start, PIFV* end) { - Hook(this_module); - return ogSecurityInitCookie(); + int ret = ogInittermE(start, end); + if (ret == 0) { + // if initialize successfully + SetUnhandledExceptionFilter(UnhandledExFilter); + Hook(this_module); + } + return ret; } BOOL APIENTRY DllMain(HMODULE this_module_, DWORD ul_reason_for_call, LPVOID) { @@ -125,7 +130,7 @@ BOOL APIENTRY DllMain(HMODULE this_module_, DWORD ul_reason_for_call, LPVOID) { VirtualProtect((PVOID)TEXT_SECTION_OFFSET, TEXT_SECTION_SIZE, PAGE_EXECUTE_WRITECOPY, &old); ogSokuMain = SokuLib::TamperNearJmpOpr(0x821844, SokuMain); - ogSecurityInitCookie = SokuLib::TamperNearJmpOpr(0x8218b2, mySecurityInitCookie); + ogInittermE = SokuLib::TamperNearJmpOpr(0x8240b3, myInittermE); VirtualProtect((PVOID)TEXT_SECTION_OFFSET, TEXT_SECTION_SIZE, old, &old); break; }