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
DefinitionDocumentJson.cs
1using Newtonsoft.Json;
2using Newtonsoft.Json.Linq;
3
5
7internal static class DefinitionDocumentJson
8{
16 internal static List<T> Read<T>(
17 string json,
18 Func<string, T> read,
19 Func<T, string> identity,
20 Action<T>? legacy = null,
21 StringComparer? identityComparer = null) where T : class
22 {
23 try
24 {
25 var root = JToken.Parse(
26 json,
27 new JsonLoadSettings
28 { DuplicatePropertyNameHandling = DuplicatePropertyNameHandling.Error }) as JObject
29 ?? throw new InvalidDataException("Definition document must be a JSON object.");
30
31 var properties = root.Properties()
32 .Where(p => string.Equals(p.Name, "Definitions", StringComparison.OrdinalIgnoreCase)).ToArray();
33
34 if (properties.Length == 0)
35 {
36 var singleton = read(root.ToString());
37 legacy?.Invoke(singleton);
38 return [singleton];
39 }
40
41 if (properties.Length != 1 || root.Properties().Count() != 1)
42 throw new InvalidDataException(
43 "Definition document cannot combine Definitions with singleton properties.");
44
45 if (properties[0].Value is not JArray array)
46 throw new InvalidDataException("Definitions must be a non-null JSON array.");
47
48 var definitions = new List<T>();
49
50 foreach (var token in array)
51 {
52 if (token is not JObject member || member.Properties().Any(p => string.Equals(
53 p.Name,
54 "Definitions",
55 StringComparison.OrdinalIgnoreCase)))
56 throw new InvalidDataException(
57 $"Definitions[{definitions.Count}] must be a non-null definition object.");
58
59 definitions.Add(read(member.ToString()));
60 }
61
62 Check(definitions, identity, identityComparer: identityComparer);
63 return definitions;
64 }
65 catch (JsonException ex)
66 {
67 throw new InvalidDataException("Failed to deserialize definition document.", ex);
68 }
69 }
70
76 internal static void Check<T>(
77 IReadOnlyList<T>? definitions,
78 Func<T, string> identity,
79 bool allowUnnamedSingleton = false,
80 StringComparer? identityComparer = null) where T : class
81 {
82 if (definitions is null)
83 throw new InvalidDataException("Definitions must not be null.");
84
85 var identities = new HashSet<string>(identityComparer ?? StringComparer.Ordinal);
86
87 for (int i = 0; i < definitions.Count; i++)
88 {
89 var member = definitions[i] ?? throw new InvalidDataException($"Definitions[{i}] must not be null.");
90 var key = identity(member);
91
92 if (string.IsNullOrWhiteSpace(key) && !(allowUnnamedSingleton && definitions.Count == 1))
93 throw new InvalidDataException($"Definitions[{i}] requires a non-empty logical identity.");
94
95 if (!identities.Add(key))
96 throw new InvalidDataException($"Duplicate definition identity '{key}'.");
97 }
98 }
99
106 internal static T Select<T>(
107 IReadOnlyList<T> definitions,
108 Func<T, string> identity,
109 string? key = null,
110 StringComparer? identityComparer = null)
111 {
112 if (key is null)
113 {
114 if (definitions.Count != 1)
115 throw new InvalidDataException(
116 $"Document contains {definitions.Count} definitions. Specify a logical identity or use LoadAll.");
117
118 return definitions[0];
119 }
120
121 ArgumentException.ThrowIfNullOrWhiteSpace(key);
122
123 foreach (var member in definitions)
124 if ((identityComparer ?? StringComparer.Ordinal).Equals(identity(member), key))
125 return member;
126
127 throw new InvalidDataException($"Definition '{key}' was not found in the document.");
128 }
129}