1using System.Collections.Concurrent;
4using Newtonsoft.Json.Linq;
16 private readonly ConcurrentDictionary<string, BadSettings>
m_Properties;
42 m_Properties =
new ConcurrentDictionary<string, BadSettings>();
55 m_Properties =
new ConcurrentDictionary<string, BadSettings>();
63 public BadSettings(Dictionary<string, BadSettings> properties,
string sourcePath)
67 m_Properties =
new ConcurrentDictionary<string, BadSettings>(properties);
70 foreach (KeyValuePair<string, BadSettings> kvp
in m_Properties)
151 if (
m_Value?.Type == JTokenType.Array && !typeof(T).IsArray)
164 public void SetValue(JToken? value,
bool invokeOnChange =
true)
184 SetValue(value ==
null ? JValue.CreateNull() : JToken.FromObject(value));
275 if (setting.
GetValue() is JArray arr2)
277 foreach (JToken jToken
in arr2)
284 arr.Add(setting.
GetValue() ?? JValue.CreateNull());
335 string[] path = propertyPath.Split(
'.');
338 foreach (
string s
in path)
340 if (!current.HasProperty(s))
343 current.SetProperty(s, se);
362 string[] path = propertyPath.Split(
'.');
365 foreach (
string s
in path)
367 if (!current.HasProperty(s))
391 StringBuilder sb =
new StringBuilder();
400 .Replace(
"\n",
"\n\t");
401 sb.AppendLine($
" {propertyName}: {str}");
406 return sb.ToString();
422 for (
int i = 0; i < str.Length; i++)
424 if (str[i] !=
'$' || i + 1 >= str.Length || str[i + 1] !=
'(')
429 int end = str.IndexOf(
')', i + 2);
433 throw new Exception(
"Unclosed environment variable");
436 string envVar = str.Substring(i + 2, end - i - 2);
446 throw new Exception($
"Environment variable '{envVar}' not found");
450 str = str.Replace(str.Substring(i, end - i + 1), env);
476 string value = settings.
GetValue<
string>()!;
480 else if (settings.
HasValue<
string[]>())
482 string[] value = settings.
GetValue<
string[]>()!;
484 for (
int i = 0; i < value.Length; i++)
492 foreach (
string propertyName
in settings.PropertyNames)
Public Api for the Settings System.
void Populate(bool invokeOnChanged, params BadSettings[] settings)
Populates the current object with the settings provided.
BadSettings? FindProperty(string propertyPath)
Finds a property based on the property path relative to this object.
JToken? m_Value
The Json Token of the Settings Object.
IEnumerable< string > PropertyNames
The Property Names of the Settings Object.
BadSettings(JToken? value, string sourcePath)
Creates a new Settings Object from a Json Token.
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.
override string ToString()
Returns a string representation of the current object.
string SourcePath
The Source Path of the Settings Object.
void SetProperty(string propertyName, BadSettings value, bool invokeOnChange=true)
Sets the Property with the given Name.
void SetValue(JToken? value, bool invokeOnChange=true)
Sets the Json Token of the Current Settings Object.
bool HasValue()
Returns true if the Settings Object has a JToken Value.
static void ResolveEnvironmentVariables(BadSettings root)
Resolves Environment Variables in the current object.
T? GetValue< T >()
Returns a Deserialized Value of the Settings Object.
static string ResolveEnvironmentVariables(BadSettings root, BadSettings parent, string str)
Resolves Environment Variables in the current object.
BadSettings(Dictionary< string, BadSettings > properties, string sourcePath)
Creates a new Settings Object from a Dictionary of Properties.
readonly ConcurrentDictionary< string, BadSettings > m_Properties
The properties of the Current Settings Object.
bool HasValue< T >()
Returns true if the Settings Object can be Deserialized into the given Type.
void InvokeValueChanged()
BadSettings GetProperty(string propertyName)
Returns the Property with the given Name.
void SetValue(object? value)
Sets the Json Token of the Current Settings Object.
JToken? GetValue()
Returns the Json Token of the Settings Object.
BadSettings FindOrCreateProperty(string propertyPath)
Finds a property based on the property path relative to this object.
BadSettings(string sourcePath)
Creates a new empty Settings Object.
bool HasProperty(string propertyName)
Returns true if the Settings Object has the given Property.
bool RemoveProperty(string propertyName, bool invokeOnChange=true)
Removes the Property with the given Name.
T? FindProperty< T >(string propertyName)
Finds a property based on the property path relative to this object.
static void ResolveEnvironmentVariables(BadSettings root, BadSettings settings, BadSettings parent)
Resolves Environment Variables in the current object.
void PropertyValueChanged()
Contains the Settings Implementation.