using System.Runtime.CompilerServices;
namespace OECS;
///
/// Registry of component types discovered at compile time by the
/// OECS.SourceGen incremental generator. The consuming project's
/// generated code populates this via a module initializer, so no
/// manual registration is needed.
///
public static class ComponentRegistry
{
private static ComponentDescriptor[]? _descriptors;
private static Dictionary? _byTypeName;
///
/// All discovered component descriptors. Populated automatically
/// by the source generator at module initialization.
///
public static ComponentDescriptor[] Descriptors =>
_descriptors ?? Array.Empty();
///
/// Lookup by assembly-qualified type name. Populated automatically
/// by the source generator at module initialization.
///
public static IReadOnlyDictionary ByTypeName
{
get
{
if (_byTypeName == null)
{
var dict = new Dictionary();
foreach (var desc in Descriptors)
dict[desc.TypeName] = desc;
_byTypeName = dict;
}
return _byTypeName;
}
}
///
/// Called by generated code to register discovered component types.
/// Must be called before any serialization occurs.
///
public static void Initialize(ComponentDescriptor[] descriptors)
{
_descriptors = descriptors;
_byTypeName = null; // Rebuild on next access.
}
}