6public static partial class TilesheetDefinitionSerializer
12 public static TilesheetDefinitionDocument FromDocumentJson(
string json,
string? sourceIdentity =
null) =>
new()
16 json => ParseDefinition(json,
null),
17 definition => definition.Name)
23 public static string ToDocumentJson(TilesheetDefinitionDocument document)
25 ArgumentNullException.ThrowIfNull(document);
26 DefinitionDocumentJson.Check(document.Definitions, definition => definition.Name);
27 return DefinitionDocumentPersistence.Serialize(document, Settings);
33 public static TilesheetDefinitionDocument LoadDocument(
string filePath)
35 ArgumentException.ThrowIfNullOrWhiteSpace(filePath);
36 var path = Path.GetFullPath(filePath);
37 var document = FromDocumentJson(File.ReadAllText(path), path);
39 foreach (var definition
in document.Definitions)
42 definition.Source = TilesheetDefinitionSource.LooseDefinitionFile(path);
44 definition.LoadStamp =
new(definition, filePath: path, definitionIdentity: definition.Name);
53 public static TilesheetDefinitionDocument LoadDocument(Stream stream)
55 ArgumentNullException.ThrowIfNull(stream);
58 throw new ArgumentException(
"Stream must be readable.", nameof(stream));
60 using var reader =
new StreamReader(stream, leaveOpen:
true);
61 return FromDocumentJson(reader.ReadToEnd());
68 public static TilesheetDefinitionDocument LoadDocument(AssetsFile assetsFile,
string entryName)
70 ArgumentNullException.ThrowIfNull(assetsFile);
71 ArgumentException.ThrowIfNullOrWhiteSpace(entryName);
73 using var stream = assetsFile.
Get(
AssetTypes.TilesheetDefinition, entryName)
74 ??
throw new FileNotFoundException($
"GTS asset entry not found: {entryName}", entryName);
76 using var reader =
new StreamReader(stream);
77 var document = FromDocumentJson(reader.ReadToEnd(), entryName);
79 foreach (var definition
in document.Definitions)
81 definition.Source =
string.IsNullOrWhiteSpace(assetsFile.
SourcePath)
82 ? TilesheetDefinitionSource.None()
83 : TilesheetDefinitionSource.PackedDefinitionFile(assetsFile.
SourcePath, entryName);
85 definition.LoadStamp =
new(
89 definitionIdentity: definition.Name);
98 public static IReadOnlyList<string> ValidateDocument(TilesheetDefinitionDocument document)
100 ArgumentNullException.ThrowIfNull(document);
101 var errors =
new List<string>();
105 DefinitionDocumentJson.Check(document.Definitions, definition => definition.Name);
107 catch (InvalidDataException ex)
109 errors.Add(ex.Message);
112 foreach (var definition
in document.Definitions ?? [])
113 if (definition is not
null)
115 TilesheetDefinitionValidator.Validate(definition)
116 .Select(error => $
"Tilesheet '{definition.Name}': {error}"));
124 public static void SaveDocument(
string filePath, TilesheetDefinitionDocument document)
126 ArgumentNullException.ThrowIfNull(document);
127 SaveAll(filePath, document.Definitions);
133 public static void SaveAll(
string filePath, IEnumerable<TilesheetDefinition> definitions) =>
134 SaveAll(filePath, definitions,
null);
140 public static void SaveAll(
142 IEnumerable<TilesheetDefinition> definitions,
143 Action<string>? beforeInstall)
145 ArgumentException.ThrowIfNullOrWhiteSpace(filePath);
146 ArgumentNullException.ThrowIfNull(definitions);
147 var path = Path.GetFullPath(filePath);
148 var members = definitions.ToList();
149 DefinitionDocumentJson.Check(members, definition => definition.Name, allowUnnamedSingleton:
true);
150 var saved = members.Select(definition => PrepareDocumentMember(definition, path)).ToList();
151 var json = ToDocumentJson(
new() { Definitions = saved });
152 DefinitionDocumentPersistence.WriteAtomic(path, json, beforeInstall);
157 var clone = ParseDefinition(ToJson(definition),
null);
158 var origin = definition.Source.GtsFilePath ?? definition.Source.AssetsFilePath;
160 var oldDirectory = definition.LoadStamp?.ReferenceBaseDirectory ??
161 (
string.IsNullOrWhiteSpace(origin) || !Path.IsPathFullyQualified(origin)
163 : Path.GetDirectoryName(origin));
165 var newDirectory = Path.GetDirectoryName(path)!;
167 string? RebasePath(
string? value) =>
168 string.IsNullOrWhiteSpace(value) || (oldDirectory is
null && !Path.IsPathRooted(value))
170 : Path.GetRelativePath(
172 oldDirectory is
null ? Path.GetFullPath(value) : Path.GetFullPath(value, oldDirectory));
174 if (clone.Image is { } image)
176 if (
string.IsNullOrWhiteSpace(image.FilePath) &&
string.IsNullOrWhiteSpace(image.AssetsFilePath) &&
177 !
string.IsNullOrWhiteSpace(image.AssetEntryName))
178 image.AssetsFilePath = definition.LoadStamp?.AssetsFilePath ?? definition.Source.AssetsFilePath;
180 image.FilePath = RebasePath(image.FilePath);
181 image.AssetsFilePath = RebasePath(image.AssetsFilePath);
184 ApplyDefaultSource(clone, TilesheetDefinitionSource.LooseDefinitionFile(path));
191 public static IReadOnlyList<TilesheetDefinition> LoadAll(
string filePath) => LoadDocument(filePath).Definitions;
198 DefinitionDocumentJson.Select(LoadAll(filePath), definition => definition.Name, name);
203 public static IReadOnlyList<TilesheetDefinition> LoadAll(Stream stream) => LoadDocument(stream).Definitions;
210 DefinitionDocumentJson.Select(LoadAll(stream), definition => definition.Name, name);
216 public static IReadOnlyList<TilesheetDefinition> LoadAll(AssetsFile assetsFile,
string entryName) =>
217 LoadDocument(assetsFile, entryName).Definitions;
224 public static TilesheetDefinition Load(AssetsFile assetsFile,
string entryName,
string? name) =>
225 DefinitionDocumentJson.Select(LoadAll(assetsFile, entryName), definition => definition.Name, name);
232 Load(assetsFile, entryName,
null);
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.
@ TilesheetDefinition
Represents a tilesheet definition file (.gts) for Gondwana.Drawing.Tilesheets.GTS....
TilesheetDefinitionSourceKind
Indicates where a tilesheet definition came from.