Slight refactor to clean up resource loading from plugins

This commit is contained in:
2025-05-16 14:22:14 +02:00
parent fdfca99e71
commit a40d85fdae
19 changed files with 94 additions and 108216 deletions

View File

@@ -1,3 +1,4 @@
using System.Reflection;
using PkmnLib.Static.Libraries;
namespace PkmnLib.Dynamic.ScriptHandling.Registry;
@@ -13,9 +14,10 @@ public interface IResourceProvider
LibrarySettings? Settings { get; }
/// <summary>
/// Gets the resource for the given type. This is used to load resources from the plugin.
/// Gets the resource for the given type. This is used to load resources from the plugin. Returns null if the
/// plugin does not provide the resource.
/// </summary>
Stream? GetResource(ResourceFileType request);
IResourceResult? GetResource(ResourceFileType request);
}
/// <summary>
@@ -62,4 +64,36 @@ public enum ResourceFileType
/// The type for the species of Pokémon. This includes the species names and the species effects.
/// </summary>
Species,
}
/// <summary>
/// Interface for a resource result. This is used to load resources from the plugin.
/// </summary>
public interface IResourceResult
{
/// <summary>
/// Opens the resource and returns a stream. This is used to load the resource from the plugin.
/// </summary>
Stream Open();
}
/// <summary>
/// Class for a resource result that is loaded from an embedded resource in an assembly.
/// </summary>
public class AssemblyResourceResult : IResourceResult
{
private readonly string _resourceName;
private readonly Assembly _assembly;
/// <inheritdoc cref="AssemblyResourceResult" />
public AssemblyResourceResult(string resourceName, Assembly assembly)
{
_resourceName = resourceName;
_assembly = assembly;
}
/// <inheritdoc />
public Stream Open() => _assembly.GetManifestResourceStream(_resourceName) ??
throw new InvalidOperationException(
$"Resource '{_resourceName}' not found in assembly '{_assembly.FullName}'.");
}