Files
MovementTests/addons/forge/editor/statescript/CustomNodeEditorRegistry.cs
Minimata 1d856fd937
All checks were successful
Create tag and build when new code gets to main / BumpTag (push) Successful in 26s
Create tag and build when new code gets to main / Export (push) Successful in 5m42s
Replicated the weapon flying tick setup using resources
2026-04-07 16:32:26 +02:00

53 lines
1.7 KiB
C#

// 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;
/// <summary>
/// Registry of <see cref="CustomNodeEditor"/> implementations. Custom node editors are discovered automatically via
/// reflection. Any concrete subclass of <see cref="CustomNodeEditor"/> in the executing assembly is registered and
/// overrides the default property rendering for its handled node type.
/// </summary>
internal static class CustomNodeEditorRegistry
{
private static readonly Dictionary<string, Func<CustomNodeEditor>> _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)!;
}
}
/// <summary>
/// Tries to create a new custom node editor for the given runtime type name.
/// </summary>
/// <param name="runtimeTypeName">The runtime type name of the node.</param>
/// <param name="editor">The newly created editor, or <see langword="null"/> if none is registered.</param>
/// <returns><see langword="true"/> if a custom editor was created.</returns>
public static bool TryCreate(string runtimeTypeName, [NotNullWhen(true)] out CustomNodeEditor? editor)
{
if (_factories.TryGetValue(runtimeTypeName, out Func<CustomNodeEditor>? factory))
{
editor = factory();
return true;
}
editor = null;
return false;
}
}
#endif