4public static class SpriteDefinitionValidator
11 public static IReadOnlyList<string> Validate(SpriteDefinition definition) => Validate(definition,
false);
13 internal static IReadOnlyList<string> Validate(SpriteDefinition definition,
bool allowDuplicateNicknames)
15 ArgumentNullException.ThrowIfNull(definition);
16 var errors =
new List<string>();
17 var ids =
new HashSet<Guid>();
18 var names =
new HashSet<string>(StringComparer.Ordinal);
20 if (definition.Sprites is
null)
21 errors.Add(
"Sprites collection is required.");
23 foreach (var sprite
in definition.Sprites ?? [])
27 errors.Add(
"Sprites cannot contain null entries.");
31 var label = $
"Sprite '{sprite.Nickname ?? sprite.Id.ToString()}'";
33 if (sprite.Id != Guid.Empty && !ids.Add(sprite.Id))
34 errors.Add($
"{label}: duplicate Id.");
36 if (!
string.IsNullOrWhiteSpace(sprite.Nickname) && !names.Add(sprite.Nickname) && !allowDuplicateNicknames)
37 errors.Add($
"{label}: duplicate Nickname.");
39 if (
string.IsNullOrWhiteSpace(sprite.SceneId))
40 errors.Add($
"{label}: SceneId is required.");
42 if (
string.IsNullOrWhiteSpace(sprite.SceneLayerId))
43 errors.Add($
"{label}: SceneLayerId is required.");
46 if (sprite.Frame is { } frame)
48 if (
string.IsNullOrWhiteSpace(frame.Tilesheet))
49 errors.Add($
"{label}: Frame Tilesheet is required.");
51 if (
string.IsNullOrWhiteSpace(frame.RegionName))
52 errors.Add($
"{label}: Frame RegionName is required.");
54 if (frame.XTile < 0 || frame.YTile < 0)
55 errors.Add($
"{label}: Frame coordinates must be nonnegative.");
58 if (sprite.AnimationKey is not
null &&
string.IsNullOrWhiteSpace(sprite.AnimationKey))
59 errors.Add($
"{label}: AnimationKey cannot be empty or whitespace.");
61 if (sprite.StartAnimation &&
string.IsNullOrWhiteSpace(sprite.AnimationKey))
62 errors.Add($
"{label}: StartAnimation requires AnimationKey.");
64 if (!
float.IsFinite(sprite.Position.X) || !
float.IsFinite(sprite.Position.Y))
65 errors.Add($
"{label}: Position must be finite.");
67 if (!
float.IsFinite(sprite.Rotation))
68 errors.Add($
"{label}: Rotation must be finite.");
70 if (sprite.RenderSize.Width < 0 || sprite.RenderSize.Height < 0)
71 errors.Add($
"{label}: RenderSize must be nonnegative.");
73 if (sprite.ZOrder < 1)
74 errors.Add($
"{label}: ZOrder must be at least 1.");
76 if (!Enum.IsDefined(sprite.HorizAlign) || !Enum.IsDefined(sprite.VertAlign))
77 errors.Add($
"{label}: invalid alignment.");
79 if (!Enum.IsDefined(sprite.CollisionType))
80 errors.Add($
"{label}: invalid CollisionType.");
83 var sheets =
new HashSet<string>(StringComparer.Ordinal);
85 foreach (var source
in definition.TilesheetSources ?? [])
89 errors.Add(
"Null TilesheetSources entry.");
93 if (
string.IsNullOrWhiteSpace(source.Tilesheet) || !sheets.Add(source.Tilesheet))
94 errors.Add(
"Invalid or duplicate TilesheetSources identity.");
98 Enum.IsDefined(source.Kind),
100 source.AssetsFilePath,
101 source.AssetEntryName,
106 var scenes =
new HashSet<string>(StringComparer.Ordinal);
108 foreach (var source
in definition.SceneSources ?? [])
112 errors.Add(
"Null SceneSources entry.");
116 if (
string.IsNullOrWhiteSpace(source.SceneId) || !scenes.Add(source.SceneId))
117 errors.Add(
"Invalid or duplicate SceneSources identity.");
121 Enum.IsDefined(source.Kind),
123 source.AssetsFilePath,
124 source.AssetEntryName,
129 var animations =
new HashSet<string>(StringComparer.OrdinalIgnoreCase);
131 foreach (var source
in definition.AnimationSources ?? [])
135 errors.Add(
"Null AnimationSources entry.");
139 if (
string.IsNullOrWhiteSpace(source.AnimationKey) || !animations.Add(source.AnimationKey))
140 errors.Add(
"Invalid or duplicate AnimationSources identity.");
144 Enum.IsDefined(source.Kind),
146 source.AssetsFilePath,
147 source.AssetEntryName,
155 private static void ValidateSource(
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.");
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.