6public static partial class AudioDefinitionSerializer
12 public static AudioDefinitionDocument FromDocumentJson(
string json,
string? sourceIdentity =
null) =>
new()
16 json => ParseDefinition(json,
null),
17 definition => definition.Key,
20 if (
string.IsNullOrWhiteSpace(definition.Key))
21 definition.Key = Path.GetFileNameWithoutExtension(sourceIdentity) is { Length: > 0 } inferred
30 public static string ToDocumentJson(AudioDefinitionDocument document)
32 ArgumentNullException.ThrowIfNull(document);
33 DefinitionDocumentJson.Check(document.Definitions, definition => definition.Key);
34 return DefinitionDocumentPersistence.Serialize(document, Settings);
40 public static AudioDefinitionDocument LoadDocument(
string filePath)
42 ArgumentException.ThrowIfNullOrWhiteSpace(filePath);
43 var path = Path.GetFullPath(filePath);
44 var document = FromDocumentJson(File.ReadAllText(path), path);
46 foreach (var definition
in document.Definitions)
48 definition.Source = AudioDefinitionSource.LooseDefinitionFile(path);
49 definition.LoadStamp =
new(definition, filePath: path, definitionIdentity: definition.Key);
58 public static AudioDefinitionDocument LoadDocument(Stream stream)
60 ArgumentNullException.ThrowIfNull(stream);
63 throw new ArgumentException(
"Stream must be readable.", nameof(stream));
65 using var reader =
new StreamReader(stream, leaveOpen:
true);
66 return FromDocumentJson(reader.ReadToEnd());
73 public static AudioDefinitionDocument LoadDocument(AssetsFile assetsFile,
string entryName)
75 ArgumentNullException.ThrowIfNull(assetsFile);
76 ArgumentException.ThrowIfNullOrWhiteSpace(entryName);
78 using var stream = assetsFile.
Get(
AssetTypes.AudioDefinition, entryName)
79 ??
throw new FileNotFoundException($
"GSND asset entry not found: {entryName}", entryName);
81 using var reader =
new StreamReader(stream);
82 var document = FromDocumentJson(reader.ReadToEnd(), entryName);
84 foreach (var definition
in document.Definitions)
86 definition.Source =
string.IsNullOrWhiteSpace(assetsFile.
SourcePath)
87 ? AudioDefinitionSource.None()
88 : AudioDefinitionSource.PackedDefinitionFile(assetsFile.
SourcePath, entryName);
90 definition.LoadStamp =
new(
94 definitionIdentity: definition.Key);
103 public static IReadOnlyList<string> ValidateDocument(AudioDefinitionDocument document)
105 ArgumentNullException.ThrowIfNull(document);
106 var errors =
new List<string>();
110 DefinitionDocumentJson.Check(document.Definitions, definition => definition.Key);
112 catch (InvalidDataException ex)
114 errors.Add(ex.Message);
117 foreach (var definition
in document.Definitions ?? [])
118 if (definition is not
null)
120 AudioDefinitionValidator.Validate(definition)
121 .Select(error => $
"Audio '{definition.Key}': {error}"));
129 public static void SaveDocument(
string filePath, AudioDefinitionDocument document)
131 ArgumentNullException.ThrowIfNull(document);
132 SaveAll(filePath, document.Definitions);
138 public static void SaveAll(
string filePath, IEnumerable<AudioDefinition> definitions) =>
139 SaveAll(filePath, definitions,
null);
145 public static void SaveAll(
string filePath, IEnumerable<AudioDefinition> definitions, Action<string>? beforeInstall)
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);
159 var clone = ParseDefinition(ToJson(definition),
null);
161 if (
string.IsNullOrWhiteSpace(clone.Key))
162 clone.Key = Path.GetFileNameWithoutExtension(path);
164 var origin = definition.Source.GsndFilePath ?? definition.Source.AssetsFilePath;
166 var oldDirectory = definition.LoadStamp?.ReferenceBaseDirectory ??
167 (
string.IsNullOrWhiteSpace(origin) || !Path.IsPathFullyQualified(origin)
169 : Path.GetDirectoryName(origin));
171 var newDirectory = Path.GetDirectoryName(path)!;
173 string? RebasePath(
string? value) =>
174 string.IsNullOrWhiteSpace(value) || (oldDirectory is
null && !Path.IsPathRooted(value))
176 : Path.GetRelativePath(
178 oldDirectory is
null ? Path.GetFullPath(value) : Path.GetFullPath(value, oldDirectory));
180 foreach (var resource
in clone.Resources)
185 resource.FilePath = RebasePath(resource.FilePath);
186 resource.AssetsFilePath = RebasePath(resource.AssetsFilePath);
189 clone.Source = AudioDefinitionSource.LooseDefinitionFile(path);
196 public static IReadOnlyList<AudioDefinition> LoadAll(
string filePath) => LoadDocument(filePath).Definitions;
203 DefinitionDocumentJson.Select(LoadAll(filePath), definition => definition.Key, key);
208 public static IReadOnlyList<AudioDefinition> LoadAll(Stream stream) => LoadDocument(stream).Definitions;
215 DefinitionDocumentJson.Select(LoadAll(stream), definition => definition.Key, key);
221 public static IReadOnlyList<AudioDefinition> LoadAll(AssetsFile assetsFile,
string entryName) =>
222 LoadDocument(assetsFile, entryName).Definitions;
229 public static AudioDefinition Load(AssetsFile assetsFile,
string entryName,
string? key) =>
230 DefinitionDocumentJson.Select(LoadAll(assetsFile, entryName), definition => definition.Key, key);
237 public static IReadOnlyList<AudioResource> LoadIntoManager(
240 bool overwriteExisting =
true) =>
241 LoadIntoManager(Load(filePath, key), overwriteExisting);
247 public static IReadOnlyList<IReadOnlyList<AudioResource>> LoadAllIntoManager(
249 bool overwriteExisting =
true) =>
250 LoadDocumentIntoManager(LoadDocument(filePath), overwriteExisting);
258 public static IReadOnlyList<AudioResource> LoadIntoManager(
259 AssetsFile assetsFile,
262 bool overwriteExisting =
true) =>
263 LoadIntoManager(Load(assetsFile, entryName, key), overwriteExisting);
270 public static IReadOnlyList<IReadOnlyList<AudioResource>> LoadAllIntoManager(
271 AssetsFile assetsFile,
273 bool overwriteExisting =
true) =>
274 LoadDocumentIntoManager(LoadDocument(assetsFile, entryName), overwriteExisting);
280 public static IReadOnlyList<IReadOnlyList<AudioResource>> LoadDocumentIntoManager(
281 AudioDefinitionDocument document,
282 bool overwriteExisting =
true)
284 var errors = ValidateDocument(document);
286 if (errors.Count != 0)
287 throw new InvalidDataException(
string.Join(Environment.NewLine, errors));
289 var manager = AudioResourceManager.Instance;
291 return manager.LoadDefinitionDocument<IReadOnlyList<IReadOnlyList<AudioResource>>>(() =>
293 var groups = document.Definitions.Select(definition => LoadIntoManager(definition, overwriteExisting))
296 if (groups.Count != 1)
297 manager.DefinitionProvenance =
null;
306 public static AudioDefinition FromResources(IEnumerable<AudioResource> resources)
308 ArgumentNullException.ThrowIfNull(resources);
312 Resources = resources.OrderBy(resource => resource.Key, StringComparer.Ordinal).Select(FromResource)
314 Source = AudioDefinitionSource.Generated()
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.
@ AudioDefinition
Represents an audio definition file (.gsnd) for Gondwana.Audio.GSND.AudioDefinition.
AudioResourceSourceKind
Identifies how a GSND audio resource locates its underlying media.