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
SpriteDefinitionValidator.cs
2
4public static class SpriteDefinitionValidator
5{
11 public static IReadOnlyList<string> Validate(SpriteDefinition definition) => Validate(definition, false);
12
13 internal static IReadOnlyList<string> Validate(SpriteDefinition definition, bool allowDuplicateNicknames)
14 {
15 ArgumentNullException.ThrowIfNull(definition);
16 var errors = new List<string>();
17 var ids = new HashSet<Guid>();
18 var names = new HashSet<string>(StringComparer.Ordinal);
19
20 if (definition.Sprites is null)
21 errors.Add("Sprites collection is required.");
22
23 foreach (var sprite in definition.Sprites ?? [])
24 {
25 if (sprite is null)
26 {
27 errors.Add("Sprites cannot contain null entries.");
28 continue;
29 }
30
31 var label = $"Sprite '{sprite.Nickname ?? sprite.Id.ToString()}'";
32
33 if (sprite.Id != Guid.Empty && !ids.Add(sprite.Id))
34 errors.Add($"{label}: duplicate Id.");
35
36 if (!string.IsNullOrWhiteSpace(sprite.Nickname) && !names.Add(sprite.Nickname) && !allowDuplicateNicknames)
37 errors.Add($"{label}: duplicate Nickname.");
38
39 if (string.IsNullOrWhiteSpace(sprite.SceneId))
40 errors.Add($"{label}: SceneId is required.");
41
42 if (string.IsNullOrWhiteSpace(sprite.SceneLayerId))
43 errors.Add($"{label}: SceneLayerId is required.");
44
45 // A default/unassigned frame is valid in the existing Sprite persistence contract.
46 if (sprite.Frame is { } frame)
47 {
48 if (string.IsNullOrWhiteSpace(frame.Tilesheet))
49 errors.Add($"{label}: Frame Tilesheet is required.");
50
51 if (string.IsNullOrWhiteSpace(frame.RegionName))
52 errors.Add($"{label}: Frame RegionName is required.");
53
54 if (frame.XTile < 0 || frame.YTile < 0)
55 errors.Add($"{label}: Frame coordinates must be nonnegative.");
56 }
57
58 if (sprite.AnimationKey is not null && string.IsNullOrWhiteSpace(sprite.AnimationKey))
59 errors.Add($"{label}: AnimationKey cannot be empty or whitespace.");
60
61 if (sprite.StartAnimation && string.IsNullOrWhiteSpace(sprite.AnimationKey))
62 errors.Add($"{label}: StartAnimation requires AnimationKey.");
63
64 if (!float.IsFinite(sprite.Position.X) || !float.IsFinite(sprite.Position.Y))
65 errors.Add($"{label}: Position must be finite.");
66
67 if (!float.IsFinite(sprite.Rotation))
68 errors.Add($"{label}: Rotation must be finite.");
69
70 if (sprite.RenderSize.Width < 0 || sprite.RenderSize.Height < 0)
71 errors.Add($"{label}: RenderSize must be nonnegative.");
72
73 if (sprite.ZOrder < 1)
74 errors.Add($"{label}: ZOrder must be at least 1.");
75
76 if (!Enum.IsDefined(sprite.HorizAlign) || !Enum.IsDefined(sprite.VertAlign))
77 errors.Add($"{label}: invalid alignment.");
78
79 if (!Enum.IsDefined(sprite.CollisionType))
80 errors.Add($"{label}: invalid CollisionType.");
81 }
82
83 var sheets = new HashSet<string>(StringComparer.Ordinal);
84
85 foreach (var source in definition.TilesheetSources ?? [])
86 {
87 if (source is null)
88 {
89 errors.Add("Null TilesheetSources entry.");
90 continue;
91 }
92
93 if (string.IsNullOrWhiteSpace(source.Tilesheet) || !sheets.Add(source.Tilesheet))
94 errors.Add("Invalid or duplicate TilesheetSources identity.");
95
96 ValidateSource(
97 source.Kind == SpriteTilesheetSourceKind.LooseDefinitionFile,
98 Enum.IsDefined(source.Kind),
99 source.GtsPath,
100 source.AssetsFilePath,
101 source.AssetEntryName,
102 "TilesheetSources",
103 errors);
104 }
105
106 var scenes = new HashSet<string>(StringComparer.Ordinal);
107
108 foreach (var source in definition.SceneSources ?? [])
109 {
110 if (source is null)
111 {
112 errors.Add("Null SceneSources entry.");
113 continue;
114 }
115
116 if (string.IsNullOrWhiteSpace(source.SceneId) || !scenes.Add(source.SceneId))
117 errors.Add("Invalid or duplicate SceneSources identity.");
118
119 ValidateSource(
120 source.Kind == SpriteSceneSourceKind.LooseDefinitionFile,
121 Enum.IsDefined(source.Kind),
122 source.GscnPath,
123 source.AssetsFilePath,
124 source.AssetEntryName,
125 "SceneSources",
126 errors);
127 }
128
129 var animations = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
130
131 foreach (var source in definition.AnimationSources ?? [])
132 {
133 if (source is null)
134 {
135 errors.Add("Null AnimationSources entry.");
136 continue;
137 }
138
139 if (string.IsNullOrWhiteSpace(source.AnimationKey) || !animations.Add(source.AnimationKey))
140 errors.Add("Invalid or duplicate AnimationSources identity.");
141
142 ValidateSource(
143 source.Kind == SpriteAnimationSourceKind.LooseDefinitionFile,
144 Enum.IsDefined(source.Kind),
145 source.GaniPath,
146 source.AssetsFilePath,
147 source.AssetEntryName,
148 "AnimationSources",
149 errors);
150 }
151
152 return errors;
153 }
154
155 private static void ValidateSource(
156 bool loose,
157 bool validKind,
158 string? path,
159 string? archive,
160 string? entry,
161 string label,
162 List<string> errors)
163 {
164 if (!validKind || (loose
165 ? string.IsNullOrWhiteSpace(path)
166 : string.IsNullOrWhiteSpace(archive) || string.IsNullOrWhiteSpace(entry)))
167 errors.Add($"{label}: source location is incomplete or invalid.");
168 }
169}
SpriteSceneSourceKind
Identifies how a GSPR definition can locate one of its logical GSCN dependencies for authoring and to...
SpriteTilesheetSourceKind
Identifies how a GSPR definition can locate one of its logical GTS dependencies for authoring and too...
SpriteAnimationSourceKind
Storage form of an optional GANI authoring dependency.