< Summary

Information
Class: Lattice.ECS.Core.DeltaCompressor
Assembly: Lattice
File(s): /home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Core/DeltaCompressor.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 60
Coverable lines: 60
Total lines: 179
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 60
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
Compress(...)0%1056320%
Decompress(...)0%4260%
FindChangedBlocks(...)0%506220%

File(s)

/home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Core/DeltaCompressor.cs

#LineLine coverage
 1// Copyright (c) 2026 Sideline Authors. All rights reserved.
 2// Licensed under GPL-3.0.
 3
 4using System;
 5
 6namespace 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
 027        public DeltaCompressor(int maxFrameSize = 1024 * 1024) // 1MB
 28        {
 029            _tempBuffer = new byte[maxFrameSize];
 030            _bufferSize = maxFrameSize;
 031        }
 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        {
 046            if (baseline.Length != current.Length)
 047                throw new ArgumentException("Baseline and current must have same length");
 48
 049            int length = baseline.Length;
 050            int deltaPos = 0;
 51
 052            fixed (byte* basePtr = baseline)
 053            fixed (byte* currPtr = current)
 054            fixed (byte* deltaPtr = deltaOutput)
 55            {
 056                int i = 0;
 057                while (i < length)
 58                {
 59                    // 找到第一个不同的字节
 060                    while (i < length && basePtr[i] == currPtr[i])
 061                        i++;
 62
 063                    if (i >= length)
 64                        break;
 65
 66                    // 记录偏移量
 067                    if (deltaPos + 4 > deltaOutput.Length)
 068                        throw new InvalidOperationException("Delta output buffer too small");
 69
 070                    *(int*)(deltaPtr + deltaPos) = i;
 071                    deltaPos += 4;
 72
 73                    // 找到连续不同的范围
 074                    int start = i;
 075                    while (i < length && basePtr[i] != currPtr[i])
 076                        i++;
 77
 078                    int count = i - start;
 79
 80                    // 写入长度
 081                    if (deltaPos + 4 > deltaOutput.Length)
 082                        throw new InvalidOperationException("Delta output buffer too small");
 83
 084                    *(int*)(deltaPtr + deltaPos) = count;
 085                    deltaPos += 4;
 86
 87                    // 写入差异数据
 088                    if (deltaPos + count > deltaOutput.Length)
 089                        throw new InvalidOperationException("Delta output buffer too small");
 90
 091                    Buffer.BlockCopy(current, start, deltaOutput, deltaPos, count);
 092                    deltaPos += count;
 93                }
 94            }
 95
 096            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            // 先复制基准帧
 0109            Buffer.BlockCopy(baseline, 0, output, 0, baseline.Length);
 110
 0111            fixed (byte* deltaPtr = delta)
 112            {
 0113                int pos = 0;
 0114                while (pos < deltaLength)
 115                {
 116                    // 读取偏移量
 0117                    int offset = *(int*)(deltaPtr + pos);
 0118                    pos += 4;
 119
 120                    // 读取长度
 0121                    int count = *(int*)(deltaPtr + pos);
 0122                    pos += 4;
 123
 124                    // 应用差异
 0125                    Buffer.BlockCopy(delta, pos, output, offset, count);
 0126                    pos += count;
 127                }
 128            }
 0129        }
 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        {
 0145            if (baseline.Length != current.Length)
 0146                throw new ArgumentException("Length mismatch");
 147
 0148            int numBlocks = (baseline.Length + blockSize - 1) / blockSize;
 0149            int count = 0;
 150
 0151            fixed (byte* basePtr = baseline)
 0152            fixed (byte* currPtr = current)
 153            {
 0154                for (int i = 0; i < numBlocks && count < changedBlocks.Length; i++)
 155                {
 0156                    int start = i * blockSize;
 0157                    int end = System.Math.Min(start + blockSize, baseline.Length);
 158
 159                    // 检查块是否有变化
 0160                    bool changed = false;
 0161                    for (int j = start; j < end && !changed; j++)
 162                    {
 0163                        if (basePtr[j] != currPtr[j])
 0164                            changed = true;
 165                    }
 166
 0167                    if (changed)
 168                    {
 0169                        changedBlocks[count++] = i;
 170                    }
 171                }
 172            }
 173
 0174            return count;
 175        }
 176
 177        #endregion
 178    }
 179}