Skip to content

Commit 5c67077

Browse files
committed
ChaosMod: Wait until threads have stopped
Fixes OnStop not being run on lua scripts properly when reloading the game
1 parent 0d7f9b9 commit 5c67077

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

ChaosMod/Components/EffectDispatcher.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "Components/EffectSound/EffectSoundManager.h"
77
#include "Components/MetaModifiers.h"
88
#include "Effects/EffectCategory.h"
9+
#include "Effects/EffectThreads.h"
910
#include "Effects/EffectTimedType.h"
1011
#include "Effects/EnabledEffects.h"
1112
#include "Effects/Register/RegisteredEffectsMetadata.h"
@@ -284,7 +285,18 @@ void EffectDispatcher::UpdateEffects(float deltaTime)
284285
m_PermanentEffects.clear();
285286
SharedState.DispatchedEffectsLog.clear();
286287

287-
EffectThreads::StopThreadsImmediately();
288+
static bool startedStopping = false;
289+
290+
if (!startedStopping)
291+
{
292+
EffectThreads::StopThreadsImmediately();
293+
startedStopping = true;
294+
}
295+
296+
if (EffectThreads::GetThreadCount() > 0)
297+
return;
298+
299+
startedStopping = false;
288300

289301
// Ensure player control isn't stuck in disabled state
290302
SET_PLAYER_CONTROL(PLAYER_ID(), true, 0);

ChaosMod/Effects/EffectThreads.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ namespace EffectThreads
7979
it = _StopThreadImmediately(it);
8080
}
8181

82+
int GetThreadCount()
83+
{
84+
return m_Threads.size();
85+
}
86+
8287
void PauseThisThread(DWORD timeMs)
8388
{
8489
auto fiber = GetCurrentFiber();

ChaosMod/Effects/EffectThreads.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ namespace EffectThreads
5555
bool DoesThreadExist(LPVOID threadId);
5656
bool HasThreadOnStartExecuted(LPVOID threadId);
5757

58+
int GetThreadCount();
59+
5860
bool IsThreadAnEffectThread();
5961

6062
EffectThreadSharedData *GetThreadSharedData(LPVOID threadId);

ChaosMod/Main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,17 @@ static void MainRun()
216216

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

219+
if (ComponentExists<EffectDispatcher>())
220+
{
221+
const auto dispatcher = GetComponent<EffectDispatcher>();
222+
dispatcher->ClearEffects();
223+
while (dispatcher->IsClearingEffects())
224+
{
225+
dispatcher->OnRun();
226+
WAIT(0);
227+
}
228+
}
229+
219230
for (auto &component : g_Components)
220231
component->OnModPauseCleanup();
221232

0 commit comments

Comments
 (0)