// Copyright © Gamesmiths Guild.
#if TOOLS
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
namespace Gamesmiths.Forge.Godot.Editor.Statescript;
///
/// Registry of implementations. Custom node editors are discovered automatically via
/// reflection. Any concrete subclass of in the executing assembly is registered and
/// overrides the default property rendering for its handled node type.
///
internal static class CustomNodeEditorRegistry
{
private static readonly Dictionary> _factories = [];
static CustomNodeEditorRegistry()
{
Type[] allTypes = Assembly.GetExecutingAssembly().GetTypes();
foreach (Type type in allTypes.Where(
x => x.IsSubclassOf(typeof(CustomNodeEditor)) && !x.IsAbstract))
{
Type captured = type;
using var temp = (CustomNodeEditor)Activator.CreateInstance(captured)!;
_factories[temp.HandledRuntimeTypeName] = () => (CustomNodeEditor)Activator.CreateInstance(captured)!;
}
}
///
/// Tries to create a new custom node editor for the given runtime type name.
///
/// The runtime type name of the node.
/// The newly created editor, or if none is registered.
/// if a custom editor was created.
public static bool TryCreate(string runtimeTypeName, [NotNullWhen(true)] out CustomNodeEditor? editor)
{
if (_factories.TryGetValue(runtimeTypeName, out Func? factory))
{
editor = factory();
return true;
}
editor = null;
return false;
}
}
#endif