Set up DynamicLibrary from plugins

This commit is contained in:
Deukhoofd 2024-07-28 12:01:43 +02:00
parent c7a6823896
commit 5b495ac871
1 changed files with 21 additions and 1 deletions

View File

@ -1,3 +1,5 @@
using PkmnLib.Dynamic.ScriptHandling;
using PkmnLib.Dynamic.ScriptHandling.Registry;
using PkmnLib.Static.Libraries;
namespace PkmnLib.Dynamic.Libraries;
@ -33,7 +35,24 @@ public interface IDynamicLibrary
public class DynamicLibraryImpl : IDynamicLibrary
{
public DynamicLibraryImpl(IStaticLibrary staticLibrary, IBattleStatCalculator statCalculator,
public static IDynamicLibrary Create(IStaticLibrary staticLibrary, IEnumerable<Plugin> plugins)
{
var registry = new ScriptRegistry();
foreach (var plugin in plugins.OrderBy(x => x.LoadOrder))
{
plugin.Register(registry);
}
if (registry.DamageCalculator is null)
throw new InvalidOperationException("Damage calculator not found in plugins.");
if (registry.BattleStatCalculator is null)
throw new InvalidOperationException("Stat calculator not found in plugins.");
if (registry.MiscLibrary is null)
throw new InvalidOperationException("Misc library not found in plugins.");
return new DynamicLibraryImpl(staticLibrary, registry.BattleStatCalculator,
registry.DamageCalculator, registry.MiscLibrary);
}
private DynamicLibraryImpl(IStaticLibrary staticLibrary, IBattleStatCalculator statCalculator,
IDamageCalculator damageCalculator, IMiscLibrary miscLibrary)
{
StaticLibrary = staticLibrary;
@ -42,6 +61,7 @@ public class DynamicLibraryImpl : IDynamicLibrary
MiscLibrary = miscLibrary;
}
/// <inheritdoc />
public IStaticLibrary StaticLibrary { get; }
/// <inheritdoc />