diff --git a/src/Arch/Core/Utils/CompileTimeStatics.cs b/src/Arch/Core/Utils/CompileTimeStatics.cs index 6b843ce0..b5c78609 100644 --- a/src/Arch/Core/Utils/CompileTimeStatics.cs +++ b/src/Arch/Core/Utils/CompileTimeStatics.cs @@ -79,6 +79,25 @@ public static class ComponentRegistry /// private static readonly Dictionary _types = new(128); + /// + /// All registered components mapped to their as a . + /// + public static Dictionary Types + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => _types; + } + + /// + /// TODO: Store array somewhere and update it to reduce allocations. + /// All registered components as an array. + /// + public static ComponentType[] TypesArray + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => _types.Values.ToArray(); + } + /// /// Gets or sets the total number of registered components in the project. /// @@ -188,11 +207,20 @@ public static void Replace() [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Replace(Type oldType, Type newType) { - var oldComponentType = _types[oldType]; - _types.Remove(oldType); + var id = 0; + if (TryGet(oldType, out var oldComponentType)) + { + id = oldComponentType.Id; + _types.Remove(oldType); + } + else + { + id = Size; + Size++; + } var size = newType.IsValueType ? Marshal.SizeOf(newType) : IntPtr.Size; - _types.Add(newType, new ComponentType(oldComponentType.Id, newType, size, newType.GetFields().Length == 0)); + _types.Add(newType, new ComponentType(id, newType, size, newType.GetFields().Length == 0)); } ///