Skip to content

Commit

Permalink
Re-implemented properties to acess the stored types directly. Made th…
Browse files Browse the repository at this point in the history
…e replace method work even if the old type did not exist.
  • Loading branch information
genaray committed Feb 16, 2023
1 parent aadd3c4 commit 9d850ff
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/Arch/Core/Utils/CompileTimeStatics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,25 @@ public static class ComponentRegistry
/// </summary>
private static readonly Dictionary<Type, ComponentType> _types = new(128);

/// <summary>
/// All registered components mapped to their <see cref="Type"/> as a <see cref="Dictionary{TKey,TValue}"/>.
/// </summary>
public static Dictionary<Type, ComponentType> Types
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _types;
}

/// <summary>
/// TODO: Store array somewhere and update it to reduce allocations.
/// All registered components as an <see cref="ComponentType"/> array.
/// </summary>
public static ComponentType[] TypesArray
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _types.Values.ToArray();
}

/// <summary>
/// Gets or sets the total number of registered components in the project.
/// </summary>
Expand Down Expand Up @@ -188,11 +207,20 @@ public static void Replace<T0,T1>()
[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));
}

/// <summary>
Expand Down

0 comments on commit 9d850ff

Please sign in to comment.