BadScript 2
Loading...
Searching...
No Matches
BadTable.cs
Go to the documentation of this file.
1using System.Collections;
2using System.Text;
3
7
9
14{
15
16 public event Action<string, BadObject, BadObject> OnChangedProperty = delegate { };
17 private Func<string, BadObject, BadObject, bool>? m_OnChangeProperty;
18
19 public void SetChangeInterceptor(Func<string, BadObject, BadObject, bool>? interceptor)
20 {
21 m_OnChangeProperty = interceptor;
22 }
23
28
29 private readonly Dictionary<string, BadObjectReference> m_ReferenceCache = new Dictionary<string, BadObjectReference>();
30
34 public BadTable()
35 {
36 InnerTable = new Dictionary<string, BadObject>();
37 PropertyInfos = new Dictionary<string, BadPropertyInfo>();
38 }
39
44 public BadTable(Dictionary<string, BadObject> table)
45 {
46 InnerTable = table;
47 PropertyInfos = new Dictionary<string, BadPropertyInfo>();
48
49 foreach (KeyValuePair<string, BadObject> kvp in InnerTable)
50 {
52 }
53 }
54
56
60 public Dictionary<string, BadObject> InnerTable { get; }
61
65 public Dictionary<string, BadPropertyInfo> PropertyInfos { get; }
66
71 public IEnumerator<BadObject> GetEnumerator()
72 {
73 return InnerTable.Select(
74 kvp => new BadTable(
75 new Dictionary<string, BadObject>
76 {
77 {
78 "Key", kvp.Key
79 },
80 {
81 "Value", kvp.Value
82 },
83 }
84 )
85 )
86 .Cast<BadObject>()
88 }
89
94 IEnumerator IEnumerable.GetEnumerator()
95 {
96 return GetEnumerator();
97 }
98
99
102 {
103 return Prototype;
104 }
105
111 public BadPropertyInfo GetPropertyInfo(string propName)
112 {
113 return PropertyInfos[propName];
114 }
115
120 public bool RemoveKey(string key)
121 {
122 PropertyInfos.Remove(key);
123
124 return InnerTable.Remove(key);
125 }
126
127
129 public override bool HasProperty(string propName, BadScope? caller = null)
130 {
131 return InnerTable.ContainsKey(propName) || caller != null && caller.Provider.HasObject<BadTable>(propName);
132 }
133
134
142 public BadObjectReference GetProperty(string propName, bool useExtensions, BadScope? caller = null)
143 {
144 return !useExtensions ? GetLocalReference(propName) : GetProperty(propName, caller);
145 }
146
147 private bool OnChangePropertyInternal(string propName, BadObject oldValue, BadObject newValue)
148 {
149 if (m_OnChangeProperty != null)
150 {
151 return m_OnChangeProperty(propName, oldValue, newValue);
152 }
153
154 return false;
155 }
156
162 private BadObjectReference GetLocalReference(string propName)
163 {
164 if (m_ReferenceCache.TryGetValue(propName, out BadObjectReference reference))
165 {
166 return reference;
167 }
168
170 $"BadTable.{propName}",
171 () => InnerTable[propName],
172 (o, t, noChange) =>
173 {
174 if (InnerTable.TryGetValue(propName, out BadObject? propValue))
175 {
176 BadPropertyInfo info = GetPropertyInfo(propName);
177
178 if (propValue != Null && info.IsReadOnly)
179 {
180 throw new BadRuntimeException($"{propName} is read-only");
181 }
182
183 if (info.Type != null && !info.Type.IsAssignableFrom(o))
184 {
185 throw new BadRuntimeException(
186 $"Cannot assign object {o.GetType().Name} to property '{propName}' of type '{info.Type.Name}'"
187 );
188 }
189 if (propValue is BadObjectReference propRef)
190 {
191 var oldValueRef = propRef.Dereference();
192 if (OnChangePropertyInternal(propName, oldValueRef, o))
193 return;
194 propRef.Set(o, t);
195 if(!noChange)
196 OnChangedProperty(propName, oldValueRef, o);
197 return;
198 }
199 }
200 else
201 {
203
204 if (t?.Type != null && !t.Type.IsAssignableFrom(o))
205 {
206 throw new BadRuntimeException(
207 $"Cannot assign object {o.GetType().Name} to property '{propName}' of type '{t.Type.Name}'"
208 );
209 }
210 }
211
212 if (OnChangePropertyInternal(propName, propValue ?? Null, o))
213 return;
214 InnerTable[propName] = o;
215 if(!noChange)
216 OnChangedProperty(propName, propValue ?? Null, o);
217 },
218 () => { InnerTable.Remove(propName); }
219 );
220
221 m_ReferenceCache[propName] = r;
222
223 return r;
224 }
225
227 public override BadObjectReference GetProperty(string propName, BadScope? caller = null)
228 {
229 if (!InnerTable.ContainsKey(propName) && caller != null && caller.Provider.HasObject<BadTable>(propName))
230 {
231 return caller.Provider.GetObjectReference(GetType(), propName, this, caller);
232 }
233
234 return GetLocalReference(propName);
235 }
236
237
238
240 public override string ToSafeString(List<BadObject> done)
241 {
242 done.Add(this);
243 StringBuilder sb = new StringBuilder();
244 sb.Append("{");
245 sb.AppendLine();
246
247 foreach (KeyValuePair<string, BadObject> kvp in InnerTable)
248 {
249 if (kvp.Value is BadScope)
250 {
251 sb.AppendLine("RECURSION_PROTECT");
252
253 continue;
254 }
255
256 string kStr = kvp.Key;
257
258 string vStr = "{...}";
259
260 if (!done.Contains(kvp.Value))
261 {
262 vStr = kvp.Value.ToSafeString(done).Trim();
263 }
264
265 if (kStr.Contains("\n"))
266 {
267 kStr = kStr.Replace("\n", "\n\t");
268 }
269
270 if (vStr.Contains("\n"))
271 {
272 vStr = vStr.Replace("\n", "\n\t");
273 }
274
275 sb.AppendLine($"\t{kStr}: {vStr}");
276 }
277
278 sb.AppendLine("}");
279
280 return sb.ToString();
281 }
282}
Implements the Scope for the Script Engine.
Definition BadScope.cs:238
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
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.
Stores Meta Information about a Property.
Implements a Table Structure for the BadScript Language.
Definition BadTable.cs:14
BadObjectReference GetProperty(string propName, bool useExtensions, BadScope? caller=null)
Returns a Reference to the Property with the given Name.
Definition BadTable.cs:142
override BadClassPrototype GetPrototype()
Definition BadTable.cs:101
bool OnChangePropertyInternal(string propName, BadObject oldValue, BadObject newValue)
Definition BadTable.cs:147
Func< string, BadObject, BadObject, bool >? m_OnChangeProperty
Definition BadTable.cs:17
static ? BadClassPrototype s_Prototype
The Prototype for the BadScript Table.
Definition BadTable.cs:27
Action< string, BadObject, BadObject > OnChangedProperty
Definition BadTable.cs:16
readonly Dictionary< string, BadObjectReference > m_ReferenceCache
Definition BadTable.cs:29
BadTable(Dictionary< string, BadObject > table)
Creates a new Table Object.
Definition BadTable.cs:44
override string ToSafeString(List< BadObject > done)
Definition BadTable.cs:240
void SetChangeInterceptor(Func< string, BadObject, BadObject, bool >? interceptor)
Definition BadTable.cs:19
Dictionary< string, BadPropertyInfo > PropertyInfos
A Table of additional property information.
Definition BadTable.cs:65
BadTable()
Creates a new Table Object.
Definition BadTable.cs:34
BadObjectReference GetLocalReference(string propName)
Returns a Reference to a Local Property with the given Name.
Definition BadTable.cs:162
static BadClassPrototype Prototype
Definition BadTable.cs:55
IEnumerator< BadObject > GetEnumerator()
Returns the Enumerator for this Table.
Definition BadTable.cs:71
Dictionary< string, BadObject > InnerTable
The Inner Table for this Object.
Definition BadTable.cs:60
bool RemoveKey(string key)
Removes a Property from the Table.
Definition BadTable.cs:120
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...
Definition BadTable.cs:129
BadPropertyInfo GetPropertyInfo(string propName)
Returns Property Information for a given Key.
Definition BadTable.cs:111
override BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.The Property Reference
Definition BadTable.cs:227
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.
Defines a BadScript Enumerable.
Contains the Error Objects for the BadScript2 Language.
Contains the Interop Abstractions and Implementations for the BadScript2 Language.
Contains the Runtime Objects.
Definition BadArray.cs:10