11public static partial class AnimationDefinitionSerializer
13 private static readonly JsonSerializerSettings Settings =
new()
15 Formatting = Formatting.Indented,
16 NullValueHandling = NullValueHandling.Ignore,
17 DefaultValueHandling = DefaultValueHandling.Include,
18 MissingMemberHandling = MissingMemberHandling.Ignore
28 return DefinitionDocumentJson.Select(
30 definition => definition.Key,
31 identityComparer: StringComparer.OrdinalIgnoreCase);
41 return DefinitionDocumentJson.Select(
43 definition => definition.Key,
44 identityComparer: StringComparer.OrdinalIgnoreCase);
55 return DefinitionDocumentJson.Select(
56 LoadAll(assetsFile, entryName),
57 definition => definition.Key,
58 identityComparer: StringComparer.OrdinalIgnoreCase);
68 ArgumentNullException.ThrowIfNull(definition);
69 SaveAll(filePath, [definition]);
77 public static void Save(
string filePath, Cycle cycle)
79 ArgumentNullException.ThrowIfNull(cycle);
80 Save(filePath, FromCycle(cycle));
89 DefinitionDocumentJson.Select(
90 FromDocumentJson(json).Definitions,
91 definition => definition.Key,
92 identityComparer: StringComparer.OrdinalIgnoreCase);
101 ArgumentNullException.ThrowIfNull(definition);
102 return JsonConvert.SerializeObject(definition, Settings);
112 ArgumentNullException.ThrowIfNull(cycle);
116 Key = cycle.CycleKey,
117 ThrottleTime = cycle.ThrottleTime,
118 CycleType = cycle.Sequence.SequenceCycleType,
119 HideTileOnCycleEnd = cycle.HideTileOnCycleEnd,
120 NextCycleKey = cycle.NextCycle?.CycleKey,
121 Frames = cycle.Sequence.FrameList
122 .Select((frame, index) =>
124 var definition = CreateFrameDefinition(frame);
125 definition.DurationSeconds = cycle.Sequence.GetDurationSeconds(index);
129 Source = AnimationDefinitionSource.Generated()
132 foreach (var name
in definition.Frames.Select(frame => frame.Tilesheet)
133 .Where(name => !
string.IsNullOrWhiteSpace(name))
134 .Distinct(StringComparer.Ordinal))
136 var source = DefinitionDependencyProvenance.ForTilesheet(
137 TilesheetRegistry.Instance.GetOrNull(name));
139 if (source?.FilePath is { } loose)
140 definition.TilesheetSources.Add(AnimationTilesheetSourceDefinition.Loose(name, loose));
141 else if (source?.AssetsFilePath is { } archive && source.AssetEntryName is { } entry)
142 definition.TilesheetSources.Add(AnimationTilesheetSourceDefinition.Packed(name, archive, entry));
153 public static string ToJson(Cycle cycle) => ToJson(FromCycle(cycle));
165 var previous = Cycle._cycles.TryGetValue(definition.Key, out var existing)
169 var cycle = MaterializeCycle(definition);
173 ApplyNextCycle(cycle, definition);
178 if (Cycle._cycles.TryGetValue(cycle.CycleKey, out var registered) &&
179 ReferenceEquals(registered, cycle))
183 if (previous is not
null)
184 Cycle._cycles[definition.Key] = previous;
196 public static Cycle LoadCycle(
string filePath) => ToCycle(Load(filePath));
204 public static Cycle LoadCycle(AssetsFile assetsFile,
string entryName) =>
205 ToCycle(Load(assetsFile, entryName));
209 ArgumentNullException.ThrowIfNull(definition);
211 var errors = AnimationDefinitionValidator.Validate(definition);
213 if (errors.Count != 0)
215 throw new InvalidDataException(
216 "GANI definition is invalid:" + Environment.NewLine +
217 string.Join(Environment.NewLine, errors.Select(error =>
"- " + error)));
220 var frames = definition.Frames
221 .Select(ResolveFrame)
224 var sequence =
new FrameSequence(frames)
226 SequenceCycleType = definition.CycleType
229 sequence.SetDurations(definition.Frames.Select(frame => frame.DurationSeconds));
233 definition.ThrottleTime,
235 definition.HideTileOnCycleEnd);
238 internal static void ApplyNextCycle(
242 ArgumentNullException.ThrowIfNull(cycle);
243 ArgumentNullException.ThrowIfNull(definition);
245 if (
string.IsNullOrWhiteSpace(definition.NextCycleKey) ||
247 definition.NextCycleKey,
249 StringComparison.OrdinalIgnoreCase))
251 cycle.NextCycle = cycle;
252 CaptureProvenance(cycle, definition);
256 if (!Cycle._cycles.TryGetValue(definition.NextCycleKey, out var nextCycle))
258 throw new InvalidDataException(
259 $
"GANI animation '{definition.Key}' references next cycle '{definition.NextCycleKey}', but it is not registered.");
262 cycle.NextCycle = nextCycle;
263 CaptureProvenance(cycle, definition);
267 cycle.DefinitionProvenance = definition.LoadStamp?.Materialized(
269 () => FromCycle(cycle));
273 string? sourceDescription)
275 if (
string.IsNullOrWhiteSpace(json))
276 throw new ArgumentException(
"GANI JSON content must be a non-empty string.", nameof(json));
282 if (definition is
null)
284 var source =
string.IsNullOrWhiteSpace(sourceDescription)
285 ?
"GANI JSON content"
288 throw new InvalidDataException($
"Failed to deserialize {source}. Result was null.");
291 definition.TilesheetSources ??= [];
293 for (
int i = 0; i < definition.TilesheetSources.Count; i++)
295 if (definition.TilesheetSources[i] is
null)
297 throw new InvalidDataException(
298 $
"GANI TilesheetSources cannot contain a null entry at index {i}.");
302 definition.Frames ??= [];
304 for (
int i = 0; i < definition.Frames.Count; i++)
306 if (definition.Frames[i] is
null)
307 throw new InvalidDataException($
"GANI Frames cannot contain a null entry at index {i}.");
312 catch (JsonException ex)
314 var source =
string.IsNullOrWhiteSpace(sourceDescription)
315 ?
"GANI JSON content"
318 throw new InvalidDataException($
"Failed to deserialize {source}.", ex);
322 private static AnimationFrameDefinition CreateFrameDefinition(Frame frame)
326 throw new InvalidOperationException(
327 "A GANI animation frame cannot be created from an unassigned runtime Frame.");
330 return new AnimationFrameDefinition
339 private static Frame ResolveFrame(AnimationFrameDefinition definition)
341 var tilesheet = TilesheetRegistry.Instance.GetOrNull(definition.Tilesheet)
342 ??
throw new InvalidDataException(
343 $
"GANI references tilesheet '{definition.Tilesheet}', but it is not registered.");
345 var region = tilesheet.GetRegion(definition.RegionName)
346 ??
throw new InvalidDataException(
347 $
"GANI references region '{definition.RegionName}' on tilesheet '{definition.Tilesheet}', but it does not exist.");
349 if (definition.XTile < 0 ||
350 definition.YTile < 0 ||
351 definition.XTile >= region.Columns ||
352 definition.YTile >= region.Rows)
354 throw new InvalidDataException(
355 $
"GANI frame ({definition.XTile}, {definition.YTile}) is outside region '{definition.RegionName}' on tilesheet '{definition.Tilesheet}'.");
358 return tilesheet.GetFrame(
359 definition.RegionName,
string Name
Gets the name.
@ AnimationDefinition
Represents an animation definition file (.gani) for Gondwana.Drawing.Animation.GANI....
CycleType
Simple is self-terminating; the other two are repeating.
readonly Tilesheet Tilesheet
The tilesheet that contains the source bitmap for this frame.
readonly string RegionName
The tilesheet region that contains the source bitmap for this frame.
readonly int XTile
The horizontal tile coordinate (column index) within the tilesheet.