Skip to content

Commit ae8b509

Browse files
committed
Fixes, improvements and some other stuff.
1 parent 1fa675b commit ae8b509

23 files changed

+1083
-793
lines changed

src/Arch.Tests/BitSetTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Numerics;
2+
using Arch.Core;
23
using Arch.Core.Utils;
34
using static NUnit.Framework.Assert;
45

src/Arch.Tests/WorldTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public void TrimExcess()
337337
var archetype = world.Archetypes[0];
338338
That(world.Size, Is.EqualTo(1));
339339
That(world.Capacity, Is.EqualTo(archetype.EntitiesPerChunk));
340-
That(archetype.ChunkCount, Is.EqualTo(0));
340+
That(archetype.ChunkCount, Is.EqualTo(1));
341341
That(archetype.ChunkCapacity, Is.EqualTo(1));
342342

343343
// Recycled ids must be trimmed too so that the newest created entity is not out of bounds!

src/Arch/Arch.csproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<PackageId>Arch</PackageId>
1616
<Title>Arch</Title>
17-
<Version>2.0.0-beta</Version>
17+
<Version>2.0.0</Version>
1818
<Authors>genaray</Authors>
1919
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
2020
<Description>A high performance c# net.7 and net.8 archetype based ECS ( Entity component system ).</Description>
@@ -350,11 +350,6 @@
350350
<DesignTime>True</DesignTime>
351351
<DependentUpon>World.Remove.tt</DependentUpon>
352352
</Compile>
353-
<Compile Update="Templates\World.Set.cs">
354-
<AutoGen>True</AutoGen>
355-
<DesignTime>True</DesignTime>
356-
<DependentUpon>World.Set.tt</DependentUpon>
357-
</Compile>
358353
<Compile Update="Templates\World.SetWithQueryDescription.cs">
359354
<AutoGen>True</AutoGen>
360355
<DesignTime>True</DesignTime>
@@ -590,6 +585,11 @@
590585
<DesignTime>True</DesignTime>
591586
<DependentUpon>World.EntityQuery.tt</DependentUpon>
592587
</Compile>
588+
<Compile Update="Templates\World.Set.cs">
589+
<AutoGen>True</AutoGen>
590+
<DesignTime>True</DesignTime>
591+
<DependentUpon>World.Set.tt</DependentUpon>
592+
</Compile>
593593
</ItemGroup>
594594

595595
</Project>

src/Arch/Buffer/CommandBuffer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public void Playback(World world, bool dispose = true)
330330
Debug.Assert(world.IsAlive(entity), $"CommandBuffer can not to set components to the dead {wrappedEntity.Entity}");
331331

332332
// Get entity chunk
333-
var entityInfo = world.EntityInfo[entity.Id];
333+
var entityInfo = world.EntityInfo.GetEntityData(entity.Id);
334334
var archetype = entityInfo.Archetype;
335335
ref readonly var chunk = ref archetype.GetChunk(entityInfo.Slot.ChunkIndex);
336336
var chunkIndex = entityInfo.Slot.Index;

src/Arch/Core/Archetype.cs

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Collections.Pooled;
99
using CommunityToolkit.HighPerformance;
1010
using Array = System.Array;
11+
using System.Runtime.InteropServices;
1112

1213
namespace Arch.Core;
1314

@@ -44,7 +45,6 @@ public Slot(int index, int chunkIndex)
4445
/// <param name="first">The first <see cref="Slot"/>.</param>
4546
/// <param name="second">The second <see cref="Slot"/>.</param>
4647
/// <returns>The result <see cref="Slot"/>.</returns>
47-
4848
public static Slot operator +(Slot first, Slot second)
4949
{
5050
return new Slot(first.Index + second.Index, first.ChunkIndex + second.ChunkIndex);
@@ -55,7 +55,6 @@ public Slot(int index, int chunkIndex)
5555
/// </summary>
5656
/// <param name="slot">The <see cref="Slot"/>.</param>
5757
/// <returns>The <see cref="Slot"/> with index increased by one..</returns>
58-
5958
public static Slot operator ++(Slot slot)
6059
{
6160
slot.Index++;
@@ -66,7 +65,6 @@ public Slot(int index, int chunkIndex)
6665
/// Validates the <see cref="Slot"/>, moves the <see cref="Slot"/> if it is outside a <see cref="Chunk.Capacity"/> to match it.
6766
/// </summary>
6867
/// <returns></returns>
69-
7068
public void Wrap(int capacity)
7169
{
7270
// Result outside valid chunk, wrap into next one
@@ -90,7 +88,6 @@ public void Wrap(int capacity)
9088
/// <param name="source">The <see cref="Slot"/> to shift by one.</param>
9189
/// <param name="sourceCapacity">The capacity of the chunk the slot is in.</param>
9290
/// <returns></returns>
93-
9491
public static Slot Shift(ref Slot source, int sourceCapacity)
9592
{
9693
source.Index++;
@@ -106,7 +103,6 @@ public static Slot Shift(ref Slot source, int sourceCapacity)
106103
/// <param name="destination">The destination <see cref="Slot"/>, a reference point at which the copy or shift operation starts.</param>
107104
/// <param name="sourceCapacity">The source <see cref="Chunk.Capacity"/>.</param>
108105
/// <param name="destinationCapacity">The destination <see cref="Chunk.Capacity"/></param>
109-
110106
public static Slot Shift(in Slot source, int sourceCapacity, in Slot destination, int destinationCapacity)
111107
{
112108
var freeSpot = destination;
@@ -136,14 +132,14 @@ public class Archetypes : IDisposable
136132
/// </summary>
137133
public Archetypes(int capacity)
138134
{
139-
Items = new PooledList<Archetype>(capacity, ClearMode.Never);
135+
Items = new NetStandardList<Archetype>(capacity);
140136
_hashCode = -1;
141137
}
142138

143139
/// <summary>
144140
/// The <see cref="PooledList{T}"/> that contains all <see cref="Archetype"/>s.
145141
/// </summary>
146-
public PooledList<Archetype> Items { get; }
142+
public NetStandardList<Archetype> Items { get; }
147143

148144
/// <summary>
149145
/// The count of this instance.
@@ -184,7 +180,7 @@ public void Remove(Archetype archetype)
184180
/// <returns>The <see cref="Span{T}"/>.</returns>
185181
public Span<Archetype> AsSpan()
186182
{
187-
return Items.Span;
183+
return Items.AsSpan();
188184
}
189185

190186
/// <summary>
@@ -253,7 +249,7 @@ public void Clear()
253249
/// </summary>
254250
public void Dispose()
255251
{
256-
Items.Dispose();
252+
Items.Clear();
257253
}
258254
}
259255

@@ -264,7 +260,6 @@ public void Dispose()
264260
/// </summary>
265261
public sealed partial class Archetype
266262
{
267-
268263
/// <summary>
269264
/// A lookup array that maps the component id to an index within the component array of a <see cref="Chunk"/> to quickly find the correct array for the component type.
270265
/// Is being stored here since all <see cref="Chunks"/> share the same instance to reduce allocations.
@@ -298,46 +293,6 @@ internal Archetype(Signature signature, int baseChunkSize, int baseChunkEntityCo
298293
_removeEdges = new SparseJaggedArray<Archetype>(BucketSize);
299294
}
300295

301-
/// <summary>
302-
/// Try get the index of a component within this archetype. Returns false if the archetype does not have this
303-
/// component.
304-
/// </summary>
305-
[Pure]
306-
internal bool TryIndex<T>(out int i)
307-
{
308-
var id = Component<T>.ComponentType.Id;
309-
Debug.Assert(id != -1, $"Supplied component index is invalid");
310-
311-
if (id >= _componentIdToArrayIndex.Length)
312-
{
313-
i = -1;
314-
return false;
315-
}
316-
317-
i = _componentIdToArrayIndex.DangerousGetReferenceAt(id);
318-
return i != -1;
319-
}
320-
321-
/// <summary>
322-
/// Try get the index of a component within this archetype. Returns false if the archetype does not have this
323-
/// component.
324-
/// </summary>
325-
[Pure]
326-
internal bool TryIndex(ComponentType type, out int i)
327-
{
328-
var id = type.Id;
329-
Debug.Assert(id != -1, $"Supplied component index is invalid");
330-
331-
if (id >= _componentIdToArrayIndex.Length)
332-
{
333-
i = -1;
334-
return false;
335-
}
336-
337-
i = _componentIdToArrayIndex.DangerousGetReferenceAt(id);
338-
return i != -1;
339-
}
340-
341296
/// <summary>
342297
/// The component types that the <see cref="Arch.Core.Entity"/>'s stored here have.
343298
/// The base size of a <see cref="Chunk"/> within the <see cref="Chunks"/> in KB.
@@ -616,6 +571,52 @@ public bool Has<T>()
616571
return BitSet.IsSet(id);
617572
}
618573

574+
/// <summary>
575+
/// Try get the index of a component within this archetype. Returns false if the archetype does not have this
576+
/// component.
577+
/// </summary>
578+
/// <param name="i">The index.</param>
579+
/// <typeparam name="T">The type.</typeparam>
580+
/// <returns>True if it was successfully.</returns>
581+
[Pure]
582+
internal bool TryIndex<T>(out int i)
583+
{
584+
var id = Component<T>.ComponentType.Id;
585+
Debug.Assert(id != -1, $"Supplied component index is invalid");
586+
587+
if (id >= _componentIdToArrayIndex.Length)
588+
{
589+
i = -1;
590+
return false;
591+
}
592+
593+
i = _componentIdToArrayIndex.DangerousGetReferenceAt(id);
594+
return i != -1;
595+
}
596+
597+
/// <summary>
598+
/// Try get the index of a component within this archetype. Returns false if the archetype does not have this
599+
/// component.
600+
/// </summary>
601+
/// <param name="type">The <see cref="ComponentType"/>.</param>
602+
/// <param name="i">The index.</param>
603+
/// <returns>True if it was successfully.</returns>
604+
[Pure]
605+
internal bool TryIndex(ComponentType type, out int i)
606+
{
607+
var id = type.Id;
608+
Debug.Assert(id != -1, $"Supplied component index is invalid");
609+
610+
if (id >= _componentIdToArrayIndex.Length)
611+
{
612+
i = -1;
613+
return false;
614+
}
615+
616+
i = _componentIdToArrayIndex.DangerousGetReferenceAt(id);
617+
return i != -1;
618+
}
619+
619620
/// <summary>
620621
/// Returns a reference of the component of an <see cref="Arch.Core.Entity"/> at a given <see cref="Slot"/>.
621622
/// </summary>
@@ -786,7 +787,7 @@ internal void EnsureEntityCapacity(int newCapacity)
786787
/// </summary>
787788
internal void TrimExcess()
788789
{
789-
Chunks.Count = Count; // By setting the Count we will assure that unnecessary chunks are trimmed.
790+
Chunks.Count = Count + 1; // By setting the Count we will assure that unnecessary chunks are trimmed.
790791
Chunks.TrimExcess();
791792
}
792793
}

src/Arch/Core/Chunk.cs

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -250,35 +250,6 @@ public void Copy<T>(int index, in T cmp)
250250
Unsafe.Add(ref item, index) = cmp;
251251
}
252252

253-
[Pure]
254-
internal bool TryIndex<T>(out int i)
255-
{
256-
var id = Component<T>.ComponentType.Id;
257-
return TryIndex(id, out i);
258-
}
259-
260-
[Pure]
261-
internal bool TryIndex(int id, out int i)
262-
{
263-
Debug.Assert(id != -1, $"Supplied component index is invalid");
264-
265-
if (id >= ComponentIdToArrayIndex.Length)
266-
{
267-
i = -1;
268-
return false;
269-
}
270-
271-
i = ComponentIdToArrayIndex.DangerousGetReferenceAt(id);
272-
return i != -1;
273-
}
274-
275-
[Pure]
276-
public bool Has(int id)
277-
{
278-
var idToArrayIndex = ComponentIdToArrayIndex;
279-
return id < idToArrayIndex.Length && idToArrayIndex.DangerousGetReferenceAt(id) != -1;
280-
}
281-
282253
/// <summary>
283254
/// Checks if a component is included in this <see cref="Chunk"/>.
284255
/// </summary>
@@ -399,6 +370,20 @@ public override string ToString()
399370
public partial struct Chunk
400371
{
401372

373+
/// <summary>
374+
/// Try get the index of a component within this <see cref="Chunk"/>. Returns false if the <see cref="Chunk"/> does not have this
375+
/// component.
376+
/// </summary>
377+
/// <param name="i">The index.</param>
378+
/// <typeparam name="T">The type.</typeparam>
379+
/// <returns>True if it was successfully.</returns>
380+
[Pure]
381+
internal bool TryIndex<T>(out int i)
382+
{
383+
var id = Component<T>.ComponentType.Id;
384+
return TryIndex(id, out i);
385+
}
386+
402387
/// <summary>
403388
/// Returns the component array index of a component.
404389
/// </summary>
@@ -470,6 +455,18 @@ public void Copy(int index, object cmp)
470455
array.SetValue(cmp, index);
471456
}
472457

458+
/// <summary>
459+
/// Checks if a component is included in this <see cref="Chunk"/>.
460+
/// </summary>
461+
/// <param name="id">The <see cref="ComponentType"/> id.</param>
462+
/// <returns>True if included, false otherwise.</returns>
463+
[Pure]
464+
public bool Has(int id)
465+
{
466+
var idToArrayIndex = ComponentIdToArrayIndex;
467+
return id < idToArrayIndex.Length && idToArrayIndex.DangerousGetReferenceAt(id) != -1;
468+
}
469+
473470
/// <summary>
474471
/// Checks if a component is included in this <see cref="Chunk"/>.
475472
/// </summary>
@@ -495,6 +492,28 @@ public bool Has(ComponentType t)
495492
return array.GetValue(index);
496493
}
497494

495+
/// <summary>
496+
/// Try get the index of a component within this <see cref="Chunk"/>. Returns false if the <see cref="Chunk"/> does not have this
497+
/// component.
498+
/// </summary>
499+
/// <param name="id">The <see cref="ComponentType"/> Id.</param>
500+
/// <param name="i">The index.</param>
501+
/// <returns>True if it was successfully.</returns>
502+
[Pure]
503+
internal bool TryIndex(int id, out int i)
504+
{
505+
Debug.Assert(id != -1, $"Supplied component index is invalid");
506+
507+
if (id >= ComponentIdToArrayIndex.Length)
508+
{
509+
i = -1;
510+
return false;
511+
}
512+
513+
i = ComponentIdToArrayIndex.DangerousGetReferenceAt(id);
514+
return i != -1;
515+
}
516+
498517
/// <summary>
499518
/// Returns the component array index of a component by its type.
500519
/// </summary>
@@ -600,11 +619,8 @@ internal static void Copy(
600619
/// <param name="destination">The destination <see cref="Chunk"/>.</param>
601620
/// <param name="destinationIndex">The start index in the destination <see cref="Chunk"/>.</param>
602621
/// <param name="length">The length indicating the amount of <see cref="Entity"/>s being copied.</param>
603-
internal static void CopyComponents(
604-
ref Chunk source, int index, ref Signature sourceSignature,
605-
ref Chunk destination, int destinationIndex,
606-
int length)
607-
{
622+
internal static void CopyComponents(ref Chunk source, int index, ref Signature sourceSignature, ref Chunk destination, int destinationIndex, int length) {
623+
608624
// Arrays
609625
var sourceComponents = source.Components;
610626

0 commit comments

Comments
 (0)