| | | 1 | | // Copyright (c) 2026 Sideline Authors. All rights reserved. |
| | | 2 | | // Licensed under GPL-3.0. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | |
| | | 6 | | namespace Lattice.ECS.Core |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Delta 压缩器 - FrameSync 风格 |
| | | 10 | | /// |
| | | 11 | | /// 用于计算两个帧之间的差异,减少网络传输数据量 |
| | | 12 | | /// </summary> |
| | | 13 | | public unsafe sealed class DeltaCompressor |
| | | 14 | | { |
| | | 15 | | #region 字段 |
| | | 16 | | |
| | | 17 | | /// <summary>临时缓冲区</summary> |
| | | 18 | | private byte[] _tempBuffer; |
| | | 19 | | |
| | | 20 | | /// <summary>缓冲区大小</summary> |
| | | 21 | | private int _bufferSize; |
| | | 22 | | |
| | | 23 | | #endregion |
| | | 24 | | |
| | | 25 | | #region 构造函数 |
| | | 26 | | |
| | 0 | 27 | | public DeltaCompressor(int maxFrameSize = 1024 * 1024) // 1MB |
| | | 28 | | { |
| | 0 | 29 | | _tempBuffer = new byte[maxFrameSize]; |
| | 0 | 30 | | _bufferSize = maxFrameSize; |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | #endregion |
| | | 34 | | |
| | | 35 | | #region Delta 压缩 |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// 计算两个帧之间的差异 |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="baseline">基准帧数据</param> |
| | | 41 | | /// <param name="current">当前帧数据</param> |
| | | 42 | | /// <param name="deltaOutput">差异输出</param> |
| | | 43 | | /// <returns>差异数据长度</returns> |
| | | 44 | | public int Compress(byte[] baseline, byte[] current, byte[] deltaOutput) |
| | | 45 | | { |
| | 0 | 46 | | if (baseline.Length != current.Length) |
| | 0 | 47 | | throw new ArgumentException("Baseline and current must have same length"); |
| | | 48 | | |
| | 0 | 49 | | int length = baseline.Length; |
| | 0 | 50 | | int deltaPos = 0; |
| | | 51 | | |
| | 0 | 52 | | fixed (byte* basePtr = baseline) |
| | 0 | 53 | | fixed (byte* currPtr = current) |
| | 0 | 54 | | fixed (byte* deltaPtr = deltaOutput) |
| | | 55 | | { |
| | 0 | 56 | | int i = 0; |
| | 0 | 57 | | while (i < length) |
| | | 58 | | { |
| | | 59 | | // 找到第一个不同的字节 |
| | 0 | 60 | | while (i < length && basePtr[i] == currPtr[i]) |
| | 0 | 61 | | i++; |
| | | 62 | | |
| | 0 | 63 | | if (i >= length) |
| | | 64 | | break; |
| | | 65 | | |
| | | 66 | | // 记录偏移量 |
| | 0 | 67 | | if (deltaPos + 4 > deltaOutput.Length) |
| | 0 | 68 | | throw new InvalidOperationException("Delta output buffer too small"); |
| | | 69 | | |
| | 0 | 70 | | *(int*)(deltaPtr + deltaPos) = i; |
| | 0 | 71 | | deltaPos += 4; |
| | | 72 | | |
| | | 73 | | // 找到连续不同的范围 |
| | 0 | 74 | | int start = i; |
| | 0 | 75 | | while (i < length && basePtr[i] != currPtr[i]) |
| | 0 | 76 | | i++; |
| | | 77 | | |
| | 0 | 78 | | int count = i - start; |
| | | 79 | | |
| | | 80 | | // 写入长度 |
| | 0 | 81 | | if (deltaPos + 4 > deltaOutput.Length) |
| | 0 | 82 | | throw new InvalidOperationException("Delta output buffer too small"); |
| | | 83 | | |
| | 0 | 84 | | *(int*)(deltaPtr + deltaPos) = count; |
| | 0 | 85 | | deltaPos += 4; |
| | | 86 | | |
| | | 87 | | // 写入差异数据 |
| | 0 | 88 | | if (deltaPos + count > deltaOutput.Length) |
| | 0 | 89 | | throw new InvalidOperationException("Delta output buffer too small"); |
| | | 90 | | |
| | 0 | 91 | | Buffer.BlockCopy(current, start, deltaOutput, deltaPos, count); |
| | 0 | 92 | | deltaPos += count; |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | return deltaPos; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// 应用差异到基准帧 |
| | | 101 | | /// </summary> |
| | | 102 | | /// <param name="baseline">基准帧数据</param> |
| | | 103 | | /// <param name="delta">差异数据</param> |
| | | 104 | | /// <param name="deltaLength">差异数据长度</param> |
| | | 105 | | /// <param name="output">输出帧</param> |
| | | 106 | | public void Decompress(byte[] baseline, byte[] delta, int deltaLength, byte[] output) |
| | | 107 | | { |
| | | 108 | | // 先复制基准帧 |
| | 0 | 109 | | Buffer.BlockCopy(baseline, 0, output, 0, baseline.Length); |
| | | 110 | | |
| | 0 | 111 | | fixed (byte* deltaPtr = delta) |
| | | 112 | | { |
| | 0 | 113 | | int pos = 0; |
| | 0 | 114 | | while (pos < deltaLength) |
| | | 115 | | { |
| | | 116 | | // 读取偏移量 |
| | 0 | 117 | | int offset = *(int*)(deltaPtr + pos); |
| | 0 | 118 | | pos += 4; |
| | | 119 | | |
| | | 120 | | // 读取长度 |
| | 0 | 121 | | int count = *(int*)(deltaPtr + pos); |
| | 0 | 122 | | pos += 4; |
| | | 123 | | |
| | | 124 | | // 应用差异 |
| | 0 | 125 | | Buffer.BlockCopy(delta, pos, output, offset, count); |
| | 0 | 126 | | pos += count; |
| | | 127 | | } |
| | | 128 | | } |
| | 0 | 129 | | } |
| | | 130 | | |
| | | 131 | | #endregion |
| | | 132 | | |
| | | 133 | | #region 简化版本(位图标记) |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// 使用位图标记变化的块(更高效的版本) |
| | | 137 | | /// </summary> |
| | | 138 | | /// <param name="baseline">基准帧</param> |
| | | 139 | | /// <param name="current">当前帧</param> |
| | | 140 | | /// <param name="blockSize">块大小</param> |
| | | 141 | | /// <param name="changedBlocks">变化的块索引列表</param> |
| | | 142 | | /// <returns>变化的块数量</returns> |
| | | 143 | | public int FindChangedBlocks(byte[] baseline, byte[] current, int blockSize, Span<int> changedBlocks) |
| | | 144 | | { |
| | 0 | 145 | | if (baseline.Length != current.Length) |
| | 0 | 146 | | throw new ArgumentException("Length mismatch"); |
| | | 147 | | |
| | 0 | 148 | | int numBlocks = (baseline.Length + blockSize - 1) / blockSize; |
| | 0 | 149 | | int count = 0; |
| | | 150 | | |
| | 0 | 151 | | fixed (byte* basePtr = baseline) |
| | 0 | 152 | | fixed (byte* currPtr = current) |
| | | 153 | | { |
| | 0 | 154 | | for (int i = 0; i < numBlocks && count < changedBlocks.Length; i++) |
| | | 155 | | { |
| | 0 | 156 | | int start = i * blockSize; |
| | 0 | 157 | | int end = System.Math.Min(start + blockSize, baseline.Length); |
| | | 158 | | |
| | | 159 | | // 检查块是否有变化 |
| | 0 | 160 | | bool changed = false; |
| | 0 | 161 | | for (int j = start; j < end && !changed; j++) |
| | | 162 | | { |
| | 0 | 163 | | if (basePtr[j] != currPtr[j]) |
| | 0 | 164 | | changed = true; |
| | | 165 | | } |
| | | 166 | | |
| | 0 | 167 | | if (changed) |
| | | 168 | | { |
| | 0 | 169 | | changedBlocks[count++] = i; |
| | | 170 | | } |
| | | 171 | | } |
| | | 172 | | } |
| | | 173 | | |
| | 0 | 174 | | return count; |
| | | 175 | | } |
| | | 176 | | |
| | | 177 | | #endregion |
| | | 178 | | } |
| | | 179 | | } |