libultraship
Reimplementations of libultra (N64 SDK) functions for modern hardware
Loading...
Searching...
No Matches
Ship::ConsoleVariable Class Reference

Manages a named collection of console variables (CVars). More...

#include <ConsoleVariable.h>

Inheritance diagram for Ship::ConsoleVariable:
[legend]
Collaboration diagram for Ship::ConsoleVariable:
[legend]

Public Member Functions

 ConsoleVariable (std::shared_ptr< Config > config=nullptr)
 
 ~ConsoleVariable ()
 
std::shared_ptr< CVarGet (const char *name)
 Returns the raw CVar entry for the given name, or nullptr if not found.
 
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.
 
float GetFloat (const char *name, float defaultValue)
 Returns the float value of a CVar, or the default if not found or wrong type.
 
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.
 
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.
 
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 SetInteger (const char *name, int32_t value)
 Sets (or creates) an integer CVar.
 
void SetFloat (const char *name, float value)
 Sets (or creates) a float CVar.
 
void SetString (const char *name, const char *value)
 Sets (or creates) a string CVar.
 
void SetColor (const char *name, Color_RGBA8 value)
 Sets (or creates) an RGBA colour CVar.
 
void SetColor24 (const char *name, Color_RGB8 value)
 Sets (or creates) an RGB colour CVar.
 
void RegisterInteger (const char *name, int32_t defaultValue)
 Registers an integer 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.
 
void RegisterString (const char *name, const char *defaultValue)
 Registers a string CVar with a default value if it does not already exist.
 
void RegisterColor (const char *name, Color_RGBA8 defaultValue)
 Registers an RGBA colour CVar with a default value if it does not already exist.
 
void RegisterColor24 (const char *name, Color_RGB8 defaultValue)
 Registers an RGB colour CVar with a default value if it does not already exist.
 
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 CopyVariable (const char *from, const char *to)
 Copies the value of one CVar to another (creating the destination if needed).
 
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.
 
- Public Member Functions inherited from Ship::Component
 Component (const std::string &name, std::shared_ptr< Context > context=nullptr)
 Constructs a Component with the given name.
 
virtual ~Component ()
 
void Init (const nlohmann::json &initArgs=nlohmann::json::object())
 Performs one-time initialization of this component.
 
bool IsInitialized () const
 Returns true once Init() (or MarkInitialized()) has completed successfully.
 
const std::string & GetName () const
 Returns the name of this Component.
 
std::string ToString () const
 Returns a human-readable string representation (e.g. "Name (id)").
 
std::string ToTreeString (int depth=0) const
 Returns a human-readable tree representation of this component and its children.
 
 operator std::string () const
 Conversion operator to std::string; equivalent to ToString().
 
ParentComponentListGetParents ()
 Returns a mutable reference to the parent list.
 
const ParentComponentListGetParents () const
 Returns a const reference to the parent list.
 
ComponentListGetChildren ()
 Returns a mutable reference to the child list.
 
const ComponentListGetChildren () const
 Returns a const reference to the child list.
 
virtual std::shared_ptr< ComponentTryGetSharedComponent () noexcept
 Returns a shared_ptr to this Component when available, otherwise nullptr.
 
virtual std::shared_ptr< ComponentGetSharedComponent ()
 Returns a shared_ptr to this Component via the correct enable_shared_from_this base.
 
template<typename T >
bool HasInChildren () const
 Checks whether any descendant Component matches type T via BFS.
 
template<typename T >
std::shared_ptr< T > GetFirstInChildren () const
 Returns the first descendant that matches type T via BFS.
 
template<typename T >
std::shared_ptr< std::vector< std::shared_ptr< T > > > GetInChildren () const
 Returns all descendants that match type T via BFS.
 
template<typename T >
bool HasInParents () const
 Checks whether any ancestor Component matches type T via BFS.
 
template<typename T >
std::shared_ptr< T > GetFirstInParents () const
 Returns the first ancestor that matches type T via BFS.
 
template<typename T >
std::shared_ptr< std::vector< std::shared_ptr< T > > > GetInParents () const
 Returns all ancestors that match type T via BFS.
 
- Public Member Functions inherited from Ship::Part
 Part ()
 Constructs a Part and assigns it a unique ID.
 
 Part (std::shared_ptr< Context > context)
 Constructs a Part with an explicit Context reference and unique ID.
 
virtual ~Part ()=default
 
uint64_t GetId () const
 Returns the unique identifier for this Part.
 
bool operator== (const Part &other) const
 Compares two Parts for equality by their unique IDs.
 
std::shared_ptr< ContextGetContext () const
 Returns the Context this Part belongs to, or nullptr if unset.
 
void SetContext (std::shared_ptr< Context > ctx)
 Sets the Context this Part belongs to.
 

Protected Member Functions

void LoadFromPath (std::string path, nlohmann::detail::iteration_proxy< nlohmann::detail::iter_impl< nlohmann::json > > items)
 
void LoadLegacy ()
 
- Protected Member Functions inherited from Ship::Component
virtual void OnInit (const nlohmann::json &initArgs=nlohmann::json::object())
 Override this to implement component-specific initialization logic.
 
void MarkInitialized ()
 Marks this component as initialized without going through Init().
 
template<typename T >
std::shared_ptr< T > RequireDependency (const std::shared_ptr< T > &dependency, const std::string &dependencyName) const
 Returns a cached dependency after validating it exists and is initialized.
 
- Protected Member Functions inherited from Ship::Part
virtual void OnAdded (bool forced)
 Called after this Part has been added to a PartList.
 
virtual void OnRemoved (bool forced)
 Called after this Part has been removed from a PartList.
 

Detailed Description

Manages a named collection of console variables (CVars).

ConsoleVariable persists CVar values across sessions by serialising them to and from a JSON file via the Config layer. Values can be registered with defaults, queried, mutated, copied, and cleared at runtime.

Required Context children (looked up at runtime):

Obtain the instance from Context::GetChildren().GetFirst<ConsoleVariable>().

Constructor & Destructor Documentation

◆ ConsoleVariable()

Ship::ConsoleVariable::ConsoleVariable ( std::shared_ptr< Config config = nullptr)
explicit

◆ ~ConsoleVariable()

Ship::ConsoleVariable::~ConsoleVariable ( )

Member Function Documentation

◆ ClearBlock()

void Ship::ConsoleVariable::ClearBlock ( const char *  name)

Removes all CVars whose names start with the given prefix.

Parameters
namePrefix string to match.

◆ ClearVariable()

void Ship::ConsoleVariable::ClearVariable ( const char *  name)

Removes a single CVar entry from the map.

Parameters
nameCVar name to remove.

◆ CopyVariable()

void Ship::ConsoleVariable::CopyVariable ( const char *  from,
const char *  to 
)

Copies the value of one CVar to another (creating the destination if needed).

Parameters
fromSource CVar name.
toDestination CVar name.

◆ Get()

std::shared_ptr< CVar > Ship::ConsoleVariable::Get ( const char *  name)

Returns the raw CVar entry for the given name, or nullptr if not found.

Parameters
nameCVar name (case-sensitive).

◆ GetColor()

Color_RGBA8 Ship::ConsoleVariable::GetColor ( const char *  name,
Color_RGBA8  defaultValue 
)

Returns the RGBA colour value of a CVar, or the default if not found or wrong type.

Parameters
nameCVar name.
defaultValueFallback colour.

◆ GetColor24()

Color_RGB8 Ship::ConsoleVariable::GetColor24 ( const char *  name,
Color_RGB8  defaultValue 
)

Returns the RGB colour value of a CVar, or the default if not found or wrong type.

Parameters
nameCVar name.
defaultValueFallback colour.

◆ GetFloat()

float Ship::ConsoleVariable::GetFloat ( const char *  name,
float  defaultValue 
)

Returns the float value of a CVar, or the default if not found or wrong type.

Parameters
nameCVar name.
defaultValueValue to return when the CVar is absent.

◆ GetInteger()

int32_t Ship::ConsoleVariable::GetInteger ( const char *  name,
int32_t  defaultValue 
)

Returns the integer value of a CVar, or the default if not found or wrong type.

Parameters
nameCVar name.
defaultValueValue to return when the CVar is absent.

◆ GetString()

const char * Ship::ConsoleVariable::GetString ( const char *  name,
const char *  defaultValue 
)

Returns the string value of a CVar, or the default if not found or wrong type.

Parameters
nameCVar name.
defaultValueValue to return when the CVar is absent.
Returns
Pointer to internal storage; do not free or store long-term.

◆ Load()

void Ship::ConsoleVariable::Load ( )

Loads CVars from the backing JSON config file, overwriting in-memory values.

◆ LoadFromPath()

void Ship::ConsoleVariable::LoadFromPath ( std::string  path,
nlohmann::detail::iteration_proxy< nlohmann::detail::iter_impl< nlohmann::json > >  items 
)
protected

◆ LoadLegacy()

void Ship::ConsoleVariable::LoadLegacy ( )
protected

◆ RegisterColor()

void Ship::ConsoleVariable::RegisterColor ( const char *  name,
Color_RGBA8  defaultValue 
)

Registers an RGBA colour CVar with a default value if it does not already exist.

Parameters
nameCVar name.
defaultValueDefault RGBA colour.

◆ RegisterColor24()

void Ship::ConsoleVariable::RegisterColor24 ( const char *  name,
Color_RGB8  defaultValue 
)

Registers an RGB colour CVar with a default value if it does not already exist.

Parameters
nameCVar name.
defaultValueDefault RGB colour.

◆ RegisterFloat()

void Ship::ConsoleVariable::RegisterFloat ( const char *  name,
float  defaultValue 
)

Registers a float CVar with a default value if it does not already exist.

Parameters
nameCVar name.
defaultValueDefault value.

◆ RegisterInteger()

void Ship::ConsoleVariable::RegisterInteger ( const char *  name,
int32_t  defaultValue 
)

Registers an integer CVar with a default value if it does not already exist.

Parameters
nameCVar name.
defaultValueDefault value to set when first registered.

◆ RegisterString()

void Ship::ConsoleVariable::RegisterString ( const char *  name,
const char *  defaultValue 
)

Registers a string CVar with a default value if it does not already exist.

Parameters
nameCVar name.
defaultValueDefault string (copied internally).

◆ Save()

void Ship::ConsoleVariable::Save ( )

Serializes all CVars to the backing JSON config file.

◆ SetColor()

void Ship::ConsoleVariable::SetColor ( const char *  name,
Color_RGBA8  value 
)

Sets (or creates) an RGBA colour CVar.

Parameters
nameCVar name.
valueNew RGBA colour.

◆ SetColor24()

void Ship::ConsoleVariable::SetColor24 ( const char *  name,
Color_RGB8  value 
)

Sets (or creates) an RGB colour CVar.

Parameters
nameCVar name.
valueNew RGB colour.

◆ SetFloat()

void Ship::ConsoleVariable::SetFloat ( const char *  name,
float  value 
)

Sets (or creates) a float CVar.

Parameters
nameCVar name.
valueNew float value.

◆ SetInteger()

void Ship::ConsoleVariable::SetInteger ( const char *  name,
int32_t  value 
)

Sets (or creates) an integer CVar.

Parameters
nameCVar name.
valueNew integer value.

◆ SetString()

void Ship::ConsoleVariable::SetString ( const char *  name,
const char *  value 
)

Sets (or creates) a string CVar.

Parameters
nameCVar name.
valueNew string value (copied internally).

The documentation for this class was generated from the following file: