Remove websocket dependency to switcher-data internals

This commit is contained in:
WarmUpTill 2023-11-17 23:20:56 +01:00 committed by WarmUpTill
parent fb664b6f08
commit d7c58b3eb3
8 changed files with 81 additions and 45 deletions

View File

@ -347,12 +347,8 @@ void SwitcherData::SetPreconditions()
InvalidateMacroTempVarValues();
}
void ClearWebsocketMessages();
void SwitcherData::ResetForNextInterval()
{
// Core reset functions
ClearWebsocketMessages();
// Plugin reset functions
for (const auto &func : resetIntervalSteps) {
func();
@ -776,6 +772,9 @@ void OpenSettingsWindow()
}
}
void SetupConnectionManager();
void SetupWebsocketHelpers();
extern "C" void InitSceneSwitcher(obs_module_t *module, translateFunc translate)
{
blog(LOG_INFO, "version: %s", g_GIT_TAG);
@ -786,6 +785,8 @@ extern "C" void InitSceneSwitcher(obs_module_t *module, translateFunc translate)
PlatformInit();
LoadPlugins();
SetupDock();
SetupConnectionManager();
SetupWebsocketHelpers();
obs_frontend_add_save_callback(SaveSceneSwitcher, nullptr);
obs_frontend_add_event_callback(OBSEvent, switcher);

View File

@ -506,7 +506,6 @@ void SwitcherData::LoadSettings(obs_data_t *obj)
// selections to be available.
loadSceneGroups(obj);
LoadVariables(obj);
LoadConnections(obj);
for (const auto &func : loadSteps) {
func(obj);
@ -549,7 +548,6 @@ void SwitcherData::SaveSettings(obs_data_t *obj)
saveSceneGroups(obj);
SaveMacros(obj);
SaveConnections(obj);
SaveVariables(obj);
saveWindowTitleSwitches(obj);
saveScreenRegionSwitches(obj);

View File

@ -34,28 +34,24 @@ static bool matchRegex(const RegexConfig &conf, const std::string &msg,
bool MacroConditionWebsocket::CheckCondition()
{
const std::vector<std::string> *messages = nullptr;
std::vector<std::string> messages;
switch (_type) {
case MacroConditionWebsocket::Type::REQUEST:
messages = &switcher->websocketMessages;
messages = GetWebsocketMessages();
break;
case MacroConditionWebsocket::Type::EVENT: {
auto connection = _connection.lock();
if (!connection) {
return false;
}
messages = &connection->Events();
messages = connection->Events();
break;
}
default:
break;
}
if (!messages) {
return false;
}
for (const auto &msg : *messages) {
for (const auto &msg : messages) {
if (_regex.Enabled()) {
if (matchRegex(_regex, msg, _message)) {
SetVariableValue(msg);

View File

@ -81,7 +81,6 @@ public:
void SaveSettings(obs_data_t *obj);
void SaveMacros(obs_data_t *obj);
void SaveConnections(obs_data_t *obj);
void SaveVariables(obs_data_t *obj);
void SaveGeneralSettings(obs_data_t *obj);
void SaveHotkeys(obs_data_t *obj);
@ -90,7 +89,6 @@ public:
void LoadSettings(obs_data_t *obj);
void LoadMacros(obs_data_t *obj);
void LoadConnections(obs_data_t *obj);
void LoadVariables(obs_data_t *obj);
void LoadGeneralSettings(obs_data_t *obj);
void LoadHotkeys(obs_data_t *obj);
@ -175,8 +173,6 @@ public:
bool macroSceneSwitched = false;
Curlhelper curl;
std::deque<std::shared_ptr<Item>> connections;
std::vector<std::string> websocketMessages;
std::deque<std::shared_ptr<Item>> variables;
std::string lastTitle;

View File

@ -1,18 +1,33 @@
#include "connection-manager.hpp"
#include "utility.hpp"
#include "obs-module-helper.hpp"
#include "plugin-state-helper.hpp"
#include "name-dialog.hpp"
#include "switcher-data.hpp"
#include <algorithm>
#include <QAction>
#include <QMenu>
#include <obs-module.h>
Q_DECLARE_METATYPE(advss::Connection *);
namespace advss {
void SwitcherData::SaveConnections(obs_data_t *obj)
static std::deque<std::shared_ptr<Item>> connections;
static void saveConnections(obs_data_t *obj);
static void loadConnections(obs_data_t *obj);
void SetupConnectionManager()
{
static bool done = false;
if (done) {
return;
}
AddSaveStep(saveConnections);
AddLoadStep(loadConnections);
done = true;
}
static void saveConnections(obs_data_t *obj)
{
obs_data_array_t *connectionArray = obs_data_array_create();
for (const auto &c : connections) {
@ -25,7 +40,7 @@ void SwitcherData::SaveConnections(obs_data_t *obj)
obs_data_array_release(connectionArray);
}
void SwitcherData::LoadConnections(obs_data_t *obj)
static void loadConnections(obs_data_t *obj)
{
connections.clear();
@ -192,7 +207,7 @@ Connection *GetConnectionByName(const QString &name)
Connection *GetConnectionByName(const std::string &name)
{
for (auto &con : switcher->connections) {
for (auto &con : connections) {
if (con->Name() == name) {
return dynamic_cast<Connection *>(con.get());
}
@ -202,7 +217,7 @@ Connection *GetConnectionByName(const std::string &name)
std::weak_ptr<Connection> GetWeakConnectionByName(const std::string &name)
{
for (const auto &c : switcher->connections) {
for (const auto &c : connections) {
if (c->Name() == name) {
std::weak_ptr<Connection> wp =
std::dynamic_pointer_cast<Connection>(c);
@ -226,6 +241,11 @@ std::string GetWeakConnectionName(std::weak_ptr<Connection> connection)
return con->Name();
}
std::deque<std::shared_ptr<Item>> &GetConnections()
{
return connections;
}
static bool ConnectionNameAvailable(const QString &name)
{
return !GetConnectionByName(name);
@ -244,8 +264,7 @@ static bool AskForSettingsWrapper(QWidget *parent, Item &settings)
}
ConnectionSelection::ConnectionSelection(QWidget *parent)
: ItemSelection(switcher->connections, Connection::Create,
AskForSettingsWrapper,
: ItemSelection(connections, Connection::Create, AskForSettingsWrapper,
"AdvSceneSwitcher.connection.select",
"AdvSceneSwitcher.connection.add",
"AdvSceneSwitcher.item.nameNotAvailable",
@ -296,7 +315,7 @@ void ConnectionSelection::SetConnection(
ConnectionSettingsDialog::ConnectionSettingsDialog(QWidget *parent,
const Connection &settings)
: ItemSettingsDialog(settings, switcher->connections,
: ItemSettingsDialog(settings, connections,
"AdvSceneSwitcher.connection.select",
"AdvSceneSwitcher.connection.add",
"AdvSceneSwitcher.item.nameNotAvailable", parent),

View File

@ -1,5 +1,6 @@
#pragma once
#include "item-selection-helpers.hpp"
#include "websocket-helpers.hpp"
#include <QComboBox>
#include <QPushButton>
@ -14,10 +15,11 @@
#include <QGridLayout>
#include <deque>
#include <obs.hpp>
#include <websocket-helpers.hpp>
namespace advss {
void SetupConnectionManager();
class ConnectionSelection;
class ConnectionSettingsDialog;
@ -69,6 +71,7 @@ Connection *GetConnectionByName(const std::string &);
std::weak_ptr<Connection> GetWeakConnectionByName(const std::string &name);
std::weak_ptr<Connection> GetWeakConnectionByQString(const QString &name);
std::string GetWeakConnectionName(std::weak_ptr<Connection>);
std::deque<std::shared_ptr<Item>> &GetConnections();
class ConnectionSettingsDialog : public ItemSettingsDialog {
Q_OBJECT

View File

@ -1,6 +1,8 @@
#include "websocket-helpers.hpp"
#include "connection-manager.hpp"
#include "switcher-data.hpp"
#include "log-helper.hpp"
#include "plugin-state-helper.hpp"
#include "sync-helper.hpp"
#include <QCryptographicHash>
#include <obs-websocket-api.h>
@ -13,16 +15,38 @@ using websocketpp::lib::bind;
#define RPC_VERSION 1
constexpr char VendorName[] = "AdvancedSceneSwitcher";
constexpr char VendorRequest[] = "AdvancedSceneSwitcherMessage";
constexpr char VendorEvent[] = "AdvancedSceneSwitcherEvent";
obs_websocket_vendor vendor;
void ClearWebsocketMessages()
static void clearWebsocketMessages();
static std::vector<std::string> websocketMessages;
void SetupWebsocketHelpers()
{
switcher->websocketMessages.clear();
for (auto &connection : switcher->connections) {
Connection *c = dynamic_cast<Connection *>(connection.get());
if (c) {
c->Events().clear();
static bool done = false;
if (done) {
return;
}
AddIntervalResetStep(clearWebsocketMessages);
done = true;
}
std::vector<std::string> &GetWebsocketMessages()
{
return websocketMessages;
}
static void clearWebsocketMessages()
{
websocketMessages.clear();
for (auto &connection : GetConnections()) {
auto c = dynamic_cast<Connection *>(connection.get());
if (!c) {
continue;
}
c->Events().clear();
}
}
@ -44,8 +68,8 @@ static void receiveWebsocketMessage(obs_data_t *request_data, obs_data_t *,
}
auto msg = obs_data_get_string(request_data, "message");
std::lock_guard<std::mutex> lock(switcher->m);
switcher->websocketMessages.emplace_back(msg);
auto lock = LockContext();
websocketMessages.emplace_back(msg);
vblog(LOG_INFO, "received message: %s", msg);
}
@ -294,7 +318,7 @@ void WSConnection::HandleEvent(obs_data_t *msg)
return;
}
auto eventDataNested = obs_data_get_obj(eventData, "eventData");
std::lock_guard<std::mutex> lock(switcher->m);
auto lock = LockContext();
_messages.emplace_back(obs_data_get_string(eventDataNested, "message"));
vblog(LOG_INFO, "received event msg \"%s\"",
obs_data_get_string(eventDataNested, "message"));
@ -326,7 +350,7 @@ void WSConnection::OnGenericMessage(connection_hdl, client::message_ptr message)
return;
}
std::lock_guard<std::mutex> lock(switcher->m);
auto lock = LockContext();
const auto payload = message->get_payload();
_messages.emplace_back(payload);
vblog(LOG_INFO, "received event msg \"%s\"", payload.c_str());

View File

@ -21,18 +21,17 @@
namespace advss {
using websocketpp::connection_hdl;
typedef websocketpp::server<websocketpp::config::asio> server;
typedef websocketpp::client<websocketpp::config::asio_client> client;
constexpr char VendorName[] = "AdvancedSceneSwitcher";
constexpr char VendorRequest[] = "AdvancedSceneSwitcherMessage";
constexpr char VendorEvent[] = "AdvancedSceneSwitcherEvent";
void SetupWebsocketHelpers();
void ClearWebsocketMessages();
void SendWebsocketEvent(const std::string &);
std::string ConstructVendorRequestMessage(const std::string &message);
std::vector<std::string> &GetWebsocketMessages();
class WSConnection : public QObject {
using server = websocketpp::server<websocketpp::config::asio>;
using client = websocketpp::client<websocketpp::config::asio_client>;
public:
explicit WSConnection(bool useOBSProtocol = true);
virtual ~WSConnection();