BadScript 2
Loading...
Searching...
No Matches
BadSettingsObject.cs
Go to the documentation of this file.
10
12
17{
21 public static readonly BadClassPrototype Prototype =
22 new BadNativeClassPrototype<BadSettingsObject>("BadSettings", (_, args) => CreateObj(args),
23 null);
24
28 private readonly Dictionary<string, BadObjectReference> m_PropertyReferences;
29
33 private readonly BadSettings m_Settings;
34
40 {
41 m_Settings = settings;
42
43 Dictionary<string, BadObject> properties = new Dictionary<string, BadObject>
44 {
45 {
46 "HasValue", new BadDynamicInteropFunction("HasValue",
47 _ => m_Settings.HasValue(),
49 )
50 },
51 {
52 "GetValue", new BadDynamicInteropFunction("GetValue",
55 )
56 },
57 {
58 "SetValue", new BadDynamicInteropFunction<BadObject>("SetValue",
59 (_, obj) =>
60 {
62
63 return Null;
64 },
66 new BadFunctionParameter("obj", false, true, false, null, BadAnyPrototype.Instance)
67 )
68 },
69 {
70 "HasProperty", new BadDynamicInteropFunction<string>("HasProperty",
71 (_, name) => m_Settings.HasProperty(name),
73 new BadFunctionParameter("name", false, true, false, null, BadNativeClassBuilder.GetNative("string"))
74 )
75 },
76 {
77 "GetProperty", new BadDynamicInteropFunction<string>("GetProperty",
78 (_, name) =>
80 .GetProperty(name)
81 ),
83 new BadFunctionParameter("name", false, true, false, null, BadNativeClassBuilder.GetNative("string"))
84 )
85 },
86 {
87 "FindProperty", new BadDynamicInteropFunction<string>("FindProperty",
88 (_, name) =>
89 {
90 BadSettings? obj =
92
93 return obj == null
94 ? Null
95 : new BadSettingsObject(obj);
96 },
98 new BadFunctionParameter("name", false, true, false, null, BadNativeClassBuilder.GetNative("string"))
99 )
100 },
101 {
102 "FindOrCreateProperty", new BadDynamicInteropFunction<string>("FindOrCreateProperty",
103 (_, name) =>
104 {
105 BadSettings obj =
108
109 return new BadSettingsObject(obj);
110 },
112 new BadFunctionParameter("name", false, true, false, null, BadNativeClassBuilder.GetNative("string"))
113 )
114 },
115 {
116 "SetProperty", new BadDynamicInteropFunction<string, BadSettingsObject>("SetProperty",
117 (_, name, prop) =>
118 {
119 m_Settings.SetProperty(name, prop.m_Settings);
120
121 return Null;
122 },
124 new BadFunctionParameter("name", false, true, false, null, BadNativeClassBuilder.GetNative("string")),
125 new BadFunctionParameter("obj", false, true, false, null, Prototype)
126 )
127 },
128 {
129 "RemoveProperty", new BadDynamicInteropFunction<string>("RemoveProperty",
130 (_, name) => m_Settings.RemoveProperty(name),
132 new BadFunctionParameter("name", false, true, false, null, BadNativeClassBuilder.GetNative("string"))
133 )
134 },
135 {
136 "GetPropertyNames", new BadDynamicInteropFunction("GetPropertyNames",
138 .Select(x => (BadObject)x)
139 .ToList()
140 ),
142 )
143 },
144 {
145 "GetEnumerator", new BadDynamicInteropFunction("GetEnumerator",
147 .Select(x => (BadObject)x)
148 .GetEnumerator()
149 ),
151 )
152 },
153 {
156 (_, name) => BadObjectReference.Make($"BadSettings.{name}",
157 (p) => new BadSettingsObject(m_Settings.GetProperty(name)),
158 (o, p, _) =>
159 {
160 if (o is not BadSettingsObject obj)
161 {
162 throw BadRuntimeException.Create(null, "BadSettingsObject expected", p
163 );
164 }
165
166 m_Settings.SetProperty(name, obj.m_Settings);
167 }
168 ),
170 new BadFunctionParameter("name", false, true, false, null, BadNativeClassBuilder.GetNative("string"))
171 )
172 },
173 };
174
175 m_PropertyReferences = new Dictionary<string, BadObjectReference>();
176
177 foreach (KeyValuePair<string, BadObject> property in properties)
178 {
179 m_PropertyReferences.Add(property.Key,
180 BadObjectReference.Make($"BadSettings.{property.Key}",
181 (p) => properties[property.Key]
182 )
183 );
184 }
185 }
186
189 {
190 return Prototype;
191 }
192
199 private static BadObject CreateObj(IReadOnlyList<BadObject> args)
200 {
201 if (args.Count != 1)
202 {
203 return new BadSettingsObject(new BadSettings(string.Empty));
204 }
205
206 if (args[0] is not BadSettingsObject settings)
207 {
208 throw new BadRuntimeException("BadSettings expected");
209 }
210
211 return settings;
212 }
213
215 public override string ToSafeString(List<BadObject> done)
216 {
217 return m_Settings.ToString();
218 }
219
221 public override bool HasProperty(string propName, BadScope? caller = null)
222 {
223 return m_PropertyReferences.ContainsKey(propName) ||
224 m_Settings.HasProperty(propName) ||
225 base.HasProperty(propName, caller);
226 }
227
229 public override BadObjectReference GetProperty(string propName, BadScope? caller = null)
230 {
231 if (m_PropertyReferences.TryGetValue(propName, out BadObjectReference? property))
232 {
233 return property;
234 }
235
236 if (m_Settings.HasProperty(propName))
237 {
238 return BadObjectReference.Make($"BadSettings.{propName}",
239 (p) => new BadSettingsObject(m_Settings.GetProperty(propName))
240 );
241 }
242
243 return base.GetProperty(propName, caller);
244 }
245}
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:65
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:16
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:141
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< BadSourcePosition?, BadObject > getter, Action< BadObject, BadSourcePosition?, BadPropertyInfo?>? setter=null, Action< BadSourcePosition?>? 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 Runtime Function Objects.
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains the Runtime Implementation.
Contains the Settings Implementation.