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
41 Dictionary<string, BadObject> properties = new Dictionary<string, BadObject>
42 {
43 {
44 "HasValue", new BadDynamicInteropFunction("HasValue",
45 _ => m_Settings.HasValue(),
47 )
48 },
49 {
50 "GetValue", new BadDynamicInteropFunction("GetValue",
53 )
54 },
55 {
56 "SetValue", new BadDynamicInteropFunction<BadObject>("SetValue",
57 (_, obj) =>
58 {
60
61 return Null;
62 },
63 BadAnyPrototype.Instance
64 )
65 },
66 {
67 "HasProperty", new BadDynamicInteropFunction<string>("HasProperty",
68 (_, name) => m_Settings.HasProperty(name),
70 )
71 },
72 {
73 "GetProperty", new BadDynamicInteropFunction<string>("GetProperty",
74 (_, name) =>
76 .GetProperty(name)
77 ),
79 )
80 },
81 {
82 "FindProperty", new BadDynamicInteropFunction<string>("FindProperty",
83 (_, name) =>
84 {
85 BadSettings? obj =
87
88 return obj == null
89 ? Null
90 : new BadSettingsObject(obj);
91 },
92 BadAnyPrototype.Instance
93 )
94 },
95 {
96 "FindOrCreateProperty", new BadDynamicInteropFunction<string>("FindOrCreateProperty",
97 (_, name) =>
98 {
99 BadSettings obj =
102
103 return new BadSettingsObject(obj);
104 },
105 BadAnyPrototype.Instance
106 )
107 },
108 {
109 "SetProperty", new BadDynamicInteropFunction<string, BadSettingsObject>("SetProperty",
110 (_, name, prop) =>
111 {
112 m_Settings.SetProperty(name, prop.m_Settings);
113
114 return Null;
115 },
116 BadAnyPrototype.Instance
117 )
118 },
119 {
120 "RemoveProperty", new BadDynamicInteropFunction<string>("RemoveProperty",
121 (_, name) => m_Settings.RemoveProperty(name),
123 )
124 },
125 {
126 "GetPropertyNames", new BadDynamicInteropFunction("GetPropertyNames",
128 .Select(x => (BadObject)x)
129 .ToList()
130 ),
132 )
133 },
134 {
135 "GetEnumerator", new BadDynamicInteropFunction("GetEnumerator",
137 .Select(x => (BadObject)x)
138 .GetEnumerator()
139 ),
141 )
142 },
143 {
146 (_, name) => BadObjectReference.Make($"BadSettings.{name}",
147 (p) => new BadSettingsObject(m_Settings.GetProperty(name)),
148 (o, p, _) =>
149 {
150 if (o is not BadSettingsObject obj)
151 {
152 throw BadRuntimeException.Create(null, "BadSettingsObject expected", p
153 );
154 }
155
156 m_Settings.SetProperty(name, obj.m_Settings);
157 }
158 ),
160 )
161 },
162 };
163
164 m_PropertyReferences = new Dictionary<string, BadObjectReference>();
165
166 foreach (KeyValuePair<string, BadObject> property in properties)
167 {
168 m_PropertyReferences.Add(property.Key,
169 BadObjectReference.Make($"BadSettings.{property.Key}",
170 (p) => properties[property.Key]
171 )
172 );
173 }
174 }
175
178 {
179 return Prototype;
180 }
181
188 private static BadObject CreateObj(IReadOnlyList<BadObject> args)
189 {
190 if (args.Count != 1)
191 {
192 return new BadSettingsObject(new BadSettings(string.Empty));
193 }
194
195 if (args[0] is not BadSettingsObject settings)
196 {
197 throw new BadRuntimeException("BadSettings expected");
198 }
199
200 return settings;
201 }
202
204 public override string ToSafeString(List<BadObject> done)
205 {
206 return m_Settings.ToString();
207 }
208
210 public override bool HasProperty(string propName, BadScope? caller = null)
211 {
212 return m_PropertyReferences.ContainsKey(propName) ||
213 m_Settings.HasProperty(propName) ||
214 base.HasProperty(propName, caller);
215 }
216
218 public override BadObjectReference GetProperty(string propName, BadScope? caller = null)
219 {
220 if (m_PropertyReferences.TryGetValue(propName, out BadObjectReference? property))
221 {
222 return property;
223 }
224
225 if (m_Settings.HasProperty(propName))
226 {
227 return BadObjectReference.Make($"BadSettings.{propName}",
228 (p) => new BadSettingsObject(m_Settings.GetProperty(propName))
229 );
230 }
231
232 return base.GetProperty(propName, caller);
233 }
234}
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 the Runtime Objects.
Definition BadArray.cs:10
Contains the Runtime Implementation.
Contains the Settings Implementation.