完善client agent

master
孙钦者 6 days ago
parent af72792875
commit 5e956f5989
  1. 51
      LocalPackages/com.nimin.lowlevel/Runtime/TypedBuffer.cs
  2. 3
      LocalPackages/com.nimin.lowlevel/Runtime/TypedBuffer.cs.meta
  3. 77
      LocalPackages/com.nimin.network/Runtime/ClientAgent.cs

@ -0,0 +1,51 @@
using System;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
namespace GameCore.LowLevel
{
public ref struct TypedBuffer<T> where T : unmanaged
{
public NativeList<byte> buffer;
public int Length => buffer.Length / UnsafeUtility.SizeOf<T>();
public TypedBuffer(NativeList<byte> buffer)
{
this.buffer = buffer;
}
public unsafe void Add(T data)
{
var oldLength = buffer.Length;
buffer.Resize(oldLength + sizeof(T), NativeArrayOptions.UninitializedMemory);
UnsafeUtility.CopyStructureToPtr(ref data, (byte*)buffer.GetUnsafePtr() + oldLength);
}
public unsafe T this[int index]
{
get
{
if (index < 0 || index >= Length)
throw new IndexOutOfRangeException(nameof(index));
var p = (T*)buffer.GetUnsafeReadOnlyPtr();
return p[index];
}
}
}
public static class TypedBufferExtensions
{
public static TypedBuffer<T> ToTypedBuffer<T>(this NativeList<byte> buffer)
where T : unmanaged
=> new(buffer);
public static unsafe void Sort<T>(this TypedBuffer<T> typedBuffer)
where T : unmanaged, IComparable<T>
{
var p = (T*)typedBuffer.buffer.GetUnsafePtr();
var l = typedBuffer.buffer.Length / sizeof(T);
NativeSortExtension.Sort(p, l);
}
}
}

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: edfdc737cb64451ebdd0dc178e8c07af
timeCreated: 1767469202

@ -5,16 +5,87 @@ namespace GameCore.Network
{
public struct ClientAgent : IDisposable
{
private NativeList<int> dirtySet;
private struct Entry
{
public int srcIndex;
public int generation;
public int confirmedFrameId;
public int deleteFrameId;
public bool inView;
}
private NativeList<Entry> entryList;
private NativeHashMap<int, int> entryIndexMap;
private NativeList<int> dirtyList;
private NativeList<byte> tempBuffer;
public ClientAgent(AllocatorManager.AllocatorHandle allocator)
{
entryList = new NativeList<Entry>(allocator) { default };
entryIndexMap = new NativeHashMap<int, int>(16, allocator);
dirtyList = new NativeList<int>(allocator);
tempBuffer = new NativeList<byte>(allocator);
}
public void Dispose()
{
//TODO
entryList.Dispose();
entryIndexMap.Dispose();
dirtyList.Dispose();
tempBuffer.Dispose();
}
public void UpdateDirtySet(NetDatabase.Snapshot netDatabase, int frameId)
{
// clear inView flag
for (int i = 1, l = entryList.Length; i < l; ++i)
{
entryList.ElementAt(i).inView = false;
}
var tempIntList = dirtyList;
tempIntList.Clear();
// 收集旧视野中的东西,包括等待删除的 entity
for (int i = 1, l = entryList.Length; i < l; ++i)
{
ref readonly var entry = ref entryList.ElementAt(i);
if (entry.srcIndex == 0)
{
//TODO
}
else
{
tempIntList.Add(i);
}
}
var oldInViewCount = tempIntList.Length;
netDatabase.GatherNetDataIndex(tempIntList);
// 遍历 tempIntList 中新视野部分 (oldInViewCount 之后)
{
var j = oldInViewCount;
for (int i = oldInViewCount, l = tempIntList.Length; i < l; ++i)
{
var srcIndex = tempIntList[i];
if (entryIndexMap.TryGetValue(srcIndex, out var index))
{
entryList.ElementAt(index).inView = true;
}
else
{
tempIntList[j++] = srcIndex;
}
}
tempIntList.Resize(j, NativeArrayOptions.UninitializedMemory);
}
//TODO
}
}
}
Loading…
Cancel
Save