Skip to content

Commit

Permalink
Some small refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
genaray committed Aug 16, 2023
1 parent 3b31531 commit 15623cc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
11 changes: 10 additions & 1 deletion src/Arch/Core/Archetype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,16 @@ internal Archetype(ComponentType[] types)
internal int[] LookupArray
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _componentIdToArrayIndex;
get
{
int[] managedArray = new int[_componentIdToArrayIndex.Length];
for (int i = 0; i < _componentIdToArrayIndex.Length; i++)
{
managedArray[i] = _componentIdToArrayIndex[i];
}

return managedArray;
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ public static class DangerousChunkExtensions
/// <returns></returns>
public static Chunk CreateChunk(int capacity, int[] lookupArray, ComponentType[] types)
{
return new Chunk(capacity, lookupArray, types);
var unsafeLookupArray = new UnsafeArray<int>(lookupArray.Length);
for (var index = 0; index < lookupArray.Length; index++)
{
unsafeLookupArray[index] = lookupArray[index];
}

return new Chunk(capacity, unsafeLookupArray, types);
}

/// <summary>
Expand Down
40 changes: 26 additions & 14 deletions src/Arch/Core/Utils/CompileTimeStatics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ public readonly record struct ComponentType
/// </summary>
/// <param name="id">Its unique id.</param>
/// <param name="type">Its type.</param>
/// <param name="isManaged">If the type is managed.</param>
/// <param name="byteSize">Its size in bytes.</param>
/// <param name="zeroSized">True if its zero sized ( empty struct).</param>
public ComponentType(int id, Type type, int byteSize, bool zeroSized)
public ComponentType(int id, Type type, bool isManaged, int byteSize, bool zeroSized)
{
Id = id;
Type = type;
Expand Down Expand Up @@ -126,7 +127,7 @@ private static ComponentType Add(Type type, int typeSize)
}

// Register and assign component id
meta = new ComponentType(Size + 1, type, typeSize, type.GetFields().Length == 0);
meta = ToComponentType(type);
_types.Add(type, meta);

Size++;
Expand All @@ -144,7 +145,29 @@ public static ComponentType Add(ComponentType type)
{
return Add(type.Type, type.ByteSize);
}

/// <summary>
/// Adds a new component and registers it.
/// </summary>
/// <typeparam name="T">The generic type.</typeparam>
/// <returns>Its <see cref="ComponentType"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ComponentType Add<T>()
{
return Add(typeof(T), SizeOf<T>());
}

/// <summary>
/// Adds a new component and registers it.
/// </summary>
/// <param name="type">Its <see cref="Type"/>.</param>
/// <returns>Its <see cref="ComponentType"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ComponentType Add(Type type)
{
return Add(type, SizeOf(type));
}

/// <summary>
/// Converts a <see cref="Type"/> into a fitting <see cref="ComponentType"/>.
/// </summary>
Expand Down Expand Up @@ -182,17 +205,6 @@ public static ComponentType ToComponentType(Type type)
return new ComponentType(Size, type, managed, size, type.GetFields().Length == 0);
}

/// <summary>
/// Adds a new component and registers it.
/// </summary>
/// <param name="type">Its <see cref="Type"/>.</param>
/// <returns>Its <see cref="ComponentType"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ComponentType Add(Type type)
{
return Add(type, SizeOf(type));
}

// NOTE: Should this be `Contains` to follow other existing .NET APIs (ICollection<T>.Contains(T))?
/// <summary>
/// Checks if a component is registered.
Expand Down Expand Up @@ -272,7 +284,7 @@ public static void Replace(Type oldType, Type newType, int newTypeSize)
id = ++Size;
}

_types.Add(newType, new ComponentType(id, newType, newTypeSize, newType.GetFields().Length == 0));
_types.Add(newType, new ComponentType(id, newType, oldComponentType.IsManaged, newTypeSize,newType.GetFields().Length == 0));
}

/// <summary>
Expand Down

0 comments on commit 15623cc

Please sign in to comment.