< Summary

Information
Class: Lattice.ECS.Session.SessionInputContractStamp
Assembly: Lattice
File(s): /home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Session/SessionProtocolVersioning.cs
Line coverage
85%
Covered lines: 17
Uncovered lines: 3
Coverable lines: 20
Total lines: 235
Line coverage: 85%
Branch coverage
75%
Covered branches: 9
Total branches: 12
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%2287.5%
get_ContractVersion()100%11100%
get_ContractKind()100%11100%
get_MissingInputPolicy()100%11100%
get_WritePolicy()100%11100%
get_PlayerOrder()100%11100%
Equals(...)100%88100%
Equals(...)0%620%
GetHashCode()100%210%

File(s)

/home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Session/SessionProtocolVersioning.cs

#LineLine coverage
 1using System;
 2using Lattice.ECS.Core;
 3
 4namespace Lattice.ECS.Session
 5{
 6    /// <summary>
 7    /// 输入 payload 兼容规则。
 8    /// 当前第一轮正式协议采用“显式 codec 标识 + 显式版本号 + 精确匹配”策略,
 9    /// 避免 payload 格式继续依赖隐式约定演进。
 10    /// </summary>
 11    public enum SessionPayloadCompatibilityMode
 12    {
 13        ExactMatchRequired = 0
 14    }
 15
 16    /// <summary>
 17    /// 输入 payload codec 的版本描述。
 18    /// </summary>
 19    public readonly struct SessionInputPayloadFormat : IEquatable<SessionInputPayloadFormat>
 20    {
 21        public SessionInputPayloadFormat(
 22            string codecId,
 23            int version,
 24            SessionPayloadCompatibilityMode compatibilityMode = SessionPayloadCompatibilityMode.ExactMatchRequired)
 25        {
 26            if (string.IsNullOrWhiteSpace(codecId))
 27            {
 28                throw new ArgumentException("Codec id cannot be null or whitespace.", nameof(codecId));
 29            }
 30
 31            if (version <= 0)
 32            {
 33                throw new ArgumentOutOfRangeException(nameof(version), "Input payload format version must be greater tha
 34            }
 35
 36            CodecId = codecId;
 37            Version = version;
 38            CompatibilityMode = compatibilityMode;
 39        }
 40
 41        public string CodecId { get; }
 42
 43        public int Version { get; }
 44
 45        public SessionPayloadCompatibilityMode CompatibilityMode { get; }
 46
 47        public bool Equals(SessionInputPayloadFormat other)
 48        {
 49            return string.Equals(CodecId, other.CodecId, StringComparison.Ordinal) &&
 50                   Version == other.Version &&
 51                   CompatibilityMode == other.CompatibilityMode;
 52        }
 53
 54        public override bool Equals(object? obj)
 55        {
 56            return obj is SessionInputPayloadFormat other && Equals(other);
 57        }
 58
 59        public override int GetHashCode()
 60        {
 61            return HashCode.Combine(CodecId, Version, (int)CompatibilityMode);
 62        }
 63    }
 64
 65    /// <summary>
 66    /// 带显式 payload 版本声明的输入 codec。
 67    /// 当输入需要接入网络、回放或文件载荷时,应显式声明 codec 身份与版本,而不是只暴露裸序列化函数。
 68    /// </summary>
 69    public interface IVersionedInputPayloadCodec<TInput> : IInputPayloadCodec<TInput>
 70        where TInput : IPlayerInput
 71    {
 72        SessionInputPayloadFormat PayloadFormat { get; }
 73    }
 74
 75    /// <summary>
 76    /// 当前运行时输入契约的正式 stamp。
 77    /// 用于把输入模型、缺失语义、重复写入语义与协议版本一起收口。
 78    /// </summary>
 79    public readonly struct SessionInputContractStamp : IEquatable<SessionInputContractStamp>
 80    {
 81        public SessionInputContractStamp(
 82            int contractVersion,
 83            SessionInputContractKind contractKind,
 84            SessionMissingInputPolicy missingInputPolicy,
 85            SessionInputWritePolicy writePolicy,
 86            SessionInputOrder playerOrder)
 87        {
 3188            if (contractVersion <= 0)
 89            {
 090                throw new ArgumentOutOfRangeException(nameof(contractVersion), "Input contract version must be greater t
 91            }
 92
 3193            ContractVersion = contractVersion;
 3194            ContractKind = contractKind;
 3195            MissingInputPolicy = missingInputPolicy;
 3196            WritePolicy = writePolicy;
 3197            PlayerOrder = playerOrder;
 3198        }
 99
 32100        public int ContractVersion { get; }
 101
 30102        public SessionInputContractKind ContractKind { get; }
 103
 27104        public SessionMissingInputPolicy MissingInputPolicy { get; }
 105
 27106        public SessionInputWritePolicy WritePolicy { get; }
 107
 27108        public SessionInputOrder PlayerOrder { get; }
 109
 110        public bool Equals(SessionInputContractStamp other)
 111        {
 14112            return ContractVersion == other.ContractVersion &&
 14113                   ContractKind == other.ContractKind &&
 14114                   MissingInputPolicy == other.MissingInputPolicy &&
 14115                   WritePolicy == other.WritePolicy &&
 14116                   PlayerOrder == other.PlayerOrder;
 117        }
 118
 119        public override bool Equals(object? obj)
 120        {
 0121            return obj is SessionInputContractStamp other && Equals(other);
 122        }
 123
 124        public override int GetHashCode()
 125        {
 0126            return HashCode.Combine(ContractVersion, (int)ContractKind, (int)MissingInputPolicy, (int)WritePolicy, (int)
 127        }
 128    }
 129
 130    /// <summary>
 131    /// Session 输入协议治理入口。
 132    /// </summary>
 133    public static class SessionInputProtocol
 134    {
 135        public const int CurrentContractVersion = 1;
 136
 137        public static SessionInputContractStamp CreateStamp(SessionRuntimeInputBoundary boundary)
 138        {
 139            ArgumentNullException.ThrowIfNull(boundary);
 140            return new SessionInputContractStamp(
 141                CurrentContractVersion,
 142                boundary.ContractKind,
 143                boundary.MissingInputPolicy,
 144                boundary.WritePolicy,
 145                boundary.PlayerOrder);
 146        }
 147
 148        public static void EnsureCompatible(SessionInputContractStamp expected, SessionInputContractStamp actual, string
 149        {
 150            if (!expected.Equals(actual))
 151            {
 152                throw new InvalidOperationException(
 153                    $"Input contract protocol mismatch while restoring {context}. " +
 154                    $"ExpectedVersion={expected.ContractVersion}, ActualVersion={actual.ContractVersion}, " +
 155                    $"ExpectedKind={expected.ContractKind}, ActualKind={actual.ContractKind}.");
 156            }
 157        }
 158
 159        public static void EnsurePayloadCompatible(SessionInputPayloadFormat expected, SessionInputPayloadFormat actual,
 160        {
 161            if (!expected.Equals(actual))
 162            {
 163                throw new InvalidOperationException(
 164                    $"Input payload codec mismatch for {context}. " +
 165                    $"Expected={expected.CodecId}@{expected.Version}, Actual={actual.CodecId}@{actual.Version}.");
 166            }
 167        }
 168    }
 169
 170    /// <summary>
 171    /// 当前 checkpoint 的正式协议 stamp。
 172    /// 第一轮治理采用精确匹配策略:
 173    /// - 输入契约必须一致
 174    /// - checkpoint 存储类型必须一致
 175    /// - packed snapshot 格式版本必须一致
 176    /// - 组件 schema 摘要必须一致
 177    /// </summary>
 178    public readonly struct SessionCheckpointProtocol : IEquatable<SessionCheckpointProtocol>
 179    {
 180        public const int CurrentVersion = 1;
 181
 182        public SessionCheckpointProtocol(
 183            int version,
 184            SessionCheckpointStorageKind checkpointStorageKind,
 185            SessionInputContractStamp inputContract,
 186            int packedSnapshotFormatVersion,
 187            ComponentSchemaManifest componentSchema)
 188        {
 189            if (version <= 0)
 190            {
 191                throw new ArgumentOutOfRangeException(nameof(version), "Checkpoint protocol version must be greater than
 192            }
 193
 194            if (packedSnapshotFormatVersion <= 0)
 195            {
 196                throw new ArgumentOutOfRangeException(nameof(packedSnapshotFormatVersion), "Packed snapshot format versi
 197            }
 198
 199            Version = version;
 200            CheckpointStorageKind = checkpointStorageKind;
 201            InputContract = inputContract;
 202            PackedSnapshotFormatVersion = packedSnapshotFormatVersion;
 203            ComponentSchema = componentSchema;
 204        }
 205
 206        public int Version { get; }
 207
 208        public SessionCheckpointStorageKind CheckpointStorageKind { get; }
 209
 210        public SessionInputContractStamp InputContract { get; }
 211
 212        public int PackedSnapshotFormatVersion { get; }
 213
 214        public ComponentSchemaManifest ComponentSchema { get; }
 215
 216        public bool Equals(SessionCheckpointProtocol other)
 217        {
 218            return Version == other.Version &&
 219                   CheckpointStorageKind == other.CheckpointStorageKind &&
 220                   InputContract.Equals(other.InputContract) &&
 221                   PackedSnapshotFormatVersion == other.PackedSnapshotFormatVersion &&
 222                   ComponentSchema.Equals(other.ComponentSchema);
 223        }
 224
 225        public override bool Equals(object? obj)
 226        {
 227            return obj is SessionCheckpointProtocol other && Equals(other);
 228        }
 229
 230        public override int GetHashCode()
 231        {
 232            return HashCode.Combine(Version, (int)CheckpointStorageKind, InputContract, PackedSnapshotFormatVersion, Com
 233        }
 234    }
 235}