mirror of
https://github.com/CalcProgrammer1/OpenRGB.git
synced 2026-01-16 19:17:49 -05:00
[WIP] SDK Version 6 Updates
* SDK Protocol
* Server sends its name to client
* ProfileManager
* Rename existing profile commands
* Add Upload Profile, Download Profile, and Get Active Profile commands
* SettingsManager
* Add Get, Set, and Save Settings commands
* NetworkServer
* Formatting cleanup
* Use per-controller threads for handling NetworkServer controller-specific packets to avoid delays from controller mutexes
* NetworkClient
* Formatting cleanup
* RGBController
* Clean up and modularize descriptor functions
This commit is contained in:
@@ -13,8 +13,22 @@
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include "SettingsManager.h"
|
||||
#include "LogManager.h"
|
||||
#include "NetworkClient.h"
|
||||
#include "ResourceManager.h"
|
||||
#include "SettingsManager.h"
|
||||
#include "StringUtils.h"
|
||||
|
||||
static const std::string ui_settings_keys[7] =
|
||||
{
|
||||
"UserInterface",
|
||||
"AutoStart",
|
||||
"Theme",
|
||||
"Plugins",
|
||||
"Client",
|
||||
"LogManager",
|
||||
"Server"
|
||||
};
|
||||
|
||||
SettingsManager::SettingsManager()
|
||||
{
|
||||
@@ -28,31 +42,113 @@ SettingsManager::~SettingsManager()
|
||||
|
||||
json SettingsManager::GetSettings(std::string settings_key)
|
||||
{
|
||||
/*-----------------------------------------------------*\
|
||||
| Check to see if the key exists in the settings store |
|
||||
| and return the settings associated with the key if it |
|
||||
| exists. We lock the mutex to protect the value from |
|
||||
| changing while data is being read and copy before |
|
||||
| unlocking. |
|
||||
\*-----------------------------------------------------*/
|
||||
json result;
|
||||
bool ui_settings_key = false;
|
||||
|
||||
mutex.lock();
|
||||
if(settings_data.contains(settings_key))
|
||||
/*-----------------------------------------------------*\
|
||||
| Remove any excess null termination from settings key |
|
||||
\*-----------------------------------------------------*/
|
||||
settings_key = StringUtils::remove_null_terminating_chars(settings_key);
|
||||
|
||||
for(std::size_t settings_key_idx = 0; settings_key_idx < 7; settings_key_idx++)
|
||||
{
|
||||
result = settings_data[settings_key];
|
||||
if(settings_key == ui_settings_keys[settings_key_idx])
|
||||
{
|
||||
ui_settings_key = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
mutex.unlock();
|
||||
if(!ui_settings_key && ResourceManager::get()->IsLocalClient())
|
||||
{
|
||||
/*-------------------------------------------------*\
|
||||
| If this is a local client, request the settings |
|
||||
| from the server |
|
||||
\*-------------------------------------------------*/
|
||||
try
|
||||
{
|
||||
result = nlohmann::json::parse(ResourceManager::get()->GetLocalClient()->SettingsManager_GetSettings(settings_key));
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/*-------------------------------------------------*\
|
||||
| Check to see if the key exists in the settings |
|
||||
| store and return the settings associated with the |
|
||||
| key if it exists. We lock the mutex to protect |
|
||||
| the value from changing while data is being read |
|
||||
| and copy before unlocking. |
|
||||
\*-------------------------------------------------*/
|
||||
mutex.lock();
|
||||
if(settings_data.contains(settings_key))
|
||||
{
|
||||
result = settings_data[settings_key];
|
||||
}
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void SettingsManager::SetSettings(std::string settings_key, json new_settings)
|
||||
{
|
||||
mutex.lock();
|
||||
settings_data[settings_key] = new_settings;
|
||||
mutex.unlock();
|
||||
bool ui_settings_key = false;
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
| Remove any excess null termination from settings key |
|
||||
\*-----------------------------------------------------*/
|
||||
settings_key = StringUtils::remove_null_terminating_chars(settings_key);
|
||||
|
||||
for(std::size_t settings_key_idx = 0; settings_key_idx < 7; settings_key_idx++)
|
||||
{
|
||||
if(settings_key == ui_settings_keys[settings_key_idx])
|
||||
{
|
||||
ui_settings_key = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!ui_settings_key && ResourceManager::get()->IsLocalClient())
|
||||
{
|
||||
/*-------------------------------------------------*\
|
||||
| If this is a local client, request the settings |
|
||||
| from the server |
|
||||
\*-------------------------------------------------*/
|
||||
nlohmann::json settings_json;
|
||||
|
||||
settings_json[settings_key] = new_settings;
|
||||
|
||||
ResourceManager::get()->GetLocalClient()->SettingsManager_SetSettings(settings_json.dump());
|
||||
}
|
||||
else
|
||||
{
|
||||
mutex.lock();
|
||||
settings_data[settings_key] = new_settings;
|
||||
mutex.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
void SettingsManager::SetSettingsFromJsonString(std::string settings_json_str)
|
||||
{
|
||||
/*-----------------------------------------------------*\
|
||||
| Parse the JSON string |
|
||||
\*-----------------------------------------------------*/
|
||||
nlohmann::json settings_json = nlohmann::json::parse(settings_json_str);
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
| Get key/value pairs from JSON, call SetSettings for |
|
||||
| each key. This use of `auto` is acceptable due to |
|
||||
| how the JSON library implements iterators, the type |
|
||||
| would change based on the library version. |
|
||||
\*-----------------------------------------------------*/
|
||||
for(auto& element : settings_json.items())
|
||||
{
|
||||
SetSettings(element.key(), element.value());
|
||||
}
|
||||
}
|
||||
|
||||
void SettingsManager::LoadSettings(const filesystem::path& filename)
|
||||
@@ -109,6 +205,15 @@ void SettingsManager::LoadSettings(const filesystem::path& filename)
|
||||
|
||||
void SettingsManager::SaveSettings()
|
||||
{
|
||||
if(ResourceManager::get()->IsLocalClient())
|
||||
{
|
||||
/*-------------------------------------------------*\
|
||||
| If this is a local client, save the settings on |
|
||||
| the server |
|
||||
\*-------------------------------------------------*/
|
||||
ResourceManager::get()->GetLocalClient()->SettingsManager_SaveSettings();
|
||||
}
|
||||
|
||||
mutex.lock();
|
||||
std::ofstream settings_file(settings_filename, std::ios::out | std::ios::binary);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user