From 7c5c0f42da6f575a61122e9e8a55b2fb23420858 Mon Sep 17 00:00:00 2001 From: tt2468 Date: Thu, 26 Aug 2021 23:06:23 -0700 Subject: [PATCH] Requests: Merge global and profile persistent data requests The code was largely shared, so it made more sense to just merge the requests and create realms. --- src/requesthandler/RequestHandler.cpp | 4 +- src/requesthandler/RequestHandler.h | 4 +- src/requesthandler/RequestHandler_Config.cpp | 100 +++++++++++-------- 3 files changed, 60 insertions(+), 48 deletions(-) diff --git a/src/requesthandler/RequestHandler.cpp b/src/requesthandler/RequestHandler.cpp index f5ef5152..a5491a79 100644 --- a/src/requesthandler/RequestHandler.cpp +++ b/src/requesthandler/RequestHandler.cpp @@ -14,14 +14,14 @@ const std::map RequestHandler::_handlerMap {"Sleep", &RequestHandler::Sleep}, // Config + {"GetPersistentData", &RequestHandler::GetPersistentData}, + {"SetPersistentData", &RequestHandler::SetPersistentData}, {"GetSceneCollectionList", &RequestHandler::GetSceneCollectionList}, {"SetCurrentSceneCollection", &RequestHandler::SetCurrentSceneCollection}, {"GetProfileList", &RequestHandler::GetProfileList}, {"SetCurrentProfile", &RequestHandler::SetCurrentProfile}, {"GetProfileParameter", &RequestHandler::GetProfileParameter}, {"SetProfileParameter", &RequestHandler::SetProfileParameter}, - {"GetProfilePersistentData", &RequestHandler::GetProfilePersistentData}, - {"SetProfilePersistentData", &RequestHandler::SetProfilePersistentData}, {"GetVideoSettings", &RequestHandler::GetVideoSettings}, {"SetVideoSettings", &RequestHandler::SetVideoSettings}, {"GetStreamServiceSettings", &RequestHandler::GetStreamServiceSettings}, diff --git a/src/requesthandler/RequestHandler.h b/src/requesthandler/RequestHandler.h index 8507ce70..6bf25761 100644 --- a/src/requesthandler/RequestHandler.h +++ b/src/requesthandler/RequestHandler.h @@ -29,14 +29,14 @@ class RequestHandler { RequestResult Sleep(const Request&); // Config + RequestResult GetPersistentData(const Request&); + RequestResult SetPersistentData(const Request&); RequestResult GetSceneCollectionList(const Request&); RequestResult SetCurrentSceneCollection(const Request&); RequestResult GetProfileList(const Request&); RequestResult SetCurrentProfile(const Request&); RequestResult GetProfileParameter(const Request&); RequestResult SetProfileParameter(const Request&); - RequestResult GetProfilePersistentData(const Request&); - RequestResult SetProfilePersistentData(const Request&); RequestResult GetVideoSettings(const Request&); RequestResult SetVideoSettings(const Request&); RequestResult GetStreamServiceSettings(const Request&); diff --git a/src/requesthandler/RequestHandler_Config.cpp b/src/requesthandler/RequestHandler_Config.cpp index 1cb9d788..1aa776f2 100644 --- a/src/requesthandler/RequestHandler_Config.cpp +++ b/src/requesthandler/RequestHandler_Config.cpp @@ -3,6 +3,62 @@ #include "RequestHandler.h" #include "../plugin-macros.generated.h" +RequestResult RequestHandler::GetPersistentData(const Request& request) +{ + RequestStatus::RequestStatus statusCode; + std::string comment; + if (!(request.ValidateString("realm", statusCode, comment) && request.ValidateString("slotName", statusCode, comment))) + return RequestResult::Error(statusCode, comment); + + std::string realm = request.RequestData["realm"]; + std::string slotName = request.RequestData["slotName"]; + + std::string persistentDataPath = Utils::Obs::StringHelper::GetCurrentProfilePath(); + if (realm == "OBS_WEBSOCKET_DATA_REALM_GLOBAL") + persistentDataPath += "../../../obsWebSocketPersistentData.json"; + else if (realm == "OBS_WEBSOCKET_DATA_REALM_PROFILE") + persistentDataPath += "/obsWebSocketPersistentData.json"; + else + return RequestResult::Error(RequestStatus::DataRealmNotFound, "You have specified an invalid persistent data realm."); + + json responseData; + json persistentData; + if (!(Utils::Json::GetJsonFileContent(persistentDataPath, persistentData) && persistentData.contains(slotName))) + responseData["slotData"] = nullptr; + else + responseData["slotData"] = persistentData[slotName]; + + return RequestResult::Success(responseData); +} + +RequestResult RequestHandler::SetPersistentData(const Request& request) +{ + RequestStatus::RequestStatus statusCode; + std::string comment; + if (!(request.ValidateString("realm", statusCode, comment) && request.ValidateString("slotName", statusCode, comment) && request.ValidateBasic("slotName", statusCode, comment))) + return RequestResult::Error(statusCode, comment); + + std::string realm = request.RequestData["realm"]; + std::string slotName = request.RequestData["slotName"]; + json slotData = request.RequestData["slotData"]; + + std::string persistentDataPath = Utils::Obs::StringHelper::GetCurrentProfilePath(); + if (realm == "OBS_WEBSOCKET_DATA_REALM_GLOBAL") + persistentDataPath += "../../../obsWebSocketPersistentData.json"; + else if (realm == "OBS_WEBSOCKET_DATA_REALM_PROFILE") + persistentDataPath += "/obsWebSocketPersistentData.json"; + else + return RequestResult::Error(RequestStatus::DataRealmNotFound, "You have specified an invalid persistent data realm."); + + json persistentData = json::object(); + Utils::Json::GetJsonFileContent(persistentDataPath, persistentData); + persistentData[slotName] = slotData; + if (!Utils::Json::SetJsonFileContent(persistentDataPath, persistentData)) + return RequestResult::Error(RequestStatus::RequestProcessingFailed, "Unable to write persistent data. No permissions?"); + + return RequestResult::Success(); +} + RequestResult RequestHandler::GetSceneCollectionList(const Request& request) { json responseData; @@ -124,50 +180,6 @@ RequestResult RequestHandler::SetProfileParameter(const Request& request) return RequestResult::Success(); } -RequestResult RequestHandler::GetProfilePersistentData(const Request& request) -{ - RequestStatus::RequestStatus statusCode; - std::string comment; - if (!request.ValidateString("slotName", statusCode, comment)) - return RequestResult::Error(statusCode, comment); - - std::string slotName = request.RequestData["slotName"]; - - std::string persistentDataPath = Utils::Obs::StringHelper::GetCurrentProfilePath(); - persistentDataPath += "/obsWebSocketPersistentData.json"; - - json responseData; - json persistentData; - if (!(Utils::Json::GetJsonFileContent(persistentDataPath, persistentData) && persistentData.contains(slotName))) - responseData["slotData"] = nullptr; - else - responseData["slotData"] = persistentData[slotName]; - - return RequestResult::Success(responseData); -} - -RequestResult RequestHandler::SetProfilePersistentData(const Request& request) -{ - RequestStatus::RequestStatus statusCode; - std::string comment; - if (!(request.ValidateString("slotName", statusCode, comment) && request.ValidateBasic("slotName", statusCode, comment))) - return RequestResult::Error(statusCode, comment); - - std::string slotName = request.RequestData["slotName"]; - json slotData = request.RequestData["slotData"]; - - std::string persistentDataPath = Utils::Obs::StringHelper::GetCurrentProfilePath(); - persistentDataPath += "/obsWebSocketPersistentData.json"; - - json persistentData = json::object(); - Utils::Json::GetJsonFileContent(persistentDataPath, persistentData); - persistentData[slotName] = slotData; - if (!Utils::Json::SetJsonFileContent(persistentDataPath, persistentData)) - return RequestResult::Error(RequestStatus::RequestProcessingFailed, "Unable to write persistent data. No permissions?"); - - return RequestResult::Success(); -} - RequestResult RequestHandler::GetVideoSettings(const Request& request) { struct obs_video_info ovi;