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
AudioDefinitionSerializer.Documents.cs
3
4namespace Gondwana.Audio.GSND;
5
6public static partial class AudioDefinitionSerializer
7{
12 public static AudioDefinitionDocument FromDocumentJson(string json, string? sourceIdentity = null) => new()
13 {
14 Definitions = DefinitionDocumentJson.Read<AudioDefinition>(
15 json,
16 json => ParseDefinition(json, null),
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 : "audio";
24 })
25 };
26
30 public static string ToDocumentJson(AudioDefinitionDocument 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 AudioDefinitionDocument 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 = AudioDefinitionSource.LooseDefinitionFile(path);
49 definition.LoadStamp = new(definition, filePath: path, definitionIdentity: definition.Key);
50 }
51
52 return document;
53 }
54
58 public static AudioDefinitionDocument 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 AudioDefinitionDocument LoadDocument(AssetsFile assetsFile, string entryName)
74 {
75 ArgumentNullException.ThrowIfNull(assetsFile);
76 ArgumentException.ThrowIfNullOrWhiteSpace(entryName);
77
78 using var stream = assetsFile.Get(AssetTypes.AudioDefinition, entryName)
79 ?? throw new FileNotFoundException($"GSND 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 ? AudioDefinitionSource.None()
88 : AudioDefinitionSource.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(AudioDefinitionDocument 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 AudioDefinitionValidator.Validate(definition)
121 .Select(error => $"Audio '{definition.Key}': {error}"));
122
123 return errors;
124 }
125
129 public static void SaveDocument(string filePath, AudioDefinitionDocument document)
130 {
131 ArgumentNullException.ThrowIfNull(document);
132 SaveAll(filePath, document.Definitions);
133 }
134
138 public static void SaveAll(string filePath, IEnumerable<AudioDefinition> definitions) =>
139 SaveAll(filePath, definitions, null);
140
145 public static void SaveAll(string filePath, IEnumerable<AudioDefinition> definitions, Action<string>? beforeInstall)
146 {
147 ArgumentException.ThrowIfNullOrWhiteSpace(filePath);
148 ArgumentNullException.ThrowIfNull(definitions);
149 var path = Path.GetFullPath(filePath);
150 var members = definitions.ToList();
151 DefinitionDocumentJson.Check(members, definition => definition.Key, allowUnnamedSingleton: true);
152 var saved = members.Select(definition => PrepareDocumentMember(definition, path)).ToList();
153 var json = ToDocumentJson(new() { Definitions = saved });
154 DefinitionDocumentPersistence.WriteAtomic(path, json, beforeInstall);
155 }
156
157 private static AudioDefinition PrepareDocumentMember(AudioDefinition definition, string path)
158 {
159 var clone = ParseDefinition(ToJson(definition), null);
160
161 if (string.IsNullOrWhiteSpace(clone.Key))
162 clone.Key = Path.GetFileNameWithoutExtension(path);
163
164 var origin = definition.Source.GsndFilePath ?? definition.Source.AssetsFilePath;
165
166 var oldDirectory = definition.LoadStamp?.ReferenceBaseDirectory ??
167 (string.IsNullOrWhiteSpace(origin) || !Path.IsPathFullyQualified(origin)
168 ? null
169 : Path.GetDirectoryName(origin));
170
171 var newDirectory = Path.GetDirectoryName(path)!;
172
173 string? RebasePath(string? value) =>
174 string.IsNullOrWhiteSpace(value) || (oldDirectory is null && !Path.IsPathRooted(value))
175 ? value
176 : Path.GetRelativePath(
177 newDirectory,
178 oldDirectory is null ? Path.GetFullPath(value) : Path.GetFullPath(value, oldDirectory));
179
180 foreach (var resource in clone.Resources)
181 {
182 if (resource.SourceKind == AudioResourceSourceKind.Uri)
183 continue;
184
185 resource.FilePath = RebasePath(resource.FilePath);
186 resource.AssetsFilePath = RebasePath(resource.AssetsFilePath);
187 }
188
189 clone.Source = AudioDefinitionSource.LooseDefinitionFile(path);
190 return clone;
191 }
192
196 public static IReadOnlyList<AudioDefinition> LoadAll(string filePath) => LoadDocument(filePath).Definitions;
197
202 public static AudioDefinition Load(string filePath, string? key) =>
203 DefinitionDocumentJson.Select(LoadAll(filePath), definition => definition.Key, key);
204
208 public static IReadOnlyList<AudioDefinition> LoadAll(Stream stream) => LoadDocument(stream).Definitions;
209
214 public static AudioDefinition Load(Stream stream, string? key) =>
215 DefinitionDocumentJson.Select(LoadAll(stream), definition => definition.Key, key);
216
221 public static IReadOnlyList<AudioDefinition> LoadAll(AssetsFile assetsFile, string entryName) =>
222 LoadDocument(assetsFile, entryName).Definitions;
223
229 public static AudioDefinition Load(AssetsFile assetsFile, string entryName, string? key) =>
230 DefinitionDocumentJson.Select(LoadAll(assetsFile, entryName), definition => definition.Key, key);
231
237 public static IReadOnlyList<AudioResource> LoadIntoManager(
238 string filePath,
239 string key,
240 bool overwriteExisting = true) =>
241 LoadIntoManager(Load(filePath, key), overwriteExisting);
242
247 public static IReadOnlyList<IReadOnlyList<AudioResource>> LoadAllIntoManager(
248 string filePath,
249 bool overwriteExisting = true) =>
250 LoadDocumentIntoManager(LoadDocument(filePath), overwriteExisting);
251
258 public static IReadOnlyList<AudioResource> LoadIntoManager(
259 AssetsFile assetsFile,
260 string entryName,
261 string key,
262 bool overwriteExisting = true) =>
263 LoadIntoManager(Load(assetsFile, entryName, key), overwriteExisting);
264
270 public static IReadOnlyList<IReadOnlyList<AudioResource>> LoadAllIntoManager(
271 AssetsFile assetsFile,
272 string entryName,
273 bool overwriteExisting = true) =>
274 LoadDocumentIntoManager(LoadDocument(assetsFile, entryName), overwriteExisting);
275
280 public static IReadOnlyList<IReadOnlyList<AudioResource>> LoadDocumentIntoManager(
281 AudioDefinitionDocument document,
282 bool overwriteExisting = true)
283 {
284 var errors = ValidateDocument(document);
285
286 if (errors.Count != 0)
287 throw new InvalidDataException(string.Join(Environment.NewLine, errors));
288
289 var manager = AudioResourceManager.Instance;
290
291 return manager.LoadDefinitionDocument<IReadOnlyList<IReadOnlyList<AudioResource>>>(() =>
292 {
293 var groups = document.Definitions.Select(definition => LoadIntoManager(definition, overwriteExisting))
294 .ToList();
295
296 if (groups.Count != 1)
297 manager.DefinitionProvenance = null;
298
299 return groups;
300 });
301 }
302
306 public static AudioDefinition FromResources(IEnumerable<AudioResource> resources)
307 {
308 ArgumentNullException.ThrowIfNull(resources);
309
310 return new()
311 {
312 Resources = resources.OrderBy(resource => resource.Key, StringComparer.Ordinal).Select(FromResource)
313 .ToList(),
314 Source = AudioDefinitionSource.Generated()
315 };
316 }
317}
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
@ AudioDefinition
Represents an audio definition file (.gsnd) for Gondwana.Audio.GSND.AudioDefinition.
Definition AssetTypes.cs:62
AudioResourceSourceKind
Identifies how a GSND audio resource locates its underlying media.