6public static partial class AnimationDefinitionSerializer
12 public static AnimationDefinitionDocument FromDocumentJson(
string json,
string? sourceIdentity =
null) =>
new()
16 json => ParseDefinition(json,
null),
17 definition => definition.Key,
18 identityComparer: StringComparer.OrdinalIgnoreCase)
24 public static string ToDocumentJson(AnimationDefinitionDocument document)
26 ArgumentNullException.ThrowIfNull(document);
28 DefinitionDocumentJson.Check(
30 definition => definition.Key,
31 identityComparer: StringComparer.OrdinalIgnoreCase);
33 return DefinitionDocumentPersistence.Serialize(document, Settings);
39 public static AnimationDefinitionDocument LoadDocument(
string filePath)
41 ArgumentException.ThrowIfNullOrWhiteSpace(filePath);
42 var path = Path.GetFullPath(filePath);
43 var document = FromDocumentJson(File.ReadAllText(path), path);
45 foreach (var definition
in document.Definitions)
47 definition.Source = AnimationDefinitionSource.LooseDefinitionFile(path);
48 definition.LoadStamp =
new(definition, filePath: path, definitionIdentity: definition.Key);
57 public static AnimationDefinitionDocument LoadDocument(Stream stream)
59 ArgumentNullException.ThrowIfNull(stream);
62 throw new ArgumentException(
"Stream must be readable.", nameof(stream));
64 using var reader =
new StreamReader(stream, leaveOpen:
true);
65 return FromDocumentJson(reader.ReadToEnd());
72 public static AnimationDefinitionDocument LoadDocument(AssetsFile assetsFile,
string entryName)
74 ArgumentNullException.ThrowIfNull(assetsFile);
75 ArgumentException.ThrowIfNullOrWhiteSpace(entryName);
77 using var stream = assetsFile.
Get(
AssetTypes.AnimationDefinition, entryName)
78 ??
throw new FileNotFoundException($
"GANI asset entry not found: {entryName}", entryName);
80 using var reader =
new StreamReader(stream);
81 var document = FromDocumentJson(reader.ReadToEnd(), entryName);
83 foreach (var definition
in document.Definitions)
85 definition.Source =
string.IsNullOrWhiteSpace(assetsFile.
SourcePath)
86 ? AnimationDefinitionSource.None()
87 : AnimationDefinitionSource.PackedDefinitionFile(assetsFile.
SourcePath, entryName);
89 definition.LoadStamp =
new(
93 definitionIdentity: definition.Key);
102 public static IReadOnlyList<string> ValidateDocument(AnimationDefinitionDocument document)
104 ArgumentNullException.ThrowIfNull(document);
105 var errors =
new List<string>();
109 DefinitionDocumentJson.Check(
110 document.Definitions,
111 definition => definition.Key,
112 identityComparer: StringComparer.OrdinalIgnoreCase);
114 catch (InvalidDataException ex)
116 errors.Add(ex.Message);
119 foreach (var definition
in document.Definitions ?? [])
120 if (definition is not
null)
122 AnimationDefinitionValidator.Validate(definition)
123 .Select(error => $
"Animation '{definition.Key}': {error}"));
131 public static void SaveDocument(
string filePath, AnimationDefinitionDocument document)
133 ArgumentNullException.ThrowIfNull(document);
134 SaveAll(filePath, document.Definitions);
140 public static void SaveAll(
string filePath, IEnumerable<AnimationDefinition> definitions) =>
141 SaveAll(filePath, definitions,
null);
147 public static void SaveAll(
149 IEnumerable<AnimationDefinition> definitions,
150 Action<string>? beforeInstall)
152 ArgumentException.ThrowIfNullOrWhiteSpace(filePath);
153 ArgumentNullException.ThrowIfNull(definitions);
154 var path = Path.GetFullPath(filePath);
155 var members = definitions.ToList();
157 DefinitionDocumentJson.Check(
159 definition => definition.Key,
160 allowUnnamedSingleton:
true,
161 identityComparer: StringComparer.OrdinalIgnoreCase);
163 var saved = members.Select(definition => PrepareDocumentMember(definition, path)).ToList();
164 var json = ToDocumentJson(
new() { Definitions = saved });
165 DefinitionDocumentPersistence.WriteAtomic(path, json, beforeInstall);
170 var clone = ParseDefinition(ToJson(definition),
null);
171 var origin = definition.Source.GaniFilePath ?? definition.Source.AssetsFilePath;
173 var oldDirectory = definition.LoadStamp?.ReferenceBaseDirectory ??
174 (
string.IsNullOrWhiteSpace(origin) || !Path.IsPathFullyQualified(origin)
176 : Path.GetDirectoryName(origin));
178 var newDirectory = Path.GetDirectoryName(path)!;
180 string? RebasePath(
string? value) =>
181 string.IsNullOrWhiteSpace(value) || (oldDirectory is
null && !Path.IsPathRooted(value))
183 : Path.GetRelativePath(
185 oldDirectory is
null ? Path.GetFullPath(value) : Path.GetFullPath(value, oldDirectory));
187 foreach (var source
in clone.TilesheetSources)
189 source.GtsPath = RebasePath(source.GtsPath);
190 source.AssetsFilePath = RebasePath(source.AssetsFilePath);
193 clone.Source = AnimationDefinitionSource.LooseDefinitionFile(path);
200 public static IReadOnlyList<AnimationDefinition> LoadAll(
string filePath) => LoadDocument(filePath).Definitions;
207 DefinitionDocumentJson.Select(
209 definition => definition.Key,
211 StringComparer.OrdinalIgnoreCase);
216 public static IReadOnlyList<AnimationDefinition> LoadAll(Stream stream) => LoadDocument(stream).Definitions;
223 DefinitionDocumentJson.Select(
225 definition => definition.Key,
227 StringComparer.OrdinalIgnoreCase);
233 public static IReadOnlyList<AnimationDefinition> LoadAll(AssetsFile assetsFile,
string entryName) =>
234 LoadDocument(assetsFile, entryName).Definitions;
241 public static AnimationDefinition Load(AssetsFile assetsFile,
string entryName,
string? key) =>
242 DefinitionDocumentJson.Select(
243 LoadAll(assetsFile, entryName),
244 definition => definition.Key,
246 StringComparer.OrdinalIgnoreCase);
252 public static Cycle LoadCycle(
string filePath,
string? identity) => ToCycle(Load(filePath, identity));
257 public static IReadOnlyList<Cycle> LoadAllCycles(
string filePath) => ToAllCycles(LoadDocument(filePath));
264 public static Cycle LoadCycle(AssetsFile assetsFile,
string entryName,
string? identity) =>
265 ToCycle(Load(assetsFile, entryName, identity));
271 public static IReadOnlyList<Cycle> LoadAllCycles(AssetsFile assetsFile,
string entryName) =>
272 ToAllCycles(LoadDocument(assetsFile, entryName));
277 public static IReadOnlyList<Cycle> ToAllCycles(AnimationDefinitionDocument document)
279 var errors = ValidateDocument(document);
281 if (errors.Count != 0)
282 throw new InvalidDataException(
string.Join(Environment.NewLine, errors));
284 var created =
new List<Cycle>();
285 var previous =
new Dictionary<string, Cycle>(Cycle._cycles, StringComparer.OrdinalIgnoreCase);
289 foreach (var definition
in document.Definitions)
290 created.Add(MaterializeCycle(definition));
292 for (
int i = 0; i < created.Count; i++)
293 ApplyNextCycle(created[i], document.Definitions[i]);
299 foreach (var item
in created)
302 foreach (var pair
in previous)
303 Cycle._cycles[pair.Key] = pair.Value;
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.
@ AnimationDefinition
Represents an animation definition file (.gani) for Gondwana.Drawing.Animation.GANI....