Store RGBA Cvar as JSON object (#996)

* Store RGBA Cvar as JSON object

* Use existing split string method

* Extract key setting to var
This commit is contained in:
David Chavez 2022-08-03 00:09:33 +02:00 committed by GitHub
parent 85c4cd3863
commit d9443d98f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 27 deletions

View File

@ -6,6 +6,7 @@
#include <filesystem> #include <filesystem>
#include <unordered_map> #include <unordered_map>
#include <any> #include <any>
#include <Utils/StringHelper.h>
namespace fs = std::filesystem; namespace fs = std::filesystem;
using json = nlohmann::json; using json = nlohmann::json;
@ -16,18 +17,9 @@ Mercury::Mercury(std::string path) : path_(std::move(path)) {
this->reload(); this->reload();
} }
std::vector<std::string> split(const std::string& s, const char delimiter) {
std::vector<std::string> result;
std::stringstream ss(s);
std::string item;
while (getline(ss, item, delimiter)) {
result.push_back(item);
}
return result;
}
std::string Mercury::formatNestedKey(const std::string& key) { std::string Mercury::formatNestedKey(const std::string& key) {
const std::vector<std::string> dots = split(key, '.'); std::vector<std::string> dots = StringHelper::Split(key, ".");
std::string tmp; std::string tmp;
if (dots.size() > 1) if (dots.size() > 1)
for (const auto& dot : dots) { for (const auto& dot : dots) {
@ -40,7 +32,7 @@ std::string Mercury::formatNestedKey(const std::string& key) {
} }
json Mercury::nested(const std::string& key) { json Mercury::nested(const std::string& key) {
std::vector<std::string> dots = split(key, '.'); std::vector<std::string> dots = StringHelper::Split(key, ".");
if (!this->vjson.is_object()) if (!this->vjson.is_object())
return this->vjson; return this->vjson;
json gjson = this->vjson.unflatten(); json gjson = this->vjson.unflatten();

View File

@ -4,6 +4,8 @@
#include <string> #include <string>
#include "../nlohmann/json.hpp" #include "../nlohmann/json.hpp"
static const std::string mercuryRGBAObjectType = "RGBA";
class Mercury { class Mercury {
protected: protected:
std::string path_; std::string path_;

View File

@ -568,20 +568,18 @@ void DebugConsole_LoadCVars() {
switch (value.type()) { switch (value.type()) {
case nlohmann::detail::value_t::array: case nlohmann::detail::value_t::array:
break; break;
case nlohmann::detail::value_t::string: case nlohmann::detail::value_t::object:
if (StringHelper::StartsWith(value.get<std::string>(), "#")) if (value["Type"].get<std::string>() == mercuryRGBAObjectType) {
{
uint32_t val = std::stoul(&value.get<std::string>().c_str()[1], nullptr, 16);
Color_RGBA8 clr; Color_RGBA8 clr;
clr.r = val >> 24; clr.r = value["R"].get<uint8_t>();
clr.g = val >> 16; clr.g = value["G"].get<uint8_t>();
clr.b = val >> 8; clr.b = value["B"].get<uint8_t>();
clr.a = val & 0xFF; clr.a = value["A"].get<uint8_t>();
CVar_SetRGBA(item.key().c_str(), clr);
} }
else
CVar_SetString(item.key().c_str(), value.get<std::string>().c_str()); break;
case nlohmann::detail::value_t::string:
CVar_SetString(item.key().c_str(), value.get<std::string>().c_str());
break; break;
case nlohmann::detail::value_t::boolean: case nlohmann::detail::value_t::boolean:
CVar_SetS32(item.key().c_str(), value.get<bool>()); CVar_SetS32(item.key().c_str(), value.get<bool>());
@ -618,10 +616,13 @@ void DebugConsole_SaveCVars()
pConf->setFloat(key, cvar.second->value.valueFloat); pConf->setFloat(key, cvar.second->value.valueFloat);
else if (cvar.second->type == CVarType::RGBA) else if (cvar.second->type == CVarType::RGBA)
{ {
auto keyStr = key.c_str();
Color_RGBA8 clr = cvar.second->value.valueRGBA; Color_RGBA8 clr = cvar.second->value.valueRGBA;
uint32_t val = (clr.r << 24) + (clr.g << 16) + (clr.b << 8) + clr.a; pConf->setUInt(StringHelper::Sprintf("%s.R", keyStr), clr.r);
std::string str = StringHelper::Sprintf("#%08X", val); pConf->setUInt(StringHelper::Sprintf("%s.G", keyStr), clr.r);
pConf->setString(key, str); pConf->setUInt(StringHelper::Sprintf("%s.B", keyStr), clr.r);
pConf->setUInt(StringHelper::Sprintf("%s.A", keyStr), clr.r);
pConf->setString(StringHelper::Sprintf("%s.Type", keyStr), mercuryRGBAObjectType);
} }
} }