Skip to content

Commit

Permalink
Fix compatibility with new mods which use the C runtime of soku
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Hagb committed Jun 11, 2024
1 parent a40e3b1 commit afda4d5
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions swrstoys/dummy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit afda4d5

Please sign in to comment.