diff --git a/lib/advanced-scene-switcher.cpp b/lib/advanced-scene-switcher.cpp index ffee766f..23209992 100644 --- a/lib/advanced-scene-switcher.cpp +++ b/lib/advanced-scene-switcher.cpp @@ -529,9 +529,7 @@ bool SwitcherData::AnySceneTransitionStarted() extern "C" EXPORT void FreeSceneSwitcher() { PlatformCleanup(); - for (const auto &cleanupStep : switcher->pluginCleanupSteps) { - cleanupStep(); - } + RunPluginCleanupSteps(); delete switcher; switcher = nullptr; @@ -761,13 +759,6 @@ QWidget *GetSettingsWindow() void SetupActionQueues(); -extern "C" EXPORT void RunPluginPostLoadSteps() -{ - for (const auto &postLoadStep : switcher->pluginPostLoadSteps) { - postLoadStep(); - } -} - extern "C" EXPORT void InitSceneSwitcher(obs_module_t *module, translateFunc translate) { @@ -781,9 +772,7 @@ extern "C" EXPORT void InitSceneSwitcher(obs_module_t *module, SetupDock(); SetupActionQueues(); - for (const auto &initStep : switcher->pluginInitSteps) { - initStep(); - } + RunPluginInitSteps(); obs_frontend_add_save_callback(SaveSceneSwitcher, nullptr); obs_frontend_add_event_callback(OBSEvent, switcher); diff --git a/lib/switcher-data.cpp b/lib/switcher-data.cpp index 877bec1e..9308227a 100644 --- a/lib/switcher-data.cpp +++ b/lib/switcher-data.cpp @@ -187,24 +187,6 @@ void SwitcherData::RunPostLoadSteps() postLoadSteps.clear(); } -void SwitcherData::AddPluginInitStep(std::function function) -{ - std::lock_guard lock(switcher->m); - pluginInitSteps.emplace_back(function); -} - -void SwitcherData::AddPluginPostLoadStep(std::function function) -{ - std::lock_guard lock(switcher->m); - pluginPostLoadSteps.emplace_back(function); -} - -void SwitcherData::AddPluginCleanupStep(std::function function) -{ - std::lock_guard lock(switcher->m); - pluginCleanupSteps.emplace_back(function); -} - void SwitcherData::AddSaveStep(std::function function) { std::lock_guard lock(switcher->m); diff --git a/lib/switcher-data.hpp b/lib/switcher-data.hpp index 1c74e61f..9c7e41c1 100644 --- a/lib/switcher-data.hpp +++ b/lib/switcher-data.hpp @@ -65,9 +65,6 @@ public: void AddPostLoadStep(std::function); void AddIntervalResetStep(std::function, bool lock = true); void RunPostLoadSteps(); - void AddPluginInitStep(std::function); - void AddPluginPostLoadStep(std::function); - void AddPluginCleanupStep(std::function); bool CheckForMatch(OBSWeakSource &scene, OBSWeakSource &transition, int &linger, bool &setPreviousSceneAsMatch, bool ¯oMatch); @@ -106,9 +103,6 @@ public: std::vector> loadSteps; std::vector> postLoadSteps; std::vector> resetIntervalSteps; - std::vector> pluginInitSteps; - std::vector> pluginPostLoadSteps; - std::vector> pluginCleanupSteps; bool firstBoot = true; bool transitionActive = false; diff --git a/lib/utils/plugin-state-helpers.cpp b/lib/utils/plugin-state-helpers.cpp index a02e3fda..bfcf5e9e 100644 --- a/lib/utils/plugin-state-helpers.cpp +++ b/lib/utils/plugin-state-helpers.cpp @@ -3,6 +3,26 @@ namespace advss { +static std::vector> &getPluginInitSteps() +{ + static std::vector> steps; + return steps; +} + +static std::vector> &getPluginPostLoadSteps() +{ + static std::vector> steps; + return steps; +} + +static std::vector> &getPluginCleanupSteps() +{ + static std::vector> steps; + return steps; +} + +static std::mutex mutex; + void SavePluginSettings(obs_data_t *obj) { GetSwitcher()->SaveSettings(obj); @@ -40,17 +60,44 @@ void RunPostLoadSteps() void AddPluginInitStep(std::function step) { - GetSwitcher()->AddPluginInitStep(step); + std::lock_guard lock(mutex); + getPluginInitSteps().emplace_back(step); } void AddPluginPostLoadStep(std::function step) { - GetSwitcher()->AddPluginPostLoadStep(step); + std::lock_guard lock(mutex); + getPluginPostLoadSteps().emplace_back(step); } void AddPluginCleanupStep(std::function step) { - GetSwitcher()->AddPluginCleanupStep(step); + std::lock_guard lock(mutex); + getPluginCleanupSteps().emplace_back(step); +} + +void RunPluginInitSteps() +{ + std::lock_guard lock(mutex); + for (const auto &step : getPluginInitSteps()) { + step(); + } +} + +void RunPluginPostLoadSteps() +{ + std::lock_guard lock(mutex); + for (const auto &step : getPluginPostLoadSteps()) { + step(); + } +} + +void RunPluginCleanupSteps() +{ + std::lock_guard lock(mutex); + for (const auto &step : getPluginCleanupSteps()) { + step(); + } } void StopPlugin() diff --git a/lib/utils/plugin-state-helpers.hpp b/lib/utils/plugin-state-helpers.hpp index 05116534..f5daa539 100644 --- a/lib/utils/plugin-state-helpers.hpp +++ b/lib/utils/plugin-state-helpers.hpp @@ -17,6 +17,9 @@ EXPORT void RunPostLoadSteps(); EXPORT void AddPluginInitStep(std::function); EXPORT void AddPluginPostLoadStep(std::function); EXPORT void AddPluginCleanupStep(std::function); +void RunPluginInitSteps(); +extern "C" EXPORT void RunPluginPostLoadSteps(); +void RunPluginCleanupSteps(); EXPORT void StopPlugin(); EXPORT void StartPlugin();