Plasma Engine  2.0
Loading...
Searching...
No Matches
CVar.h
1#pragma once
2
3#include <Foundation/Communication/Event.h>
4#include <Foundation/Configuration/Plugin.h>
5#include <Foundation/Strings/String.h>
6#include <Foundation/Types/Bitflags.h>
7#include <Foundation/Utilities/EnumerableClass.h>
8
9class plCVar;
10
13{
14 enum Enum
15 {
20 ENUM_COUNT
21 };
22};
23
26{
27 using StorageType = plUInt8;
28
29 enum Enum
30 {
31 None = 0,
32
35 Save = PL_BIT(0),
36
41
42 ShowRequiresRestartMsg = PL_BIT(2),
43
47 RequiresRestart = Save | RequiresDelayedSync | ShowRequiresRestartMsg,
48
50 Default = None
51 };
52
53 struct Bits
54 {
55 StorageType Save : 1;
56 StorageType RequiresDelayedSync : 1;
57 StorageType ShowRequiresRestartMsg : 1;
58 };
59};
60
61PL_DECLARE_FLAGS_OPERATORS(plCVarFlags);
62
84
108class PL_FOUNDATION_DLL plCVar : public plEnumerable<plCVar>
109{
110 PL_DECLARE_ENUMERABLE_CLASS(plCVar);
111
112public:
119 static void SetStorageFolder(plStringView sFolder); // [tested]
120
122 static plCVar* FindCVarByName(plStringView sName); // [tested]
123
129 static void SaveCVars(); // [tested]
130
137 static void SaveCVarsToFile(plStringView sPath, bool bIgnoreSaveFlag = false);
138
140 static void LoadCVars(bool bOnlyNewOnes = true, bool bSetAsCurrentValue = true); // [tested]
141
161 static void LoadCVarsFromFile(bool bOnlyNewOnes = true, bool bSetAsCurrentValue = true, plDynamicArray<plCVar*>* pOutCVars = nullptr); // [tested]
162
182 static void LoadCVarsFromFile(plStringView sPath, bool bOnlyNewOnes = true, bool bSetAsCurrentValue = true, bool bIgnoreSaveFlag = false, plDynamicArray<plCVar*>* pOutCVars = nullptr);
183
192 static void LoadCVarsFromCommandLine(bool bOnlyNewOnes = true, bool bSetAsCurrentValue = true, plDynamicArray<plCVar*>* pOutCVars = nullptr); // [tested]
193
199 virtual void SetToDelayedSyncValue() = 0; // [tested]
200
202 plStringView GetName() const { return m_sName; } // [tested]
203
205 virtual plCVarType::Enum GetType() const = 0; // [tested]
206
208 plStringView GetDescription() const { return m_sDescription; } // [tested]
209
211 plBitflags<plCVarFlags> GetFlags() const { return m_Flags; } // [tested]
212
215
218
220 plStringView GetPluginName() const { return m_sPluginName; }
221
225 static void ListOfCVarsChanged(plStringView sSetPluginNameTo);
226
227protected:
228 plCVar(plStringView sName, plBitflags<plCVarFlags> Flags, plStringView sDescription);
229
230private:
231 PL_MAKE_SUBSYSTEM_STARTUP_FRIEND(Foundation, CVars);
232
233 static void AssignSubSystemPlugin(plStringView sPluginName);
234 static void PluginEventHandler(const plPluginEvent& EventData);
235
237 static void LoadCVarsFromFileInternal(plStringView path, const plDynamicArray<plCVar*>& vars, bool bOnlyNewOnes, bool bSetAsCurrentValue, plDynamicArray<plCVar*>* pOutCVars);
238
240 static void SaveCVarsToFileInternal(plStringView path, const plDynamicArray<plCVar*>& vars);
241
242 bool m_bHasNeverBeenLoaded = true; // next time 'LoadCVars' is called, its state will be changed
243 plStringView m_sName;
244 plStringView m_sDescription;
245 plStringView m_sPluginName;
247
248 static plString s_sStorageFolder;
249};
250
253{
254 enum Enum
255 {
260 ENUM_COUNT
261 };
262};
263
265template <typename Type, plCVarType::Enum CVarType>
266class plTypedCVar : public plCVar
267{
268public:
269 plTypedCVar(plStringView sName, const Type& value, plBitflags<plCVarFlags> flags, plStringView sDescription);
270
272 operator const Type&() const; // [tested]
273
275 const Type& GetValue(plCVarValue::Enum val = plCVarValue::Current) const; // [tested]
276
281 void operator=(const Type& value); // [tested]
282
283 virtual plCVarType::Enum GetType() const override;
284 virtual void SetToDelayedSyncValue() override;
285
288 {
289 return m_Values[plCVarValue::Current] != m_Values[plCVarValue::DelayedSync];
290 }
291
292private:
293 friend class plCVar;
294
295 Type m_Values[plCVarValue::ENUM_COUNT];
296};
297
300
303
306
309
310
311
312#include <Foundation/Configuration/Implementation/CVar_inl.h>
CVars are global variables that are used for configuring the engine.
Definition CVar.h:109
virtual void SetToDelayedSyncValue()=0
Copies the 'DelayedSync' value into the 'Current' value.
static plEvent< const plCVarEvent & > s_AllCVarEvents
Broadcasts changes to ANY CVar. Thus code that needs to update when any one of them changes can use t...
Definition CVar.h:217
plStringView GetPluginName() const
Returns the name of the plugin which this CVar is declared in.
Definition CVar.h:220
plStringView GetName() const
Returns the (display) name of the CVar.
Definition CVar.h:202
plBitflags< plCVarFlags > GetFlags() const
Returns all the CVar flags.
Definition CVar.h:211
plStringView GetDescription() const
Returns the description of the CVar.
Definition CVar.h:208
virtual plCVarType::Enum GetType() const =0
Returns the type of the CVar.
plEvent< const plCVarEvent &, plNoMutex, plStaticsAllocatorWrapper > m_CVarEvents
Code that needs to be execute whenever a cvar is changed can register itself here to be notified of s...
Definition CVar.h:214
Definition DynamicArray.h:81
Base class to add the ability to another class to enumerate all active instance of it,...
Definition EnumerableClass.h:28
Definition Event.h:177
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
[internal] Helper class to implement plCVarInt, plCVarFlag, plCVarBool and plCVarString.
Definition CVar.h:267
void operator=(const Type &value)
Changes the CVar's value and broadcasts the proper events.
Definition CVar_inl.h:51
virtual void SetToDelayedSyncValue() override
Copies the 'DelayedSync' value into the 'Current' value.
Definition CVar_inl.h:28
const Type & GetValue(plCVarValue::Enum val=plCVarValue::Current) const
Returns the internal values of the CVar.
Definition CVar_inl.h:45
virtual plCVarType::Enum GetType() const override
Returns the type of the CVar.
Definition CVar_inl.h:22
bool HasDelayedSyncValueChanged() const
Checks whether a new value was set and now won't be visible until SetToDelayedSyncValue() is called.
Definition CVar.h:287
The plBitflags class allows you to work with type-safe bitflags.
Definition Bitflags.h:82
The data that is broadcast whenever a cvar is changed.
Definition CVar.h:65
Type
Definition CVar.h:72
@ ListOfVarsChanged
A CVar was added or removed dynamically (not just by loading a plugin), some stuff may need to update...
Definition CVar.h:75
@ DelayedSyncValueChanged
Sent whenever the 'DelayedSync' value of the CVar changes. It might actually change back to the 'Curr...
Definition CVar.h:74
@ ValueChanged
Sent whenever the 'Current' value of the CVar is changed.
Definition CVar.h:73
plCVar * m_pCVar
Which CVar is involved. This is only for convenience, it is always the CVar on which the event is tri...
Definition CVar.h:82
Type m_EventType
The type of this event.
Definition CVar.h:79
Definition CVar.h:54
The flags that can be used on an plCVar.
Definition CVar.h:26
Enum
Definition CVar.h:30
@ Default
By default CVars are not saved.
Definition CVar.h:50
@ RequiresRestart
Indicates that changing this CVar will only take effect after the proper subsystem has been reinitial...
Definition CVar.h:47
@ Save
If this flag is set, the CVar will be stored on disk and loaded again. Otherwise all changes to it wi...
Definition CVar.h:35
@ RequiresDelayedSync
If the CVar value is changed, the new value will not be visible by default, until SetToDelayedSyncVal...
Definition CVar.h:40
Describes of which type a CVar is. Use that info to cast an plCVar* to the proper derived class.
Definition CVar.h:13
Enum
Definition CVar.h:15
@ String
Can cast the plCVar* to plCVarString*.
Definition CVar.h:19
@ Bool
Can cast the plCVar* to plCVarBool*.
Definition CVar.h:18
@ Int
Can cast the plCVar* to plCVarInt*.
Definition CVar.h:16
@ Float
Can cast the plCVar* to plCVarFloat*.
Definition CVar.h:17
Each CVar stores several values internally. The 'Current' value is the most important one.
Definition CVar.h:253
Enum
Definition CVar.h:255
@ Stored
The value that was read from disk (or the default). Can be used to reset a CVar to the 'saved' state,...
Definition CVar.h:258
@ Current
The value that should be used.
Definition CVar.h:256
@ DelayedSync
The state that will be stored for later. This is identical to 'Current' unless the 'RequiresDelayedSy...
Definition CVar.h:259
@ Default
The 'default' value of the CVar. Can be used to reset a variable to its default state.
Definition CVar.h:257
The data that is broadcast whenever a plugin is (un-) loaded.
Definition Plugin.h:11