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
SpriteDefinitionSerializer.Documents.cs
3
5
6public static partial class SpriteDefinitionSerializer
7{
12 public static SpriteDefinitionDocument FromDocumentJson(string json, string? sourceIdentity = null) => new()
13 {
14 Definitions = DefinitionDocumentJson.Read<SpriteDefinition>(
15 json,
16 ParseDefinition,
17 definition => definition.Key,
18 definition =>
19 {
20 if (string.IsNullOrWhiteSpace(definition.Key))
21 definition.Key = Path.GetFileNameWithoutExtension(sourceIdentity) is { Length: > 0 } inferred
22 ? inferred
23 : "sprite";
24 })
25 };
26
30 public static string ToDocumentJson(SpriteDefinitionDocument document)
31 {
32 ArgumentNullException.ThrowIfNull(document);
33 DefinitionDocumentJson.Check(document.Definitions, definition => definition.Key);
34 return DefinitionDocumentPersistence.Serialize(document, Settings);
35 }
36
40 public static SpriteDefinitionDocument LoadDocument(string filePath)
41 {
42 ArgumentException.ThrowIfNullOrWhiteSpace(filePath);
43 var path = Path.GetFullPath(filePath);
44 var document = FromDocumentJson(File.ReadAllText(path), path);
45
46 foreach (var definition in document.Definitions)
47 {
48 definition.Source = SpriteDefinitionSource.LooseDefinitionFile(path);
49 definition.LoadStamp = new(definition, filePath: path, definitionIdentity: definition.Key);
50 }
51
52 return document;
53 }
54
58 public static SpriteDefinitionDocument LoadDocument(Stream stream)
59 {
60 ArgumentNullException.ThrowIfNull(stream);
61
62 if (!stream.CanRead)
63 throw new ArgumentException("Stream must be readable.", nameof(stream));
64
65 using var reader = new StreamReader(stream, leaveOpen: true);
66 return FromDocumentJson(reader.ReadToEnd());
67 }
68
73 public static SpriteDefinitionDocument LoadDocument(AssetsFile assetsFile, string entryName)
74 {
75 ArgumentNullException.ThrowIfNull(assetsFile);
76 ArgumentException.ThrowIfNullOrWhiteSpace(entryName);
77
78 using var stream = assetsFile.Get(AssetTypes.SpriteDefinition, entryName)
79 ?? throw new FileNotFoundException($"GSPR asset entry not found: {entryName}", entryName);
80
81 using var reader = new StreamReader(stream);
82 var document = FromDocumentJson(reader.ReadToEnd(), entryName);
83
84 foreach (var definition in document.Definitions)
85 {
86 definition.Source = string.IsNullOrWhiteSpace(assetsFile.SourcePath)
87 ? SpriteDefinitionSource.None()
88 : SpriteDefinitionSource.PackedDefinitionFile(assetsFile.SourcePath, entryName);
89
90 definition.LoadStamp = new(
91 definition,
92 assetsFilePath: assetsFile.SourcePath,
93 entryName: entryName,
94 definitionIdentity: definition.Key);
95 }
96
97 return document;
98 }
99
103 public static IReadOnlyList<string> ValidateDocument(SpriteDefinitionDocument document)
104 {
105 ArgumentNullException.ThrowIfNull(document);
106 var errors = new List<string>();
107
108 try
109 {
110 DefinitionDocumentJson.Check(document.Definitions, definition => definition.Key);
111 }
112 catch (InvalidDataException ex)
113 {
114 errors.Add(ex.Message);
115 }
116
117 foreach (var definition in document.Definitions ?? [])
118 if (definition is not null)
119 errors.AddRange(
120 SpriteDefinitionValidator.Validate(definition)
121 .Select(error => $"Sprite '{definition.Key}': {error}"));
122
123 return errors;
124 }
125
129 public static void SaveDocument(string filePath, SpriteDefinitionDocument document)
130 {
131 ArgumentNullException.ThrowIfNull(document);
132 SaveAll(filePath, document.Definitions);
133 }
134
138 public static void SaveAll(string filePath, IEnumerable<SpriteDefinition> definitions) =>
139 SaveAll(filePath, definitions, null);
140
145 public static void SaveAll(
146 string filePath,
147 IEnumerable<SpriteDefinition> definitions,
148 Action<string>? beforeInstall)
149 {
150 ArgumentException.ThrowIfNullOrWhiteSpace(filePath);
151 ArgumentNullException.ThrowIfNull(definitions);
152 var path = Path.GetFullPath(filePath);
153 var members = definitions.ToList();
154 DefinitionDocumentJson.Check(members, definition => definition.Key, allowUnnamedSingleton: true);
155 var saved = members.Select(definition => PrepareDocumentMember(definition, path)).ToList();
156 var json = ToDocumentJson(new() { Definitions = saved });
157 DefinitionDocumentPersistence.WriteAtomic(path, json, beforeInstall);
158 }
159
160 private static SpriteDefinition PrepareDocumentMember(SpriteDefinition definition, string path)
161 {
162 var clone = ParseDefinition(ToJson(definition));
163
164 if (string.IsNullOrWhiteSpace(clone.Key))
165 clone.Key = Path.GetFileNameWithoutExtension(path);
166
167 var origin = definition.Source.GsprFilePath ?? definition.Source.AssetsFilePath;
168
169 var oldDirectory = definition.LoadStamp?.ReferenceBaseDirectory ??
170 (string.IsNullOrWhiteSpace(origin) || !Path.IsPathFullyQualified(origin)
171 ? null
172 : Path.GetDirectoryName(origin));
173
174 var newDirectory = Path.GetDirectoryName(path)!;
175
176 string? RebasePath(string? value) =>
177 string.IsNullOrWhiteSpace(value) || (oldDirectory is null && !Path.IsPathRooted(value))
178 ? value
179 : Path.GetRelativePath(
180 newDirectory,
181 oldDirectory is null ? Path.GetFullPath(value) : Path.GetFullPath(value, oldDirectory));
182
183 foreach (var source in clone.TilesheetSources)
184 {
185 source.GtsPath = RebasePath(source.GtsPath);
186 source.AssetsFilePath = RebasePath(source.AssetsFilePath);
187 }
188
189 foreach (var source in clone.SceneSources)
190 {
191 source.GscnPath = RebasePath(source.GscnPath);
192 source.AssetsFilePath = RebasePath(source.AssetsFilePath);
193 }
194
195 foreach (var source in clone.AnimationSources)
196 {
197 source.GaniPath = RebasePath(source.GaniPath);
198 source.AssetsFilePath = RebasePath(source.AssetsFilePath);
199 }
200
201 clone.Source = SpriteDefinitionSource.LooseDefinitionFile(path);
202 return clone;
203 }
204
208 public static IReadOnlyList<SpriteDefinition> LoadAll(string filePath) => LoadDocument(filePath).Definitions;
209
214 public static SpriteDefinition Load(string filePath, string? key) =>
215 DefinitionDocumentJson.Select(LoadAll(filePath), definition => definition.Key, key);
216
220 public static IReadOnlyList<SpriteDefinition> LoadAll(Stream stream) => LoadDocument(stream).Definitions;
221
226 public static SpriteDefinition Load(Stream stream, string? key) =>
227 DefinitionDocumentJson.Select(LoadAll(stream), definition => definition.Key, key);
228
233 public static IReadOnlyList<SpriteDefinition> LoadAll(AssetsFile assetsFile, string entryName) =>
234 LoadDocument(assetsFile, entryName).Definitions;
235
241 public static SpriteDefinition Load(AssetsFile assetsFile, string entryName, string? key) =>
242 DefinitionDocumentJson.Select(LoadAll(assetsFile, entryName), definition => definition.Key, key);
243
248 public static List<Sprite> LoadSprites(string filePath, string? identity) => ToSprites(Load(filePath, identity));
249
253 public static IReadOnlyList<List<Sprite>> LoadAllSprites(string filePath) => ToAllSprites(LoadDocument(filePath));
254
260 public static List<Sprite> LoadSprites(AssetsFile assetsFile, string entryName, string? identity) =>
261 ToSprites(Load(assetsFile, entryName, identity));
262
267 public static IReadOnlyList<List<Sprite>> LoadAllSprites(AssetsFile assetsFile, string entryName) =>
268 ToAllSprites(LoadDocument(assetsFile, entryName));
269
273 public static IReadOnlyList<List<Sprite>> ToAllSprites(SpriteDefinitionDocument document)
274 {
275 var errors = ValidateDocument(document);
276
277 if (errors.Count != 0)
278 throw new InvalidDataException(string.Join(Environment.NewLine, errors));
279
280 var created = new List<List<Sprite>>();
281 var previous = SpriteManager.Instance.DefinitionProvenance;
282
283 try
284 {
285 foreach (var definition in document.Definitions)
286 created.Add(ToSprites(definition));
287
288 if (created.Count != 1)
289 SpriteManager.Instance.DefinitionProvenance = null;
290
291 return created;
292 }
293 catch
294 {
295 foreach (var sprite in created.SelectMany(group => group))
296 {
297 SpriteManager.Instance._spriteList.Remove(sprite);
298 sprite.DisposeImmediate();
299 }
300
301 SpriteManager.Instance.DefinitionProvenance = previous;
302 throw;
303 }
304 }
305}
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
@ SpriteDefinition
Gondwana sprite collection definition (.gspr).
Definition AssetTypes.cs:65