|
|
|
|
@ -16,12 +16,14 @@ namespace GameCore.Network |
|
|
|
|
public ushort historyOffset; |
|
|
|
|
public ushort historyCount; |
|
|
|
|
public int parentIndex; |
|
|
|
|
public int childrenListIndex; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private NativeDataContainer<Entry> entries; |
|
|
|
|
private NativeHashMap<int, UnsafeList<int>> childrenIndexMap; |
|
|
|
|
private NativeArray<UnsafeDataContainer> dataContainers; |
|
|
|
|
private NativeList<FrameHistoryItem> frameHistoryList; |
|
|
|
|
private NativeList<UnsafeList<int>> childrenList; |
|
|
|
|
private NativeList<int> freeChildrenListIndexStack; |
|
|
|
|
|
|
|
|
|
private readonly int maxHistoryCount; |
|
|
|
|
private readonly AllocatorManager.AllocatorHandle allocator; |
|
|
|
|
@ -30,7 +32,6 @@ namespace GameCore.Network |
|
|
|
|
AllocatorManager.AllocatorHandle allocator) |
|
|
|
|
{ |
|
|
|
|
entries = new(allocator); |
|
|
|
|
childrenIndexMap = new(8, allocator); |
|
|
|
|
|
|
|
|
|
var typeCount = typeMetaDatabase.GetTotalTypeCount(); |
|
|
|
|
dataContainers = new NativeArray<UnsafeDataContainer>(typeCount, allocator.ToAllocator); |
|
|
|
|
@ -44,6 +45,9 @@ namespace GameCore.Network |
|
|
|
|
|
|
|
|
|
frameHistoryList = new NativeList<FrameHistoryItem>(allocator); |
|
|
|
|
|
|
|
|
|
childrenList = new NativeList<UnsafeList<int>>(allocator) {default}; |
|
|
|
|
freeChildrenListIndexStack = new NativeList<int>(allocator); |
|
|
|
|
|
|
|
|
|
this.maxHistoryCount = maxHistoryCount; |
|
|
|
|
this.allocator = allocator; |
|
|
|
|
} |
|
|
|
|
@ -52,20 +56,22 @@ namespace GameCore.Network |
|
|
|
|
{ |
|
|
|
|
entries.Dispose(); |
|
|
|
|
|
|
|
|
|
foreach (var kvp in childrenIndexMap) |
|
|
|
|
{ |
|
|
|
|
kvp.Value.Dispose(); |
|
|
|
|
} |
|
|
|
|
childrenIndexMap.Dispose(); |
|
|
|
|
|
|
|
|
|
var typeCount = dataContainers.Length; |
|
|
|
|
for (var i = 0; i < typeCount; i++) |
|
|
|
|
{ |
|
|
|
|
if (dataContainers[i].IsCreated) |
|
|
|
|
dataContainers[i].Dispose(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
dataContainers.Dispose(); |
|
|
|
|
frameHistoryList.Dispose(); |
|
|
|
|
|
|
|
|
|
for (int i = 0, len = childrenList.Length; i < len; i++) |
|
|
|
|
{ |
|
|
|
|
childrenList[i].Dispose(); |
|
|
|
|
} |
|
|
|
|
childrenList.Dispose(); |
|
|
|
|
freeChildrenListIndexStack.Dispose(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public int Add<T>(T data, int frameId, int parentIndex, out int generation, |
|
|
|
|
@ -87,14 +93,26 @@ namespace GameCore.Network |
|
|
|
|
dataContainer.ElementAt<T>(dataIndex) = data; |
|
|
|
|
|
|
|
|
|
entry.parentIndex = parentIndex; |
|
|
|
|
entry.childrenListIndex = 0; |
|
|
|
|
if (parentIndex > 0) |
|
|
|
|
{ |
|
|
|
|
if (!childrenIndexMap.TryGetValue(parentIndex, out var list)) |
|
|
|
|
ref var parentEntry = ref entries.ElementAt(parentIndex); |
|
|
|
|
if (parentEntry.childrenListIndex == 0) |
|
|
|
|
{ |
|
|
|
|
list = new UnsafeList<int>(4, allocator); |
|
|
|
|
if (freeChildrenListIndexStack.Length > 0) |
|
|
|
|
{ |
|
|
|
|
parentEntry.childrenListIndex = freeChildrenListIndexStack[^1]; |
|
|
|
|
freeChildrenListIndexStack.RemoveAt(freeChildrenListIndexStack.Length - 1); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
parentEntry.childrenListIndex = childrenList.Length; |
|
|
|
|
childrenList.Add(new UnsafeList<int>(4, allocator)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ref var list = ref childrenList.ElementAt(parentEntry.childrenListIndex); |
|
|
|
|
list.Add(entryIndex); |
|
|
|
|
childrenIndexMap[parentIndex] = list; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
entry.historyOffset = 0; |
|
|
|
|
@ -178,25 +196,26 @@ namespace GameCore.Network |
|
|
|
|
|
|
|
|
|
if (entry.parentIndex > 0) |
|
|
|
|
{ |
|
|
|
|
if (removeFromParent && childrenIndexMap.TryGetValue(entry.parentIndex, out var list)) |
|
|
|
|
if (removeFromParent) |
|
|
|
|
{ |
|
|
|
|
var i = list.IndexOf(index); |
|
|
|
|
list.RemoveAtSwapBack(i); |
|
|
|
|
childrenIndexMap[entry.parentIndex] = list; |
|
|
|
|
var parentEntry = entries.ElementAt(entry.parentIndex); |
|
|
|
|
ref var list = ref childrenList.ElementAt(parentEntry.childrenListIndex); |
|
|
|
|
|
|
|
|
|
var selfIndex = list.IndexOf(index); |
|
|
|
|
list.RemoveAtSwapBack(selfIndex); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (entry.childrenListIndex > 0) |
|
|
|
|
{ |
|
|
|
|
if (childrenIndexMap.TryGetValue(index, out var list)) |
|
|
|
|
ref var list = ref childrenList.ElementAt(entry.childrenListIndex); |
|
|
|
|
for (int i = 0, len = list.Length; i < len; ++i) |
|
|
|
|
{ |
|
|
|
|
for (int i = 0, len = list.Length; i < len; ++i) |
|
|
|
|
{ |
|
|
|
|
RemoveInternal(list[i], false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
list.Dispose(); |
|
|
|
|
childrenIndexMap.Remove(index); |
|
|
|
|
RemoveInternal(list[i], false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
list.Clear(); |
|
|
|
|
freeChildrenListIndexStack.Add(entry.childrenListIndex); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
entries.Free(index); |
|
|
|
|
|