diff --git a/lib/general.cpp b/lib/general.cpp index 3e65440b..cfa94af2 100644 --- a/lib/general.cpp +++ b/lib/general.cpp @@ -451,16 +451,14 @@ void SwitcherData::LoadSettings(obs_data_t *obj) } // New post load steps to be declared during load - postLoadSteps.clear(); + ClearPostLoadSteps(); // Needs to be loaded before any entries which might rely on scene group // selections to be available. loadSceneGroups(obj); LoadVariables(obj); - for (const auto &func : loadSteps) { - func(obj); - } + RunLoadSteps(obj); LoadMacros(obj); LoadGlobalMacroSettings(obj); @@ -516,9 +514,7 @@ void SwitcherData::SaveSettings(obs_data_t *obj) SaveUISettings(obj); SaveVersion(obj, g_GIT_SHA1); - for (const auto &func : saveSteps) { - func(obj); - } + RunSaveSteps(obj); } void SwitcherData::SaveGeneralSettings(obs_data_t *obj) diff --git a/lib/switcher-data.cpp b/lib/switcher-data.cpp index bfc6fade..95cc0d8a 100644 --- a/lib/switcher-data.cpp +++ b/lib/switcher-data.cpp @@ -168,31 +168,6 @@ void SwitcherData::SaveVersion(obs_data_t *obj, obs_data_set_string(obj, "version", currentVersion.c_str()); } -void SwitcherData::RunPostLoadSteps() -{ - for (const auto &func : postLoadSteps) { - func(); - } - postLoadSteps.clear(); -} - -void SwitcherData::AddSaveStep(std::function function) -{ - std::lock_guard lock(switcher->m); - saveSteps.emplace_back(function); -} - -void SwitcherData::AddLoadStep(std::function function) -{ - std::lock_guard lock(switcher->m); - loadSteps.emplace_back(function); -} - -void SwitcherData::AddPostLoadStep(std::function function) -{ - postLoadSteps.emplace_back(function); -} - static void startHotkeyFunc(void *, obs_hotkey_id, obs_hotkey_t *, bool pressed) { if (pressed) { diff --git a/lib/switcher-data.hpp b/lib/switcher-data.hpp index 880b77cd..c76b9685 100644 --- a/lib/switcher-data.hpp +++ b/lib/switcher-data.hpp @@ -57,10 +57,6 @@ public: bool AnySceneTransitionStarted(); void SetPreconditions(); - void AddSaveStep(std::function); - void AddLoadStep(std::function); - void AddPostLoadStep(std::function); - void RunPostLoadSteps(); bool CheckForMatch(OBSWeakSource &scene, OBSWeakSource &transition, int &linger, bool &setPreviousSceneAsMatch, bool ¯oMatch); @@ -95,10 +91,6 @@ public: bool stop = false; std::condition_variable cv; - std::vector> saveSteps; - std::vector> loadSteps; - std::vector> postLoadSteps; - bool firstBoot = true; bool transitionActive = false; bool sceneCollectionStop = false; diff --git a/lib/utils/plugin-state-helpers.cpp b/lib/utils/plugin-state-helpers.cpp index c1c98ce3..4adb46f5 100644 --- a/lib/utils/plugin-state-helpers.cpp +++ b/lib/utils/plugin-state-helpers.cpp @@ -4,6 +4,10 @@ namespace advss { +static std::mutex initMutex; +static std::mutex postLoadMutex; +static std::mutex mutex; + static std::vector> &getPluginInitSteps() { static std::vector> steps; @@ -40,7 +44,23 @@ static std::vector> &getStopSteps() return steps; } -static std::mutex mutex; +static std::vector> &getSaveSteps() +{ + static std::vector> steps; + return steps; +} + +static std::vector> &getLoadSteps() +{ + static std::vector> steps; + return steps; +} + +static std::vector> &getPostLoadSteps() +{ + static std::vector> steps; + return steps; +} void SavePluginSettings(obs_data_t *obj) { @@ -54,17 +74,20 @@ void LoadPluginSettings(obs_data_t *obj) void AddSaveStep(std::function step) { - GetSwitcher()->AddSaveStep(step); + std::lock_guard lock(mutex); + getSaveSteps().emplace_back(step); } void AddLoadStep(std::function step) { - GetSwitcher()->AddLoadStep(step); + std::lock_guard lock(mutex); + getLoadSteps().emplace_back(step); } void AddPostLoadStep(std::function step) { - GetSwitcher()->AddPostLoadStep(step); + std::lock_guard lock(postLoadMutex); + getPostLoadSteps().emplace_back(step); } void AddIntervalResetStep(std::function step) @@ -73,20 +96,45 @@ void AddIntervalResetStep(std::function step) getResetIntervalSteps().emplace_back(step); } +void RunSaveSteps(obs_data_t *obj) +{ + std::lock_guard lock(mutex); + for (const auto &func : getSaveSteps()) { + func(obj); + } +} + +void RunLoadSteps(obs_data_t *obj) +{ + std::lock_guard lock(mutex); + for (const auto &func : getLoadSteps()) { + func(obj); + } +} + void RunPostLoadSteps() { - GetSwitcher()->RunPostLoadSteps(); + std::lock_guard lock(postLoadMutex); + for (const auto &func : getPostLoadSteps()) { + func(); + } +} + +void ClearPostLoadSteps() +{ + std::lock_guard lock(postLoadMutex); + getPostLoadSteps().clear(); } void AddPluginInitStep(std::function step) { - std::lock_guard lock(mutex); + std::lock_guard lock(initMutex); getPluginInitSteps().emplace_back(step); } void AddPluginPostLoadStep(std::function step) { - std::lock_guard lock(mutex); + std::lock_guard lock(initMutex); getPluginPostLoadSteps().emplace_back(step); } @@ -98,7 +146,7 @@ void AddPluginCleanupStep(std::function step) void RunPluginInitSteps() { - std::lock_guard lock(mutex); + std::lock_guard lock(initMutex); for (const auto &step : getPluginInitSteps()) { step(); } diff --git a/lib/utils/plugin-state-helpers.hpp b/lib/utils/plugin-state-helpers.hpp index 58afcbe0..e5f42110 100644 --- a/lib/utils/plugin-state-helpers.hpp +++ b/lib/utils/plugin-state-helpers.hpp @@ -12,7 +12,10 @@ EXPORT void AddSaveStep(std::function); EXPORT void AddLoadStep(std::function); EXPORT void AddPostLoadStep(std::function); EXPORT void AddIntervalResetStep(std::function); +void RunSaveSteps(obs_data_t *); +void RunLoadSteps(obs_data_t *); EXPORT void RunPostLoadSteps(); +void ClearPostLoadSteps(); EXPORT void AddPluginInitStep(std::function); EXPORT void AddPluginPostLoadStep(std::function);