BadScript 2
Loading...
Searching...
No Matches
BadScript2.Settings.BadSettings Class Reference

Public Api for the Settings System. More...

Public Member Functions

 BadSettings (string sourcePath)
 Creates a new empty Settings Object.
 
 BadSettings (JToken? value, string sourcePath)
 Creates a new Settings Object from a Json Token.
 
 BadSettings (Dictionary< string, BadSettings > properties, string sourcePath)
 Creates a new Settings Object from a Dictionary of Properties.
 
JToken? GetValue ()
 Returns the Json Token of the Settings Object.
 
T? GetValue< T > ()
 Returns a Deserialized Value of the Settings Object.
 
bool HasValue ()
 Returns true if the Settings Object has a JToken Value.
 
void SetValue (JToken? value, bool invokeOnChange=true)
 Sets the Json Token of the Current Settings Object.
 
bool HasProperty (string propertyName)
 Returns true if the Settings Object has the given Property.
 
BadSettings GetProperty (string propertyName)
 Returns the Property with the given Name.
 
void SetProperty (string propertyName, BadSettings value, bool invokeOnChange=true)
 Sets the Property with the given Name.
 
bool RemoveProperty (string propertyName, bool invokeOnChange=true)
 Removes the Property with the given Name.
 
void Populate (bool invokeOnChanged, params BadSettings[] settings)
 Populates the current object with the settings provided.
 
T? FindProperty< T > (string propertyName)
 Finds a property based on the property path relative to this object.
 
BadSettings FindOrCreateProperty (string propertyPath)
 Finds a property based on the property path relative to this object.
 
BadSettingsFindProperty (string propertyPath)
 Finds a property based on the property path relative to this object.
 
override string ToString ()
 Returns a string representation of the current object.
 

Static Public Member Functions

static void ResolveEnvironmentVariables (BadSettings root)
 Resolves Environment Variables in the current object.
 
static void ResolveEnvironmentVariables (BadSettings root, BadSettings settings, BadSettings parent)
 Resolves Environment Variables in the current object.
 

Properties

bool HasSourcePath [get]
 
string SourcePath [get]
 
IEnumerable< string > PropertyNames [get]
 The Property Names of the Settings Object.
 

Events

Action OnValueChanged = delegate { }
 

Private Member Functions

void PropertyValueChanged ()
 
void InvokeValueChanged ()
 
bool HasValue< T > ()
 Returns true if the Settings Object can be Deserialized into the given Type.
 
void SetValue (object? value)
 Sets the Json Token of the Current Settings Object.
 

Static Private Member Functions

static string ResolveEnvironmentVariables (BadSettings root, BadSettings parent, string str)
 Resolves Environment Variables in the current object.
 

Private Attributes

readonly Dictionary< string, BadSettingsm_Properties
 The properties of the Current Settings Object.
 
readonly string m_SourcePath
 The Source Path of the Settings Object.
 
object? m_Cache
 Cache for the Serialized Value of the Settings.
 
bool m_IsDirty
 Indicates if the current object is dirty and needs to be re-serialized.
 
JToken? m_Value
 The Json Token of the Settings Object.
 

Detailed Description

Public Api for the Settings System.

Definition at line 10 of file BadSettings.cs.

Constructor & Destructor Documentation

◆ BadSettings() [1/3]

BadScript2.Settings.BadSettings.BadSettings ( string  sourcePath)

Creates a new empty Settings Object.

Parameters
sourcePathThe Source Path of the Settings Object

Definition at line 44 of file BadSettings.cs.

45 {
46 m_SourcePath = sourcePath;
47 m_Value = null;
48 m_IsDirty = true;
49 m_Properties = new Dictionary<string, BadSettings>();
50 }
readonly Dictionary< string, BadSettings > m_Properties
The properties of the Current Settings Object.
readonly string m_SourcePath
The Source Path of the Settings Object.
JToken? m_Value
The Json Token of the Settings Object.
bool m_IsDirty
Indicates if the current object is dirty and needs to be re-serialized.

◆ BadSettings() [2/3]

BadScript2.Settings.BadSettings.BadSettings ( JToken?  value,
string  sourcePath 
)

Creates a new Settings Object from a Json Token.

Parameters
valueThe Json Token
sourcePathThe Source Path of the Settings Object

Definition at line 57 of file BadSettings.cs.

58 {
59 m_Value = value;
60 m_SourcePath = sourcePath;
61 m_IsDirty = true;
62 m_Properties = new Dictionary<string, BadSettings>();
63 }

◆ BadSettings() [3/3]

BadScript2.Settings.BadSettings.BadSettings ( Dictionary< string, BadSettings properties,
string  sourcePath 
)

Creates a new Settings Object from a Dictionary of Properties.

Parameters
propertiesThe Properties
sourcePathThe Source Path of the Settings Object

Definition at line 70 of file BadSettings.cs.

71 {
72 m_Value = null;
73 m_IsDirty = true;
74 m_Properties = properties;
75 m_SourcePath = sourcePath;
76 foreach (KeyValuePair<string, BadSettings> kvp in m_Properties)
77 {
78 kvp.Value.OnValueChanged += PropertyValueChanged;
79 }
80 }

Member Function Documentation

◆ FindOrCreateProperty()

BadSettings BadScript2.Settings.BadSettings.FindOrCreateProperty ( string  propertyPath)

Finds a property based on the property path relative to this object.

Parameters
propertyPathProperty Path
Returns
Settings Object

Definition at line 329 of file BadSettings.cs.

330 {
331 string[] path = propertyPath.Split('.');
332 BadSettings current = this;
333
334 foreach (string s in path)
335 {
336 if (!current.HasProperty(s))
337 {
338 BadSettings se = new BadSettings(string.Empty);
339 current.SetProperty(s, se);
340 current = se;
341 }
342 else
343 {
344 current = current.GetProperty(s);
345 }
346 }
347
348 return current;
349 }
BadSettings(string sourcePath)
Creates a new empty Settings Object.

◆ FindProperty()

BadSettings? BadScript2.Settings.BadSettings.FindProperty ( string  propertyPath)

Finds a property based on the property path relative to this object.

Parameters
propertyPathProperty Path
Returns
Settings Object

Definition at line 356 of file BadSettings.cs.

357 {
358 string[] path = propertyPath.Split('.');
359 BadSettings current = this;
360
361 foreach (string s in path)
362 {
363 if (!current.HasProperty(s))
364 {
365 return null;
366 }
367
368 current = current.GetProperty(s);
369 }
370
371 return current;
372 }

◆ FindProperty< T >()

T? BadScript2.Settings.BadSettings.FindProperty< T > ( string  propertyName)

Finds a property based on the property path relative to this object.

Parameters
propertyNameProperty Path
Template Parameters
TType to deserialize into
Returns
Deserialized Value
Type Constraints
T :class 

Definition at line 317 of file BadSettings.cs.

317 : class
318 {
319 BadSettings? settings = FindProperty(propertyName);
320
321 return settings?.GetValue<T>();
322 }
BadSettings? FindProperty(string propertyPath)
Finds a property based on the property path relative to this object.

◆ GetProperty()

BadSettings BadScript2.Settings.BadSettings.GetProperty ( string  propertyName)

Returns the Property with the given Name.

Parameters
propertyNameThe Property Name
Returns
The Property

Definition at line 200 of file BadSettings.cs.

201 {
202 return m_Properties[propertyName];
203 }

◆ GetValue()

JToken? BadScript2.Settings.BadSettings.GetValue ( )

Returns the Json Token of the Settings Object.

Returns
Json Token

Definition at line 104 of file BadSettings.cs.

105 {
106 return m_Value;
107 }

◆ GetValue< T >()

Returns a Deserialized Value of the Settings Object.

Template Parameters
TThe Type to Deserialize into
Returns
Deserialized Result

Definition at line 114 of file BadSettings.cs.

115 {
116 if (!m_IsDirty)
117 {
118 return (T?)m_Cache;
119 }
120
121 if (m_Value == null)
122 {
123 return default;
124 }
125
126 T? v = m_Value.ToObject<T>();
127
128 m_Cache = v;
129 m_IsDirty = false;
130
131 return v;
132 }
object? m_Cache
Cache for the Serialized Value of the Settings.

◆ HasProperty()

bool BadScript2.Settings.BadSettings.HasProperty ( string  propertyName)

Returns true if the Settings Object has the given Property.

Parameters
propertyNameThe Property Name
Returns
true if the Property exists

Definition at line 190 of file BadSettings.cs.

191 {
192 return m_Properties.ContainsKey(propertyName);
193 }

◆ HasValue()

bool BadScript2.Settings.BadSettings.HasValue ( )

Returns true if the Settings Object has a JToken Value.

Returns

Definition at line 138 of file BadSettings.cs.

139 {
140 return m_Value != null;
141 }

◆ HasValue< T >()

Returns true if the Settings Object can be Deserialized into the given Type.

Template Parameters
TThe Type
Returns
true if Deserializable

Definition at line 148 of file BadSettings.cs.

149 {
150 if (m_Value?.Type == JTokenType.Array && !typeof(T).IsArray)
151 {
152 return false;
153 }
154
155 return m_Value != null && m_Value.ToObject<T>() != null;
156 }

◆ InvokeValueChanged()

void BadScript2.Settings.BadSettings.InvokeValueChanged ( )
private

Definition at line 95 of file BadSettings.cs.

◆ Populate()

void BadScript2.Settings.BadSettings.Populate ( bool  invokeOnChanged,
params BadSettings[]  settings 
)

Populates the current object with the settings provided.

Parameters
invokeOnChangedIndicates if the OnChanged Event should be invoked
settingsThe settings this object will be populated with

Definition at line 261 of file BadSettings.cs.

262 {
263 if (m_Value is JArray arr)
264 {
265 foreach (BadSettings setting in settings)
266 {
267 if (!setting.HasValue())
268 {
269 continue;
270 }
271
272 if (setting.GetValue() is JArray arr2)
273 {
274 foreach (JToken jToken in arr2)
275 {
276 arr.Add(jToken);
277 }
278 }
279 else
280 {
281 arr.Add(setting.GetValue() ?? JValue.CreateNull());
282 }
283 }
284 }
285
286 foreach (BadSettings setting in settings)
287 {
288 foreach (string propertyName in setting.PropertyNames)
289 {
290 if (HasProperty(propertyName))
291 {
292 GetProperty(propertyName).Populate(false, setting.GetProperty(propertyName));
293 }
294 else
295 {
296 SetProperty(propertyName, setting.GetProperty(propertyName), false);
297 }
298 }
299 }
300
301 if (invokeOnChanged)
302 {
304 }
305 else
306 {
307 m_IsDirty = true;
308 }
309 }
void Populate(bool invokeOnChanged, params BadSettings[] settings)
Populates the current object with the settings provided.
void SetProperty(string propertyName, BadSettings value, bool invokeOnChange=true)
Sets the Property with the given Name.
BadSettings GetProperty(string propertyName)
Returns the Property with the given Name.
bool HasProperty(string propertyName)
Returns true if the Settings Object has the given Property.

◆ PropertyValueChanged()

void BadScript2.Settings.BadSettings.PropertyValueChanged ( )
private

Definition at line 89 of file BadSettings.cs.

90 {
91 m_IsDirty = true;
93 }

◆ RemoveProperty()

bool BadScript2.Settings.BadSettings.RemoveProperty ( string  propertyName,
bool  invokeOnChange = true 
)

Removes the Property with the given Name.

Parameters
propertyNameThe Property Name
invokeOnChangeIndicates if the OnChange Event should be invoked

Definition at line 234 of file BadSettings.cs.

235 {
236 if (m_Properties.TryGetValue(propertyName, out BadSettings? old))
237 {
238 old.OnValueChanged -= PropertyValueChanged;
239 }
240
241 bool r = m_Properties.Remove(propertyName);
242
243 if (invokeOnChange)
244 {
246 }
247 else
248 {
249 m_IsDirty = true;
250 }
251
252 return r;
253 }

◆ ResolveEnvironmentVariables() [1/3]

static void BadScript2.Settings.BadSettings.ResolveEnvironmentVariables ( BadSettings  root)
static

Resolves Environment Variables in the current object.

Parameters
rootThe Root settings

Definition at line 453 of file BadSettings.cs.

454 {
455 ResolveEnvironmentVariables(root, root, root);
456 }
static string ResolveEnvironmentVariables(BadSettings root, BadSettings parent, string str)
Resolves Environment Variables in the current object.

◆ ResolveEnvironmentVariables() [2/3]

static string BadScript2.Settings.BadSettings.ResolveEnvironmentVariables ( BadSettings  root,
BadSettings  parent,
string  str 
)
staticprivate

Resolves Environment Variables in the current object.

Parameters
rootThe Root settings
parentThe Parent Object
strThe String to Expand
Returns
the Resolved variable
Exceptions
ExceptionGets raised if the environment variable syntax is invalid or the environment variable is not found

Definition at line 412 of file BadSettings.cs.

413 {
414 for (int i = 0; i < str.Length; i++)
415 {
416 if (str[i] != '$' || i + 1 >= str.Length || str[i + 1] != '(')
417 {
418 continue;
419 }
420
421 int end = str.IndexOf(')', i + 2);
422
423 if (end == -1)
424 {
425 throw new Exception("Unclosed environment variable");
426 }
427
428 string envVar = str.Substring(i + 2, end - i - 2);
429
430 string? env = root.FindProperty<string>(envVar);
431
432 if (env == null)
433 {
434 env = parent.FindProperty<string>(envVar);
435
436 if (env == null)
437 {
438 throw new Exception($"Environment variable '{envVar}' not found");
439 }
440 }
441
442 str = str.Replace(str.Substring(i, end - i + 1), env);
443 i--;
444 }
445
446 return str;
447 }

◆ ResolveEnvironmentVariables() [3/3]

static void BadScript2.Settings.BadSettings.ResolveEnvironmentVariables ( BadSettings  root,
BadSettings  settings,
BadSettings  parent 
)
static

Resolves Environment Variables in the current object.

Parameters
rootThe Root settings
settingsThe Current Settings Object
parentThe Parent Object

Definition at line 464 of file BadSettings.cs.

465 {
466 if (settings.HasValue<string>())
467 {
468 string value = settings.GetValue<string>()!;
469
470 settings.SetValue(ResolveEnvironmentVariables(root, parent, value));
471 }
472 else if (settings.HasValue<string[]>())
473 {
474 string[] value = settings.GetValue<string[]>()!;
475
476 for (int i = 0; i < value.Length; i++)
477 {
478 value[i] = ResolveEnvironmentVariables(root, parent, value[i]);
479 }
480
481 settings.SetValue(value);
482 }
483
484 foreach (string propertyName in settings.PropertyNames)
485 {
486 ResolveEnvironmentVariables(root, settings.GetProperty(propertyName), settings);
487 }
488 }

◆ SetProperty()

void BadScript2.Settings.BadSettings.SetProperty ( string  propertyName,
BadSettings  value,
bool  invokeOnChange = true 
)

Sets the Property with the given Name.

Parameters
propertyNameThe Property Name
valueThe Property Value
invokeOnChangeIndicates if the OnChange Event should be invoked

Definition at line 211 of file BadSettings.cs.

212 {
213 if (m_Properties.TryGetValue(propertyName, out BadSettings? old))
214 {
215 old.OnValueChanged -= PropertyValueChanged;
216 }
217
218 m_Properties[propertyName] = value;
219 if (invokeOnChange)
220 {
222 }
223 else
224 {
225 m_IsDirty = true;
226 }
227 }

◆ SetValue() [1/2]

void BadScript2.Settings.BadSettings.SetValue ( JToken?  value,
bool  invokeOnChange = true 
)

Sets the Json Token of the Current Settings Object.

Parameters
valueJson Token
invokeOnChangeIndicates if the OnChange Event should be invoked

Definition at line 163 of file BadSettings.cs.

164 {
165 m_Value = value;
166 if (invokeOnChange)
167 {
169 }
170 else
171 {
172 m_IsDirty = true;
173 }
174 }

◆ SetValue() [2/2]

void BadScript2.Settings.BadSettings.SetValue ( object?  value)
private

Sets the Json Token of the Current Settings Object.

Parameters
valueThe Deserialized Object

Definition at line 180 of file BadSettings.cs.

181 {
182 SetValue(value == null ? JValue.CreateNull() : JToken.FromObject(value));
183 }
void SetValue(JToken? value, bool invokeOnChange=true)
Sets the Json Token of the Current Settings Object.

◆ ToString()

override string BadScript2.Settings.BadSettings.ToString ( )

Returns a string representation of the current object.

Returns
String Representation

Definition at line 378 of file BadSettings.cs.

379 {
380 if (HasValue())
381 {
382 return GetValue()?.ToString() ?? "NULL";
383 }
384
385 StringBuilder sb = new StringBuilder();
386
387 sb.AppendLine("{");
388
389 foreach (string propertyName in PropertyNames)
390 {
391 BadSettings s = GetProperty(propertyName);
392 string str = s.ToString().Replace("\n", "\n\t");
393 sb.AppendLine($" {propertyName}: {str}");
394 }
395
396 sb.AppendLine("}");
397
398 return sb.ToString();
399 }
IEnumerable< string > PropertyNames
The Property Names of the Settings Object.
bool HasValue()
Returns true if the Settings Object has a JToken Value.
JToken? GetValue()
Returns the Json Token of the Settings Object.

Member Data Documentation

◆ m_Cache

object? BadScript2.Settings.BadSettings.m_Cache
private

Cache for the Serialized Value of the Settings.

Definition at line 28 of file BadSettings.cs.

◆ m_IsDirty

bool BadScript2.Settings.BadSettings.m_IsDirty
private

Indicates if the current object is dirty and needs to be re-serialized.

Definition at line 33 of file BadSettings.cs.

◆ m_Properties

readonly Dictionary<string, BadSettings> BadScript2.Settings.BadSettings.m_Properties
private

The properties of the Current Settings Object.

Definition at line 15 of file BadSettings.cs.

◆ m_SourcePath

readonly string BadScript2.Settings.BadSettings.m_SourcePath
private

The Source Path of the Settings Object.

Definition at line 20 of file BadSettings.cs.

◆ m_Value

JToken? BadScript2.Settings.BadSettings.m_Value
private

The Json Token of the Settings Object.

Definition at line 38 of file BadSettings.cs.

Property Documentation

◆ HasSourcePath

bool BadScript2.Settings.BadSettings.HasSourcePath
get

Definition at line 22 of file BadSettings.cs.

◆ PropertyNames

IEnumerable<string> BadScript2.Settings.BadSettings.PropertyNames
get

The Property Names of the Settings Object.

Definition at line 85 of file BadSettings.cs.

◆ SourcePath

string BadScript2.Settings.BadSettings.SourcePath
get

Definition at line 23 of file BadSettings.cs.

Event Documentation

◆ OnValueChanged

Action BadScript2.Settings.BadSettings.OnValueChanged = delegate { }

Definition at line 87 of file BadSettings.cs.

87{ };

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