Bunch more moves, changes in how additional information for items works.

This commit is contained in:
2025-04-14 15:29:26 +02:00
parent 2adbb12367
commit 7c2845502d
60 changed files with 4275 additions and 963 deletions

View File

@@ -8,26 +8,46 @@ namespace PkmnLib.Dynamic.ScriptHandling.Registry;
/// </summary>
public static class ScriptUtils
{
private static readonly Dictionary<Type, StringKey> NameCache = new();
private static readonly Dictionary<Type, (ScriptCategory category, StringKey name)> Cache = 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<T>() where T : Script => ResolveName(typeof(T));
/// <summary>
/// Resolve name from the <see cref="ScriptAttribute"/> of the given type.
/// </summary>
public static StringKey ResolveName(Type type)
public static StringKey ResolveName(Type type) => GetFromCacheOrAdd(type).name;
/// <summary>
/// Resolve category from the <see cref="ScriptAttribute"/> of the given script.
/// </summary>
public static ScriptCategory ResolveCategory(this Script script) => ResolveCategory(script.GetType());
/// <summary>
/// Resolve category from the <see cref="ScriptAttribute"/> of the given script.
/// </summary>
public static ScriptCategory ResolveCategory<T>() where T : Script => ResolveCategory(typeof(T));
/// <summary>
/// Resolve category from the <see cref="ScriptAttribute"/> of the given type.
/// </summary>
public static ScriptCategory ResolveCategory(Type type) => GetFromCacheOrAdd(type).category;
private static (ScriptCategory category, StringKey name) GetFromCacheOrAdd(Type type)
{
if (NameCache.TryGetValue(type, out var name))
return name;
if (Cache.TryGetValue(type, out var key))
return key;
var scriptAttr = type.GetCustomAttribute<ScriptAttribute>();
if (scriptAttr == null)
throw new InvalidOperationException($"Type {type} does not have a {nameof(ScriptAttribute)}.");
return NameCache[type] = scriptAttr.Name;
return Cache[type] = (scriptAttr.Category, scriptAttr.Name);
}
}