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
AnimationDefinitionSerializer.Documents.cs
3
5
6public static partial class AnimationDefinitionSerializer
7{
12 public static AnimationDefinitionDocument FromDocumentJson(string json, string? sourceIdentity = null) => new()
13 {
14 Definitions = DefinitionDocumentJson.Read<AnimationDefinition>(
15 json,
16 json => ParseDefinition(json, null),
17 definition => definition.Key,
18 identityComparer: StringComparer.OrdinalIgnoreCase)
19 };
20
24 public static string ToDocumentJson(AnimationDefinitionDocument document)
25 {
26 ArgumentNullException.ThrowIfNull(document);
27
28 DefinitionDocumentJson.Check(
29 document.Definitions,
30 definition => definition.Key,
31 identityComparer: StringComparer.OrdinalIgnoreCase);
32
33 return DefinitionDocumentPersistence.Serialize(document, Settings);
34 }
35
39 public static AnimationDefinitionDocument LoadDocument(string filePath)
40 {
41 ArgumentException.ThrowIfNullOrWhiteSpace(filePath);
42 var path = Path.GetFullPath(filePath);
43 var document = FromDocumentJson(File.ReadAllText(path), path);
44
45 foreach (var definition in document.Definitions)
46 {
47 definition.Source = AnimationDefinitionSource.LooseDefinitionFile(path);
48 definition.LoadStamp = new(definition, filePath: path, definitionIdentity: definition.Key);
49 }
50
51 return document;
52 }
53
57 public static AnimationDefinitionDocument LoadDocument(Stream stream)
58 {
59 ArgumentNullException.ThrowIfNull(stream);
60
61 if (!stream.CanRead)
62 throw new ArgumentException("Stream must be readable.", nameof(stream));
63
64 using var reader = new StreamReader(stream, leaveOpen: true);
65 return FromDocumentJson(reader.ReadToEnd());
66 }
67
72 public static AnimationDefinitionDocument LoadDocument(AssetsFile assetsFile, string entryName)
73 {
74 ArgumentNullException.ThrowIfNull(assetsFile);
75 ArgumentException.ThrowIfNullOrWhiteSpace(entryName);
76
77 using var stream = assetsFile.Get(AssetTypes.AnimationDefinition, entryName)
78 ?? throw new FileNotFoundException($"GANI asset entry not found: {entryName}", entryName);
79
80 using var reader = new StreamReader(stream);
81 var document = FromDocumentJson(reader.ReadToEnd(), entryName);
82
83 foreach (var definition in document.Definitions)
84 {
85 definition.Source = string.IsNullOrWhiteSpace(assetsFile.SourcePath)
86 ? AnimationDefinitionSource.None()
87 : AnimationDefinitionSource.PackedDefinitionFile(assetsFile.SourcePath, entryName);
88
89 definition.LoadStamp = new(
90 definition,
91 assetsFilePath: assetsFile.SourcePath,
92 entryName: entryName,
93 definitionIdentity: definition.Key);
94 }
95
96 return document;
97 }
98
102 public static IReadOnlyList<string> ValidateDocument(AnimationDefinitionDocument document)
103 {
104 ArgumentNullException.ThrowIfNull(document);
105 var errors = new List<string>();
106
107 try
108 {
109 DefinitionDocumentJson.Check(
110 document.Definitions,
111 definition => definition.Key,
112 identityComparer: StringComparer.OrdinalIgnoreCase);
113 }
114 catch (InvalidDataException ex)
115 {
116 errors.Add(ex.Message);
117 }
118
119 foreach (var definition in document.Definitions ?? [])
120 if (definition is not null)
121 errors.AddRange(
122 AnimationDefinitionValidator.Validate(definition)
123 .Select(error => $"Animation '{definition.Key}': {error}"));
124
125 return errors;
126 }
127
131 public static void SaveDocument(string filePath, AnimationDefinitionDocument document)
132 {
133 ArgumentNullException.ThrowIfNull(document);
134 SaveAll(filePath, document.Definitions);
135 }
136
140 public static void SaveAll(string filePath, IEnumerable<AnimationDefinition> definitions) =>
141 SaveAll(filePath, definitions, null);
142
147 public static void SaveAll(
148 string filePath,
149 IEnumerable<AnimationDefinition> definitions,
150 Action<string>? beforeInstall)
151 {
152 ArgumentException.ThrowIfNullOrWhiteSpace(filePath);
153 ArgumentNullException.ThrowIfNull(definitions);
154 var path = Path.GetFullPath(filePath);
155 var members = definitions.ToList();
156
157 DefinitionDocumentJson.Check(
158 members,
159 definition => definition.Key,
160 allowUnnamedSingleton: true,
161 identityComparer: StringComparer.OrdinalIgnoreCase);
162
163 var saved = members.Select(definition => PrepareDocumentMember(definition, path)).ToList();
164 var json = ToDocumentJson(new() { Definitions = saved });
165 DefinitionDocumentPersistence.WriteAtomic(path, json, beforeInstall);
166 }
167
168 private static AnimationDefinition PrepareDocumentMember(AnimationDefinition definition, string path)
169 {
170 var clone = ParseDefinition(ToJson(definition), null);
171 var origin = definition.Source.GaniFilePath ?? definition.Source.AssetsFilePath;
172
173 var oldDirectory = definition.LoadStamp?.ReferenceBaseDirectory ??
174 (string.IsNullOrWhiteSpace(origin) || !Path.IsPathFullyQualified(origin)
175 ? null
176 : Path.GetDirectoryName(origin));
177
178 var newDirectory = Path.GetDirectoryName(path)!;
179
180 string? RebasePath(string? value) =>
181 string.IsNullOrWhiteSpace(value) || (oldDirectory is null && !Path.IsPathRooted(value))
182 ? value
183 : Path.GetRelativePath(
184 newDirectory,
185 oldDirectory is null ? Path.GetFullPath(value) : Path.GetFullPath(value, oldDirectory));
186
187 foreach (var source in clone.TilesheetSources)
188 {
189 source.GtsPath = RebasePath(source.GtsPath);
190 source.AssetsFilePath = RebasePath(source.AssetsFilePath);
191 }
192
193 clone.Source = AnimationDefinitionSource.LooseDefinitionFile(path);
194 return clone;
195 }
196
200 public static IReadOnlyList<AnimationDefinition> LoadAll(string filePath) => LoadDocument(filePath).Definitions;
201
206 public static AnimationDefinition Load(string filePath, string? key) =>
207 DefinitionDocumentJson.Select(
208 LoadAll(filePath),
209 definition => definition.Key,
210 key,
211 StringComparer.OrdinalIgnoreCase);
212
216 public static IReadOnlyList<AnimationDefinition> LoadAll(Stream stream) => LoadDocument(stream).Definitions;
217
222 public static AnimationDefinition Load(Stream stream, string? key) =>
223 DefinitionDocumentJson.Select(
224 LoadAll(stream),
225 definition => definition.Key,
226 key,
227 StringComparer.OrdinalIgnoreCase);
228
233 public static IReadOnlyList<AnimationDefinition> LoadAll(AssetsFile assetsFile, string entryName) =>
234 LoadDocument(assetsFile, entryName).Definitions;
235
241 public static AnimationDefinition Load(AssetsFile assetsFile, string entryName, string? key) =>
242 DefinitionDocumentJson.Select(
243 LoadAll(assetsFile, entryName),
244 definition => definition.Key,
245 key,
246 StringComparer.OrdinalIgnoreCase);
247
252 public static Cycle LoadCycle(string filePath, string? identity) => ToCycle(Load(filePath, identity));
253
257 public static IReadOnlyList<Cycle> LoadAllCycles(string filePath) => ToAllCycles(LoadDocument(filePath));
258
264 public static Cycle LoadCycle(AssetsFile assetsFile, string entryName, string? identity) =>
265 ToCycle(Load(assetsFile, entryName, identity));
266
271 public static IReadOnlyList<Cycle> LoadAllCycles(AssetsFile assetsFile, string entryName) =>
272 ToAllCycles(LoadDocument(assetsFile, entryName));
273
277 public static IReadOnlyList<Cycle> ToAllCycles(AnimationDefinitionDocument document)
278 {
279 var errors = ValidateDocument(document);
280
281 if (errors.Count != 0)
282 throw new InvalidDataException(string.Join(Environment.NewLine, errors));
283
284 var created = new List<Cycle>();
285 var previous = new Dictionary<string, Cycle>(Cycle._cycles, StringComparer.OrdinalIgnoreCase);
286
287 try
288 {
289 foreach (var definition in document.Definitions)
290 created.Add(MaterializeCycle(definition));
291
292 for (int i = 0; i < created.Count; i++)
293 ApplyNextCycle(created[i], document.Definitions[i]);
294
295 return created;
296 }
297 catch
298 {
299 foreach (var item in created)
300 item.Dispose();
301
302 foreach (var pair in previous)
303 Cycle._cycles[pair.Key] = pair.Value;
304
305 throw;
306 }
307 }
308}
string? SourcePath
Gets the archive's reconstructible origin, including an optional stream origin.
Stream? Get(AssetTypes type, string name)
Retrieves a stream for the specified asset type and name.
AssetTypes
Defines the types of assets that can be stored and managed by the engine.
Definition AssetTypes.cs:8
@ AnimationDefinition
Represents an animation definition file (.gani) for Gondwana.Drawing.Animation.GANI....
Definition AssetTypes.cs:57