using Unity.Collections; using Unity.Collections.LowLevel.Unsafe; using Unity.Mathematics; namespace GameCore.Network { public readonly struct FrameHistoryItem { public readonly ulong mask; public readonly int frameId; public const int kMaxBitCount = 64; public bool IsDirty => mask != 0ul; private FrameHistoryItem(ulong mask, int frameId) { this.mask = mask; this.frameId = frameId; } public static unsafe FrameHistoryItem Make(T baseData, T newData, NativeArray fieldSizeArray, int frameId) where T : unmanaged { var basePtr = (byte*) &baseData; var newPtr = (byte*) &newData; ulong mask = 0ul; var offset = 0; for (int i = 0, n = math.min(64, fieldSizeArray.Length); i < n; ++i) { var nextOffset = fieldSizeArray[i]; var fieldSize = nextOffset - offset; if (UnsafeUtility.MemCmp(basePtr + offset, newPtr + offset, fieldSize) != 0) { mask |= 1ul << i; } offset = nextOffset; } return new FrameHistoryItem(mask, frameId); } } }