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
SceneDefinitionValidator.cs
2
4
8public static class SceneDefinitionValidator
9{
10 private static readonly string[] StandardGroups =
11 ["WorldStatic", "Actors", "Projectiles", "Triggers"];
12
13 private static readonly string[] StandardProfiles =
14 [
15 CollisionProfileNames.World, CollisionProfileNames.Actor, CollisionProfileNames.Projectile,
16 CollisionProfileNames.Sensor
17 ];
18
24 public static IReadOnlyList<string> Validate(SceneDefinition definition)
25 {
26 ArgumentNullException.ThrowIfNull(definition);
27
28 var errors = new List<string>();
29 ValidateTilesheetSources(definition, errors);
30 ValidateAnimationSources(definition, errors);
31
32 var knownGroups = new HashSet<string>(StandardGroups, StringComparer.OrdinalIgnoreCase);
33 var definedGroups = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
34
35 foreach (var group in definition.CollisionGroups ?? [])
36 {
37 if (string.IsNullOrWhiteSpace(group))
38 {
39 errors.Add("CollisionGroups contains an empty name.");
40 continue;
41 }
42
43 if (!definedGroups.Add(group))
44 errors.Add($"Collision group '{group}' is duplicated.");
45
46 knownGroups.Add(group);
47 }
48
49 var knownProfiles = new HashSet<string>(StandardProfiles, StringComparer.OrdinalIgnoreCase);
50 var definedProfiles = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
51
52 foreach (var profile in definition.CollisionProfiles ?? [])
53 {
54 if (profile is null)
55 {
56 errors.Add("CollisionProfiles contains a null entry.");
57 continue;
58 }
59
60 if (string.IsNullOrWhiteSpace(profile.Name))
61 errors.Add("CollisionProfiles contains a profile with an empty name.");
62 else
63 {
64 if (!definedProfiles.Add(profile.Name))
65 errors.Add($"Collision profile '{profile.Name}' is duplicated.");
66
67 knownProfiles.Add(profile.Name);
68 }
69
70 if (string.IsNullOrWhiteSpace(profile.CollisionGroup))
71 errors.Add($"Collision profile '{profile.Name}' has an empty CollisionGroup.");
72 else if (!knownGroups.Contains(profile.CollisionGroup))
73 errors.Add($"Collision profile '{profile.Name}' references unknown group '{profile.CollisionGroup}'.");
74
75 foreach (var group in profile.CollidesWith ?? [])
76 {
77 if (!knownGroups.Contains(group))
78 errors.Add($"Collision profile '{profile.Name}' references unknown CollidesWith group '{group}'.");
79 }
80 }
81
82 var layerIds = new HashSet<string>(StringComparer.Ordinal);
83 int layerIndex = 0;
84
85 foreach (var layer in definition.Layers ?? [])
86 {
87 if (layer is null)
88 {
89 errors.Add($"Layers contains a null entry at index {layerIndex++}.");
90 continue;
91 }
92
93 var label = string.IsNullOrWhiteSpace(layer.ID)
94 ? $"Layer #{layerIndex}"
95 : $"Layer '{layer.ID}'";
96
97 if (!string.IsNullOrWhiteSpace(layer.ID) && !layerIds.Add(layer.ID))
98 errors.Add($"{label}: duplicate ID.");
99
100 if (layer.Columns < 0 || layer.Rows < 0)
101 errors.Add($"{label}: Columns and Rows cannot be negative.");
102
103 if (layer.TileWidth <= 0 || layer.TileHeight <= 0)
104 errors.Add($"{label}: TileWidth and TileHeight must be positive.");
105
106 if (!float.IsFinite(layer.Parallax))
107 errors.Add($"{label}: Parallax must be finite.");
108
109 if (!Enum.IsDefined(layer.CoordinateSystemType))
110 errors.Add($"{label}: unknown coordinate system type '{layer.CoordinateSystemType}'.");
111
112 if (string.IsNullOrWhiteSpace(layer.DefaultTileCollisionProfile))
113 errors.Add($"{label}: DefaultTileCollisionProfile cannot be empty.");
114 else if (!knownProfiles.Contains(layer.DefaultTileCollisionProfile))
115 errors.Add($"{label}: unknown default collision profile '{layer.DefaultTileCollisionProfile}'.");
116
117 var coordinates = new HashSet<(int X, int Y)>();
118
119 foreach (var tile in layer.Tiles ?? [])
120 {
121 if (tile is null)
122 {
123 errors.Add($"{label}: Tiles contains a null entry.");
124 continue;
125 }
126
127 var tileLabel = $"{label}, tile ({tile.X}, {tile.Y})";
128
129 if (!coordinates.Add((tile.X, tile.Y)))
130 errors.Add($"{tileLabel}: duplicate tile entry.");
131
132 if (tile.X < 0 || tile.Y < 0 || tile.X >= layer.Columns || tile.Y >= layer.Rows)
133 errors.Add($"{tileLabel}: coordinates are outside the layer grid.");
134
135 if (!Enum.IsDefined(tile.Transform))
136 errors.Add($"{tileLabel}: unknown tile transform '{tile.Transform}'.");
137
138 if (!Enum.IsDefined(tile.CollisionType))
139 errors.Add($"{tileLabel}: unknown collision type '{tile.CollisionType}'.");
140
141 if (!string.IsNullOrWhiteSpace(tile.CollisionProfileName) &&
142 !knownProfiles.Contains(tile.CollisionProfileName))
143 {
144 errors.Add($"{tileLabel}: unknown collision profile '{tile.CollisionProfileName}'.");
145 }
146
147 if (tile.Frame is { } frame)
148 {
149 if (string.IsNullOrWhiteSpace(frame.Tilesheet))
150 errors.Add($"{tileLabel}: frame Tilesheet cannot be empty.");
151
152 if (string.IsNullOrWhiteSpace(frame.RegionName))
153 errors.Add($"{tileLabel}: frame RegionName cannot be empty.");
154
155 if (frame.XTile < 0 || frame.YTile < 0)
156 errors.Add($"{tileLabel}: frame coordinates cannot be negative.");
157 }
158
159 bool hasAnimationKey =
160 !string.IsNullOrWhiteSpace(tile.AnimationKey);
161
162 if (tile.AnimationKey is not null && !hasAnimationKey)
163 errors.Add($"{tileLabel}: AnimationKey cannot be empty.");
164
165 if (tile.StartAnimation && !hasAnimationKey)
166 errors.Add($"{tileLabel}: StartAnimation requires an AnimationKey.");
167 }
168
169 layerIndex++;
170 }
171
172 return errors;
173 }
174
175 private static void ValidateTilesheetSources(
176 SceneDefinition definition,
177 List<string> errors)
178 {
179 definition.TilesheetSources ??= [];
180 var names = new HashSet<string>(StringComparer.Ordinal);
181
182 for (int i = 0; i < definition.TilesheetSources.Count; i++)
183 {
184 var source = definition.TilesheetSources[i];
185 var label = $"Tilesheet source {i}";
186
187 if (source is null)
188 {
189 errors.Add($"{label}: source definition is null.");
190 continue;
191 }
192
193 if (string.IsNullOrWhiteSpace(source.Tilesheet))
194 errors.Add($"{label}: tilesheet name is empty.");
195 else if (!names.Add(source.Tilesheet))
196 errors.Add($"{label}: duplicate source for tilesheet '{source.Tilesheet}'.");
197
198 if (!Enum.IsDefined(source.Kind))
199 {
200 errors.Add($"{label}: unknown source kind '{source.Kind}'.");
201 continue;
202 }
203
204 switch (source.Kind)
205 {
206 case SceneTilesheetSourceKind.LooseDefinitionFile:
207 if (string.IsNullOrWhiteSpace(source.GtsPath))
208 errors.Add($"{label}: loose GTS source path is empty.");
209
210 break;
211
212 case SceneTilesheetSourceKind.PackedDefinitionFile:
213 if (string.IsNullOrWhiteSpace(source.AssetsFilePath))
214 errors.Add($"{label}: assets file path is empty.");
215
216 if (string.IsNullOrWhiteSpace(source.AssetEntryName))
217 errors.Add($"{label}: packed GTS entry name is empty.");
218
219 break;
220 }
221 }
222 }
223
224 private static void ValidateAnimationSources(
225 SceneDefinition definition,
226 List<string> errors)
227 {
228 definition.AnimationSources ??= [];
229 var keys = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
230
231 for (int i = 0; i < definition.AnimationSources.Count; i++)
232 {
233 var source = definition.AnimationSources[i];
234 var label = $"Animation source {i}";
235
236 if (source is null)
237 {
238 errors.Add($"{label}: source definition is null.");
239 continue;
240 }
241
242 if (string.IsNullOrWhiteSpace(source.AnimationKey))
243 errors.Add($"{label}: animation key is empty.");
244 else if (!keys.Add(source.AnimationKey))
245 errors.Add($"{label}: duplicate source for animation '{source.AnimationKey}'.");
246
247 if (!Enum.IsDefined(source.Kind))
248 {
249 errors.Add($"{label}: unknown source kind '{source.Kind}'.");
250 continue;
251 }
252
253 switch (source.Kind)
254 {
255 case SceneAnimationSourceKind.LooseDefinitionFile:
256 if (string.IsNullOrWhiteSpace(source.GaniPath))
257 errors.Add($"{label}: loose GANI source path is empty.");
258
259 break;
260
261 case SceneAnimationSourceKind.PackedDefinitionFile:
262 if (string.IsNullOrWhiteSpace(source.AssetsFilePath))
263 errors.Add($"{label}: assets file path is empty.");
264
265 if (string.IsNullOrWhiteSpace(source.AssetEntryName))
266 errors.Add($"{label}: packed GANI entry name is empty.");
267
268 break;
269 }
270 }
271 }
272}
SceneAnimationSourceKind
Identifies how a GSCN definition can locate one of its logical GANI dependencies for authoring and to...
SceneTilesheetSourceKind
Identifies how a GSCN definition can locate one of its logical GTS dependencies for authoring and too...