From fb664b6f081adc5768240db750dbb2a5ed59e685 Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Fri, 1 Dec 2023 23:51:11 +0100 Subject: [PATCH] Reduce dependencies to switcher-data.hpp implementation details This is necessary for the switch to libremidi version 4 due to incompatabilities with websocketpp, but also might save some headaches later on. --- CMakeLists.txt | 2 + src/general.cpp | 21 ++++--- src/legacy/switch-random.cpp | 3 +- src/macro-core/macro-action-plugin-state.cpp | 31 +++++----- .../macro-action-scene-collection.cpp | 4 +- src/macro-external/midi/midi-helpers.cpp | 6 +- src/macro-external/twitch/chat-connection.cpp | 5 +- src/macro-external/twitch/event-sub.cpp | 4 +- src/macro-external/twitch/token.cpp | 10 ++- src/switcher-data.cpp | 2 +- src/switcher-data.hpp | 4 +- src/utils/plugin-state-helper.cpp | 61 +++++++++++++++++++ src/utils/plugin-state-helper.hpp | 24 ++++++++ src/utils/temp-variable.cpp | 6 +- 14 files changed, 137 insertions(+), 46 deletions(-) create mode 100644 src/utils/plugin-state-helper.cpp create mode 100644 src/utils/plugin-state-helper.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index caada187..1f2a3f10 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -291,6 +291,8 @@ target_sources( src/utils/obs-module-helper.hpp src/utils/osc-helpers.cpp src/utils/osc-helpers.hpp + src/utils/plugin-state-helper.cpp + src/utils/plugin-state-helper.hpp src/utils/priority-helper.cpp src/utils/priority-helper.hpp src/utils/process-config.cpp diff --git a/src/general.cpp b/src/general.cpp index e56b1182..e570b375 100644 --- a/src/general.cpp +++ b/src/general.cpp @@ -33,7 +33,7 @@ void AdvSceneSwitcher::on_noMatchDontSwitch_clicked() } std::lock_guard lock(switcher->m); - switcher->switchIfNotMatching = SwitcherData::NoMatch::NO_SWITCH; + switcher->switchIfNotMatching = NoMatchBehavior::NO_SWITCH; ui->noMatchSwitchScene->setEnabled(false); ui->randomDisabledWarning->setVisible(true); } @@ -45,7 +45,7 @@ void AdvSceneSwitcher::on_noMatchSwitch_clicked() } std::lock_guard lock(switcher->m); - switcher->switchIfNotMatching = SwitcherData::NoMatch::SWITCH; + switcher->switchIfNotMatching = NoMatchBehavior::SWITCH; ui->noMatchSwitchScene->setEnabled(true); UpdateNonMatchingScene(ui->noMatchSwitchScene->currentText()); ui->randomDisabledWarning->setVisible(true); @@ -58,7 +58,7 @@ void AdvSceneSwitcher::on_noMatchRandomSwitch_clicked() } std::lock_guard lock(switcher->m); - switcher->switchIfNotMatching = SwitcherData::NoMatch::RANDOM_SWITCH; + switcher->switchIfNotMatching = NoMatchBehavior::RANDOM_SWITCH; ui->noMatchSwitchScene->setEnabled(false); ui->randomDisabledWarning->setVisible(false); } @@ -624,9 +624,9 @@ void SwitcherData::LoadGeneralSettings(obs_data_t *obj) interval = obs_data_get_int(obj, "interval"); obs_data_set_default_int(obj, "switch_if_not_matching", - static_cast(NoMatch::NO_SWITCH)); - switchIfNotMatching = - (NoMatch)obs_data_get_int(obj, "switch_if_not_matching"); + static_cast(NoMatchBehavior::NO_SWITCH)); + switchIfNotMatching = static_cast( + obs_data_get_int(obj, "switch_if_not_matching")); std::string nonMatchingSceneName = obs_data_get_string(obj, "non_matching_scene"); nonMatchingScene = GetWeakSourceByName(nonMatchingSceneName.c_str()); @@ -800,12 +800,13 @@ void SwitcherData::CheckNoMatchSwitch(bool &match, OBSWeakSource &scene, return; } - if (switchIfNotMatching == NoMatch::SWITCH && nonMatchingScene) { + if (switchIfNotMatching == NoMatchBehavior::SWITCH && + nonMatchingScene) { match = true; scene = nonMatchingScene; transition = nullptr; } - if (switchIfNotMatching == NoMatch::RANDOM_SWITCH) { + if (switchIfNotMatching == NoMatchBehavior::RANDOM_SWITCH) { match = checkRandom(scene, transition, sleep); } } @@ -956,11 +957,11 @@ void AdvSceneSwitcher::SetupGeneralTab() { PopulateSceneSelection(ui->noMatchSwitchScene, false); - if (switcher->switchIfNotMatching == SwitcherData::NoMatch::SWITCH) { + if (switcher->switchIfNotMatching == NoMatchBehavior::SWITCH) { ui->noMatchSwitch->setChecked(true); ui->noMatchSwitchScene->setEnabled(true); } else if (switcher->switchIfNotMatching == - SwitcherData::NoMatch::NO_SWITCH) { + NoMatchBehavior::NO_SWITCH) { ui->noMatchDontSwitch->setChecked(true); ui->noMatchSwitchScene->setEnabled(false); } else { diff --git a/src/legacy/switch-random.cpp b/src/legacy/switch-random.cpp index e81f4352..f210d315 100644 --- a/src/legacy/switch-random.cpp +++ b/src/legacy/switch-random.cpp @@ -143,8 +143,7 @@ void AdvSceneSwitcher::SetupRandomTab() border-radius: 7px; \ border-color: rgb(0,0,0,0) \ }"); - if (switcher->switchIfNotMatching != - SwitcherData::NoMatch::RANDOM_SWITCH) { + if (switcher->switchIfNotMatching != NoMatchBehavior::RANDOM_SWITCH) { if (!switcher->disableHints) { PulseWidget(ui->randomDisabledWarning, QColor(Qt::red), QColor(0, 0, 0, 0)); diff --git a/src/macro-core/macro-action-plugin-state.cpp b/src/macro-core/macro-action-plugin-state.cpp index 511c6ad1..ea22710f 100644 --- a/src/macro-core/macro-action-plugin-state.cpp +++ b/src/macro-core/macro-action-plugin-state.cpp @@ -1,8 +1,9 @@ #include "macro-action-plugin-state.hpp" -#include "switcher-data.hpp" +#include "plugin-state-helper.hpp" #include "utility.hpp" #include +#include #include using namespace std::chrono_literals; @@ -27,40 +28,38 @@ const static std::map actionTypes = { "AdvSceneSwitcher.action.pluginState.type.terminate"}, }; -const static std::map noMatchValues = { - {SwitcherData::NoMatch::NO_SWITCH, +const static std::map noMatchValues = { + {NoMatchBehavior::NO_SWITCH, "AdvSceneSwitcher.generalTab.generalBehavior.onNoMet.dontSwitch"}, - {SwitcherData::NoMatch::SWITCH, + {NoMatchBehavior::SWITCH, "AdvSceneSwitcher.generalTab.generalBehavior.onNoMet.switchTo"}, - {SwitcherData::NoMatch::RANDOM_SWITCH, + {NoMatchBehavior::RANDOM_SWITCH, "AdvSceneSwitcher.generalTab.generalBehavior.onNoMet.switchToRandom"}, }; static void stopPlugin() { - std::thread t([]() { switcher->Stop(); }); + std::thread t([]() { StopPlugin(); }); t.detach(); } static void importSettings(const std::string &path) { - if (switcher->settingsWindowOpened) { + if (SettingsWindowIsOpened()) { return; } - obs_data_t *obj = obs_data_create_from_json_file(path.c_str()); + OBSDataAutoRelease obj = obs_data_create_from_json_file(path.c_str()); if (!obj) { return; } - switcher->LoadSettings(obj); - obs_data_release(obj); + LoadPluginSettings(obj); } static void setNoMatchBehaviour(int value, OBSWeakSource &scene) { - switcher->switchIfNotMatching = - static_cast(value); - if (switcher->switchIfNotMatching == SwitcherData::NoMatch::SWITCH) { - switcher->nonMatchingScene = scene; + SetPluginNoMatchBehavior(static_cast(value)); + if (GetPluginNoMatchBehavior() == NoMatchBehavior::SWITCH) { + SetNoMatchScene(scene); } } @@ -342,8 +341,8 @@ void MacroActionPluginStateEdit::SetWidgetVisibility() break; case PluginStateAction::NO_MATCH_BEHAVIOUR: _values->show(); - if (static_cast(_entryData->_value) == - SwitcherData::NoMatch::SWITCH) { + if (static_cast(_entryData->_value) == + NoMatchBehavior::SWITCH) { _scenes->show(); } break; diff --git a/src/macro-core/macro-action-scene-collection.cpp b/src/macro-core/macro-action-scene-collection.cpp index 21df17f4..3ae798bf 100644 --- a/src/macro-core/macro-action-scene-collection.cpp +++ b/src/macro-core/macro-action-scene-collection.cpp @@ -1,5 +1,5 @@ #include "macro-action-scene-collection.hpp" -#include "switcher-data.hpp" +#include "plugin-state-helper.hpp" #include "utility.hpp" namespace advss { @@ -17,7 +17,7 @@ bool MacroActionSceneCollection::PerformAction() // Changing the scene collection will also reload the settings of the // scene switcher, so to avoid issues of not being able to change // settings ignore this action if the settings dialog is opened. - if (switcher->settingsWindowOpened) { + if (SettingsWindowIsOpened()) { return false; } obs_frontend_set_current_scene_collection(_sceneCollection.c_str()); diff --git a/src/macro-external/midi/midi-helpers.cpp b/src/macro-external/midi/midi-helpers.cpp index c9457b75..b6793805 100644 --- a/src/macro-external/midi/midi-helpers.cpp +++ b/src/macro-external/midi/midi-helpers.cpp @@ -1,15 +1,17 @@ #include "midi-helpers.hpp" #include +#include #include -#include +#include +#include namespace advss { static std::map, MidiDeviceInstance *> SetupMidiMessageVector() { - GetSwitcher()->AddIntervalResetStep( + AddIntervalResetStep( MidiDeviceInstance::ClearMessageBuffersOfAllDevices); return {}; } diff --git a/src/macro-external/twitch/chat-connection.cpp b/src/macro-external/twitch/chat-connection.cpp index d6571c4d..5dc408c5 100644 --- a/src/macro-external/twitch/chat-connection.cpp +++ b/src/macro-external/twitch/chat-connection.cpp @@ -3,7 +3,7 @@ #include "twitch-helpers.hpp" #include -#include +#include namespace advss { @@ -364,8 +364,7 @@ TwitchChatConnection::~TwitchChatConnection() static bool setupChatMessageClear() { - GetSwitcher()->AddIntervalResetStep( - &TwitchChatConnection::ClearAllMessages); + AddIntervalResetStep(&TwitchChatConnection::ClearAllMessages); return true; } diff --git a/src/macro-external/twitch/event-sub.cpp b/src/macro-external/twitch/event-sub.cpp index 9c7d5b53..e15887b0 100644 --- a/src/macro-external/twitch/event-sub.cpp +++ b/src/macro-external/twitch/event-sub.cpp @@ -3,7 +3,7 @@ #include "twitch-helpers.hpp" #include -#include +#include namespace advss { @@ -58,7 +58,7 @@ EventSub::~EventSub() static bool setupEventSubMessageClear() { - GetSwitcher()->AddIntervalResetStep(&EventSub::ClearAllEvents); + AddIntervalResetStep(&EventSub::ClearAllEvents); return true; } diff --git a/src/macro-external/twitch/token.cpp b/src/macro-external/twitch/token.cpp index 952895cf..c30e7e44 100644 --- a/src/macro-external/twitch/token.cpp +++ b/src/macro-external/twitch/token.cpp @@ -1,10 +1,14 @@ #include "token.hpp" #include "twitch-helpers.hpp" -#include +#include +#include +#include #include #include #include +#include +#include namespace advss { @@ -78,8 +82,8 @@ static void loadConnections(obs_data_t *obj); bool setupTwitchTokenSupport() { - GetSwitcher()->AddSaveStep(saveConnections); - GetSwitcher()->AddLoadStep(loadConnections); + AddSaveStep(saveConnections); + AddLoadStep(loadConnections); return true; } diff --git a/src/switcher-data.cpp b/src/switcher-data.cpp index b252bfa0..f12e3409 100644 --- a/src/switcher-data.cpp +++ b/src/switcher-data.cpp @@ -45,7 +45,7 @@ void SwitcherData::Prune() } if (nonMatchingScene && !WeakSourceValid(nonMatchingScene)) { - switchIfNotMatching = NoMatch::NO_SWITCH; + switchIfNotMatching = NoMatchBehavior::NO_SWITCH; nonMatchingScene = nullptr; } diff --git a/src/switcher-data.hpp b/src/switcher-data.hpp index fa1e57af..531a30da 100644 --- a/src/switcher-data.hpp +++ b/src/switcher-data.hpp @@ -21,6 +21,7 @@ #include "curl-helper.hpp" #include "priority-helper.hpp" #include "log-helper.hpp" +#include "plugin-state-helper.hpp" #include #include @@ -143,8 +144,7 @@ public: int interval = default_interval; OBSWeakSource nonMatchingScene; - enum class NoMatch { NO_SWITCH = 0, SWITCH = 1, RANDOM_SWITCH = 2 }; - NoMatch switchIfNotMatching = NoMatch::NO_SWITCH; + NoMatchBehavior switchIfNotMatching = NoMatchBehavior::NO_SWITCH; Duration noMatchDelay; enum class StartupBehavior { PERSIST = 0, START = 1, STOP = 2 }; StartupBehavior startupBehavior = StartupBehavior::PERSIST; diff --git a/src/utils/plugin-state-helper.cpp b/src/utils/plugin-state-helper.cpp new file mode 100644 index 00000000..9bb8b2b3 --- /dev/null +++ b/src/utils/plugin-state-helper.cpp @@ -0,0 +1,61 @@ +#include "plugin-state-helper.hpp" +#include "switcher-data.hpp" + +namespace advss { + +void LoadPluginSettings(obs_data_t *obj) +{ + GetSwitcher()->LoadSettings(obj); +} + +void AddSaveStep(std::function step) +{ + GetSwitcher()->AddSaveStep(step); +} + +void AddLoadStep(std::function step) +{ + GetSwitcher()->AddLoadStep(step); +} + +void AddPostLoadStep(std::function step) +{ + GetSwitcher()->AddPostLoadStep(step); +} + +void AddIntervalResetStep(std::function step) +{ + GetSwitcher()->AddIntervalResetStep(step); +} + +void StopPlugin() +{ + GetSwitcher()->Stop(); +} + +void StartPlugin() +{ + GetSwitcher()->Start(); +} + +void SetPluginNoMatchBehavior(NoMatchBehavior behavior) +{ + GetSwitcher()->switchIfNotMatching = behavior; +} + +void SetNoMatchScene(const OBSWeakSource &scene) +{ + GetSwitcher()->nonMatchingScene = scene; +} + +NoMatchBehavior GetPluginNoMatchBehavior() +{ + return GetSwitcher()->switchIfNotMatching; +} + +bool SettingsWindowIsOpened() +{ + return GetSwitcher()->settingsWindowOpened; +} + +} // namespace advss diff --git a/src/utils/plugin-state-helper.hpp b/src/utils/plugin-state-helper.hpp new file mode 100644 index 00000000..8e2148ab --- /dev/null +++ b/src/utils/plugin-state-helper.hpp @@ -0,0 +1,24 @@ +#pragma once +#include +#include + +namespace advss { + +void LoadPluginSettings(obs_data_t *); + +void AddSaveStep(std::function); +void AddLoadStep(std::function); +void AddPostLoadStep(std::function); +void AddIntervalResetStep(std::function); + +void StopPlugin(); +void StartPlugin(); + +enum class NoMatchBehavior { NO_SWITCH = 0, SWITCH = 1, RANDOM_SWITCH = 2 }; +void SetPluginNoMatchBehavior(NoMatchBehavior); +NoMatchBehavior GetPluginNoMatchBehavior(); +void SetNoMatchScene(const OBSWeakSource &); + +bool SettingsWindowIsOpened(); + +} // namespace advss diff --git a/src/utils/temp-variable.cpp b/src/utils/temp-variable.cpp index 4ba0559c..33c189bd 100644 --- a/src/utils/temp-variable.cpp +++ b/src/utils/temp-variable.cpp @@ -2,7 +2,7 @@ #include "advanced-scene-switcher.hpp" #include "macro.hpp" #include "macro-segment.hpp" -#include "switcher-data.hpp" +#include "plugin-state-helper.hpp" #include "utility.hpp" #include @@ -170,7 +170,7 @@ void TempVariableRef::Load(obs_data_t *obj, Macro *macro, const char *name) _id = obs_data_get_string(data, "id"); const auto type = static_cast(obs_data_get_int(data, "type")); - switcher->AddPostLoadStep([this, idx, type, macro]() { + AddPostLoadStep([this, idx, type, macro]() { this->PostLoad(idx, type, macro); }); } @@ -427,7 +427,7 @@ void NotifyUIAboutTempVarChange() obs_queue_task( OBS_TASK_UI, [](void *) { - if (!GetSwitcher()->settingsWindowOpened) { + if (!SettingsWindowIsOpened()) { return; } AdvSceneSwitcher::window->SegmentTempVarsChanged();