feat: cleanups and API updates

This commit is contained in:
redglow
2026-07-29 09:38:54 +02:00
parent fc1df2f740
commit 2c0a3f47a4
9 changed files with 68 additions and 46 deletions
@@ -14,7 +14,7 @@ namespace GodotHostTest.AetherBind.Editor;
/// </summary>
public static class GodotScriptPathCache
{
private static readonly ConcurrentDictionary<Type, string> TypeToPath = new();
// private static readonly ConcurrentDictionary<Type, string> TypeToPath = new();
private static readonly ConcurrentDictionary<string, Type> PathToType = new();
private static readonly Type GodotObject = typeof(GodotObject);
private static readonly Type ScriptPathAttribute = typeof(ScriptPathAttribute);
@@ -32,9 +32,9 @@ public static class GodotScriptPathCache
private static void Initialize()
{
GD.Print($"Initializing {nameof(GodotScriptPathCache)}");
TypeToPath.Clear();
// TypeToPath.Clear();
PathToType.Clear();
_assemblyLoadEventHandler = (sender, args) => CacheScriptsInAssembly(args.LoadedAssembly);
_assemblyLoadEventHandler = (_, args) => CacheScriptsInAssembly(args.LoadedAssembly);
AppDomain.CurrentDomain.AssemblyLoad += _assemblyLoadEventHandler;
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
CacheScriptsInAssembly(assembly);
@@ -49,7 +49,7 @@ public static class GodotScriptPathCache
if (type.GetCustomAttributes(ScriptPathAttribute, false).FirstOrDefault() is ScriptPathAttribute
scriptPath)
{
TypeToPath[type] = scriptPath.Path;
// TypeToPath[type] = scriptPath.Path;
PathToType[scriptPath.Path] = type;
}
}
@@ -60,31 +60,31 @@ public static class GodotScriptPathCache
GD.Print($"Deinitializing {nameof(GodotScriptPathCache)}");
AppDomain.CurrentDomain.AssemblyLoad -= _assemblyLoadEventHandler;
_assemblyLoadEventHandler = null;
TypeToPath.Clear();
// TypeToPath.Clear();
PathToType.Clear();
}
public static bool TryGetScriptPath(Type type, [MaybeNullWhen(false)] out string path)
{
InitializeIfNeeded();
return TypeToPath.TryGetValue(type, out path);
}
// public static bool TryGetScriptPath(Type type, [MaybeNullWhen(false)] out string path)
// {
// InitializeIfNeeded();
// return TypeToPath.TryGetValue(type, out path);
// }
public static Script GetScriptFromType(Type type)
{
InitializeIfNeeded();
return TryGetScriptPath(type, out string path)
? GD.Load<Script>(path)
: throw new InvalidOperationException("Script path not found in cache.");
}
// public static Script GetScriptFromType(Type type)
// {
// InitializeIfNeeded();
// return TryGetScriptPath(type, out string path)
// ? GD.Load<Script>(path)
// : throw new InvalidOperationException("Script path not found in cache.");
// }
public static Script GetScriptFromType<T>()
{
InitializeIfNeeded();
return GetScriptFromType(typeof(T));
}
// public static Script GetScriptFromType<T>()
// {
// InitializeIfNeeded();
// return GetScriptFromType(typeof(T));
// }
public static bool TryGetTypeFromPath(string path, [MaybeNullWhen(false)] out Type type)
private static bool TryGetTypeFromPath(string path, [MaybeNullWhen(false)] out Type type)
{
InitializeIfNeeded();
return PathToType.TryGetValue(path, out type);