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
TilesheetDefinitionSerializer.Documents.cs
3
5
6public static partial class TilesheetDefinitionSerializer
7{
12 public static TilesheetDefinitionDocument FromDocumentJson(string json, string? sourceIdentity = null) => new()
13 {
14 Definitions = DefinitionDocumentJson.Read<TilesheetDefinition>(
15 json,
16 json => ParseDefinition(json, null),
17 definition => definition.Name)
18 };
19
23 public static string ToDocumentJson(TilesheetDefinitionDocument document)
24 {
25 ArgumentNullException.ThrowIfNull(document);
26 DefinitionDocumentJson.Check(document.Definitions, definition => definition.Name);
27 return DefinitionDocumentPersistence.Serialize(document, Settings);
28 }
29
33 public static TilesheetDefinitionDocument LoadDocument(string filePath)
34 {
35 ArgumentException.ThrowIfNullOrWhiteSpace(filePath);
36 var path = Path.GetFullPath(filePath);
37 var document = FromDocumentJson(File.ReadAllText(path), path);
38
39 foreach (var definition in document.Definitions)
40 {
41 if (definition.Source.Kind != TilesheetDefinitionSourceKind.Generated)
42 definition.Source = TilesheetDefinitionSource.LooseDefinitionFile(path);
43
44 definition.LoadStamp = new(definition, filePath: path, definitionIdentity: definition.Name);
45 }
46
47 return document;
48 }
49
53 public static TilesheetDefinitionDocument LoadDocument(Stream stream)
54 {
55 ArgumentNullException.ThrowIfNull(stream);
56
57 if (!stream.CanRead)
58 throw new ArgumentException("Stream must be readable.", nameof(stream));
59
60 using var reader = new StreamReader(stream, leaveOpen: true);
61 return FromDocumentJson(reader.ReadToEnd());
62 }
63
68 public static TilesheetDefinitionDocument LoadDocument(AssetsFile assetsFile, string entryName)
69 {
70 ArgumentNullException.ThrowIfNull(assetsFile);
71 ArgumentException.ThrowIfNullOrWhiteSpace(entryName);
72
73 using var stream = assetsFile.Get(AssetTypes.TilesheetDefinition, entryName)
74 ?? throw new FileNotFoundException($"GTS asset entry not found: {entryName}", entryName);
75
76 using var reader = new StreamReader(stream);
77 var document = FromDocumentJson(reader.ReadToEnd(), entryName);
78
79 foreach (var definition in document.Definitions)
80 {
81 definition.Source = string.IsNullOrWhiteSpace(assetsFile.SourcePath)
82 ? TilesheetDefinitionSource.None()
83 : TilesheetDefinitionSource.PackedDefinitionFile(assetsFile.SourcePath, entryName);
84
85 definition.LoadStamp = new(
86 definition,
87 assetsFilePath: assetsFile.SourcePath,
88 entryName: entryName,
89 definitionIdentity: definition.Name);
90 }
91
92 return document;
93 }
94
98 public static IReadOnlyList<string> ValidateDocument(TilesheetDefinitionDocument document)
99 {
100 ArgumentNullException.ThrowIfNull(document);
101 var errors = new List<string>();
102
103 try
104 {
105 DefinitionDocumentJson.Check(document.Definitions, definition => definition.Name);
106 }
107 catch (InvalidDataException ex)
108 {
109 errors.Add(ex.Message);
110 }
111
112 foreach (var definition in document.Definitions ?? [])
113 if (definition is not null)
114 errors.AddRange(
115 TilesheetDefinitionValidator.Validate(definition)
116 .Select(error => $"Tilesheet '{definition.Name}': {error}"));
117
118 return errors;
119 }
120
124 public static void SaveDocument(string filePath, TilesheetDefinitionDocument document)
125 {
126 ArgumentNullException.ThrowIfNull(document);
127 SaveAll(filePath, document.Definitions);
128 }
129
133 public static void SaveAll(string filePath, IEnumerable<TilesheetDefinition> definitions) =>
134 SaveAll(filePath, definitions, null);
135
140 public static void SaveAll(
141 string filePath,
142 IEnumerable<TilesheetDefinition> definitions,
143 Action<string>? beforeInstall)
144 {
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);
153 }
154
155 private static TilesheetDefinition PrepareDocumentMember(TilesheetDefinition definition, string path)
156 {
157 var clone = ParseDefinition(ToJson(definition), null);
158 var origin = definition.Source.GtsFilePath ?? definition.Source.AssetsFilePath;
159
160 var oldDirectory = definition.LoadStamp?.ReferenceBaseDirectory ??
161 (string.IsNullOrWhiteSpace(origin) || !Path.IsPathFullyQualified(origin)
162 ? null
163 : Path.GetDirectoryName(origin));
164
165 var newDirectory = Path.GetDirectoryName(path)!;
166
167 string? RebasePath(string? value) =>
168 string.IsNullOrWhiteSpace(value) || (oldDirectory is null && !Path.IsPathRooted(value))
169 ? value
170 : Path.GetRelativePath(
171 newDirectory,
172 oldDirectory is null ? Path.GetFullPath(value) : Path.GetFullPath(value, oldDirectory));
173
174 if (clone.Image is { } image)
175 {
176 if (string.IsNullOrWhiteSpace(image.FilePath) && string.IsNullOrWhiteSpace(image.AssetsFilePath) &&
177 !string.IsNullOrWhiteSpace(image.AssetEntryName))
178 image.AssetsFilePath = definition.LoadStamp?.AssetsFilePath ?? definition.Source.AssetsFilePath;
179
180 image.FilePath = RebasePath(image.FilePath);
181 image.AssetsFilePath = RebasePath(image.AssetsFilePath);
182 }
183
184 ApplyDefaultSource(clone, TilesheetDefinitionSource.LooseDefinitionFile(path));
185 return clone;
186 }
187
191 public static IReadOnlyList<TilesheetDefinition> LoadAll(string filePath) => LoadDocument(filePath).Definitions;
192
197 public static TilesheetDefinition Load(string filePath, string? name) =>
198 DefinitionDocumentJson.Select(LoadAll(filePath), definition => definition.Name, name);
199
203 public static IReadOnlyList<TilesheetDefinition> LoadAll(Stream stream) => LoadDocument(stream).Definitions;
204
209 public static TilesheetDefinition Load(Stream stream, string? name) =>
210 DefinitionDocumentJson.Select(LoadAll(stream), definition => definition.Name, name);
211
216 public static IReadOnlyList<TilesheetDefinition> LoadAll(AssetsFile assetsFile, string entryName) =>
217 LoadDocument(assetsFile, entryName).Definitions;
218
224 public static TilesheetDefinition Load(AssetsFile assetsFile, string entryName, string? name) =>
225 DefinitionDocumentJson.Select(LoadAll(assetsFile, entryName), definition => definition.Name, name);
226
231 public static TilesheetDefinition Load(AssetsFile assetsFile, string entryName) =>
232 Load(assetsFile, entryName, null);
233}
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
@ TilesheetDefinition
Represents a tilesheet definition file (.gts) for Gondwana.Drawing.Tilesheets.GTS....
Definition AssetTypes.cs:47
TilesheetDefinitionSourceKind
Indicates where a tilesheet definition came from.