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
EngineState.Definitions.cs
7using Newtonsoft.Json;
8
9namespace Gondwana;
10
11public sealed partial class EngineState
12{
13 // Keep the historic archive property names, but never deserialize a live archive.
14 private sealed class AssetsFileStateEntry
15 {
19 public string FilePath { get; set; } = string.Empty;
20
24 public string? Password { get; set; }
25
29 public bool UseEncryption { get; set; }
30 }
31
32 private abstract class DefinitionStateEntry
33 {
35 [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
36 public string? DefinitionIdentity { get; set; }
37
41 [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
42 public string? AssetsFilePath { get; set; }
43
47 [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
48 public string? AssetEntryName { get; set; }
49
50 internal void Validate(string? loosePath, object? definition, object? legacy = null)
51 {
52 bool hasArchive = AssetsFilePath is not null;
53 bool hasEntry = AssetEntryName is not null;
54
55 if (hasArchive != hasEntry ||
56 (hasArchive && (string.IsNullOrWhiteSpace(AssetsFilePath) ||
57 string.IsNullOrWhiteSpace(AssetEntryName))))
58 throw new InvalidDataException(
59 $"Packed definition requires both AssetsFilePath and AssetEntryName: '{AssetsFilePath}' / '{AssetEntryName}'.");
60
61 int sources = (loosePath is not null ? 1 : 0) + (definition is not null ? 1 : 0) +
62 (hasArchive ? 1 : 0) + (legacy is not null ? 1 : 0);
63
64 if (sources != 1 || (loosePath is not null && string.IsNullOrWhiteSpace(loosePath)))
65 throw new InvalidDataException(
66 "Definition state entry requires exactly one source: inline Definition, loose path, or AssetsFilePath + AssetEntryName.");
67 }
68 }
69
70 private static IEnumerable<DefinitionStateEntry> PackedEntries(EngineStateSnapshot snapshot, EngineStateParts parts)
71 {
72 if (parts.HasFlag(EngineStateParts.Audio) && snapshot.AudioGroups is not null)
73 foreach (var entry in snapshot.AudioGroups)
74 if (entry.AssetsFilePath is not null)
75 yield return entry;
76
77 if (parts.HasFlag(EngineStateParts.Sprites) && snapshot.SpriteGroups is not null)
78 foreach (var entry in snapshot.SpriteGroups)
79 if (entry.AssetsFilePath is not null)
80 yield return entry;
81
82 if (parts.HasFlag(EngineStateParts.Tilesheets) && snapshot.Tilesheets is not null)
83 foreach (var entry in snapshot.Tilesheets.Values)
84 if (entry?.AssetsFilePath is not null)
85 yield return entry;
86
87 if (parts.HasFlag(EngineStateParts.Cycles) && snapshot.Cycles is not null)
88 foreach (var entry in snapshot.Cycles.Values)
89 if (entry?.AssetsFilePath is not null)
90 yield return entry;
91
92 if (parts.HasFlag(EngineStateParts.Scenes) && snapshot.Scenes is not null)
93 foreach (var entry in snapshot.Scenes)
94 if (entry?.AssetsFilePath is not null)
95 yield return entry;
96
97 if (parts.HasFlag(EngineStateParts.Audio) && snapshot.Audio?.AssetsFilePath is not null)
98 yield return snapshot.Audio;
99
100 if (parts.HasFlag(EngineStateParts.Sprites) && snapshot.Sprites?.AssetsFilePath is not null)
101 yield return snapshot.Sprites;
102 }
103
104 private static void ValidateSelectedSources(EngineStateSnapshot snapshot, EngineStateParts parts)
105 {
106 if (parts.HasFlag(EngineStateParts.Audio) && snapshot.AudioGroups is not null)
107 {
108 if (snapshot.Audio is not null)
109 throw new InvalidDataException("Audio and AudioGroups cannot both be present.");
110
111 foreach (var entry in snapshot.AudioGroups)
112 (entry ?? throw new InvalidDataException("AudioGroups contains null.")).Validate(
113 entry.GsndPath,
114 entry.Definition);
115 }
116
117 if (parts.HasFlag(EngineStateParts.Sprites) && snapshot.SpriteGroups is not null)
118 {
119 if (snapshot.Sprites is not null)
120 throw new InvalidDataException("Sprites and SpriteGroups cannot both be present.");
121
122 foreach (var entry in snapshot.SpriteGroups)
123 (entry ?? throw new InvalidDataException("SpriteGroups contains null.")).Validate(
124 entry.GsprPath,
125 entry.Definition,
126 entry.LegacySprites);
127 }
128
129 if (parts.HasFlag(EngineStateParts.Tilesheets) && snapshot.Tilesheets is not null)
130 foreach (var entry in snapshot.Tilesheets.Values)
131 entry?.Validate(entry.GtsPath, entry.Definition);
132
133 if (parts.HasFlag(EngineStateParts.Cycles) && snapshot.Cycles is not null)
134 foreach (var entry in snapshot.Cycles.Values)
135 entry?.Validate(entry.GaniPath, entry.Definition);
136
137 if (parts.HasFlag(EngineStateParts.Scenes) && snapshot.Scenes is not null)
138 foreach (var entry in snapshot.Scenes)
139 entry?.Validate(entry.GscnPath, entry.Definition, entry.LegacyScene);
140
141 if (parts.HasFlag(EngineStateParts.Audio) && snapshot.Audio is { } audio)
142 audio.Validate(audio.GsndPath, audio.Definition);
143
144 if (parts.HasFlag(EngineStateParts.Sprites) && snapshot.Sprites is { } sprites)
145 sprites.Validate(sprites.GsprPath, sprites.Definition, sprites.LegacySprites);
146 }
147
148 private static bool TryPreserve(
149 DefinitionStateEntry entry,
150 DefinitionProvenance? provenance,
151 object definition,
152 DefinitionPersistence persistence,
153 string? baseDirectory,
154 out string? loosePath)
155 {
156 loosePath = null;
157
158 if (persistence != DefinitionPersistence.PreserveSource || provenance is null ||
159 !provenance.Matches(definition))
160 return false;
161
162 entry.DefinitionIdentity = provenance.DefinitionIdentity;
163
164 if (provenance.AssetsFilePath is { } archivePath)
165 {
166 entry.AssetsFilePath = MakeRelativePath(archivePath, baseDirectory);
167 entry.AssetEntryName = provenance.AssetEntryName;
168 return true;
169 }
170
171 if (provenance.FilePath is { } filePath)
172 {
173 loosePath = MakeRelativePath(filePath, baseDirectory);
174 return true;
175 }
176
177 return false;
178 }
179
180 private static AssetsFile? FindAssetsFile(string path) => AssetsFile.AllAssetsFiles.FirstOrDefault(file =>
181 !string.IsNullOrWhiteSpace(file.SourcePath) && string.Equals(
182 Path.GetFullPath(file.SourcePath),
183 Path.GetFullPath(path),
184 OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal));
185
186 private static Exception DefinitionError(DefinitionStateEntry entry, string? baseDirectory, Exception error) =>
187 entry.AssetsFilePath is null
188 ? error
189 : new InvalidDataException(
190 $"Cannot load packed definition '{entry.AssetEntryName}' from GAF '{ResolvePath(entry.AssetsFilePath, baseDirectory)}'.",
191 error);
192
193 private static T WithDefinitionContext<T>(DefinitionStateEntry entry, string? baseDirectory, Func<T> materialize)
194 {
195 try
196 {
197 return materialize();
198 }
199 catch (Exception ex) when (entry.AssetsFilePath is not null && ex is not OutOfMemoryException)
200 {
201 throw DefinitionError(entry, baseDirectory, ex);
202 }
203 }
204
205 private static T LoadPacked<T>(
206 DefinitionStateEntry entry,
207 string? baseDirectory,
208 AssetTypes expectedType,
209 Func<AssetsFile, string, T> load)
210 {
211 var path = ResolvePath(entry.AssetsFilePath!, baseDirectory);
212
213 try
214 {
215 var archive = FindAssetsFile(path);
216
217 if (archive is null)
218 {
219 if (!File.Exists(path))
220 throw new FileNotFoundException("Archive not found.", path);
221
222 archive = AssetsFile.LoadOrCreate(path);
223 }
224
225 // EngineState references are exact identities, not the convenience basename
226 // aliases accepted by AssetsFile.Get for interactive runtime lookup.
227 if (!archive.GetAllEntries().Any(candidate => candidate.AssetType == expectedType &&
228 string.Equals(
229 candidate.AssetName,
230 entry.AssetEntryName,
231 StringComparison.Ordinal)))
232 throw new InvalidDataException($"Entry does not exist with expected type '{expectedType}'.");
233
234 return load(archive, entry.AssetEntryName!);
235 }
236 catch (Exception ex) when (ex is not OutOfMemoryException)
237 {
238 throw new InvalidDataException(
239 $"Cannot load packed definition '{entry.AssetEntryName}' from GAF '{path}'.",
240 ex);
241 }
242 }
243
244 private static List<AudioStateEntry>? CaptureAudioGroups(string? baseDirectory)
245 {
246 var groups = AudioResourceManager.Instance.GetAll().Values.GroupBy(resource => resource.DefinitionProvenance)
247 .ToList();
248
249 if (groups.Count <= 1)
250 return null;
251
252 return groups.Select(group =>
253 {
254 var definition = AudioDefinitionSerializer.FromResources(group);
255 var entry = new AudioStateEntry();
256
257 if (TryPreserve(
258 entry,
259 group.Key,
260 definition,
261 DefinitionPersistence.PreserveSource,
262 baseDirectory,
263 out var path))
264 entry.GsndPath = path;
265 else
266 entry.Definition = definition;
267
268 return entry;
269 }).ToList();
270 }
271
272 private static List<SpriteStateEntry>? CaptureSpriteGroups(string? baseDirectory)
273 {
274 var groups = SpriteManager.Instance.AllSprites.GroupBy(sprite => sprite.DefinitionProvenance).ToList();
275
276 if (groups.Count <= 1)
277 return null;
278
279 return groups.Select(group =>
280 {
281 var definition = SpriteDefinitionSerializer.FromSprites(group);
282 var entry = new SpriteStateEntry();
283
284 if (TryPreserve(
285 entry,
286 group.Key,
287 definition,
288 DefinitionPersistence.PreserveSource,
289 baseDirectory,
290 out var path))
291 entry.GsprPath = path;
292 else
293 entry.Definition = definition;
294
295 return entry;
296 }).ToList();
297 }
298}
Represents the complete serializable state of the game engine, including assets, scenes,...
AssetTypes
Defines the types of assets that can be stored and managed by the engine.
Definition AssetTypes.cs:8
EngineStateParts
Represents different parts of the engine state that can be managed, saved, or loaded independently....
DefinitionPersistence
Controls how EngineState persists a definition.