RequestHandler: Add some config requests

This commit is contained in:
tt2468 2021-06-07 01:14:32 -07:00
parent 62d2b3e4cd
commit 4a00d169e1
4 changed files with 149 additions and 0 deletions

View File

@ -87,6 +87,7 @@ set(obs-websocket_SOURCES
src/eventhandler/EventHandler_MediaInputs.cpp
src/requesthandler/RequestHandler.cpp
src/requesthandler/RequestHandler_General.cpp
src/requesthandler/RequestHandler_Config.cpp
src/requesthandler/rpc/Request.cpp
src/requesthandler/rpc/RequestResult.cpp
src/forms/SettingsDialog.cpp

View File

@ -13,6 +13,14 @@ const std::map<std::string, RequestMethodHandler> RequestHandler::_handlerMap
{"GetStudioModeEnabled", &RequestHandler::GetStudioModeEnabled},
{"SetStudioModeEnabled", &RequestHandler::SetStudioModeEnabled},
{"Sleep", &RequestHandler::Sleep},
// Config
{"GetSceneCollectionList", &RequestHandler::GetSceneCollectionList},
{"SetCurrentSceneCollection", &RequestHandler::SetCurrentSceneCollection},
{"GetProfileList", &RequestHandler::GetProfileList},
{"SetCurrentProfile", &RequestHandler::SetCurrentProfile},
{"GetProfileParameter", &RequestHandler::GetProfileParameter},
{"SetProfileParameter", &RequestHandler::SetProfileParameter},
};
RequestResult RequestHandler::ProcessRequest(const Request& request)

View File

@ -17,6 +17,7 @@ class RequestHandler {
std::vector<std::string> GetRequestList();
private:
// General
RequestResult GetVersion(const Request&);
RequestResult BroadcastCustomEvent(const Request&);
RequestResult GetHotkeyList(const Request&);
@ -26,5 +27,13 @@ class RequestHandler {
RequestResult SetStudioModeEnabled(const Request&);
RequestResult Sleep(const Request&);
// Config
RequestResult GetSceneCollectionList(const Request&);
RequestResult SetCurrentSceneCollection(const Request&);
RequestResult GetProfileList(const Request&);
RequestResult SetCurrentProfile(const Request&);
RequestResult GetProfileParameter(const Request&);
RequestResult SetProfileParameter(const Request&);
static const std::map<std::string, RequestMethodHandler> _handlerMap;
};

View File

@ -0,0 +1,131 @@
#include <util/config-file.h>
#include "RequestHandler.h"
#include "../plugin-macros.generated.h"
RequestResult RequestHandler::GetSceneCollectionList(const Request& request)
{
json responseData;
responseData["currentSceneCollectionName"] = Utils::Obs::StringHelper::GetCurrentSceneCollection();
responseData["sceneCollections"] = Utils::Obs::ListHelper::GetSceneCollectionList();
return RequestResult::Success(responseData);
}
RequestResult RequestHandler::SetCurrentSceneCollection(const Request& request)
{
RequestStatus::RequestStatus statusCode;
std::string comment;
if (!request.ValidateString("sceneCollectionName", statusCode, comment)) {
return RequestResult::Error(statusCode, comment);
}
std::string currentSceneCollectionName = Utils::Obs::StringHelper::GetCurrentSceneCollection();
std::string sceneCollectionName = request.RequestData["sceneCollectionName"];
auto sceneCollections = Utils::Obs::ListHelper::GetSceneCollectionList();
if (std::find(sceneCollections.begin(), sceneCollections.end(), sceneCollectionName) == sceneCollections.end())
return RequestResult::Error(RequestStatus::SceneCollectionNotFound, "Your specified scene collection was not found.");
// Avoid queueing tasks if nothing will change
if (currentSceneCollectionName != sceneCollectionName) {
obs_queue_task(OBS_TASK_UI, [](void* param) {
obs_frontend_set_current_scene_collection(reinterpret_cast<const char*>(param));
}, (void*)sceneCollectionName.c_str(), true);
}
return RequestResult::Success();
}
RequestResult RequestHandler::GetProfileList(const Request& request)
{
json responseData;
responseData["currentProfileName"] = Utils::Obs::StringHelper::GetCurrentProfile();
responseData["profiles"] = Utils::Obs::ListHelper::GetProfileList();
return RequestResult::Success(responseData);
}
RequestResult RequestHandler::SetCurrentProfile(const Request& request)
{
RequestStatus::RequestStatus statusCode;
std::string comment;
if (!request.ValidateString("profileName", statusCode, comment)) {
return RequestResult::Error(statusCode, comment);
}
std::string currentProfileName = Utils::Obs::StringHelper::GetCurrentProfile();
std::string profileName = request.RequestData["profileName"];
auto profiles = Utils::Obs::ListHelper::GetProfileList();
if (std::find(profiles.begin(), profiles.end(), profileName) == profiles.end())
return RequestResult::Error(RequestStatus::ProfileNotFound, "Your specified profile was not found.");
// Avoid queueing tasks if nothing will change
if (currentProfileName != profileName) {
obs_queue_task(OBS_TASK_UI, [](void* param) {
obs_frontend_set_current_profile(reinterpret_cast<const char*>(param));
}, (void*)profileName.c_str(), true);
}
return RequestResult::Success();
}
RequestResult RequestHandler::GetProfileParameter(const Request& request)
{
RequestStatus::RequestStatus statusCode;
std::string comment;
if (!request.ValidateString("parameterCategory", statusCode, comment) || !request.ValidateString("parameterName", statusCode, comment)) {
return RequestResult::Error(statusCode, comment);
}
std::string parameterCategory = request.RequestData["parameterCategory"];
std::string parameterName = request.RequestData["parameterName"];
config_t* profile = obs_frontend_get_profile_config();
json responseData;
if (config_has_default_value(profile, parameterCategory.c_str(), parameterName.c_str())) {
responseData["parameterValue"] =
responseData["defaultParameterValue"] = config_get_default_string(profile, parameterCategory.c_str(), parameterName.c_str());
} else {
if (config_has_user_value(profile, parameterCategory.c_str(), parameterName.c_str()))
responseData["parameterValue"] = config_get_string(profile, parameterCategory.c_str(), parameterName.c_str());
else
responseData["parameterValue"] = nullptr;
responseData["defaultParameterValue"] = nullptr;
}
return RequestResult::Success(responseData);
}
RequestResult RequestHandler::SetProfileParameter(const Request& request)
{
RequestStatus::RequestStatus statusCode;
std::string comment;
if (!request.ValidateString("parameterCategory", statusCode, comment) ||
!request.ValidateString("parameterName", statusCode, comment)) {
return RequestResult::Error(statusCode, comment);
}
std::string parameterCategory = request.RequestData["parameterCategory"];
std::string parameterName = request.RequestData["parameterName"];
config_t* profile = obs_frontend_get_profile_config();
// Using check helpers here would just make the logic more complicated
if (!request.RequestData.contains("parameterValue") || request.RequestData["parameterValue"].is_null()) {
if (!config_remove_value(profile, parameterCategory.c_str(), parameterName.c_str()))
return RequestResult::Error(RequestStatus::ConfigParameterNotFound);
} else if (request.RequestData["parameterValue"].is_string()) {
std::string parameterValue = request.RequestData["parameterValue"];
config_set_string(profile, parameterCategory.c_str(), parameterName.c_str(), parameterValue.c_str());
} else {
return RequestResult::Error(RequestStatus::InvalidRequestParameterDataType, "The parameter `parameterValue` must be a string.");
}
return RequestResult::Success();
}