Small ResourceManager refactor, add ResourceSaver<T>.
This commit is contained in:
@@ -24,7 +24,7 @@ namespace DaggerFramework.Resources
|
||||
|
||||
_logger.Info($"Loading {path} as {typeof(T)} with id \"{resourceId}\"...");
|
||||
|
||||
if (!TryGetLoader<T>(out IResourceLoader? loader))
|
||||
if (!TryGetLoader(out IResourceLoader<T>? loader))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -50,6 +50,21 @@ namespace DaggerFramework.Resources
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TrySave<T>(string path, in T resource) where T : Resource
|
||||
{
|
||||
if (!TryGetSaver(out IResourceSaver<T>? saver))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!saver.TrySave(path, in resource))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryGetResource<T>(string resourceId, [NotNullWhen(true)] out T? resource) where T : Resource
|
||||
{
|
||||
resource = null;
|
||||
@@ -75,37 +90,82 @@ namespace DaggerFramework.Resources
|
||||
|
||||
public bool IsResourceLoaded(string resourceId) => _loadedResources.ContainsKey(resourceId);
|
||||
|
||||
public void AddResourceAssociation(Type resourceType, IResourceLoader loader)
|
||||
public void AddResourceLoaderAssociation<T>(IResourceLoader<T> loader) where T : Resource
|
||||
{
|
||||
_resourceLoaderAssociations.Add(resourceType, loader);
|
||||
_logger.Info($"Added resource loader association for {typeof(T)}.");
|
||||
_resourceLoaderAssociations.Add(typeof(T), loader);
|
||||
}
|
||||
|
||||
private bool TryGetLoader<T>([NotNullWhen(true)] out IResourceLoader? loader) where T : Resource
|
||||
public void AddResourceSaverAssociation<T>(IResourceSaver<T> saver) where T : Resource
|
||||
{
|
||||
_logger.Info($"Added resource saver association for {typeof(T)}.");
|
||||
_resourceSaverAssociations.Add(typeof(T), saver);
|
||||
}
|
||||
|
||||
private bool TryGetLoader<T>([NotNullWhen(true)] out IResourceLoader<T>? loader) where T : Resource
|
||||
{
|
||||
loader = null;
|
||||
|
||||
if (!_resourceLoaderAssociations.ContainsKey(typeof(T)))
|
||||
{
|
||||
_logger.Error($"No loader for {typeof(T).ToString()} was found!");
|
||||
_logger.Error($"No loader association found for {typeof(T)}.");
|
||||
return false;
|
||||
}
|
||||
|
||||
loader = _resourceLoaderAssociations[typeof(T)];
|
||||
loader = _resourceLoaderAssociations[typeof(T)] as IResourceLoader<T>;
|
||||
|
||||
_logger.Info($"Using {loader.GetType().ToString()} for loading...");
|
||||
if (loader is not null)
|
||||
{
|
||||
_logger.Info($"Using {loader.GetType()} for loading...");
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Error($"No loader association found for {typeof(T)}.");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool TryGetSaver<T>([NotNullWhen(true)] out IResourceSaver<T>? saver) where T : Resource
|
||||
{
|
||||
saver = null;
|
||||
|
||||
if (!_resourceSaverAssociations.ContainsKey(typeof(T)))
|
||||
{
|
||||
_logger.Error($"No saver association found for {typeof(T)}.");
|
||||
return false;
|
||||
}
|
||||
|
||||
saver = _resourceSaverAssociations[typeof(T)] as IResourceSaver<T>;
|
||||
|
||||
if (saver is not null)
|
||||
{
|
||||
_logger.Info($"Using {saver.GetType()} for saving...");
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Error($"No saver association found for {typeof(T)}.");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private Logger _logger = new(nameof(ResourceManager));
|
||||
|
||||
private readonly Dictionary<Type, IResourceLoader> _resourceLoaderAssociations = new()
|
||||
private readonly Dictionary<Type, object> _resourceLoaderAssociations = new()
|
||||
{
|
||||
{typeof(Sound), new SoundLoader()},
|
||||
{typeof(Texture2d), new Texture2dLoader()},
|
||||
{typeof(Font), new FontLoader()}
|
||||
};
|
||||
|
||||
private readonly Dictionary<Type, object> _resourceSaverAssociations = new()
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
private Dictionary<string, Resource> _loadedResources = new();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user