Gondwana Game Engine Development (master) - 2.7.0
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
TypedValueBag.cs
1using Newtonsoft.Json;
2
3namespace Gondwana;
4
24public readonly record struct ValueKey<T>(string Name);
25
73public sealed class TypedValueBag : ICloneable
74{
89 [JsonIgnore] private readonly Dictionary<string, object?> _data = new();
90
95 {
96 }
97
118 {
119 if (other is null)
120 throw new ArgumentNullException(nameof(other));
121
122 foreach (var (key, value) in other._data)
123 _data[key] = CloneValue(value);
124 }
125
144 public void Set<T>(ValueKey<T> key, T value)
145 {
146 ValidateKeyName(key.Name);
147 _data[key.Name] = value;
148 }
149
189 public bool TryGet<T>(ValueKey<T> key, out T? value)
190 {
191 ValidateKeyName(key.Name);
192
193 if (!_data.TryGetValue(key.Name, out var raw))
194 {
195 value = default;
196 return false;
197 }
198
199 if (raw is null)
200 {
201 value = default;
202 return true;
203 }
204
205 if (raw is T typed)
206 {
207 value = typed;
208 return true;
209 }
210
211 throw new InvalidCastException(
212 $"Value for key '{key.Name}' is of runtime type '{raw.GetType().FullName}', " +
213 $"which is not assignable to requested type '{typeof(T).FullName}'.");
214 }
215
239 public T Get<T>(ValueKey<T> key, T defaultValue = default!)
240 {
241 return TryGet(key, out var v) && v is not null
242 ? v
243 : defaultValue;
244 }
245
261 public bool Remove<T>(ValueKey<T> key)
262 {
263 ValidateKeyName(key.Name);
264 return _data.Remove(key.Name);
265 }
266
270 public void Clear()
271 {
272 _data.Clear();
273 }
274
292 public void MergeFrom(TypedValueBag? incoming, bool overwriteExisting = false)
293 {
294 if (incoming is null)
295 return;
296
297 foreach (var (key, value) in incoming._data)
298 {
299 if (!overwriteExisting && _data.ContainsKey(key))
300 continue;
301
302 _data[key] = CloneValue(value);
303 }
304 }
305
318 public bool Contains(string keyName)
319 {
320 ValidateKeyName(keyName);
321 return _data.ContainsKey(keyName);
322 }
323
339 public bool Contains<T>(ValueKey<T> key)
340 {
341 ValidateKeyName(key.Name);
342 return _data.ContainsKey(key.Name);
343 }
344
355 public TypedValueBag Clone() => new(this);
356
365 object ICloneable.Clone() => Clone();
366
377 public Dictionary<string, object?> ToDictionary()
378 {
379 return new Dictionary<string, object?>(_data);
380 }
381
414 private static object? CloneValue(object? value)
415 {
416 if (value is null)
417 return null;
418
419 if (value is Array array)
420 return array.Clone();
421
422 if (value is ICloneable cloneable)
423 return cloneable.Clone();
424
425 return value;
426 }
427
437 private static void ValidateKeyName(string keyName)
438 {
439 if (string.IsNullOrWhiteSpace(keyName))
440 throw new ArgumentException("Value bag keys may not be null, empty, or whitespace.", nameof(keyName));
441 }
442}
bool Contains(string keyName)
Determines whether the bag contains a value for the specified key name, regardless of type.
TypedValueBag()
Initializes a new instance of the TypedValueBag class.
TypedValueBag Clone()
Creates a copy of this TypedValueBag.
TypedValueBag(TypedValueBag other)
Initializes a new instance of the TypedValueBag class by performing a copy of another TypedValueBag.
Dictionary< string, object?> ToDictionary()
Produces a shallow snapshot of the bag's current contents.
void MergeFrom(TypedValueBag? incoming, bool overwriteExisting=false)
Merges values from another TypedValueBag into this instance.
void Clear()
Removes all values from the bag.