3#include "Serialization/JsonParser.h"
4#include "Serialization/VariableReader.h"
6#include "Resources/ResourceStore.h"
8#include "Foundation/Logging/Logger.h"
9#include "Foundation/Reflection/TypeDatabase.h"
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); });
32 auto lowerCase = toLower(newValue);
34 if (lowerCase ==
"true" || lowerCase ==
"false") {
35 value =
ValueVariant((lowerCase ==
"true") ?
true :
false);
47 auto variable = get(name);
49 if(variable->getType() == ParsedDataType::Bool)
51 return variable->getBool() ?
"true" :
"false";
54 return variable->getValue(defaultValue);
57Cogs::Core::Variables::Variables(
const char ** variables,
int count)
61 if (variables && count) {
62 assert(count % 2 == 0 &&
"Variable string array size must be even.");
64 const int variableCount = count / 2;
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]);
74 set(
"resources.effects.preLoad",
true);
78void Cogs::Core::Variables::initialize(
const ResourceStore& resourceStore)
80 auto defaultConfigSetting = get(
"variables.defaultConfig",
"Default.config");
81 ResourceBuffer content = resourceStore.getResourceContents(defaultConfigSetting);
83 if (!content.size()) {
84 LOG_ERROR(logger,
"Could not open default configuration file.");
89 auto document = parseJson(content.toStringView(), JsonParseFlags::None);
91 if (!document.IsObject()) {
92 LOG_ERROR(logger,
"Could not load default configuration.");
97 readVariables(*
this, document,
"",
true);
102 if (
auto it = variables.find(name.
hash()); it != variables.end()) {
111 auto it = variables.find(name.
hash());
113 return it != variables.end() ? &it->second : ∅
118 if (
auto it = variables.find(name.
hash()); it != variables.end()) {
126 auto v = &variables[name.
hash()];
128 if (v->getName().empty()) {
140void Cogs::Core::Variables::getMatchingVariables(std::vector<const Variable*>& vars,
const StringView& keyPrefix)
143 for (
auto & it : variables) {
144 auto & name = it.second.getName();
147 vars.push_back(&(it.second));
const Variable * get(const StringView &name) const
Retrieve a pointer to the variable with the given name.
bool erase(const StringView &name)
Remove a set variable (so it will revert to default).
Variable * getIfExists(const StringView &name)
Get a pointer to a variable if it exists, otherwise nullptr. Does not modify set of variables.
Log implementation class.
Provides a weakly referenced view over the contents of a string.
constexpr const char * data() const noexcept
Get the sequence of characters referenced by the string view.
constexpr size_t length() const noexcept
Get the length of the string.
std::string to_string() const
String conversion method.
constexpr size_t hash() const noexcept
Get the hash code of the string.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Runtime control variable.
Variable & setValue(const StringView &value)
Set the value of the variable to the given string value.