libultraship
Reimplementations of libultra (N64 SDK) functions for modern hardware
Loading...
Searching...
No Matches
ConsoleVariable.h
Go to the documentation of this file.
1#pragma once
2
3#include "ship/utils/color.h"
5#include <nlohmann/json.hpp>
6#include <stdint.h>
7#include <memory>
8#include <unordered_map>
9#include <string>
10#include <string_view>
11
12namespace Ship {
13class Config;
14
17
27typedef struct CVar {
30 union {
31 int32_t Integer;
32 float Float;
33 char* String = nullptr;
36 };
38 if (Type == ConsoleVariableType::String && String != nullptr) {
39 free(String);
40 }
41 }
42} CVar;
43
58class ConsoleVariable : public Component {
59 public:
60 explicit ConsoleVariable(std::shared_ptr<Config> config = nullptr);
62
67 std::shared_ptr<CVar> Get(const char* name);
68
74 int32_t GetInteger(const char* name, int32_t defaultValue);
75
81 float GetFloat(const char* name, float defaultValue);
82
89 const char* GetString(const char* name, const char* defaultValue);
90
96 Color_RGBA8 GetColor(const char* name, Color_RGBA8 defaultValue);
97
103 Color_RGB8 GetColor24(const char* name, Color_RGB8 defaultValue);
104
110 void SetInteger(const char* name, int32_t value);
111
117 void SetFloat(const char* name, float value);
118
124 void SetString(const char* name, const char* value);
125
131 void SetColor(const char* name, Color_RGBA8 value);
132
138 void SetColor24(const char* name, Color_RGB8 value);
139
145 void RegisterInteger(const char* name, int32_t defaultValue);
146
152 void RegisterFloat(const char* name, float defaultValue);
153
159 void RegisterString(const char* name, const char* defaultValue);
160
166 void RegisterColor(const char* name, Color_RGBA8 defaultValue);
167
173 void RegisterColor24(const char* name, Color_RGB8 defaultValue);
174
179 void ClearVariable(const char* name);
180
185 void ClearBlock(const char* name);
186
192 void CopyVariable(const char* from, const char* to);
193
195 void Save();
196
198 void Load();
199
200 protected:
201 void LoadFromPath(std::string path,
202 nlohmann::detail::iteration_proxy<nlohmann::detail::iter_impl<nlohmann::json>> items);
204
205 private:
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);
210 }
211 };
212 struct TransparentStringEqual {
213 using is_transparent = void;
214 bool operator()(std::string_view a, std::string_view b) const noexcept {
215 return a == b;
216 }
217 };
218 std::unordered_map<std::string, std::shared_ptr<CVar>, TransparentStringHash, TransparentStringEqual> mVariables;
219 std::shared_ptr<Config> mConfig;
220};
221} // namespace Ship
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