Skip to content

Commit

Permalink
ChaosMod: Mitigate crashes for direct mod hot reloading hack
Browse files Browse the repository at this point in the history
  • Loading branch information
pongo1231 committed Feb 6, 2025
1 parent 5a388f1 commit da50455
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 11 deletions.
16 changes: 7 additions & 9 deletions ChaosMod/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,21 +342,16 @@ namespace Main
static HMODULE ms_ModuleHandle = NULL;
EXTERN_C IMAGE_DOS_HEADER __ImageBase;

void OnInit()
void OnRun()
{
LOG("Running mod init");
SetUnhandledExceptionFilter(CrashHandler);

if (DoesFileExist("ScriptHookV.dev"))
if (!ms_ModuleHandle && DoesFileExist("ScriptHookV.dev"))
{
WCHAR fileName[MAX_PATH] = {};
GetModuleFileName(reinterpret_cast<HINSTANCE>(&__ImageBase), fileName, MAX_PATH);
ms_ModuleHandle = LoadLibrary(fileName);
}
}

void OnRun()
{
SetUnhandledExceptionFilter(CrashHandler);

MainRun();
}
Expand Down Expand Up @@ -421,8 +416,11 @@ namespace Main
}
else if (key == 0x52 && ms_ModuleHandle) // R
{
// Prevention for (somehow) double unloading
auto handle = ms_ModuleHandle;
ms_ModuleHandle = NULL;
OnCleanup();
FreeModule(ms_ModuleHandle);
FreeModule(handle);
}
}

Expand Down
1 change: 0 additions & 1 deletion ChaosMod/Main.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ struct IDXGISwapChain;

namespace Main
{
void OnInit();
void OnRun();
void OnCleanup();
void OnKeyboardInput(DWORD key, WORD repeats, BYTE scanCode, BOOL isExtended, BOOL isWithAlt, BOOL wasDownBefore,
Expand Down
1 change: 0 additions & 1 deletion ChaosMod/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ BOOL APIENTRY DllMain(HMODULE instance, DWORD reason, LPVOID reserved)

RAW_LOG("Chaos Mod v" MOD_VERSION "\n\n");

Main::OnInit();
Memory::Init();

scriptRegister(instance, Main::OnRun);
Expand Down

0 comments on commit da50455

Please sign in to comment.