14 private sealed class AssetsFileStateEntry
19 public string FilePath {
get;
set; } =
string.Empty;
24 public string? Password {
get;
set; }
29 public bool UseEncryption {
get;
set; }
32 private abstract class DefinitionStateEntry
35 [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
36 public string? DefinitionIdentity {
get;
set; }
41 [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
42 public string? AssetsFilePath {
get;
set; }
47 [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
48 public string? AssetEntryName {
get;
set; }
50 internal void Validate(
string? loosePath,
object? definition,
object? legacy =
null)
52 bool hasArchive = AssetsFilePath is not
null;
53 bool hasEntry = AssetEntryName is not
null;
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}'.");
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);
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.");
70 private static IEnumerable<DefinitionStateEntry> PackedEntries(EngineStateSnapshot snapshot,
EngineStateParts parts)
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)
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)
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)
88 foreach (var entry
in snapshot.Cycles.Values)
89 if (entry?.AssetsFilePath is not
null)
93 foreach (var entry
in snapshot.Scenes)
94 if (entry?.AssetsFilePath is not
null)
97 if (parts.HasFlag(
EngineStateParts.Audio) && snapshot.Audio?.AssetsFilePath is not
null)
98 yield
return snapshot.Audio;
100 if (parts.HasFlag(
EngineStateParts.Sprites) && snapshot.Sprites?.AssetsFilePath is not
null)
101 yield
return snapshot.Sprites;
104 private static void ValidateSelectedSources(EngineStateSnapshot snapshot,
EngineStateParts parts)
106 if (parts.HasFlag(
EngineStateParts.Audio) && snapshot.AudioGroups is not
null)
108 if (snapshot.Audio is not
null)
109 throw new InvalidDataException(
"Audio and AudioGroups cannot both be present.");
111 foreach (var entry
in snapshot.AudioGroups)
112 (entry ??
throw new InvalidDataException(
"AudioGroups contains null.")).Validate(
117 if (parts.HasFlag(
EngineStateParts.Sprites) && snapshot.SpriteGroups is not
null)
119 if (snapshot.Sprites is not
null)
120 throw new InvalidDataException(
"Sprites and SpriteGroups cannot both be present.");
122 foreach (var entry
in snapshot.SpriteGroups)
123 (entry ??
throw new InvalidDataException(
"SpriteGroups contains null.")).Validate(
126 entry.LegacySprites);
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);
134 foreach (var entry
in snapshot.Cycles.Values)
135 entry?.Validate(entry.GaniPath, entry.Definition);
138 foreach (var entry
in snapshot.Scenes)
139 entry?.Validate(entry.GscnPath, entry.Definition, entry.LegacyScene);
142 audio.Validate(audio.GsndPath, audio.Definition);
144 if (parts.HasFlag(
EngineStateParts.Sprites) && snapshot.Sprites is { } sprites)
145 sprites.Validate(sprites.GsprPath, sprites.Definition, sprites.LegacySprites);
148 private static bool TryPreserve(
149 DefinitionStateEntry entry,
150 DefinitionProvenance? provenance,
153 string? baseDirectory,
154 out
string? loosePath)
159 !provenance.Matches(definition))
162 entry.DefinitionIdentity = provenance.DefinitionIdentity;
164 if (provenance.AssetsFilePath is { } archivePath)
166 entry.AssetsFilePath = MakeRelativePath(archivePath, baseDirectory);
167 entry.AssetEntryName = provenance.AssetEntryName;
171 if (provenance.FilePath is { } filePath)
173 loosePath = MakeRelativePath(filePath, baseDirectory);
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));
186 private static Exception DefinitionError(DefinitionStateEntry entry,
string? baseDirectory, Exception error) =>
187 entry.AssetsFilePath is
null
189 :
new InvalidDataException(
190 $
"Cannot load packed definition '{entry.AssetEntryName}' from GAF '{ResolvePath(entry.AssetsFilePath, baseDirectory)}'.",
193 private static T WithDefinitionContext<T>(DefinitionStateEntry entry,
string? baseDirectory, Func<T> materialize)
197 return materialize();
199 catch (Exception ex) when (entry.AssetsFilePath is not
null && ex is not OutOfMemoryException)
201 throw DefinitionError(entry, baseDirectory, ex);
205 private static T LoadPacked<T>(
206 DefinitionStateEntry entry,
207 string? baseDirectory,
209 Func<AssetsFile, string, T> load)
211 var path = ResolvePath(entry.AssetsFilePath!, baseDirectory);
215 var archive = FindAssetsFile(path);
219 if (!File.Exists(path))
220 throw new FileNotFoundException(
"Archive not found.", path);
222 archive = AssetsFile.LoadOrCreate(path);
227 if (!archive.GetAllEntries().Any(candidate => candidate.AssetType == expectedType &&
230 entry.AssetEntryName,
231 StringComparison.Ordinal)))
232 throw new InvalidDataException($
"Entry does not exist with expected type '{expectedType}'.");
234 return load(archive, entry.AssetEntryName!);
236 catch (Exception ex) when (ex is not OutOfMemoryException)
238 throw new InvalidDataException(
239 $
"Cannot load packed definition '{entry.AssetEntryName}' from GAF '{path}'.",
244 private static List<AudioStateEntry>? CaptureAudioGroups(
string? baseDirectory)
246 var groups = AudioResourceManager.Instance.GetAll().Values.GroupBy(resource => resource.DefinitionProvenance)
249 if (groups.Count <= 1)
252 return groups.Select(group =>
254 var definition = AudioDefinitionSerializer.FromResources(group);
255 var entry =
new AudioStateEntry();
264 entry.GsndPath = path;
266 entry.Definition = definition;
272 private static List<SpriteStateEntry>? CaptureSpriteGroups(
string? baseDirectory)
274 var groups = SpriteManager.Instance.AllSprites.GroupBy(sprite => sprite.DefinitionProvenance).ToList();
276 if (groups.Count <= 1)
279 return groups.Select(group =>
281 var definition = SpriteDefinitionSerializer.FromSprites(group);
282 var entry =
new SpriteStateEntry();
291 entry.GsprPath = path;
293 entry.Definition = definition;
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.
EngineStateParts
Represents different parts of the engine state that can be managed, saved, or loaded independently....
DefinitionPersistence
Controls how EngineState persists a definition.