15public static class DefinitionReferenceRelocator
24 public static TilesheetDefinitionDocument Relocate(
25 TilesheetDefinitionDocument document,
26 AssetLocation sourceDocument,
27 AssetLocation destinationDocument,
28 IEnumerable<ReferenceRelocation> relocations,
29 bool requireAllMappings =
true)
31 ArgumentNullException.ThrowIfNull(document);
33 var context =
new RelocationContext(
40 TilesheetDefinitionSerializer.FromDocumentJson(TilesheetDefinitionSerializer.ToDocumentJson(document));
42 foreach (var member
in result.Definitions)
44 var image = member.Image ??
45 throw new InvalidDataException($
"Tilesheet '{member.Name}': Image must not be null.");
53 $
"Tilesheet '{member.Name}' :: Image",
58 image.FilePath = file;
59 image.AssetsFilePath = archive;
60 image.AssetEntryName = entry;
63 member.Source = destinationDocument.AssetEntryName is
null
64 ? TilesheetDefinitionSource.LooseDefinitionFile(destinationDocument.
Path)
65 : TilesheetDefinitionSource.PackedDefinitionFile(
66 destinationDocument.
Path,
70 context.Complete(requireAllMappings);
81 public static AnimationDefinitionDocument Relocate(
82 AnimationDefinitionDocument document,
83 AssetLocation sourceDocument,
84 AssetLocation destinationDocument,
85 IEnumerable<ReferenceRelocation> relocations,
86 bool requireAllMappings =
true)
88 ArgumentNullException.ThrowIfNull(document);
90 var context =
new RelocationContext(
97 AnimationDefinitionSerializer.FromDocumentJson(AnimationDefinitionSerializer.ToDocumentJson(document));
99 foreach (var member
in result.Definitions)
101 for (
int i = 0; i < member.TilesheetSources.Count; i++)
103 var source = member.TilesheetSources[i] ??
104 throw new InvalidDataException($
"Member contains null TilesheetSources[{i}].");
109 source.AssetsFilePath,
110 source.AssetEntryName,
112 $
"Animation '{member.Key}' :: TilesheetSources[{i}]",
114 file, archive, entry,
117 source.GtsPath = file;
118 source.AssetsFilePath = archive;
119 source.AssetEntryName = entry;
122 ? AnimationTilesheetSourceKind.PackedDefinitionFile
123 : AnimationTilesheetSourceKind.LooseDefinitionFile;
127 member.Source = destinationDocument.AssetEntryName is
null
128 ? AnimationDefinitionSource.LooseDefinitionFile(destinationDocument.
Path)
129 : AnimationDefinitionSource.PackedDefinitionFile(
130 destinationDocument.
Path,
134 context.Complete(requireAllMappings);
145 public static SceneDefinitionDocument Relocate(
146 SceneDefinitionDocument document,
147 AssetLocation sourceDocument,
148 AssetLocation destinationDocument,
149 IEnumerable<ReferenceRelocation> relocations,
150 bool requireAllMappings =
true)
152 ArgumentNullException.ThrowIfNull(document);
154 var context =
new RelocationContext(
160 var result = SceneDefinitionSerializer.FromDocumentJson(SceneDefinitionSerializer.ToDocumentJson(document));
162 foreach (var member
in result.Definitions)
164 for (
int i = 0; i < member.TilesheetSources.Count; i++)
166 var source = member.TilesheetSources[i] ??
167 throw new InvalidDataException($
"Member contains null TilesheetSources[{i}].");
172 source.AssetsFilePath,
173 source.AssetEntryName,
175 $
"Scene '{member.ID}' :: TilesheetSources[{i}]",
177 file, archive, entry,
180 source.GtsPath = file;
181 source.AssetsFilePath = archive;
182 source.AssetEntryName = entry;
185 ? SceneTilesheetSourceKind.PackedDefinitionFile
186 : SceneTilesheetSourceKind.LooseDefinitionFile;
190 for (
int i = 0; i < member.AnimationSources.Count; i++)
192 var source = member.AnimationSources[i] ??
193 throw new InvalidDataException($
"Member contains null AnimationSources[{i}].");
198 source.AssetsFilePath,
199 source.AssetEntryName,
201 $
"Scene '{member.ID}' :: AnimationSources[{i}]",
203 file, archive, entry,
206 source.GaniPath = file;
207 source.AssetsFilePath = archive;
208 source.AssetEntryName = entry;
211 ? SceneAnimationSourceKind.PackedDefinitionFile
212 : SceneAnimationSourceKind.LooseDefinitionFile;
216 member.Source = destinationDocument.AssetEntryName is
null
217 ? SceneDefinitionSource.LooseDefinitionFile(destinationDocument.
Path)
218 : SceneDefinitionSource.PackedDefinitionFile(
219 destinationDocument.
Path,
223 context.Complete(requireAllMappings);
234 public static SpriteDefinitionDocument Relocate(
235 SpriteDefinitionDocument document,
236 AssetLocation sourceDocument,
237 AssetLocation destinationDocument,
238 IEnumerable<ReferenceRelocation> relocations,
239 bool requireAllMappings =
true)
241 ArgumentNullException.ThrowIfNull(document);
243 var context =
new RelocationContext(
249 var result = SpriteDefinitionSerializer.FromDocumentJson(SpriteDefinitionSerializer.ToDocumentJson(document));
251 foreach (var member
in result.Definitions)
253 for (
int i = 0; i < member.TilesheetSources.Count; i++)
255 var source = member.TilesheetSources[i] ??
256 throw new InvalidDataException($
"Member contains null TilesheetSources[{i}].");
261 source.AssetsFilePath,
262 source.AssetEntryName,
264 $
"Sprite '{member.Key}' :: TilesheetSources[{i}]",
266 file, archive, entry,
269 source.GtsPath = file;
270 source.AssetsFilePath = archive;
271 source.AssetEntryName = entry;
274 ? SpriteTilesheetSourceKind.PackedDefinitionFile
275 : SpriteTilesheetSourceKind.LooseDefinitionFile;
279 for (
int i = 0; i < member.SceneSources.Count; i++)
281 var source = member.SceneSources[i] ??
282 throw new InvalidDataException($
"Member contains null SceneSources[{i}].");
287 source.AssetsFilePath,
288 source.AssetEntryName,
290 $
"Sprite '{member.Key}' :: SceneSources[{i}]",
292 file, archive, entry,
295 source.GscnPath = file;
296 source.AssetsFilePath = archive;
297 source.AssetEntryName = entry;
300 ? SpriteSceneSourceKind.PackedDefinitionFile
301 : SpriteSceneSourceKind.LooseDefinitionFile;
305 for (
int i = 0; i < member.AnimationSources.Count; i++)
307 var source = member.AnimationSources[i] ??
308 throw new InvalidDataException($
"Member contains null AnimationSources[{i}].");
313 source.AssetsFilePath,
314 source.AssetEntryName,
316 $
"Sprite '{member.Key}' :: AnimationSources[{i}]",
318 file, archive, entry,
321 source.GaniPath = file;
322 source.AssetsFilePath = archive;
323 source.AssetEntryName = entry;
326 ? SpriteAnimationSourceKind.PackedDefinitionFile
327 : SpriteAnimationSourceKind.LooseDefinitionFile;
331 member.Source = destinationDocument.AssetEntryName is
null
332 ? SpriteDefinitionSource.LooseDefinitionFile(destinationDocument.
Path)
333 : SpriteDefinitionSource.PackedDefinitionFile(
334 destinationDocument.
Path,
338 context.Complete(requireAllMappings);
349 public static AudioDefinitionDocument Relocate(
350 AudioDefinitionDocument document,
351 AssetLocation sourceDocument,
352 AssetLocation destinationDocument,
353 IEnumerable<ReferenceRelocation> relocations,
354 bool requireAllMappings =
true)
356 ArgumentNullException.ThrowIfNull(document);
358 var context =
new RelocationContext(
364 var result = AudioDefinitionSerializer.FromDocumentJson(AudioDefinitionSerializer.ToDocumentJson(document));
366 foreach (var member
in result.Definitions)
368 foreach (var resource
in member.Resources)
370 if (resource is
null)
371 throw new InvalidDataException($
"Audio '{member.Key}': null resource.");
379 resource.AssetsFilePath,
380 resource.AssetEntryName,
381 (
int)resource.SourceKind,
382 $
"Audio '{member.Key}' :: Resource '{resource.Key}'",
384 file, archive, entry,
387 if (packed && string.IsNullOrWhiteSpace(resource.SourceExtension))
389 resource.SourceExtension = Path.GetExtension(
390 string.IsNullOrWhiteSpace(resource.FilePath)
391 ? resource.AssetEntryName
392 : resource.FilePath);
394 if (string.IsNullOrWhiteSpace(resource.SourceExtension) &&
395 string.IsNullOrWhiteSpace(Path.GetExtension(entry)))
396 throw new InvalidDataException(
397 $
"Audio '{member.Key}' :: Resource '{resource.Key}': cannot infer packed audio format; specify SourceExtension.");
400 resource.FilePath = file;
401 resource.AssetsFilePath = archive;
402 resource.AssetEntryName = entry;
404 resource.SourceKind =
409 member.Source = destinationDocument.AssetEntryName is
null
410 ? AudioDefinitionSource.LooseDefinitionFile(destinationDocument.
Path)
411 : AudioDefinitionSource.PackedDefinitionFile(
412 destinationDocument.
Path,
416 context.Complete(requireAllMappings);
420 private sealed class RelocationContext
422 private readonly AssetLocation _source;
423 private readonly AssetLocation _destination;
424 private readonly ReferenceRelocation[] _mappings;
425 private readonly
bool[] _used;
432 internal RelocationContext(
434 AssetLocation source,
435 AssetLocation destination,
436 IEnumerable<ReferenceRelocation> mappings)
438 ArgumentNullException.ThrowIfNull(source);
439 ArgumentNullException.ThrowIfNull(destination);
440 ArgumentNullException.ThrowIfNull(mappings);
443 throw new ArgumentException($
"Containing locations must both have type {type}.");
446 _destination = destination;
447 _mappings = mappings.ToArray();
448 _used =
new bool[_mappings.Length];
450 if (_mappings.Any(mapping => mapping is
null))
451 throw new ArgumentException(
"Relocations must not contain null.", nameof(mappings));
453 for (
int i = 0; i < _mappings.Length; i++)
454 for (
int j = 0; j < _mappings.Length; j++)
459 if (_mappings[i].Source.Matches(_mappings[j].Source))
460 throw new InvalidDataException($
"Ambiguous duplicate relocation source: {_mappings[i].Source}.");
462 if (_mappings[i].Destination.Matches(_mappings[j].Source) &&
463 !_mappings[j].Source.Matches(_mappings[j].Destination))
464 throw new InvalidDataException(
465 $
"Relocation chains/cycles are ambiguous on repeated application: {_mappings[j].Source}.");
477 internal void Rewrite(
484 Action<string?, string?, string?, bool> apply)
487 if (kind is
null &&
string.IsNullOrWhiteSpace(file) &&
string.IsNullOrWhiteSpace(archive) &&
488 !
string.IsNullOrWhiteSpace(entry) && _source.AssetEntryName is not
null)
489 archive = _source.Path;
491 bool loose = !
string.IsNullOrWhiteSpace(file);
492 bool packed = !
string.IsNullOrWhiteSpace(archive) && !
string.IsNullOrWhiteSpace(entry);
494 if (loose == packed ||
495 (loose && (!
string.IsNullOrWhiteSpace(archive) || !
string.IsNullOrWhiteSpace(entry))) ||
496 (kind is not
null && kind != (packed ? 1 : 0)))
497 throw new InvalidDataException(
498 $
"{label}: reference must identify exactly one loose file or typed GAF entry consistent with its source kind.");
500 var directory = Path.GetDirectoryName(_source.Path)!;
501 AssetLocation location;
505 location =
new AssetLocation(
507 Path.GetFullPath(loose ? file! : archive!, directory),
508 packed ? entry :
null);
510 catch (Exception ex) when (ex is ArgumentException or NotSupportedException or PathTooLongException)
512 throw new InvalidDataException($
"{label}: invalid physical reference path.", ex);
515 var target = location;
517 for (
int i = 0; i < _mappings.Length; i++)
519 if (location.Matches(_mappings[i].Destination))
522 if (!location.Matches(_mappings[i].Source))
526 target = _mappings[i].Destination;
529 if (target.Matches(location) && _source.Matches(_destination))
532 var relative = Path.GetRelativePath(Path.GetDirectoryName(_destination.Path)!, target.Path);
535 target.AssetEntryName is
null ? relative :
null,
536 target.AssetEntryName is
null ?
null : relative,
537 target.AssetEntryName,
538 target.AssetEntryName is not
null);
543 internal void Complete(
bool requireAll)
548 var missing = _mappings.Where((mapping, index) => !_used[index])
549 .Select(mapping => mapping.Source.ToString()).ToArray();
551 if (missing.Length != 0)
552 throw new InvalidDataException(
553 "Relocation sources not found in document references: " +
string.Join(
"; ", missing));
AssetTypes AssetType
Gets the resource or definition type, including for loose files.
string Path
Gets the absolute loose file path or containing GAF path.
string? AssetEntryName
Gets the typed GAF entry name, or null for a loose file.
AssetTypes
Defines the types of assets that can be stored and managed by the engine.
AudioResourceSourceKind
Identifies how a GSND audio resource locates its underlying media.