2021-04-27 02:59:50 +00:00
|
|
|
#include <obs-frontend-api.h>
|
|
|
|
|
|
|
|
#include "Config.h"
|
2021-04-27 23:41:10 +00:00
|
|
|
#include "plugin-macros.generated.h"
|
2021-07-22 10:24:53 +00:00
|
|
|
#include "utils/Utils.h"
|
2021-04-27 23:41:10 +00:00
|
|
|
|
2021-04-27 03:55:50 +00:00
|
|
|
#define CONFIG_SECTION_NAME "OBSWebSocket"
|
2021-04-27 02:59:50 +00:00
|
|
|
|
2021-05-08 08:42:06 +00:00
|
|
|
#define PARAM_FIRSTLOAD "FirstLoad"
|
2021-04-27 02:59:50 +00:00
|
|
|
#define PARAM_ENABLED "ServerEnabled"
|
|
|
|
#define PARAM_PORT "ServerPort"
|
|
|
|
#define PARAM_ALERTS "AlertsEnabled"
|
|
|
|
#define PARAM_AUTHREQUIRED "AuthRequred"
|
|
|
|
#define PARAM_PASSWORD "ServerPassword"
|
|
|
|
|
2021-04-28 22:59:29 +00:00
|
|
|
#define CMDLINE_WEBSOCKET_PORT "websocket_port"
|
|
|
|
#define CMDLINE_WEBSOCKET_PASSWORD "websocket_password"
|
2021-06-06 05:18:39 +00:00
|
|
|
#define CMDLINE_WEBSOCKET_DEBUG "websocket_debug"
|
2021-04-28 22:59:29 +00:00
|
|
|
|
2021-04-27 02:59:50 +00:00
|
|
|
Config::Config() :
|
2021-05-08 08:42:06 +00:00
|
|
|
FirstLoad(true),
|
2021-04-28 22:59:29 +00:00
|
|
|
PortOverridden(false),
|
|
|
|
PasswordOverridden(false),
|
2021-04-27 02:59:50 +00:00
|
|
|
ServerEnabled(true),
|
|
|
|
ServerPort(4444),
|
|
|
|
DebugEnabled(false),
|
|
|
|
AlertsEnabled(false),
|
|
|
|
AuthRequired(true),
|
|
|
|
ServerPassword("")
|
|
|
|
{
|
|
|
|
SetDefaultsToGlobalStore();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Config::Load()
|
|
|
|
{
|
|
|
|
config_t* obsConfig = GetConfigStore();
|
|
|
|
if (!obsConfig) {
|
2021-04-28 22:59:29 +00:00
|
|
|
blog(LOG_ERROR, "[Config::Load] Unable to fetch OBS config!");
|
2021-04-27 02:59:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-08 08:42:06 +00:00
|
|
|
FirstLoad = config_get_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_FIRSTLOAD);
|
2021-04-27 02:59:50 +00:00
|
|
|
ServerEnabled = config_get_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_ENABLED);
|
|
|
|
AlertsEnabled = config_get_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_ALERTS);
|
2021-04-28 22:59:29 +00:00
|
|
|
|
|
|
|
QString portArgument = Utils::Platform::GetCommandLineArgument(CMDLINE_WEBSOCKET_PORT);
|
|
|
|
if (portArgument != "") {
|
|
|
|
bool ok;
|
|
|
|
uint16_t serverPort = portArgument.toUShort(&ok);
|
|
|
|
if (ok) {
|
|
|
|
blog(LOG_INFO, "[Config::Load] Overriding websocket port with: %d", serverPort);
|
|
|
|
PortOverridden = true;
|
|
|
|
ServerPort = serverPort;
|
|
|
|
} else {
|
|
|
|
ServerPort = config_get_uint(obsConfig, CONFIG_SECTION_NAME, PARAM_PORT);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ServerPort = config_get_uint(obsConfig, CONFIG_SECTION_NAME, PARAM_PORT);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString passwordArgument = Utils::Platform::GetCommandLineArgument(CMDLINE_WEBSOCKET_PASSWORD);
|
|
|
|
if (passwordArgument != "") {
|
|
|
|
blog(LOG_INFO, "[Config::Load] Overriding websocket password");
|
|
|
|
PasswordOverridden = true;
|
|
|
|
AuthRequired = true;
|
|
|
|
ServerPassword = passwordArgument;
|
|
|
|
} else {
|
|
|
|
AuthRequired = config_get_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_AUTHREQUIRED);
|
2021-05-08 08:42:06 +00:00
|
|
|
if (FirstLoad) {
|
|
|
|
ServerPassword = Utils::Crypto::GeneratePassword();
|
|
|
|
} else {
|
|
|
|
ServerPassword = config_get_string(obsConfig, CONFIG_SECTION_NAME, PARAM_PASSWORD);
|
|
|
|
}
|
2021-04-28 22:59:29 +00:00
|
|
|
}
|
2021-06-06 05:18:39 +00:00
|
|
|
|
|
|
|
if (Utils::Platform::GetCommandLineFlagSet(CMDLINE_WEBSOCKET_DEBUG)) // Debug does not persist on reload, so we let people override it with a flag.
|
|
|
|
DebugEnabled = true;
|
2021-04-27 02:59:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Config::Save()
|
|
|
|
{
|
|
|
|
config_t* obsConfig = GetConfigStore();
|
|
|
|
if (!obsConfig) {
|
2021-04-28 22:59:29 +00:00
|
|
|
blog(LOG_ERROR, "[Config::Save] Unable to fetch OBS config!");
|
2021-04-27 02:59:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-08 08:42:06 +00:00
|
|
|
config_set_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_FIRSTLOAD, FirstLoad);
|
2021-04-27 02:59:50 +00:00
|
|
|
config_set_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_ENABLED, ServerEnabled);
|
2021-04-28 22:59:29 +00:00
|
|
|
if (!PortOverridden) {
|
|
|
|
config_set_uint(obsConfig, CONFIG_SECTION_NAME, PARAM_PORT, ServerPort);
|
|
|
|
}
|
2021-04-27 02:59:50 +00:00
|
|
|
config_set_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_ALERTS, AlertsEnabled);
|
2021-04-28 22:59:29 +00:00
|
|
|
if (!PasswordOverridden) {
|
|
|
|
config_set_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_AUTHREQUIRED, AuthRequired);
|
|
|
|
config_set_string(obsConfig, CONFIG_SECTION_NAME, PARAM_PASSWORD, QT_TO_UTF8(ServerPassword));
|
|
|
|
}
|
2021-04-27 02:59:50 +00:00
|
|
|
|
|
|
|
config_save(obsConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Config::SetDefaultsToGlobalStore()
|
|
|
|
{
|
|
|
|
config_t* obsConfig = GetConfigStore();
|
|
|
|
if (!obsConfig) {
|
2021-04-28 22:59:29 +00:00
|
|
|
blog(LOG_ERROR, "[Config::SetDefaultsToGlobalStore] Unable to fetch OBS config!");
|
2021-04-27 02:59:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-08 08:42:06 +00:00
|
|
|
config_set_default_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_FIRSTLOAD, FirstLoad);
|
2021-04-27 02:59:50 +00:00
|
|
|
config_set_default_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_ENABLED, ServerEnabled);
|
|
|
|
config_set_default_uint(obsConfig, CONFIG_SECTION_NAME, PARAM_PORT, ServerPort);
|
|
|
|
config_set_default_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_ALERTS, AlertsEnabled);
|
|
|
|
config_set_default_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_AUTHREQUIRED, AuthRequired);
|
|
|
|
config_set_default_string(obsConfig, CONFIG_SECTION_NAME, PARAM_PASSWORD, QT_TO_UTF8(ServerPassword));
|
|
|
|
}
|
|
|
|
|
|
|
|
config_t* Config::GetConfigStore()
|
|
|
|
{
|
|
|
|
return obs_frontend_get_global_config();
|
2021-04-27 21:52:48 +00:00
|
|
|
}
|