t8-mod/source/shared-code/utils/json_config.cpp
project-bo4 354d022967 Squashed commit of the following:
commit f44d146e4ac906ff898e8968c6857fd2280f6d20
Author: project-bo4 <127137346+project-bo4@users.noreply.github.com>
Date:   Thu May 11 13:39:29 2023 -0700

    remove lpc package from repository

commit 29fb0f63e50de468ab0b9db35f7e8fdadf58afc3
Author: project-bo4 <themanwithantidote@gmail.com>
Date:   Thu May 11 13:06:13 2023 -0700

    generic improvements

    + added identity configuration
    + objectstore improvements
    + added battlenet 'US' servers to blocklist
    + some other minor improvements

commit 5d5e31099ebcce5a637b9a02d4936a97fb0802a7
Author: project-bo4 <themanwithantidote@gmail.com>
Date:   Sat Mar 25 16:32:52 2023 -0700

    lpc improvements

    + fix lpc issue with foreign languages(non-english) not being considered in listing
    + check lpc files pre-start and warn user if missing any of core items

commit 2de1a54b6f4cc5c44dc906871021cfbc85057ca9
Author: project-bo4 <themanwithantidote@gmail.com>
Date:   Thu Mar 23 12:45:56 2023 -0700

    demonware improvements

    + handle object store uploadUserObject
    + silence bdUNK125

commit 710dcc3ec6178071d67c27e5bf6705f08cabe7e2
Author: project-bo4 <themanwithantidote@gmail.com>
Date:   Mon Mar 20 13:43:56 2023 -0700

    forgot to update names

commit 217682dfb07b35f7a09399fb2698924bb7fe8b77
Author: project-bo4 <themanwithantidote@gmail.com>
Date:   Mon Mar 20 11:09:18 2023 -0700

    upload lpc and update readme

commit 83f812b33b90791a8d13b9468f27da51e0a4f2b9
Author: project-bo4 <themanwithantidote@gmail.com>
Date:   Mon Mar 20 10:53:43 2023 -0700

    demonware emulator

    + demonware emulator
    + platform name and id
    + xxhash and picoproto
    - remove g_runframe hook
    -disable discovery(save time)
    + improvements to utils
    + add new resources
2023-05-11 13:50:11 -07:00

184 lines
4.6 KiB
C++

#include "json_config.hpp"
#include "io.hpp"
#include <rapidjson/document.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/prettywriter.h>
namespace utils::json_config
{
rapidjson::Document json_doc{};
std::string file_name = "project-bo4.json";
namespace
{
bool read_json_config()
{
std::string json_data{};
if (!io::read_file(file_name, &json_data)) return false;
json_doc.Parse(json_data);
if (json_doc.HasParseError()) {
return false;
}
return true;
}
bool write_json_config()
{
rapidjson::StringBuffer buffer;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
json_doc.Accept(writer);
std::string json_data(buffer.GetString(), buffer.GetLength());
if (!io::write_file(file_name, json_data)) return false;
return true;
}
}
rapidjson::Document& get_json_document()
{
static bool json_initialized = false;
if (!json_initialized)
{
if (!read_json_config()) {
json_doc = rapidjson::Document(rapidjson::kObjectType);
}
json_initialized = true;
}
/*if (json_doc != NULL) */return json_doc;
}
rapidjson::Value& get_json_section(const char* szSection)
{
rapidjson::Document& doc = get_json_document();
if (!doc.HasMember(szSection))
{
doc.AddMember(rapidjson::StringRef(szSection), rapidjson::kObjectType, doc.GetAllocator());
}
rapidjson::Value& section = doc[szSection];
if (!section.IsObject()) section.SetObject();
return section;
}
std::string ReadString(const char* szSection, const char* szKey, const std::string& strDefaultValue)
{
rapidjson::Document& doc = get_json_document();
rapidjson::Value& section = get_json_section(szSection);
if (!section.HasMember(szKey)) {
section.AddMember(rapidjson::StringRef(szKey), strDefaultValue, doc.GetAllocator());
}
else if (!section[szKey].IsString()) {
section[szKey].SetString(strDefaultValue, doc.GetAllocator());
}
else {
return section[szKey].GetString();
}
write_json_config();
return section[szKey].GetString();
}
void WriteString(const char* szSection, const char* szKey, const std::string& strValue)
{
rapidjson::Document& doc = get_json_document();
rapidjson::Value& section = get_json_section(szSection);
if (!section.HasMember(szKey)) {
section.AddMember(rapidjson::StringRef(szKey), strValue, doc.GetAllocator());
}
else {
section[szKey].SetString(strValue, doc.GetAllocator());
}
write_json_config();
}
unsigned int ReadUnsignedInteger(const char* szSection, const char* szKey, unsigned int iDefaultValue)
{
rapidjson::Document& doc = get_json_document();
rapidjson::Value& section = get_json_section(szSection);
if (!section.HasMember(szKey)) {
section.AddMember(rapidjson::StringRef(szKey), iDefaultValue, doc.GetAllocator());
}
else if (!section[szKey].IsUint()) {
section[szKey].SetUint(iDefaultValue);
}
else {
return section[szKey].GetUint();
}
write_json_config();
return section[szKey].GetUint();
}
int ReadInteger(const char* szSection, const char* szKey, int iDefaultValue)
{
return static_cast<int>(ReadUnsignedInteger(szSection, szKey, static_cast<unsigned int>(iDefaultValue)));
}
void WriteUnsignedInteger(const char* szSection, const char* szKey, unsigned int iValue)
{
rapidjson::Document& doc = get_json_document();
rapidjson::Value& section = get_json_section(szSection);
if (!section.HasMember(szKey)) {
section.AddMember(rapidjson::StringRef(szKey), iValue, doc.GetAllocator());
}
else {
section[szKey].SetUint(iValue);
}
write_json_config();
}
void WriteInteger(const char* szSection, const char* szKey, int iValue)
{
ReadUnsignedInteger(szSection, szKey, static_cast<unsigned int>(iValue));
}
bool ReadBoolean(const char* szSection, const char* szKey, bool bolDefaultValue)
{
rapidjson::Document& doc = get_json_document();
rapidjson::Value& section = get_json_section(szSection);
if (!section.HasMember(szKey)) {
section.AddMember(rapidjson::StringRef(szKey), bolDefaultValue, doc.GetAllocator());
}
else if (!section[szKey].IsBool()) {
section[szKey].SetBool(bolDefaultValue);
}
else {
return section[szKey].GetBool();
}
write_json_config();
return section[szKey].GetBool();
}
void WriteBoolean(const char* szSection, const char* szKey, bool bolValue)
{
rapidjson::Document& doc = get_json_document();
rapidjson::Value& section = get_json_section(szSection);
if (!section.HasMember(szKey)) {
section.AddMember(rapidjson::StringRef(szKey), bolValue, doc.GetAllocator());
}
else {
section[szKey].SetBool(bolValue);
}
write_json_config();
}
}