using System.Reflection;
using PkmnLib.Static.Utils;
namespace PkmnLib.Dynamic.ScriptHandling.Registry;
///
/// Extension methods for scripts.
///
public static class ScriptUtils
{
private static readonly Dictionary NameCache = new();
///
/// Resolve name from the of the given script.
///
public static StringKey ResolveName(this Script script) => ResolveName(script.GetType());
///
/// Resolve name from the of the given type.
///
public static StringKey ResolveName(Type type)
{
if (NameCache.TryGetValue(type, out var name))
return name;
var scriptAttr = type.GetCustomAttribute();
if (scriptAttr == null)
throw new InvalidOperationException($"Type {type} does not have a {nameof(ScriptAttribute)}.");
return NameCache[type] = scriptAttr.Name;
}
}