From 9944a1b03b9d4b8a814b7d79386277c8bd8f6d27 Mon Sep 17 00:00:00 2001 From: WarmUpTill <19472752+WarmUpTill@users.noreply.github.com> Date: Tue, 18 Mar 2025 19:40:11 +0100 Subject: [PATCH] Move interval reset handling --- lib/advanced-scene-switcher.cpp | 12 ++---------- lib/macro/macro-helpers.cpp | 4 +--- lib/switcher-data.cpp | 11 ----------- lib/switcher-data.hpp | 3 --- lib/utils/plugin-state-helpers.cpp | 19 +++++++++++++++++-- lib/utils/plugin-state-helpers.hpp | 3 ++- 6 files changed, 22 insertions(+), 30 deletions(-) diff --git a/lib/advanced-scene-switcher.cpp b/lib/advanced-scene-switcher.cpp index 9ba435dc..0204602e 100644 --- a/lib/advanced-scene-switcher.cpp +++ b/lib/advanced-scene-switcher.cpp @@ -307,7 +307,7 @@ void SwitcherData::Thread() } } - ResetForNextInterval(); + RunIntervalResetSteps(); if (match) { if (macroMatch) { @@ -357,14 +357,6 @@ void SwitcherData::SetPreconditions() InvalidateMacroTempVarValues(); } -void SwitcherData::ResetForNextInterval() -{ - // Plugin reset functions - for (const auto &func : resetIntervalSteps) { - func(); - } -} - bool SwitcherData::CheckForMatch(OBSWeakSource &scene, OBSWeakSource &transition, int &linger, bool &setPrevSceneAfterLinger, @@ -445,7 +437,7 @@ void AutoStartActionQueues(); void SwitcherData::Start() { if (!(th && th->isRunning())) { - ResetForNextInterval(); + RunIntervalResetSteps(); ResetMacros(); AutoStartActionQueues(); diff --git a/lib/macro/macro-helpers.cpp b/lib/macro/macro-helpers.cpp index eba31b9a..7afb8ed4 100644 --- a/lib/macro/macro-helpers.cpp +++ b/lib/macro/macro-helpers.cpp @@ -67,9 +67,7 @@ void SetMacroSwitchedScene(bool value) { static bool setupDone = false; if (!setupDone) { - // Will always be called with switcher lock already held - AddIntervalResetStep([]() { macroSceneSwitched = false; }, - false); + AddIntervalResetStep([]() { macroSceneSwitched = false; }); setupDone = true; } macroSceneSwitched = value; diff --git a/lib/switcher-data.cpp b/lib/switcher-data.cpp index 9308227a..0f20a437 100644 --- a/lib/switcher-data.cpp +++ b/lib/switcher-data.cpp @@ -168,17 +168,6 @@ void SwitcherData::SaveVersion(obs_data_t *obj, obs_data_set_string(obj, "version", currentVersion.c_str()); } -void SwitcherData::AddIntervalResetStep(std::function function, - bool tryLock) -{ - if (!tryLock) { - resetIntervalSteps.emplace_back(function); - return; - } - std::lock_guard lock(switcher->m); - resetIntervalSteps.emplace_back(function); -} - void SwitcherData::RunPostLoadSteps() { for (const auto &func : postLoadSteps) { diff --git a/lib/switcher-data.hpp b/lib/switcher-data.hpp index 0b83a6fb..c7a75333 100644 --- a/lib/switcher-data.hpp +++ b/lib/switcher-data.hpp @@ -57,11 +57,9 @@ public: bool AnySceneTransitionStarted(); void SetPreconditions(); - void ResetForNextInterval(); void AddSaveStep(std::function); void AddLoadStep(std::function); void AddPostLoadStep(std::function); - void AddIntervalResetStep(std::function, bool lock = true); void RunPostLoadSteps(); bool CheckForMatch(OBSWeakSource &scene, OBSWeakSource &transition, int &linger, bool &setPreviousSceneAsMatch, @@ -100,7 +98,6 @@ public: std::vector> saveSteps; std::vector> loadSteps; std::vector> postLoadSteps; - std::vector> resetIntervalSteps; bool firstBoot = true; bool transitionActive = false; diff --git a/lib/utils/plugin-state-helpers.cpp b/lib/utils/plugin-state-helpers.cpp index bfcf5e9e..c8b32bb4 100644 --- a/lib/utils/plugin-state-helpers.cpp +++ b/lib/utils/plugin-state-helpers.cpp @@ -21,6 +21,12 @@ static std::vector> &getPluginCleanupSteps() return steps; } +static std::vector> &getResetIntervalSteps() +{ + static std::vector> steps; + return steps; +} + static std::mutex mutex; void SavePluginSettings(obs_data_t *obj) @@ -48,9 +54,10 @@ void AddPostLoadStep(std::function step) GetSwitcher()->AddPostLoadStep(step); } -void AddIntervalResetStep(std::function step, bool lock) +void AddIntervalResetStep(std::function step) { - GetSwitcher()->AddIntervalResetStep(step, lock); + std::lock_guard lock(mutex); + getResetIntervalSteps().emplace_back(step); } void RunPostLoadSteps() @@ -100,6 +107,14 @@ void RunPluginCleanupSteps() } } +void RunIntervalResetSteps() +{ + std::lock_guard lock(mutex); + for (const auto &step : getResetIntervalSteps()) { + step(); + } +} + void StopPlugin() { GetSwitcher()->Stop(); diff --git a/lib/utils/plugin-state-helpers.hpp b/lib/utils/plugin-state-helpers.hpp index f5daa539..9a554039 100644 --- a/lib/utils/plugin-state-helpers.hpp +++ b/lib/utils/plugin-state-helpers.hpp @@ -11,7 +11,7 @@ EXPORT void LoadPluginSettings(obs_data_t *); EXPORT void AddSaveStep(std::function); EXPORT void AddLoadStep(std::function); EXPORT void AddPostLoadStep(std::function); -EXPORT void AddIntervalResetStep(std::function, bool lock = true); +EXPORT void AddIntervalResetStep(std::function); EXPORT void RunPostLoadSteps(); EXPORT void AddPluginInitStep(std::function); @@ -20,6 +20,7 @@ EXPORT void AddPluginCleanupStep(std::function); void RunPluginInitSteps(); extern "C" EXPORT void RunPluginPostLoadSteps(); void RunPluginCleanupSteps(); +void RunIntervalResetSteps(); EXPORT void StopPlugin(); EXPORT void StartPlugin();