BadScript 2
Loading...
Searching...
No Matches
BadSettingsObject.cs
Go to the documentation of this file.
9
11
16{
20 public static readonly BadClassPrototype Prototype =
21 new BadNativeClassPrototype<BadSettingsObject>("BadSettings", (_, args) => CreateObj(args));
22
26 private readonly Dictionary<string, BadObjectReference> m_PropertyReferences;
27
31 private readonly BadSettings m_Settings;
32
38 {
39 m_Settings = settings;
40 Dictionary<string, BadObject> properties = new Dictionary<string, BadObject>
41 {
42 {
43 "HasValue",
45 "HasValue",
46 _ => m_Settings.HasValue(),
48 )
49 },
50 {
51 "GetValue",
53 "GetValue",
56 )
57 },
58 {
60 "SetValue",
61 (_, obj) =>
62 {
64
65 return Null;
66 },
67 BadAnyPrototype.Instance
68 )
69 },
70 {
71 "HasProperty",
73 "HasProperty",
74 (_, name) => m_Settings.HasProperty(name),
76 )
77 },
78 {
79 "GetProperty", new BadDynamicInteropFunction<string>(
80 "GetProperty",
81 (_, name) => new BadSettingsObject(m_Settings.GetProperty(name)),
83 )
84 },
85 {
86 "FindProperty", new BadDynamicInteropFunction<string>(
87 "FindProperty",
88 (_, name) =>
89 {
91
92 return obj == null ? Null : new BadSettingsObject(obj);
93 },
94 BadAnyPrototype.Instance
95 )
96 },
97 {
98 "FindOrCreateProperty", new BadDynamicInteropFunction<string>(
99 "FindOrCreateProperty",
100 (_, name) =>
101 {
103
104 return new BadSettingsObject(obj);
105 },
106 BadAnyPrototype.Instance
107 )
108 },
109 {
111 "SetProperty",
112 (_, name, prop) =>
113 {
114 m_Settings.SetProperty(name, prop.m_Settings);
115
116 return Null;
117 },
118 BadAnyPrototype.Instance
119 )
120 },
121 {
122 "RemoveProperty", new BadDynamicInteropFunction<string>(
123 "RemoveProperty",
124 (_, name) => m_Settings.RemoveProperty(name),
126 )
127 },
128 {
129 "GetPropertyNames", new BadDynamicInteropFunction(
130 "GetPropertyNames",
131 _ => new BadArray(m_Settings.PropertyNames.Select(x => (BadObject)x).ToList()),
133 )
134 },
135 {
136 "GetEnumerator", new BadDynamicInteropFunction(
137 "GetEnumerator",
138 _ => new BadInteropEnumerator(m_Settings.PropertyNames.Select(x => (BadObject)x).GetEnumerator()),
140 )
141 },
142 {
145 (_, name) => BadObjectReference.Make(
146 $"BadSettings.{name}",
148 (o, _) =>
149 {
150 if (o is not BadSettingsObject obj)
151 {
152 throw new BadRuntimeException("BadSettingsObject expected");
153 }
154
155 m_Settings.SetProperty(name, obj.m_Settings);
156 }
157 ),
159 )
160 },
161 };
162
163 m_PropertyReferences = new Dictionary<string, BadObjectReference>();
164
165 foreach (KeyValuePair<string, BadObject> property in properties)
166 {
168 property.Key,
169 BadObjectReference.Make($"BadSettings.{property.Key}", () => properties[property.Key])
170 );
171 }
172 }
173
176 {
177 return Prototype;
178 }
179
186 private static BadObject CreateObj(IReadOnlyList<BadObject> args)
187 {
188 if (args.Count != 1)
189 {
190 return new BadSettingsObject(new BadSettings(string.Empty));
191 }
192
193 if (args[0] is not BadSettingsObject settings)
194 {
195 throw new BadRuntimeException("BadSettings expected");
196 }
197
198 return settings;
199 }
200
202 public override string ToSafeString(List<BadObject> done)
203 {
204 return m_Settings.ToString();
205 }
206
208 public override bool HasProperty(string propName, BadScope? caller = null)
209 {
210 return m_PropertyReferences.ContainsKey(propName) ||
211 m_Settings.HasProperty(propName) ||
212 base.HasProperty(propName, caller);
213 }
214
216 public override BadObjectReference GetProperty(string propName, BadScope? caller = null)
217 {
218 if (m_PropertyReferences.TryGetValue(propName, out BadObjectReference? property))
219 {
220 return property;
221 }
222
223 if (m_Settings.HasProperty(propName))
224 {
226 $"BadSettings.{propName}",
227 () => new BadSettingsObject(m_Settings.GetProperty(propName))
228 );
229 }
230
231 return base.GetProperty(propName, caller);
232 }
233}
Contains Static Data for the BadScript Language.
Implements a Json to BadObject Converter.
Definition BadJson.cs:17
static JToken ConvertNode(BadObject value)
Converts a BadObject to a JToken.
Definition BadJson.cs:63
Implements a Settings Object Wrapper.
static BadObject CreateObj(IReadOnlyList< BadObject > args)
Creates a new Settings Object.
override BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.The Property Reference
override string ToSafeString(List< BadObject > done)
BadSettingsObject(BadSettings settings)
Creates a new Settings Object.
override bool HasProperty(string propName, BadScope? caller=null)
Returns true if the object contains a given property or there exists an extension for the current Ins...
readonly Dictionary< string, BadObjectReference > m_PropertyReferences
Property References.
readonly BadSettings m_Settings
Inner Settings Object.
override BadClassPrototype GetPrototype()
static readonly BadClassPrototype Prototype
The Class Prototype.
Implements the Scope for the Script Engine.
Definition BadScope.cs:238
Implements a simple wrapper for C# IEnumerators to be used in BS2.
Implements a Dynamic List/Array for the BadScript Language.
Definition BadArray.cs:17
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
virtual BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.
Definition BadObject.cs:129
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
Implements the base functionality for a BadScript Reference.
static BadObjectReference Make(string refText, Func< BadObject > getter, Action< BadObject, BadPropertyInfo?>? setter=null, Action? delete=null)
Creates a new Reference Object.
The Any Prototype, Base type for all types.
static readonly BadAnyPrototype Instance
The Instance of the BadAnyPrototype.
Implements a Class Prototype for the BadScript Language.
Helper Class that Builds a Native Class from a Prototype.
static BadClassPrototype GetNative(string name)
Returns a Native Class Prototype for the given Native Type.
Public Api for the Settings System.
BadSettings? FindProperty(string propertyPath)
Finds a property based on the property path relative to this object.
IEnumerable< string > PropertyNames
The Property Names 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.
BadSettings GetProperty(string propertyName)
Returns the Property with the given Name.
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.
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.
Contains Shared Data Structures and Functionality.
Contains JSON Extensions and APIs for the BadScript2 Runtime.
Definition BadJson.cs:11
Contains the Error Objects for the BadScript2 Language.
Contains the Interop Function Classes for the BadScript2 Language.
Contains the Interop Abstractions and Implementations for the BadScript2 Language.
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains the Runtime Implementation.
Contains the Settings Implementation.