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
TilesheetRegistry.cs
1using System.Collections.Immutable;
4using Microsoft.Extensions.Logging;
5using SkiaSharp;
6
8
12public sealed class TilesheetRegistry : IDisposable
13{
14 private readonly object _gate = new();
15 private readonly Dictionary<string, Tilesheet> _sheets = new(StringComparer.Ordinal);
16
17 // --- Singleton setup ---
18 private static readonly Lazy<TilesheetRegistry> _instance =
19 new(() => new TilesheetRegistry());
20
25 public static TilesheetRegistry Instance => _instance.Value;
26
27 // Private ctor ensures no one else can new this up
28 private TilesheetRegistry()
29 {
30 }
31
41 internal void Register(Tilesheet sheet, bool disposeReplaced = true)
42 {
43 if (sheet is null)
44 {
45 Engine.Logger.LogError("TilesheetRegistry: Attempt to register a null Tilesheet.");
46 return;
47 }
48
49 Tilesheet? replaced = null;
50
51 lock (_gate)
52 {
53 if (_sheets.TryGetValue(sheet.Name, out var existing) && !ReferenceEquals(existing, sheet))
54 {
55 _sheets[sheet.Name] = sheet;
56 replaced = existing;
57 }
58 else
59 {
60 _sheets[sheet.Name] = sheet;
61 }
62
63 // Avoid duplicate subscriptions if the same instance is registered again.
64 sheet.Disposed -= OnTilesheetDisposed;
65 sheet.Disposed += OnTilesheetDisposed;
66 }
67
68 if (replaced is not null)
69 {
70 replaced.Disposed -= OnTilesheetDisposed;
71
72 if (disposeReplaced)
73 replaced.Dispose();
74 }
75 }
76
88 public bool TryGet(string name, out Tilesheet? sheet)
89 {
90 if (name is null)
91 {
92 Engine.Logger.LogWarning("TilesheetRegistry: Attempt to look up a null name.");
93 sheet = null;
94 return false;
95 }
96
97 lock (_gate)
98 return _sheets.TryGetValue(name, out sheet!);
99 }
100
110 public Tilesheet? GetOrNull(string name)
111 {
112 return TryGet(name, out var s) ? s : null;
113 }
114
127 public Tilesheet this[string name]
128 {
129 get
130 {
131 if (name is null)
132 throw new ArgumentNullException(nameof(name));
133
134 lock (_gate)
135 {
136 if (_sheets.TryGetValue(name, out var sheet))
137 return sheet;
138
139 throw new KeyNotFoundException($"Tilesheet with name '{name}' was not found in the registry.");
140 }
141 }
142 }
143
153 public bool Remove(string name, bool dispose = false)
154 {
155 if (name is null)
156 {
157 Engine.Logger.LogWarning("TilesheetRegistry: Attempt to remove a null name.");
158 return false;
159 }
160
161 Tilesheet? removed = null;
162
163 lock (_gate)
164 {
165 if (!_sheets.TryGetValue(name, out removed))
166 return false;
167
168 _sheets.Remove(name);
169 }
170
171 removed.Disposed -= OnTilesheetDisposed;
172
173 if (dispose)
174 removed.Dispose();
175
176 return true;
177 }
178
190 internal bool Remove(string name, Tilesheet expected, bool dispose = false)
191 {
192 if (name is null)
193 {
194 Engine.Logger.LogWarning("TilesheetRegistry: Attempt to remove a null name.");
195 return false;
196 }
197
198 if (expected is null)
199 {
200 Engine.Logger.LogWarning("TilesheetRegistry: Attempt to remove a null Tilesheet.");
201 return false;
202 }
203
204 Tilesheet? removed = null;
205
206 lock (_gate)
207 {
208 if (_sheets.TryGetValue(name, out var current) && ReferenceEquals(current, expected))
209 {
210 _sheets.Remove(name);
211 removed = current;
212 }
213 else
214 {
215 return false;
216 }
217 }
218
219 removed.Disposed -= OnTilesheetDisposed;
220
221 if (dispose)
222 removed.Dispose();
223
224 return true;
225 }
226
234 public void Clear()
235 {
236 List<Tilesheet> copy;
237
238 lock (_gate)
239 {
240 copy = _sheets.Values.ToList();
241 _sheets.Clear();
242 }
243
244 foreach (var ts in copy)
245 {
246 ts.Disposed -= OnTilesheetDisposed;
247 ts.Dispose();
248 }
249 }
250
258 public IReadOnlyList<string> Names
259 {
260 get
261 {
262 lock (_gate)
263 return _sheets.Keys.ToList();
264 }
265 }
266
275 public IImmutableDictionary<string, Tilesheet> GetAll()
276 {
277 lock (_gate)
278 {
279 return _sheets.ToImmutableDictionary(kv => kv.Key, kv => kv.Value, _sheets.Comparer);
280 }
281 }
282
290 public int Count
291 {
292 get
293 {
294 lock (_gate)
295 return _sheets.Count;
296 }
297 }
298
310 internal void OnTilesheetRenamed(
311 string oldName,
312 string newName,
313 Tilesheet sheet,
314 bool disposeReplaced = true)
315 {
316 if (oldName is null)
317 throw new ArgumentNullException(nameof(oldName));
318
319 if (newName is null)
320 throw new ArgumentNullException(nameof(newName));
321
322 if (sheet is null)
323 throw new ArgumentNullException(nameof(sheet));
324
325 Tilesheet? replaced = null;
326
327 lock (_gate)
328 {
329 // If old mapping points to this sheet, remove it.
330 if (_sheets.TryGetValue(oldName, out var existingAtOld) && ReferenceEquals(existingAtOld, sheet))
331 _sheets.Remove(oldName);
332
333 // If something else is already at newName, warn and replace.
334 if (_sheets.TryGetValue(newName, out var existingAtNew) && !ReferenceEquals(existingAtNew, sheet))
335 {
336 Engine.Logger.LogWarning(
337 "TilesheetRegistry: Renaming '{Old}'→'{New}' replaced a different registered Tilesheet instance.",
338 oldName,
339 newName);
340
341 replaced = existingAtNew;
342 }
343
344 _sheets[newName] = sheet;
345 }
346
347 if (replaced is not null)
348 {
349 replaced.Disposed -= OnTilesheetDisposed;
350
351 if (disposeReplaced)
352 replaced.Dispose();
353 }
354 }
355
356 private void OnTilesheetDisposed(Tilesheet sheet)
357 {
358 if (sheet is null)
359 return;
360
361 Remove(sheet.Name, sheet, dispose: false);
362 }
363
367 public void Dispose()
368 {
369 Clear();
370 }
371
372 #region public shims to TilesheetFactory
373
380 public Tilesheet LoadFromBitmap(string name, SKBitmap bitmap)
381 {
382 var tilesheet = TilesheetFactory.FromBitmap(name, bitmap);
383 Register(tilesheet);
384 return tilesheet;
385 }
386
393 public Tilesheet LoadFromStream(string name, Stream stream)
394 {
395 var tilesheet = TilesheetFactory.FromStream(name, stream);
396 Register(tilesheet);
397 return tilesheet;
398 }
399
406 public Tilesheet LoadFromImageFile(string name, string imageFilePath)
407 {
408 var tilesheet = TilesheetFactory.FromImageFile(name, imageFilePath);
409 Register(tilesheet);
410 return tilesheet;
411 }
412
419 public Tilesheet LoadFromAssetsFile(AssetsFile assetsFile, string entryName)
420 {
421 var tilesheet = TilesheetFactory.FromAssetsFile(assetsFile, entryName);
422 Register(tilesheet);
423 return tilesheet;
424 }
425
431 public Tilesheet LoadFromDefinitionFile(string gtsPath)
432 {
433 var tilesheet = TilesheetFactory.FromDefinitionFile(gtsPath);
434 Register(tilesheet);
435 return tilesheet;
436 }
437
444 public Tilesheet LoadFromDefinition(TilesheetDefinition definition, string? baseDirectory = null)
445 {
446 var tilesheet = TilesheetFactory.FromDefinition(definition, baseDirectory);
447 Register(tilesheet);
448 return tilesheet;
449 }
450
457 public Tilesheet LoadFromDefinitionAsset(AssetsFile assetsFile, string gtsEntryName)
458 {
459 var tilesheet = TilesheetFactory.FromDefinitionAsset(assetsFile, gtsEntryName);
460 Register(tilesheet);
461
462 return tilesheet;
463 }
464
465 #endregion public shims to TilesheetFactory
466
471 public Tilesheet LoadFromDefinitionFile(string gtsPath, string tilesheetName)
472 {
473 var sheet = TilesheetFactory.FromDefinitionFile(gtsPath, tilesheetName);
474 Register(sheet);
475 return sheet;
476 }
477
481 public IReadOnlyList<Tilesheet> LoadAllFromDefinitionFile(string gtsPath) =>
482 LoadAllDefinitions(
483 TilesheetDefinitionSerializer.LoadDocument(gtsPath),
484 Path.GetDirectoryName(Path.GetFullPath(gtsPath)),
485 null);
486
492 public Tilesheet LoadFromDefinitionAsset(AssetsFile assetsFile, string gtsEntryName, string tilesheetName)
493 {
494 var sheet = TilesheetFactory.FromDefinitionAsset(assetsFile, gtsEntryName, tilesheetName);
495 Register(sheet);
496 return sheet;
497 }
498
503 public IReadOnlyList<Tilesheet> LoadAllFromDefinitionAsset(AssetsFile assetsFile, string gtsEntryName) =>
504 LoadAllDefinitions(
505 TilesheetDefinitionSerializer.LoadDocument(assetsFile, gtsEntryName),
506 string.IsNullOrWhiteSpace(assetsFile.SourcePath)
507 ? null
508 : Path.GetDirectoryName(Path.GetFullPath(assetsFile.SourcePath)),
509 assetsFile);
510
511 private IReadOnlyList<Tilesheet> LoadAllDefinitions(
513 string? directory,
514 AssetsFile? archive)
515 {
516 var errors = TilesheetDefinitionSerializer.ValidateDocument(document);
517
518 if (errors.Count != 0)
519 throw new InvalidDataException(string.Join(Environment.NewLine, errors));
520
521 var created = new List<Tilesheet>();
522
523 try
524 {
525 foreach (var definition in document.Definitions)
526 created.Add(TilesheetFactory.FromDefinition(definition, directory, archive));
527 }
528 catch
529 {
530 foreach (var sheet in created)
531 sheet.Dispose();
532
533 throw;
534 }
535
536 foreach (var sheet in created)
537 Register(sheet);
538
539 return created;
540 }
541}
Represents an asset file used by the engine, providing functionality to load, manage,...
Definition AssetsFile.cs:18
A physical GTS document containing independently identified logical definitions.
List< TilesheetDefinition > Definitions
Gets or sets the definitions in document order.
Represents the root definition of a tilesheet in the GTS (Gondwana Tilesheet) file format.
IReadOnlyList< Tilesheet > LoadAllFromDefinitionAsset(AssetsFile assetsFile, string gtsEntryName)
Creates all tilesheets before committing them to the registry.
Tilesheet LoadFromDefinition(TilesheetDefinition definition, string? baseDirectory=null)
Creates a new tilesheet from a tilesheet definition object and registers it in the registry.
Tilesheet LoadFromDefinitionFile(string gtsPath)
Creates a new tilesheet from a GTS definition file and registers it in the registry.
Tilesheet LoadFromDefinitionFile(string gtsPath, string tilesheetName)
Loads and registers one selected logical tilesheet.
bool TryGet(string name, out Tilesheet? sheet)
Attempts to retrieve a Tilesheet by its name from the registry.
int Count
Gets the number of tilesheets currently registered in the registry.
static TilesheetRegistry Instance
Gets the singleton instance of the TilesheetRegistry.
void Clear()
Removes all tilesheets from the registry and disposes them.
IReadOnlyList< Tilesheet > LoadAllFromDefinitionFile(string gtsPath)
Creates all tilesheets before committing them to the registry.
Tilesheet LoadFromImageFile(string name, string imageFilePath)
Creates a new tilesheet from an image file and registers it in the registry.
bool Remove(string name, bool dispose=false)
Removes the tilesheet with the specified name from the collection.
Tilesheet LoadFromAssetsFile(AssetsFile assetsFile, string entryName)
Creates a new tilesheet from an entry in an assets file and registers it in the registry.
Tilesheet LoadFromBitmap(string name, SKBitmap bitmap)
Creates a new tilesheet from an existing SKBitmap and registers it in the registry.
Tilesheet LoadFromDefinitionAsset(AssetsFile assetsFile, string gtsEntryName)
Creates a new tilesheet from a GTS definition stored as an entry in an assets file and registers it i...
Tilesheet LoadFromDefinitionAsset(AssetsFile assetsFile, string gtsEntryName, string tilesheetName)
Loads and registers one selected logical tilesheet.
void Dispose()
Releases all resources used by the TilesheetRegistry by clearing and disposing all registered tileshe...
Tilesheet? GetOrNull(string name)
Retrieves a Tilesheet by its name from the registry, or null if not found.
Tilesheet LoadFromStream(string name, Stream stream)
Creates a new tilesheet from a stream and registers it in the registry.
IReadOnlyList< string > Names
Gets a read-only list of all tilesheet names currently registered in the registry.
IImmutableDictionary< string, Tilesheet > GetAll()
Gets an immutable snapshot of all registered tilesheets as a dictionary.
Represents a tilesheet image and metadata for rendering tiles.
Definition Tilesheet.cs:15
void Dispose()
Releases the resources owned by this instance.
Definition Tilesheet.cs:580
Represents the core game engine singleton responsible for managing the main update loop,...
Definition Engine.cs:36
static ILogger< Engine > Logger
Gets the logger instance used by the engine for diagnostic and informational messages.
Definition Engine.cs:59