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

@@ -77,17 +77,24 @@ public static class LibraryLoader
// ReSharper disable once SuspiciousTypeConversion.Global
var mutators = plugins.OfType<IPluginDataMutator>().ToList();
var typesLibrary = TypeDataLoader.LoadTypeLibrary(typesResult);
var naturesLibrary = NatureDataLoader.LoadNatureLibrary(naturesResult);
var movesLibrary = MoveDataLoader.LoadMoves(movesResult, typesLibrary,
using var typesStream = typesResult.Open();
var typesLibrary = TypeDataLoader.LoadTypeLibrary(typesStream);
using var naturesStream = naturesResult.Open();
var naturesLibrary = NatureDataLoader.LoadNatureLibrary(naturesStream);
using var movesStream = movesResult.Open();
var movesLibrary = MoveDataLoader.LoadMoves(movesStream, typesLibrary,
wrapper => mutators.ForEach(x => x.MutateMoveData(wrapper)));
var itemsLibrary = ItemDataLoader.LoadItems(itemsResult,
using var itemsStream = itemsResult.Open();
var itemsLibrary = ItemDataLoader.LoadItems(itemsStream,
items => mutators.ForEach(x => x.MutateItemData(items)));
var abilitiesLibrary = AbilityDataLoader.LoadAbilities(abilitiesResult,
using var abilitiesStream = abilitiesResult.Open();
var abilitiesLibrary = AbilityDataLoader.LoadAbilities(abilitiesStream,
abilities => mutators.ForEach(x => x.MutateAbilityData(abilities)));
var growthRatesLibrary = GrowthRateDataLoader.LoadGrowthRates(growthRatesResult,
using var growthRatesStream = growthRatesResult.Open();
var growthRatesLibrary = GrowthRateDataLoader.LoadGrowthRates(growthRatesStream,
growthRates => mutators.ForEach(x => x.MutateGrowthRateData(growthRates)));
var speciesLibrary = SpeciesDataLoader.LoadSpecies(speciesResult, typesLibrary,
using var speciesStream = speciesResult.Open();
var speciesLibrary = SpeciesDataLoader.LoadSpecies(speciesStream, typesLibrary,
map => mutators.ForEach(x => x.MutateSpeciesData(map)));
return new StaticLibraryImpl(settings, speciesLibrary, movesLibrary, abilitiesLibrary, typesLibrary,

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}'.");
}