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]
 The Source Path of the Settings Object.
 
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 ConcurrentDictionary< string, BadSettingsm_Properties
 The properties of the Current 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 11 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 37 of file BadSettings.cs.

38 {
39 SourcePath = sourcePath;
40 m_Value = null;
41 m_IsDirty = true;
42 m_Properties = new ConcurrentDictionary<string, BadSettings>();
43 }
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.
string SourcePath
The Source Path of the Settings Object.
readonly ConcurrentDictionary< string, BadSettings > m_Properties
The properties of the Current Settings Object.

◆ 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 50 of file BadSettings.cs.

51 {
52 m_Value = value;
53 SourcePath = sourcePath;
54 m_IsDirty = true;
55 m_Properties = new ConcurrentDictionary<string, BadSettings>();
56 }

◆ 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 63 of file BadSettings.cs.

64 {
65 m_Value = null;
66 m_IsDirty = true;
67 m_Properties = new ConcurrentDictionary<string, BadSettings>(properties);
68 SourcePath = sourcePath;
69
70 foreach (KeyValuePair<string, BadSettings> kvp in m_Properties)
71 {
72 kvp.Value.OnValueChanged += PropertyValueChanged;
73 }
74 }

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 333 of file BadSettings.cs.

334 {
335 string[] path = propertyPath.Split('.');
336 BadSettings current = this;
337
338 foreach (string s in path)
339 {
340 if (!current.HasProperty(s))
341 {
342 BadSettings se = new BadSettings(string.Empty);
343 current.SetProperty(s, se);
344 current = se;
345 }
346 else
347 {
348 current = current.GetProperty(s);
349 }
350 }
351
352 return current;
353 }
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 360 of file BadSettings.cs.

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

◆ 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 321 of file BadSettings.cs.

321 : class
322 {
323 BadSettings? settings = FindProperty(propertyName);
324
325 return settings?.GetValue<T>();
326 }
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 202 of file BadSettings.cs.

203 {
204 return m_Properties[propertyName];
205 }

◆ GetValue()

JToken? BadScript2.Settings.BadSettings.GetValue ( )

Returns the Json Token of the Settings Object.

Returns
Json Token

Definition at line 105 of file BadSettings.cs.

106 {
107 return m_Value;
108 }

◆ GetValue< T >()

Returns a Deserialized Value of the Settings Object.

Template Parameters
TThe Type to Deserialize into
Returns
Deserialized Result

Definition at line 115 of file BadSettings.cs.

116 {
117 if (!m_IsDirty)
118 {
119 return (T?)m_Cache;
120 }
121
122 if (m_Value == null)
123 {
124 return default;
125 }
126
127 T? v = m_Value.ToObject<T>();
128
129 m_Cache = v;
130 m_IsDirty = false;
131
132 return v;
133 }
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 192 of file BadSettings.cs.

193 {
194 return m_Properties.ContainsKey(propertyName);
195 }

◆ HasValue()

bool BadScript2.Settings.BadSettings.HasValue ( )

Returns true if the Settings Object has a JToken Value.

Returns

Definition at line 139 of file BadSettings.cs.

140 {
141 return m_Value != null;
142 }

◆ 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 149 of file BadSettings.cs.

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

◆ InvokeValueChanged()

void BadScript2.Settings.BadSettings.InvokeValueChanged ( )
private

Definition at line 96 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 264 of file BadSettings.cs.

265 {
266 if (m_Value is JArray arr)
267 {
268 foreach (BadSettings setting in settings)
269 {
270 if (!setting.HasValue())
271 {
272 continue;
273 }
274
275 if (setting.GetValue() is JArray arr2)
276 {
277 foreach (JToken jToken in arr2)
278 {
279 arr.Add(jToken);
280 }
281 }
282 else
283 {
284 arr.Add(setting.GetValue() ?? JValue.CreateNull());
285 }
286 }
287 }
288
289 foreach (BadSettings setting in settings)
290 {
291 foreach (string propertyName in setting.PropertyNames)
292 {
293 if (HasProperty(propertyName))
294 {
295 GetProperty(propertyName)
296 .Populate(false, setting.GetProperty(propertyName));
297 }
298 else
299 {
300 SetProperty(propertyName, setting.GetProperty(propertyName), false);
301 }
302 }
303 }
304
305 if (invokeOnChanged)
306 {
308 }
309 else
310 {
311 m_IsDirty = true;
312 }
313 }
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 90 of file BadSettings.cs.

91 {
92 m_IsDirty = true;
94 }

◆ 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 237 of file BadSettings.cs.

238 {
239 if (m_Properties.TryGetValue(propertyName, out BadSettings? old))
240 {
241 old.OnValueChanged -= PropertyValueChanged;
242 }
243
244 bool r = m_Properties.TryRemove(propertyName, out _);
245
246 if (invokeOnChange)
247 {
249 }
250 else
251 {
252 m_IsDirty = true;
253 }
254
255 return r;
256 }

◆ 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 461 of file BadSettings.cs.

462 {
463 ResolveEnvironmentVariables(root, root, root);
464 }
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 420 of file BadSettings.cs.

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

◆ 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 472 of file BadSettings.cs.

473 {
474 if (settings.HasValue<string>())
475 {
476 string value = settings.GetValue<string>()!;
477
478 settings.SetValue(ResolveEnvironmentVariables(root, parent, value));
479 }
480 else if (settings.HasValue<string[]>())
481 {
482 string[] value = settings.GetValue<string[]>()!;
483
484 for (int i = 0; i < value.Length; i++)
485 {
486 value[i] = ResolveEnvironmentVariables(root, parent, value[i]);
487 }
488
489 settings.SetValue(value);
490 }
491
492 foreach (string propertyName in settings.PropertyNames)
493 {
494 ResolveEnvironmentVariables(root, settings.GetProperty(propertyName), settings);
495 }
496 }

◆ 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 213 of file BadSettings.cs.

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

◆ 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 164 of file BadSettings.cs.

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

◆ 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 182 of file BadSettings.cs.

183 {
184 SetValue(value == null ? JValue.CreateNull() : JToken.FromObject(value));
185 }
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 382 of file BadSettings.cs.

383 {
384 if (HasValue())
385 {
386 return GetValue()
387 ?.ToString() ??
388 "NULL";
389 }
390
391 StringBuilder sb = new StringBuilder();
392
393 sb.AppendLine("{");
394
395 foreach (string propertyName in PropertyNames)
396 {
397 BadSettings s = GetProperty(propertyName);
398
399 string str = s.ToString()
400 .Replace("\n", "\n\t");
401 sb.AppendLine($" {propertyName}: {str}");
402 }
403
404 sb.AppendLine("}");
405
406 return sb.ToString();
407 }
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 21 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 26 of file BadSettings.cs.

◆ m_Properties

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

The properties of the Current Settings Object.

Definition at line 16 of file BadSettings.cs.

◆ m_Value

JToken? BadScript2.Settings.BadSettings.m_Value
private

The Json Token of the Settings Object.

Definition at line 31 of file BadSettings.cs.

Property Documentation

◆ HasSourcePath

bool BadScript2.Settings.BadSettings.HasSourcePath
get

Definition at line 76 of file BadSettings.cs.

◆ PropertyNames

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

The Property Names of the Settings Object.

Definition at line 86 of file BadSettings.cs.

◆ SourcePath

string BadScript2.Settings.BadSettings.SourcePath
get

The Source Path of the Settings Object.

Definition at line 81 of file BadSettings.cs.

81{ get; }

Event Documentation

◆ OnValueChanged

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

Definition at line 88 of file BadSettings.cs.

88{ };

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