Skip to content

Commit 7c417bf

Browse files
committed
feat(scripting/config): Fetch table
1 parent 3e3ed29 commit 7c417bf

File tree

3 files changed

+91
-53
lines changed

3 files changed

+91
-53
lines changed

src/configuration/Configuration.cpp

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ bool ConfigurationError(std::string configuration_file, std::string error)
1212
return false;
1313
}
1414

15-
void WritePluginFile(std::string path, rapidjson::Value &val)
15+
void WritePluginFile(std::string path, rapidjson::Value& val)
1616
{
1717
rapidjson::StringBuffer buffer;
1818
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
@@ -23,9 +23,18 @@ void WritePluginFile(std::string path, rapidjson::Value &val)
2323
Files::Write(path, content, false);
2424
}
2525

26-
rapidjson::Value &GetJSONDoc(rapidjson::Document &doc, std::string key, rapidjson::Value &defaultValue, bool &wasCreated)
26+
std::string JSONToString(rapidjson::Value& val)
2727
{
28-
rapidjson::Value *currentDoc = &doc;
28+
rapidjson::StringBuffer buffer;
29+
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
30+
31+
val.Accept(writer);
32+
return buffer.GetString();
33+
}
34+
35+
rapidjson::Value& GetJSONDoc(rapidjson::Document& doc, std::string key, rapidjson::Value& defaultValue, bool& wasCreated)
36+
{
37+
rapidjson::Value* currentDoc = &doc;
2938
auto keys = explode(key, ".");
3039

3140
while (keys.size() > 1)
@@ -51,13 +60,13 @@ rapidjson::Value &GetJSONDoc(rapidjson::Document &doc, std::string key, rapidjso
5160
}
5261

5362
template <class T>
54-
void RegisterConfiguration(bool &wasCreated, rapidjson::Document &document, std::string configFilePath, std::string config_prefix, std::string key, T default_value)
63+
void RegisterConfiguration(bool& wasCreated, rapidjson::Document& document, std::string configFilePath, std::string config_prefix, std::string key, T default_value)
5564
{
5665
rapidjson::Value defaultValue;
5766

5867
if constexpr (std::is_same<T, std::string>::value)
5968
defaultValue.SetString(default_value.c_str(), document.GetAllocator());
60-
else if constexpr (std::is_same<T, const char *>::value)
69+
else if constexpr (std::is_same<T, const char*>::value)
6170
defaultValue.SetString(default_value, document.GetAllocator());
6271
else if constexpr (std::is_same<T, bool>::value)
6372
defaultValue.SetBool(default_value);
@@ -82,7 +91,7 @@ void RegisterConfiguration(bool &wasCreated, rapidjson::Document &document, std:
8291
else if constexpr (std::is_same<T, double>::value)
8392
defaultValue.SetDouble(default_value);
8493

85-
rapidjson::Value &jsonDoc = GetJSONDoc(document, key, defaultValue, wasCreated);
94+
rapidjson::Value& jsonDoc = GetJSONDoc(document, key, defaultValue, wasCreated);
8695

8796
if constexpr (std::is_same<T, std::string>::value)
8897
{
@@ -94,7 +103,7 @@ void RegisterConfiguration(bool &wasCreated, rapidjson::Document &document, std:
94103

95104
g_Config->SetValue(config_prefix + "." + key, std::string(jsonDoc.GetString()));
96105
}
97-
else if constexpr (std::is_same<T, const char *>::value)
106+
else if constexpr (std::is_same<T, const char*>::value)
98107
{
99108
if (!jsonDoc.IsString())
100109
{
@@ -216,17 +225,17 @@ void RegisterConfiguration(bool &wasCreated, rapidjson::Document &document, std:
216225
}
217226

218227
template <class T>
219-
void RegisterConfigurationVector(bool &wasCreated, rapidjson::Document &document, std::string configFilePath, std::string config_prefix, std::string key, std::vector<T> default_value, bool shouldImplode, std::string delimiter)
228+
void RegisterConfigurationVector(bool& wasCreated, rapidjson::Document& document, std::string configFilePath, std::string config_prefix, std::string key, std::vector<T> default_value, bool shouldImplode, std::string delimiter)
220229
{
221230
rapidjson::Value defaultValue(rapidjson::kArrayType);
222231

223-
for (const T &val : default_value)
232+
for (const T& val : default_value)
224233
{
225234
rapidjson::Value defVal;
226235

227236
if constexpr (std::is_same<T, std::string>::value)
228237
defVal.SetString(val.c_str(), document.GetAllocator());
229-
else if constexpr (std::is_same<T, const char *>::value)
238+
else if constexpr (std::is_same<T, const char*>::value)
230239
defVal.SetString(val, document.GetAllocator());
231240
else if constexpr (std::is_same<T, bool>::value)
232241
defVal.SetBool(val);
@@ -254,7 +263,7 @@ void RegisterConfigurationVector(bool &wasCreated, rapidjson::Document &document
254263
defaultValue.PushBack(defVal, document.GetAllocator());
255264
}
256265

257-
rapidjson::Value &jsonDoc = GetJSONDoc(document, key, defaultValue, wasCreated);
266+
rapidjson::Value& jsonDoc = GetJSONDoc(document, key, defaultValue, wasCreated);
258267

259268
if (!jsonDoc.IsArray())
260269
{
@@ -278,7 +287,7 @@ void RegisterConfigurationVector(bool &wasCreated, rapidjson::Document &document
278287

279288
result.push_back(std::string(arr[i].GetString()));
280289
}
281-
else if constexpr (std::is_same<T, const char *>::value)
290+
else if constexpr (std::is_same<T, const char*>::value)
282291
{
283292
if (!arr[i].IsString())
284293
{
@@ -397,11 +406,11 @@ void RegisterConfigurationVector(bool &wasCreated, rapidjson::Document &document
397406
if (shouldImplode)
398407
{
399408
std::vector<std::string> implodeArr;
400-
for (const T &val : result)
409+
for (const T& val : result)
401410
{
402411
if constexpr (std::is_same<T, std::string>::value)
403412
implodeArr.push_back(val);
404-
else if constexpr (std::is_same<T, const char *>::value)
413+
else if constexpr (std::is_same<T, const char*>::value)
405414
implodeArr.push_back(std::string(val));
406415
else
407416
implodeArr.push_back(std::to_string(val));
@@ -432,8 +441,8 @@ bool Configuration::LoadConfiguration()
432441
if (loggingMode != "daily" && loggingMode != "map" && loggingMode != "permanent")
433442
return ConfigurationError("core.json", "The field \"logging.mode\" needs to be: \"daily\" or \"map\".");
434443

435-
RegisterConfigurationVector<std::string>(wasEdited, coreConfigFile, "core", "core", "commandPrefixes", {"!"}, true, " ");
436-
RegisterConfigurationVector<std::string>(wasEdited, coreConfigFile, "core", "core", "commandSilentPrefixes", {"/"}, true, " ");
444+
RegisterConfigurationVector<std::string>(wasEdited, coreConfigFile, "core", "core", "commandPrefixes", { "!" }, true, " ");
445+
RegisterConfigurationVector<std::string>(wasEdited, coreConfigFile, "core", "core", "commandSilentPrefixes", { "/" }, true, " ");
437446
RegisterConfigurationVector<std::string>(wasEdited, coreConfigFile, "core", "core", "patches_to_perform", {}, true, " ");
438447

439448
RegisterConfiguration(wasEdited, coreConfigFile, "core", "core", "CS2ServerGuidelines", "https://blog.counter-strike.net/index.php/server_guidelines/");
@@ -461,9 +470,9 @@ bool Configuration::LoadConfiguration()
461470
return true;
462471
}
463472

464-
void LoadConfigPart(std::string key, rapidjson::Value &document);
473+
void LoadConfigPart(std::string key, rapidjson::Value& document);
465474

466-
void LoadValue(const char *key, const char *keyname, rapidjson::Value &value, std::string separator = ".")
475+
void LoadValue(const char* key, const char* keyname, rapidjson::Value& value, std::string separator = ".")
467476
{
468477
std::string k = key + separator + keyname;
469478
if (value.IsBool())
@@ -484,10 +493,13 @@ void LoadValue(const char *key, const char *keyname, rapidjson::Value &value, st
484493
g_Config->SetValue(k, value.GetUint());
485494
else if (value.IsNull())
486495
g_Config->SetValue(k, nullptr);
487-
else if (value.IsObject())
496+
else if (value.IsObject()) {
497+
g_Config->SetValue(k, string_format("<%s>", JSONToString(value).c_str()));
488498
LoadConfigPart(k, value);
499+
}
489500
else if (value.IsArray())
490501
{
502+
g_Config->SetValue(k, string_format("JSON<%s>", JSONToString(value).c_str()));
491503
g_Config->SetArraySize(k, value.Size());
492504
for (size_t i = 0; i < value.Size(); i++)
493505
{
@@ -497,7 +509,7 @@ void LoadValue(const char *key, const char *keyname, rapidjson::Value &value, st
497509
}
498510
};
499511

500-
void LoadConfigPart(std::string key, rapidjson::Value &document)
512+
void LoadConfigPart(std::string key, rapidjson::Value& document)
501513
{
502514
for (auto it = document.MemberBegin(); it != document.MemberEnd(); ++it)
503515
{
@@ -538,9 +550,9 @@ void Configuration::LoadPluginConfigurations()
538550
}
539551

540552
std::string main_key = explode(configFileName, ".json")[0];
541-
rapidjson::Value &root = configurationFile;
553+
rapidjson::Value& root = configurationFile;
542554

543-
g_Config->SetValue(main_key, true);
555+
g_Config->SetValue(main_key, JSONToString(root));
544556
LoadConfigPart(main_key, root);
545557
}
546558
}
@@ -582,9 +594,9 @@ void Configuration::LoadPluginConfig(std::string key)
582594
configFileName = replace(configFileName, "\\", ".");
583595
std::string main_key = explode(configFileName, ".json")[0];
584596

585-
rapidjson::Value &root = configurationFile;
597+
rapidjson::Value& root = configurationFile;
586598

587-
g_Config->SetValue(main_key, true);
599+
g_Config->SetValue(main_key, JSONToString(root));
588600
LoadConfigPart(main_key, root);
589601
}
590602

src/configuration/Configuration.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <rapidjson/document.h>
99
#include <rapidjson/error/en.h>
1010
#include <rapidjson/prettywriter.h>
11+
#include <rapidjson/writer.h>
1112
#include <rapidjson/stringbuffer.h>
1213

1314
class Configuration
@@ -38,7 +39,7 @@ class Configuration
3839
void LoadPluginConfig(std::string key);
3940
};
4041

41-
extern Configuration *g_Config;
42+
extern Configuration* g_Config;
4243

4344
template <typename T>
4445
T Configuration::FetchValue(std::string key)
@@ -49,6 +50,6 @@ T Configuration::FetchValue(std::string key)
4950
return std::any_cast<T>(this->config.at(key));
5051
}
5152

52-
void WritePluginFile(std::string path, rapidjson::Value &val);
53+
void WritePluginFile(std::string path, rapidjson::Value& val);
5354

5455
#endif

0 commit comments

Comments
 (0)