Remove FluentResults, documentation

This commit is contained in:
2024-07-28 12:52:17 +02:00
parent 5d6149b18b
commit 10f411f076
25 changed files with 224 additions and 44 deletions

View File

@@ -1,15 +1,26 @@
using JetBrains.Annotations;
using PkmnLib.Static.Utils;
namespace PkmnLib.Dynamic.ScriptHandling;
namespace PkmnLib.Dynamic.ScriptHandling.Registry;
/// <summary>
/// Helper attribute to register scripts through reflection.
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
[MeansImplicitUse]
public class ScriptAttribute : Attribute
{
/// <summary>
/// The category of the script it should be registered in.
/// </summary>
public ScriptCategory Category { get; }
/// <summary>
/// The name of the script.
/// </summary>
public StringKey Name { get; }
/// <inheritdoc cref="ScriptAttribute"/>
public ScriptAttribute(ScriptCategory category, string name)
{
Category = category;

View File

@@ -1,17 +1,26 @@
using System.Linq.Expressions;
using System.Reflection;
using JetBrains.Annotations;
using PkmnLib.Dynamic.Libraries;
using PkmnLib.Static.Utils;
namespace PkmnLib.Dynamic.ScriptHandling;
namespace PkmnLib.Dynamic.ScriptHandling.Registry;
/// <summary>
/// A helper data class that's passed through all plugins when initializing a library, so they can
/// register their scripts and other data.
/// </summary>
public class ScriptRegistry
{
private Dictionary<(ScriptCategory category, StringKey name), Func<Script>> _scriptTypes = new();
private readonly Dictionary<(ScriptCategory category, StringKey name), Func<Script>> _scriptTypes = new();
private IBattleStatCalculator? _battleStatCalculator;
private IDamageCalculator? _damageCalculator;
private IMiscLibrary? _miscLibrary;
/// <summary>
/// Automatically register all scripts in the given assembly that have the <see cref="ScriptAttribute"/>, and
/// inherit from <see cref="Script"/>.
/// </summary>
public void RegisterAssemblyScripts(Assembly assembly)
{
var baseType = typeof(Script);
@@ -25,6 +34,10 @@ public class ScriptRegistry
}
}
/// <summary>
/// Register a script type with the given category and name.
/// </summary>
[PublicAPI]
public void RegisterScriptType(ScriptCategory category, StringKey name, Type type)
{
if (type == null)
@@ -39,12 +52,21 @@ public class ScriptRegistry
_scriptTypes[(category, name)] = Expression.Lambda<Func<Script>>(Expression.New(constructor)).Compile();
}
/// <summary>
/// Register a battle stat calculator.
/// </summary>
public void RegisterBattleStatCalculator<T>(T battleStatCalculator)
where T : IBattleStatCalculator => _battleStatCalculator = battleStatCalculator;
/// <summary>
/// Register a damage calculator.
/// </summary>
public void RegisterDamageCalculator<T>(T damageCalculator)
where T : IDamageCalculator => _damageCalculator = damageCalculator;
/// <summary>
/// Register a misc library.
/// </summary>
public void RegisterMiscLibrary<T>(T miscLibrary) where T : IMiscLibrary
=> _miscLibrary = miscLibrary;