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 <unordered_map>
#include <any>
#include <Utils/StringHelper.h>
namespace fs = std::filesystem;
using json = nlohmann::json;
@ -16,18 +17,9 @@ Mercury::Mercury(std::string path) : path_(std::move(path)) {
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) {
const std::vector<std::string> dots = split(key, '.');
std::vector<std::string> dots = StringHelper::Split(key, ".");
std::string tmp;
if (dots.size() > 1)
for (const auto& dot : dots) {
@ -40,7 +32,7 @@ std::string Mercury::formatNestedKey(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())
return this->vjson;
json gjson = this->vjson.unflatten();

View File

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

View File

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