完善NetDatabase

This commit is contained in:
sunqinzhe committed 2025-12-31 18:45:01 +08:00
1 parent 098cbfb696
commit 88125fc128
28 files changed
+771 -39

No files matched your search

@@ -14,11 +14,12 @@ namespace GameCore.TinyECS
public int generation;
}
public readonly TEnum entityType;
private NativeList<Buffer> entityBuffer;
private NativeList<int> entityIndexToDataIndex;
private TEnum entityType;
public EntityCollection(TEnum type, int capacity, Allocator allocator)
public EntityCollection(TEnum type, int capacity, AllocatorManager.AllocatorHandle allocator)
{
entityType = type;
entityBuffer = new (capacity, allocator);
@@ -19,22 +19,25 @@ namespace GameCore.TinyECS
private FixedList512Bytes<IntPtr> blockPtrList;
private int maxElementCountPerBlock;
private int elementCount;
private AllocatorManager.AllocatorHandle allocator;
public int BlockCount => blockPtrList.Length;
public int BlockCapacity => blockPtrList.Capacity;
public int ElementCount => elementCount;
public int MaxElementCount => MaxElementCountPerBlock * BlockCapacity;
public int MaxElementCountPerBlock => maxElementCountPerBlock;
public AllocatorManager.AllocatorHandle Allocator => allocator;
public bool IsFull => elementCount == MaxElementCount;
public bool IsEmpty => elementCount == 0;
public EntityDataBlockChain(EntityDataBlockMeta blockMeta, int maxElementCountPerBlock = 1024)
public EntityDataBlockChain(EntityDataBlockMeta blockMeta, AllocatorManager.AllocatorHandle allocator, int maxElementCountPerBlock = 1024)
{
this.blockMeta = blockMeta;
blockPtrList = default;
this.maxElementCountPerBlock = maxElementCountPerBlock;
elementCount = 0;
this.allocator = allocator;
}
public void Dispose()
@@ -48,7 +51,7 @@ namespace GameCore.TinyECS
void AllocBlock()
{
var bufferSize = blockMeta.elementSize * maxElementCountPerBlock;
var ptr = UnsafeUtility.Malloc(bufferSize, blockMeta.elementAlign, Allocator.Persistent);
var ptr = AllocatorManager.Allocate(allocator, bufferSize, blockMeta.elementAlign);
blockPtrList.Add((IntPtr)ptr);
}
@@ -58,7 +61,7 @@ namespace GameCore.TinyECS
var pBlock = (void*)blockPtrList[blockIndex];
if (pBlock != null)
{
UnsafeUtility.Free(pBlock, Allocator.Persistent);
AllocatorManager.Free(allocator, pBlock);
blockPtrList.RemoveAt(blockIndex);
}
}
@@ -1,9 +1,12 @@
using System;
using Unity.Collections;
namespace GameCore.TinyECS
{
public interface IEntityDataStruct : IDisposable
{
AllocatorManager.AllocatorHandle Allocator { get; }
void Add();
void Remove(int index);
@@ -14,10 +14,11 @@ namespace GameCore.TinyECS
public int Count => collection.Count;
public EntityDatabase(TEnum type, TData dataStruct, int capacity = 16)
internal EntityDatabase(TEnum type, TData dataStruct, int initCapacity,
AllocatorManager.AllocatorHandle allocator)
{
generator = new (type, capacity, Allocator.Persistent);
collection = new (type, capacity, Allocator.Persistent);
generator = new(type, initCapacity, allocator);
collection = new(type, initCapacity, allocator);
this.dataStruct = dataStruct;
}
@@ -41,7 +42,7 @@ namespace GameCore.TinyECS
var index = collection.GetIndex(entity);
return dataStruct.GetData<T>(index);
}
public ref T GetDataRef<T>(Entity<TEnum> entity) where T : unmanaged
{
var index = collection.GetIndex(entity);
@@ -55,4 +56,14 @@ namespace GameCore.TinyECS
dataStruct.Dispose();
}
}
public static class EntityDataBaseExtensions
{
public static EntityDatabase<TEnum, TData> CreateEntityDatabase<TEnum, TData>(this TData dataStruct, TEnum type, int initCapacity)
where TEnum : unmanaged, IConvertible
where TData : unmanaged, IEntityDataStruct
{
return new EntityDatabase<TEnum, TData>(type, dataStruct, initCapacity, dataStruct.Allocator);
}
}
}
@@ -7,12 +7,12 @@ namespace GameCore.TinyECS
public struct EntityGenerator<TEnum> : IDisposable
where TEnum : unmanaged, IConvertible
{
public readonly TEnum entityType;
private NativeList<int> indexToGeneration;
private int beginSearchIndex;
private int freeCount;
private TEnum entityType;
public EntityGenerator(TEnum type, int capacity, Allocator allocator)
public EntityGenerator(TEnum type, int capacity, AllocatorManager.AllocatorHandle allocator)
{
indexToGeneration = new(capacity, allocator) { 0 };
beginSearchIndex = 1;