1using System.Collections.Concurrent;
3using Microsoft.Extensions.Logging;
10public sealed class SvgResourceManager : IDisposable
12 private static readonly Lazy<SvgResourceManager> _instance =
new(() =>
new SvgResourceManager());
13 private readonly ConcurrentDictionary<string, SvgResource> _svgResources =
new();
14 private bool _disposed;
16 private SvgResourceManager()
22 public static SvgResourceManager
Instance => _instance.Value;
32 ArgumentException.ThrowIfNullOrWhiteSpace(key);
33 ArgumentException.ThrowIfNullOrWhiteSpace(path);
36 return AddOrReplace(key, resource);
46 ArgumentNullException.ThrowIfNull(resourceFile);
47 var loaded =
new List<SvgResource>();
49 foreach (var entry
in resourceFile.GetAllEntries())
51 if (entry.AssetType !=
AssetTypes.Svg || _svgResources.ContainsKey(entry.AssetName))
54 using var stream = resourceFile.Get(entry.AssetType, entry.AssetName);
57 Engine.
Logger.LogWarning(
"Failed to retrieve stream for SVG resource: {Key}", entry.AssetName);
64 loaded.Add(AddOrReplace(entry.AssetName, svg));
68 Engine.
Logger.LogError(ex,
"Error loading SVG resource from asset file for key: {Key}", entry.AssetName);
81 public bool Contains(
string key) => _svgResources.ContainsKey(key);
88 public SvgResource?
Get(
string key) => _svgResources.TryGetValue(key, out var resource) ? resource :
null;
94 public Dictionary<string, SvgResource>
GetAll() => _svgResources.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
102 if (_svgResources.TryRemove(key, out var resource))
111 foreach (var resource
in _svgResources.Values)
114 _svgResources.Clear();
129 if (_svgResources.TryGetValue(key, out var existing))
132 _svgResources[key] = resource;
Represents an asset file used by the engine, providing functionality to load, manage,...
static SvgResourceManager Instance
Gets the singleton instance of the SvgResourceManager.
void Unload(string key)
Unloads a resource by key.
List< SvgResource > LoadFromEngineAssetsFile(AssetsFile resourceFile)
Loads all SVG resources from an AssetsFile.
SvgResource? Get(string key)
Gets a resource by key or null when not found.
bool Contains(string key)
Determines whether a resource key is loaded.
Dictionary< string, SvgResource > GetAll()
Gets all loaded resources.
SvgResource LoadFromFile(string key, string path)
Loads an SVG resource from a file path.
void Clear()
Clears all loaded resources.
Represents a loaded SVG asset that can be rasterized into cached bitmaps at requested sizes.
static SvgResource Load(string path)
Loads an SVG resource from a file path.
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.
AssetTypes
Defines the types of assets that can be stored and managed by the engine.