Add entities SerializationTest

This commit is contained in:
2025-12-21 13:07:59 +09:00
parent 00b4e82ded
commit 840cf7dd5a
13 changed files with 257 additions and 29 deletions

View File

@@ -371,6 +371,16 @@ internal unsafe struct Archetype : IDisposable
_chunks.Add(newChunk);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Entity GetEntity(int chunkIndex, int rowIndex)
{
var chunk = _chunks[chunkIndex];
var chunkBase = chunk.GetUnsafePtr();
var src = chunkBase + _entityIdsOffset + (sizeof(Entity) * rowIndex);
return *(Entity*)src;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void SetEntity(int chunkIndex, int rowIndex, Entity entity)
{

View File

@@ -161,16 +161,17 @@ public unsafe partial class EntityManager : IDisposable
/// <summary>
/// Create multiple entities with specified components.
/// </summary>
/// <param name="allocator">The allocator to use for the returned array.</param>
/// <param name="entities">The span to store the created entities.</param>
/// <param name="set">A set of component type IDs to add to the entities.</param>
/// <returns>An array of the created entities.</returns>
public void CreateEntities(Span<Entity> entities, ComponentSet set)
{
var arcID = _world.GetArchetypeIDBySignatureHash(set.GetHashCode());
var hash = set.GetHashCode();
var arcID = _world.GetArchetypeIDBySignatureHash(hash);
if (arcID.IsInvalid)
{
arcID = _world.CreateArchetype(set.Components, set.GetHashCode());
arcID = _world.CreateArchetype(set.Components, hash);
}
ref var archetype = ref _world.GetArchetypeReference(arcID);

View File

@@ -48,6 +48,10 @@ public unsafe partial struct EntityQuery
where TJob : unmanaged, IJobChunk
{
var world = World.GetWorld(_worldID).GetValueOrThrow();
if (world.JobScheduler == null)
{
throw new InvalidOperationException("The World has no JobScheduler assigned.");
}
var chunkInfos = new UnsafeList<ChunkInfo>(_matchingArchetypes.Count * 2, JobScheduler.TempAllocatorHandle);

View File

@@ -597,7 +597,7 @@ public ref partial struct QueryBuilder
return queryID;
}
private void Dispose()
private readonly void Dispose()
{
_scope.Dispose();
}

View File

@@ -1096,6 +1096,10 @@ public unsafe partial struct EntityQuery
where T0 : unmanaged, IComponent
{
var world = World.GetWorld(_worldID).GetValueOrThrow();
if (world.JobScheduler == null)
{
throw new InvalidOperationException("The World has no JobScheduler assigned.");
}
// 1. Flatten the World
var chunks = new UnsafeList<IntPtr>(128, JobScheduler.TempAllocatorHandle);
@@ -1229,6 +1233,10 @@ public unsafe partial struct EntityQuery
where T1 : unmanaged, IComponent
{
var world = World.GetWorld(_worldID).GetValueOrThrow();
if (world.JobScheduler == null)
{
throw new InvalidOperationException("The World has no JobScheduler assigned.");
}
// 1. Flatten the World
var chunks = new UnsafeList<IntPtr>(128, JobScheduler.TempAllocatorHandle);
@@ -1389,6 +1397,10 @@ public unsafe partial struct EntityQuery
where T2 : unmanaged, IComponent
{
var world = World.GetWorld(_worldID).GetValueOrThrow();
if (world.JobScheduler == null)
{
throw new InvalidOperationException("The World has no JobScheduler assigned.");
}
// 1. Flatten the World
var chunks = new UnsafeList<IntPtr>(128, JobScheduler.TempAllocatorHandle);
@@ -1576,6 +1588,10 @@ public unsafe partial struct EntityQuery
where T3 : unmanaged, IComponent
{
var world = World.GetWorld(_worldID).GetValueOrThrow();
if (world.JobScheduler == null)
{
throw new InvalidOperationException("The World has no JobScheduler assigned.");
}
// 1. Flatten the World
var chunks = new UnsafeList<IntPtr>(128, JobScheduler.TempAllocatorHandle);
@@ -1790,6 +1806,10 @@ public unsafe partial struct EntityQuery
where T4 : unmanaged, IComponent
{
var world = World.GetWorld(_worldID).GetValueOrThrow();
if (world.JobScheduler == null)
{
throw new InvalidOperationException("The World has no JobScheduler assigned.");
}
// 1. Flatten the World
var chunks = new UnsafeList<IntPtr>(128, JobScheduler.TempAllocatorHandle);
@@ -2031,6 +2051,10 @@ public unsafe partial struct EntityQuery
where T5 : unmanaged, IComponent
{
var world = World.GetWorld(_worldID).GetValueOrThrow();
if (world.JobScheduler == null)
{
throw new InvalidOperationException("The World has no JobScheduler assigned.");
}
// 1. Flatten the World
var chunks = new UnsafeList<IntPtr>(128, JobScheduler.TempAllocatorHandle);
@@ -2299,6 +2323,10 @@ public unsafe partial struct EntityQuery
where T6 : unmanaged, IComponent
{
var world = World.GetWorld(_worldID).GetValueOrThrow();
if (world.JobScheduler == null)
{
throw new InvalidOperationException("The World has no JobScheduler assigned.");
}
// 1. Flatten the World
var chunks = new UnsafeList<IntPtr>(128, JobScheduler.TempAllocatorHandle);
@@ -2594,6 +2622,10 @@ public unsafe partial struct EntityQuery
where T7 : unmanaged, IComponent
{
var world = World.GetWorld(_worldID).GetValueOrThrow();
if (world.JobScheduler == null)
{
throw new InvalidOperationException("The World has no JobScheduler assigned.");
}
// 1. Flatten the World
var chunks = new UnsafeList<IntPtr>(128, JobScheduler.TempAllocatorHandle);

View File

@@ -126,6 +126,10 @@ public unsafe partial struct EntityQuery
<#= restrictions #>
{
var world = World.GetWorld(_worldID).GetValueOrThrow();
if (world.JobScheduler == null)
{
throw new InvalidOperationException("The World has no JobScheduler assigned.");
}
// 1. Flatten the World
var chunks = new UnsafeList<IntPtr>(128, JobScheduler.TempAllocatorHandle);

View File

@@ -11,11 +11,11 @@ public partial class World
private static readonly List<World?> s_worlds = new(4);
private static readonly Queue<Identifier<World>> s_freeWorldSlots = new();
internal static Identifier<Archetype> EmptyArchetypeID => new (0);
internal static Identifier<Archetype> EmptyArchetypeID => new(0);
public static int WorldCount => s_worlds.Count - s_freeWorldSlots.Count;
public static World Create(JobScheduler jobScheduler, int entityCapacity = 16)
public static World Create(JobScheduler? jobScheduler = null, int entityCapacity = 16)
{
lock (s_worlds)
{
@@ -79,11 +79,11 @@ public partial class World
public partial class World : IDisposable, IEquatable<World>
{
private readonly Identifier<World> _id;
private readonly JobScheduler _jobScheduler;
private readonly JobScheduler? _jobScheduler;
private readonly EntityManager _entityManager;
private readonly EntityCommandBuffer _entityCommandBuffer;
private readonly EntityCommandBuffer[] _threadLocalECBs;
private readonly EntityCommandBuffer[]? _threadLocalECBs;
private readonly SystemManager _systemManager;
@@ -106,7 +106,7 @@ public partial class World : IDisposable, IEquatable<World>
/// <summary>
/// Gets the job scheduler associated with this world.
/// </summary>
public JobScheduler JobScheduler => _jobScheduler;
public JobScheduler? JobScheduler => _jobScheduler;
/// <summary>
/// Gets the publicntity manager for this world.
@@ -131,14 +131,13 @@ public partial class World : IDisposable, IEquatable<World>
/// </remarks>
public EntityCommandBuffer EntityCommandBuffer => _entityCommandBuffer;
private World(Identifier<World> id, int entityCapacity, JobScheduler jobScheduler)
private World(Identifier<World> id, int entityCapacity, JobScheduler? jobScheduler)
{
_id = id;
_jobScheduler = jobScheduler;
_entityManager = new EntityManager(this, entityCapacity);
_entityCommandBuffer = new EntityCommandBuffer(_entityManager);
_threadLocalECBs = new EntityCommandBuffer[jobScheduler.WorkerCount];
_systemManager = new SystemManager(this);
@@ -148,9 +147,13 @@ public partial class World : IDisposable, IEquatable<World>
_archetypeLookup = new UnsafeHashMap<int, Identifier<Archetype>>(16, Allocator.Persistent);
_querieLookup = new UnsafeHashMap<int, Identifier<EntityQuery>>(16, Allocator.Persistent);
for (var i = 0; i < jobScheduler.WorkerCount; i++)
if (jobScheduler != null)
{
_threadLocalECBs[i] = new EntityCommandBuffer(_entityManager);
_threadLocalECBs = new EntityCommandBuffer[jobScheduler.WorkerCount];
for (var i = 0; i < jobScheduler.WorkerCount; i++)
{
_threadLocalECBs[i] = new EntityCommandBuffer(_entityManager);
}
}
// Create the empty archetype
@@ -227,9 +230,12 @@ public partial class World : IDisposable, IEquatable<World>
{
_entityCommandBuffer.Playback();
for (var i = 0; i < _threadLocalECBs.Length; i++)
if (_threadLocalECBs != null)
{
_threadLocalECBs[i].Playback();
for (var i = 0; i < _threadLocalECBs.Length; i++)
{
_threadLocalECBs[i].Playback();
}
}
}
@@ -254,6 +260,11 @@ public partial class World : IDisposable, IEquatable<World>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public EntityCommandBuffer GetThreadLocalEntityCommandBuffer(int threadIndex)
{
if (_threadLocalECBs == null)
{
throw new InvalidOperationException("This world does not have a JobScheduler associated with it.");
}
return _threadLocalECBs[threadIndex];
}
@@ -301,9 +312,13 @@ public partial class World : IDisposable, IEquatable<World>
_entityManager.Dispose();
_entityCommandBuffer.Dispose();
foreach (var v in _threadLocalECBs)
if (_threadLocalECBs != null)
{
v.Dispose();
foreach (var v in _threadLocalECBs)
{
v.Dispose();
}
}
_archetypes.Dispose();