BadScript 2
Loading...
Searching...
No Matches
BadEditableSetting.cs
Go to the documentation of this file.
2
3using Newtonsoft.Json.Linq;
4
9
15public class BadEditableSetting<T, TValue> where T : BadSettingsProvider<T>, new()
16{
20 private readonly TValue? m_DefaultValue;
21
25 private readonly string m_Name;
26
31
37 public BadEditableSetting(string name, TValue? defaultValue = default)
38 {
39 m_Name = name;
40 m_DefaultValue = defaultValue;
41 }
42
47 public BadSettings? Get()
48 {
49 if (m_SettingsObj != null ||
50 BadSettingsProvider<T>.Instance.Settings == null)
51 {
52 return m_SettingsObj;
53 }
54
56 {
57 m_SettingsObj = BadSettingsProvider<T>.Instance.Settings?.GetProperty(m_Name);
58 }
59 else
60 {
62 new BadSettings(m_DefaultValue == null ? JValue.CreateNull() : JToken.FromObject(m_DefaultValue), string.Empty);
64 }
65
66 return m_SettingsObj;
67 }
68
73 public TValue? GetValue()
74 {
75 BadSettings? setting = Get();
76
77 if (setting == null)
78 {
79 return m_DefaultValue;
80 }
81
82 TValue? value = setting.GetValue<TValue>();
83
84 return value ?? m_DefaultValue;
85 }
86
92 public void Set(TValue? value)
93 {
94 BadSettings? settings = Get();
95
96 if (settings == null)
97 {
98 throw new BadRuntimeException($"Settings Object for {typeof(T).Name}.{m_Name} is null");
99 }
100
101 settings.SetValue(value == null ? JValue.CreateNull() : JToken.FromObject(value));
102 }
103}
Implements an Editable Setting.
BadEditableSetting(string name, TValue? defaultValue=default)
Creates a new Editable Setting.
void Set(TValue? value)
Sets the value of the Editable Setting.
readonly? TValue m_DefaultValue
The Default Value of the Setting.
BadSettings? Get()
Returns the settings object of the Editable Setting.
readonly string m_Name
The Name of the Setting.
BadSettings? m_SettingsObj
The Settings Object.
TValue? GetValue()
Returns the value of the Editable Setting.
Public Api for the Settings System.
void SetValue(JToken? value, bool invokeOnChange=true)
Sets the Json Token of the Current Settings Object.
JToken? GetValue()
Returns the Json Token of the Settings Object.
bool HasProperty(string propertyName)
Returns true if the Settings Object has the given Property.
Helper class that can be used to automatically load a settings object from a file.
BadSettings? Settings
Returns the Instance of the Settings Provider.
static T Instance
Returns the Instance of the Settings Provider.
Contains the Error Objects for the BadScript2 Language.
Contains the Settings Implementation.