Slight refactor to clean up resource loading from plugins
This commit is contained in:
@@ -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}'.");
|
||||
}
|
||||
Reference in New Issue
Block a user