Skip to content

Commit

Permalink
ChaosMod: Hack to make shv reload work without manual mod unloading
Browse files Browse the repository at this point in the history
  • Loading branch information
pongo1231 committed Feb 6, 2025
1 parent 1b48a8e commit 1bfdc81
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 19 deletions.
2 changes: 1 addition & 1 deletion ChaosMod/Components/EffectSound/EffectSound3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void EffectSound3D::FreeSounds()
void EffectSound3D::OnModPauseCleanup()
{
m_IsStopping = true;
m_PauseSoundsThread.detach();
m_PauseSoundsThread.join();

FreeSounds();

Expand Down
50 changes: 35 additions & 15 deletions ChaosMod/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ static struct
bool AntiSoftlockShortcutEnabled = false;
bool RunAntiSoftlock = false;
} ms_Flags;
static bool ms_ModDisabled = false;

static std::array<BYTE, 3> ParseConfigColorString(const std::string &colorText)
{
Expand Down Expand Up @@ -215,7 +216,7 @@ static void Init()

#undef INIT_COMPONENT

LOG("Completed init");
LOG("Completed init!");
}

static void MainRun()
Expand All @@ -231,18 +232,14 @@ static void MainRun()

ms_Flags.ToggleModState = g_OptionsManager.GetConfigValue({ "DisableStartup" }, OPTION_DEFAULT_DISABLE_STARTUP);

if (!g_Components.empty())
{
for (auto component : g_Components)
component->OnModPauseCleanup();
for (auto &component : g_Components)
component->OnModPauseCleanup();

g_Components.clear();
}
ClearEntityPool();

Init();

bool isDisabled = false;
ms_ModDisabled = false;

while (true)
{
Expand All @@ -261,9 +258,9 @@ static void MainRun()

if (ms_Flags.ToggleModState || ms_Flags.DisableMod)
{
if (!isDisabled)
if (!ms_ModDisabled)
{
isDisabled = true;
ms_ModDisabled = true;

LOG("Mod has been disabled");

Expand All @@ -286,7 +283,7 @@ static void MainRun()
}
else if (ms_Flags.ToggleModState)
{
isDisabled = false;
ms_ModDisabled = false;

if (DoesFeatureFlagExist("clearlogfileonreset"))
{
Expand All @@ -304,7 +301,7 @@ static void MainRun()
ms_Flags.DisableMod = false;
}

if (isDisabled)
if (ms_ModDisabled)
continue;

if (ms_Flags.ClearAllEffects)
Expand Down Expand Up @@ -335,13 +332,26 @@ static void MainRun()
continue;
}

for (auto component : g_Components)
for (auto &component : g_Components)
component->OnRun();
}
}

namespace Main
{
static HMODULE ms_ModuleHandle = NULL;
EXTERN_C IMAGE_DOS_HEADER __ImageBase;

void OnInit()
{
LOG("Running mod init");

WCHAR fileName[MAX_PATH] = {};
GetModuleFileName(reinterpret_cast<HINSTANCE>(&__ImageBase), fileName, MAX_PATH);

ms_ModuleHandle = LoadLibrary(fileName);
}

void OnRun()
{
SetUnhandledExceptionFilter(CrashHandler);
Expand All @@ -351,8 +361,13 @@ namespace Main

void OnCleanup()
{
for (auto component : g_Components)
component->OnModPauseCleanup();
LOG("Unloading mod");

if (!ms_ModDisabled)
for (auto component : g_Components)
component->OnModPauseCleanup();

LOG("Mod unload complete!");
}

void OnKeyboardInput(DWORD key, WORD repeats, BYTE scanCode, BOOL isExtended, BOOL isWithAlt, BOOL wasDownBefore,
Expand Down Expand Up @@ -402,6 +417,11 @@ namespace Main
if (ms_Flags.ToggleModShortcutEnabled)
ms_Flags.ToggleModState = true;
}
else if (key == 0x52 && DoesFileExist("ScriptHookV.dev")) // R
{
OnCleanup();
FreeModule(ms_ModuleHandle);
}
}

for (auto component : g_Components)
Expand Down
1 change: 1 addition & 0 deletions ChaosMod/Main.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class 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
4 changes: 3 additions & 1 deletion ChaosMod/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ BOOL APIENTRY DllMain(HMODULE instance, DWORD reason, LPVOID reserved)
switch (reason)
{
case DLL_PROCESS_ATTACH:
{
SetUnhandledExceptionFilter(CrashHandler);

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

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

scriptRegister(instance, Main::OnRun);

keyboardHandlerRegister(Main::OnKeyboardInput);

break;
}
case DLL_PROCESS_DETACH:
Main::OnCleanup();
Memory::Uninit();

scriptUnregister(instance);
Expand Down
4 changes: 2 additions & 2 deletions vendor/Patterns/Patterns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

#include "Patterns.h"

#define WIN32_LEAN_AND_MEAN
#include <algorithm>
#include <windows.h>

#include <algorithm>

#ifdef PATTERNS_USE_HINTS
#include <map>
#endif
Expand Down

0 comments on commit 1bfdc81

Please sign in to comment.