< Summary

Information
Class: Lattice.ECS.Session.SessionCheckpointFactory
Assembly: Lattice
File(s): /home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Session/SessionCheckpointFactory.cs
Line coverage
83%
Covered lines: 49
Uncovered lines: 10
Coverable lines: 59
Total lines: 128
Line coverage: 83%
Branch coverage
61%
Covered branches: 27
Total branches: 44
Branch coverage: 61.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Capture(...)50%44100%
RestoreFrames(...)50%44100%
ResolveComponentSchema(...)50%391450%
ValidateCheckpointProtocol(...)72.72%322272.72%

File(s)

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

#LineLine coverage
 1using System;
 2using Lattice.ECS.Core;
 3
 4namespace Lattice.ECS.Session
 5{
 6    /// <summary>
 7    /// Session checkpoint 的捕获与恢复辅助。
 8    /// </summary>
 9    internal static class SessionCheckpointFactory
 10    {
 11        public static SessionCheckpoint Capture(
 12            int tick,
 13            Frame? verifiedFrame,
 14            Frame? predictedFrame,
 15            SessionRuntimeInputBoundary inputBoundary,
 16            SessionRuntimeDataBoundary dataBoundary)
 17        {
 1618            ArgumentNullException.ThrowIfNull(inputBoundary);
 1619            ArgumentNullException.ThrowIfNull(dataBoundary);
 20
 1621            PackedFrameSnapshot? verifiedSnapshot = verifiedFrame?.CapturePackedSnapshot(ComponentSerializationMode.Chec
 1622            PackedFrameSnapshot? predictedSnapshot = predictedFrame?.CapturePackedSnapshot(ComponentSerializationMode.Ch
 1623            ComponentSchemaManifest componentSchema = ResolveComponentSchema(verifiedSnapshot, predictedSnapshot);
 24
 1625            return new SessionCheckpoint
 1626            {
 1627                Tick = tick,
 1628                VerifiedSnapshot = verifiedSnapshot,
 1629                PredictedSnapshot = predictedSnapshot,
 1630                Protocol = new SessionCheckpointProtocol(
 1631                    SessionCheckpointProtocol.CurrentVersion,
 1632                    dataBoundary.CheckpointStorageKind,
 1633                    SessionInputProtocol.CreateStamp(inputBoundary),
 1634                    PackedFrameSnapshot.CurrentFormatVersion,
 1635                    componentSchema)
 1636            };
 37        }
 38
 39        public static SessionCheckpointFrames RestoreFrames(
 40            SessionCheckpoint checkpoint,
 41            SessionRuntimeInputBoundary inputBoundary,
 42            SessionRuntimeDataBoundary dataBoundary,
 43            Func<PackedFrameSnapshot, ComponentSerializationMode, Frame> restoreFrame)
 44        {
 1545            ArgumentNullException.ThrowIfNull(checkpoint);
 1546            ArgumentNullException.ThrowIfNull(inputBoundary);
 1547            ArgumentNullException.ThrowIfNull(dataBoundary);
 1548            ArgumentNullException.ThrowIfNull(restoreFrame);
 49
 1550            ValidateCheckpointProtocol(checkpoint, inputBoundary, dataBoundary);
 51
 1352            Frame? verifiedFrame = checkpoint.VerifiedSnapshot != null
 1353                ? restoreFrame(checkpoint.VerifiedSnapshot, ComponentSerializationMode.Checkpoint)
 1354                : null;
 1355            Frame? predictedFrame = checkpoint.PredictedSnapshot != null
 1356                ? restoreFrame(checkpoint.PredictedSnapshot, ComponentSerializationMode.Checkpoint)
 1357                : null;
 58
 1359            return new SessionCheckpointFrames(verifiedFrame, predictedFrame);
 60        }
 61
 62        private static ComponentSchemaManifest ResolveComponentSchema(PackedFrameSnapshot? verifiedSnapshot, PackedFrame
 63        {
 1664            if (verifiedSnapshot?.SchemaManifest.IsSpecified == true)
 65            {
 1666                if (predictedSnapshot?.SchemaManifest.IsSpecified == true &&
 1667                    !verifiedSnapshot.SchemaManifest.Equals(predictedSnapshot.SchemaManifest))
 68                {
 069                    throw new InvalidOperationException("Verified and predicted checkpoint snapshots produced different 
 70                }
 71
 1672                return verifiedSnapshot.SchemaManifest;
 73            }
 74
 075            if (predictedSnapshot?.SchemaManifest.IsSpecified == true)
 76            {
 077                return predictedSnapshot.SchemaManifest;
 78            }
 79
 080            return ComponentSchemaManifest.Unspecified(ComponentSerializationMode.Checkpoint);
 81        }
 82
 83        private static void ValidateCheckpointProtocol(
 84            SessionCheckpoint checkpoint,
 85            SessionRuntimeInputBoundary inputBoundary,
 86            SessionRuntimeDataBoundary dataBoundary)
 87        {
 1588            if (checkpoint.Protocol.Version != SessionCheckpointProtocol.CurrentVersion)
 89            {
 090                throw new InvalidOperationException(
 091                    $"Checkpoint protocol version {checkpoint.Protocol.Version} is not compatible with current runtime p
 92            }
 93
 1594            if (checkpoint.Protocol.CheckpointStorageKind != dataBoundary.CheckpointStorageKind)
 95            {
 096                throw new InvalidOperationException(
 097                    $"Checkpoint storage kind {checkpoint.Protocol.CheckpointStorageKind} is not compatible with current
 98            }
 99
 15100            if (checkpoint.Protocol.PackedSnapshotFormatVersion != PackedFrameSnapshot.CurrentFormatVersion)
 101            {
 1102                throw new InvalidOperationException(
 1103                    $"Checkpoint packed snapshot format version {checkpoint.Protocol.PackedSnapshotFormatVersion} is not
 104            }
 105
 14106            SessionInputProtocol.EnsureCompatible(
 14107                checkpoint.Protocol.InputContract,
 14108                SessionInputProtocol.CreateStamp(inputBoundary),
 14109                nameof(SessionCheckpoint));
 110
 13111            if (checkpoint.VerifiedSnapshot?.SchemaManifest.IsSpecified == true &&
 13112                checkpoint.Protocol.ComponentSchema.IsSpecified &&
 13113                !checkpoint.VerifiedSnapshot.SchemaManifest.Equals(checkpoint.Protocol.ComponentSchema))
 114            {
 0115                throw new InvalidOperationException("Checkpoint protocol schema manifest does not match its verified sna
 116            }
 117
 13118            if (checkpoint.PredictedSnapshot?.SchemaManifest.IsSpecified == true &&
 13119                checkpoint.Protocol.ComponentSchema.IsSpecified &&
 13120                !checkpoint.PredictedSnapshot.SchemaManifest.Equals(checkpoint.Protocol.ComponentSchema))
 121            {
 0122                throw new InvalidOperationException("Checkpoint protocol schema manifest does not match its predicted sn
 123            }
 13124        }
 125    }
 126
 127    internal readonly record struct SessionCheckpointFrames(Frame? VerifiedFrame, Frame? PredictedFrame);
 128}