Lots more work on implementing battling
This commit is contained in:
31
PkmnLib.Dynamic/ScriptHandling/Registry/ScriptUtils.cs
Normal file
31
PkmnLib.Dynamic/ScriptHandling/Registry/ScriptUtils.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.Reflection;
|
||||
using PkmnLib.Static.Utils;
|
||||
|
||||
namespace PkmnLib.Dynamic.ScriptHandling.Registry;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for scripts.
|
||||
/// </summary>
|
||||
public static class ScriptUtils
|
||||
{
|
||||
private static readonly Dictionary<Type, StringKey> NameCache = new();
|
||||
|
||||
/// <summary>
|
||||
/// Resolve name from the <see cref="ScriptAttribute"/> of the given script.
|
||||
/// </summary>
|
||||
public static StringKey ResolveName(this Script script) => ResolveName(script.GetType());
|
||||
|
||||
/// <summary>
|
||||
/// Resolve name from the <see cref="ScriptAttribute"/> of the given type.
|
||||
/// </summary>
|
||||
public static StringKey ResolveName(Type type)
|
||||
{
|
||||
if (NameCache.TryGetValue(type, out var name))
|
||||
return name;
|
||||
|
||||
var scriptAttr = type.GetCustomAttribute<ScriptAttribute>();
|
||||
if (scriptAttr == null)
|
||||
throw new InvalidOperationException($"Type {type} does not have a {nameof(ScriptAttribute)}.");
|
||||
return NameCache[type] = scriptAttr.Name;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user