5#include <nlohmann/json.hpp>
8#include <unordered_map>
38 if (
Type == ConsoleVariableType::String &&
String !=
nullptr) {
67 std::shared_ptr<CVar>
Get(
const char* name);
74 int32_t
GetInteger(
const char* name, int32_t defaultValue);
81 float GetFloat(
const char* name,
float defaultValue);
89 const char*
GetString(
const char* name,
const char* defaultValue);
202 nlohmann::detail::iteration_proxy<nlohmann::detail::iter_impl<nlohmann::json>> items);
206 struct TransparentStringHash {
207 using is_transparent = void;
208 size_t operator()(std::string_view sv)
const noexcept {
209 return std::hash<std::string_view>{}(sv);
212 struct TransparentStringEqual {
213 using is_transparent = void;
214 bool operator()(std::string_view a, std::string_view b)
const noexcept {
218 std::unordered_map<std::string, std::shared_ptr<CVar>, TransparentStringHash, TransparentStringEqual> mVariables;
219 std::shared_ptr<Config> mConfig;
A named Part with a parent/child hierarchy and optional thread safety.
Definition Component.h:34
Manages a named collection of console variables (CVars).
Definition ConsoleVariable.h:58
std::shared_ptr< CVar > Get(const char *name)
Returns the raw CVar entry for the given name, or nullptr if not found.
void RegisterColor24(const char *name, Color_RGB8 defaultValue)
Registers an RGB colour CVar with a default value if it does not already exist.
float GetFloat(const char *name, float defaultValue)
Returns the float value of a CVar, or the default if not found or wrong type.
void SetFloat(const char *name, float value)
Sets (or creates) a float CVar.
void CopyVariable(const char *from, const char *to)
Copies the value of one CVar to another (creating the destination if needed).
const char * GetString(const char *name, const char *defaultValue)
Returns the string value of a CVar, or the default if not found or wrong type.
void SetColor(const char *name, Color_RGBA8 value)
Sets (or creates) an RGBA colour CVar.
void SetInteger(const char *name, int32_t value)
Sets (or creates) an integer CVar.
void ClearVariable(const char *name)
Removes a single CVar entry from the map.
void ClearBlock(const char *name)
Removes all CVars whose names start with the given prefix.
void RegisterColor(const char *name, Color_RGBA8 defaultValue)
Registers an RGBA colour CVar with a default value if it does not already exist.
ConsoleVariable(std::shared_ptr< Config > config=nullptr)
Color_RGB8 GetColor24(const char *name, Color_RGB8 defaultValue)
Returns the RGB colour value of a CVar, or the default if not found or wrong type.
void RegisterInteger(const char *name, int32_t defaultValue)
Registers an integer CVar with a default value if it does not already exist.
void RegisterString(const char *name, const char *defaultValue)
Registers a string CVar with a default value if it does not already exist.
void RegisterFloat(const char *name, float defaultValue)
Registers a float CVar with a default value if it does not already exist.
Color_RGBA8 GetColor(const char *name, Color_RGBA8 defaultValue)
Returns the RGBA colour value of a CVar, or the default if not found or wrong type.
void Save()
Serializes all CVars to the backing JSON config file.
void Load()
Loads CVars from the backing JSON config file, overwriting in-memory values.
void LoadFromPath(std::string path, nlohmann::detail::iteration_proxy< nlohmann::detail::iter_impl< nlohmann::json > > items)
void SetString(const char *name, const char *value)
Sets (or creates) a string CVar.
int32_t GetInteger(const char *name, int32_t defaultValue)
Returns the integer value of a CVar, or the default if not found or wrong type.
void SetColor24(const char *name, Color_RGB8 value)
Sets (or creates) an RGB colour CVar.
Core namespace for the libultraship engine framework.
Definition gfx_direct3d_common.h:14
ConsoleVariableType
Discriminator tag for the active field of the CVar union.
Definition ConsoleVariable.h:16
8-bit RGB colour without an alpha channel.
Definition color.h:14
8-bit RGBA colour (red, green, blue, alpha).
Definition color.h:26
A single console variable (CVar) holding a typed value.
Definition ConsoleVariable.h:27
int32_t Integer
Active when Type == ConsoleVariableType::Integer.
Definition ConsoleVariable.h:31
~CVar()
Definition ConsoleVariable.h:37
Color_RGBA8 Color
Active when Type == ConsoleVariableType::Color.
Definition ConsoleVariable.h:34
ConsoleVariableType Type
Discriminator indicating which union field is active.
Definition ConsoleVariable.h:29
char * String
Active when Type == ConsoleVariableType::String (heap-allocated).
Definition ConsoleVariable.h:33
float Float
Active when Type == ConsoleVariableType::Float.
Definition ConsoleVariable.h:32
Color_RGB8 Color24
Active when Type == ConsoleVariableType::Color24.
Definition ConsoleVariable.h:35