1using System.Collections.Immutable;
4using Microsoft.Extensions.Logging;
12public sealed class TilesheetRegistry : IDisposable
14 private readonly
object _gate =
new();
15 private readonly Dictionary<string, Tilesheet> _sheets =
new(StringComparer.Ordinal);
18 private static readonly Lazy<TilesheetRegistry> _instance =
19 new(() =>
new TilesheetRegistry());
25 public static TilesheetRegistry
Instance => _instance.Value;
28 private TilesheetRegistry()
40 internal void Register(
Tilesheet sheet,
bool disposeReplaced =
true)
44 Engine.
Logger.LogError(
"TilesheetRegistry: Attempt to register a null Tilesheet.");
52 if (_sheets.TryGetValue(sheet.
Name, out var existing) && !ReferenceEquals(existing, sheet))
54 _sheets[sheet.
Name] = sheet;
59 _sheets[sheet.
Name] = sheet;
63 sheet.Disposed -= OnTilesheetDisposed;
64 sheet.Disposed += OnTilesheetDisposed;
67 if (replaced is not
null)
69 replaced.Disposed -= OnTilesheetDisposed;
91 Engine.
Logger.LogWarning(
"TilesheetRegistry: Attempt to look up a null name.");
97 return _sheets.TryGetValue(name, out sheet!);
111 return TryGet(name, out var s) ? s :
null;
131 throw new ArgumentNullException(nameof(name));
135 if (_sheets.TryGetValue(name, out var sheet))
138 throw new KeyNotFoundException($
"Tilesheet with name '{name}' was not found in the registry.");
152 public bool Remove(
string name,
bool dispose =
false)
156 Engine.
Logger.LogWarning(
"TilesheetRegistry: Attempt to remove a null name.");
164 if (!_sheets.TryGetValue(name, out removed))
167 _sheets.Remove(name);
170 removed.Disposed -= OnTilesheetDisposed;
189 internal bool Remove(
string name,
Tilesheet expected,
bool dispose =
false)
193 Engine.
Logger.LogWarning(
"TilesheetRegistry: Attempt to remove a null name.");
197 if (expected is
null)
199 Engine.
Logger.LogWarning(
"TilesheetRegistry: Attempt to remove a null Tilesheet.");
207 if (_sheets.TryGetValue(name, out var current) && ReferenceEquals(current, expected))
209 _sheets.Remove(name);
218 removed.Disposed -= OnTilesheetDisposed;
235 List<Tilesheet> copy;
239 copy = _sheets.Values.ToList();
243 foreach (var ts
in copy)
245 ts.Disposed -= OnTilesheetDisposed;
262 return _sheets.Keys.ToList();
274 public IImmutableDictionary<string, Tilesheet>
GetAll()
278 return _sheets.ToImmutableDictionary(kv => kv.Key, kv => kv.Value, _sheets.Comparer);
294 return _sheets.Count;
309 internal void OnTilesheetRenamed(
string oldName,
string newName,
Tilesheet sheet,
bool disposeReplaced =
true)
312 throw new ArgumentNullException(nameof(oldName));
315 throw new ArgumentNullException(nameof(newName));
318 throw new ArgumentNullException(nameof(sheet));
325 if (_sheets.TryGetValue(oldName, out var existingAtOld) && ReferenceEquals(existingAtOld, sheet))
326 _sheets.Remove(oldName);
329 if (_sheets.TryGetValue(newName, out var existingAtNew) && !ReferenceEquals(existingAtNew, sheet))
332 "TilesheetRegistry: Renaming '{Old}'→'{New}' replaced a different registered Tilesheet instance.",
334 replaced = existingAtNew;
337 _sheets[newName] = sheet;
340 if (replaced is not
null)
342 replaced.Disposed -= OnTilesheetDisposed;
349 private void OnTilesheetDisposed(Tilesheet sheet)
365 #region public shims to TilesheetFactory
375 var tilesheet = TilesheetFactory.FromBitmap(name, bitmap);
388 var tilesheet = TilesheetFactory.FromStream(name, stream);
401 var tilesheet = TilesheetFactory.FromImageFile(name, imageFilePath);
414 var tilesheet = TilesheetFactory.FromAssetsFile(assetsFile, entryName);
426 var tilesheet = TilesheetFactory.FromDefinitionFile(gtsPath);
439 var tilesheet = TilesheetFactory.FromDefinition(definition, baseDirectory);
452 var tilesheet = TilesheetFactory.FromDefinitionAsset(assetsFile, gtsEntryName);
458 #endregion public shims to TilesheetFactory
Represents an asset file used by the engine, providing functionality to load, manage,...
Represents the root definition of a tilesheet in the GTS (Gondwana Tilesheet) file format.
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.
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.
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...
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.
void Dispose()
Releases the resources owned by this instance.
string Name
Gets the name.
Represents the core game engine singleton responsible for managing the main update loop,...
static ILogger< Engine > Logger
Gets the logger instance used by the engine for diagnostic and informational messages.