Cogs.Core
Variables.cpp
1#include "Variables.h"
2
3#include "Serialization/JsonParser.h"
4#include "Serialization/VariableReader.h"
5
6#include "Resources/ResourceStore.h"
7
8#include "Foundation/Logging/Logger.h"
9#include "Foundation/Reflection/TypeDatabase.h"
10
11#include <algorithm>
12#include <cstdlib>
13#include <cctype>
14
15namespace
16{
17 std::string toLower(const Cogs::StringView & input)
18 {
19 std::string lowerCase;
20 lowerCase.resize(input.size());
21 std::transform(input.begin(), input.end(), lowerCase.begin(),
22 [](unsigned char c) -> unsigned char { return (unsigned char)std::tolower(c); });
23 return lowerCase;
24 }
25 Cogs::Logging::Log logger = Cogs::Logging::getLogger("Variables");
26}
27
29{
30 this->stringValue = newValue.to_string();
31
32 auto lowerCase = toLower(newValue);
33
34 if (lowerCase == "true" || lowerCase == "false") {
35 value = ValueVariant((lowerCase == "true") ? true : false);
36 } else {
37 value = ValueVariant(newValue);
38 }
39
40 setValid();
41
42 return *this;
43}
44
45const Cogs::StringView Cogs::Core::Variables::get(const StringView & name, StringView defaultValue) const
46{
47 auto variable = get(name);
48
49 if(variable->getType() == ParsedDataType::Bool)
50 {
51 return variable->getBool() ? "true" : "false";
52 }
53
54 return variable->getValue(defaultValue);
55}
56
57Cogs::Core::Variables::Variables(const char ** variables, int count)
58{
59 empty.setStatic();
60
61 if (variables && count) {
62 assert(count % 2 == 0 && "Variable string array size must be even.");
63
64 const int variableCount = count / 2;
65
66 for (int i = 0; i < variableCount; ++i) {
67 LOG_DEBUG(logger, "Registering variable %s: %s", variables[i * 2], variables[i * 2 + 1]);
68 set(variables[i * 2], variables[i * 2 + 1]);
69 }
70 }
71
72#ifdef EMSCRIPTEN
73 // Force async preloading on Emscripten.
74 set("resources.effects.preLoad", true);
75#endif
76}
77
78void Cogs::Core::Variables::initialize(const ResourceStore& resourceStore)
79{
80 auto defaultConfigSetting = get("variables.defaultConfig", "Default.config");
81 ResourceBuffer content = resourceStore.getResourceContents(defaultConfigSetting);
82
83 if (!content.size()) {
84 LOG_ERROR(logger, "Could not open default configuration file.");
85
86 return;
87 }
88
89 auto document = parseJson(content.toStringView(), JsonParseFlags::None);
90
91 if (!document.IsObject()) {
92 LOG_ERROR(logger, "Could not load default configuration.");
93
94 return;
95 }
96
97 readVariables(*this, document, "", true);
98}
99
101{
102 if (auto it = variables.find(name.hash()); it != variables.end()) {
103 variables.erase(it);
104 return true;
105 }
106 return false;
107}
108
110{
111 auto it = variables.find(name.hash());
112
113 return it != variables.end() ? &it->second : &empty;
114}
115
117{
118 if (auto it = variables.find(name.hash()); it != variables.end()) {
119 return &it->second;
120 }
121 return nullptr;
122}
123
125{
126 auto v = &variables[name.hash()];
127
128 if (v->getName().empty()) {
129 v->setName(name);
130 }
131
132 return v;
133}
134
135Cogs::Core::Variable & Cogs::Core::Variables::operator[](const StringView & key)
136{
137 return *get(key);
138}
139
140void Cogs::Core::Variables::getMatchingVariables(std::vector<const Variable*>& vars, const StringView& keyPrefix)
141{
142 vars.clear();
143 for (auto & it : variables) {
144 auto & name = it.second.getName();
145 if (keyPrefix.length() <= name.length()) {
146 if (keyPrefix == StringView(name.data(), keyPrefix.length())) {
147 vars.push_back(&(it.second));
148 }
149 }
150 }
151}
const Variable * get(const StringView &name) const
Retrieve a pointer to the variable with the given name.
Definition: Variables.cpp:109
bool erase(const StringView &name)
Remove a set variable (so it will revert to default).
Definition: Variables.cpp:100
Variable * getIfExists(const StringView &name)
Get a pointer to a variable if it exists, otherwise nullptr. Does not modify set of variables.
Definition: Variables.cpp:116
Log implementation class.
Definition: LogManager.h:139
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
constexpr const char * data() const noexcept
Get the sequence of characters referenced by the string view.
Definition: StringView.h:171
constexpr size_t length() const noexcept
Get the length of the string.
Definition: StringView.h:185
std::string to_string() const
String conversion method.
Definition: StringView.cpp:9
constexpr size_t hash() const noexcept
Get the hash code of the string.
Definition: StringView.h:200
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:180
Runtime control variable.
Definition: Variables.h:27
Variable & setValue(const StringView &value)
Set the value of the variable to the given string value.
Definition: Variables.cpp:28