| | | 1 | | using System; |
| | | 2 | | using Lattice.ECS.Core; |
| | | 3 | | |
| | | 4 | | namespace 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 | | { |
| | | 18 | | ArgumentNullException.ThrowIfNull(inputBoundary); |
| | | 19 | | ArgumentNullException.ThrowIfNull(dataBoundary); |
| | | 20 | | |
| | | 21 | | PackedFrameSnapshot? verifiedSnapshot = verifiedFrame?.CapturePackedSnapshot(ComponentSerializationMode.Chec |
| | | 22 | | PackedFrameSnapshot? predictedSnapshot = predictedFrame?.CapturePackedSnapshot(ComponentSerializationMode.Ch |
| | | 23 | | ComponentSchemaManifest componentSchema = ResolveComponentSchema(verifiedSnapshot, predictedSnapshot); |
| | | 24 | | |
| | | 25 | | return new SessionCheckpoint |
| | | 26 | | { |
| | | 27 | | Tick = tick, |
| | | 28 | | VerifiedSnapshot = verifiedSnapshot, |
| | | 29 | | PredictedSnapshot = predictedSnapshot, |
| | | 30 | | Protocol = new SessionCheckpointProtocol( |
| | | 31 | | SessionCheckpointProtocol.CurrentVersion, |
| | | 32 | | dataBoundary.CheckpointStorageKind, |
| | | 33 | | SessionInputProtocol.CreateStamp(inputBoundary), |
| | | 34 | | PackedFrameSnapshot.CurrentFormatVersion, |
| | | 35 | | componentSchema) |
| | | 36 | | }; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | public static SessionCheckpointFrames RestoreFrames( |
| | | 40 | | SessionCheckpoint checkpoint, |
| | | 41 | | SessionRuntimeInputBoundary inputBoundary, |
| | | 42 | | SessionRuntimeDataBoundary dataBoundary, |
| | | 43 | | Func<PackedFrameSnapshot, ComponentSerializationMode, Frame> restoreFrame) |
| | | 44 | | { |
| | | 45 | | ArgumentNullException.ThrowIfNull(checkpoint); |
| | | 46 | | ArgumentNullException.ThrowIfNull(inputBoundary); |
| | | 47 | | ArgumentNullException.ThrowIfNull(dataBoundary); |
| | | 48 | | ArgumentNullException.ThrowIfNull(restoreFrame); |
| | | 49 | | |
| | | 50 | | ValidateCheckpointProtocol(checkpoint, inputBoundary, dataBoundary); |
| | | 51 | | |
| | | 52 | | Frame? verifiedFrame = checkpoint.VerifiedSnapshot != null |
| | | 53 | | ? restoreFrame(checkpoint.VerifiedSnapshot, ComponentSerializationMode.Checkpoint) |
| | | 54 | | : null; |
| | | 55 | | Frame? predictedFrame = checkpoint.PredictedSnapshot != null |
| | | 56 | | ? restoreFrame(checkpoint.PredictedSnapshot, ComponentSerializationMode.Checkpoint) |
| | | 57 | | : null; |
| | | 58 | | |
| | | 59 | | return new SessionCheckpointFrames(verifiedFrame, predictedFrame); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | private static ComponentSchemaManifest ResolveComponentSchema(PackedFrameSnapshot? verifiedSnapshot, PackedFrame |
| | | 63 | | { |
| | | 64 | | if (verifiedSnapshot?.SchemaManifest.IsSpecified == true) |
| | | 65 | | { |
| | | 66 | | if (predictedSnapshot?.SchemaManifest.IsSpecified == true && |
| | | 67 | | !verifiedSnapshot.SchemaManifest.Equals(predictedSnapshot.SchemaManifest)) |
| | | 68 | | { |
| | | 69 | | throw new InvalidOperationException("Verified and predicted checkpoint snapshots produced different |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | return verifiedSnapshot.SchemaManifest; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | if (predictedSnapshot?.SchemaManifest.IsSpecified == true) |
| | | 76 | | { |
| | | 77 | | return predictedSnapshot.SchemaManifest; |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | return ComponentSchemaManifest.Unspecified(ComponentSerializationMode.Checkpoint); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | private static void ValidateCheckpointProtocol( |
| | | 84 | | SessionCheckpoint checkpoint, |
| | | 85 | | SessionRuntimeInputBoundary inputBoundary, |
| | | 86 | | SessionRuntimeDataBoundary dataBoundary) |
| | | 87 | | { |
| | | 88 | | if (checkpoint.Protocol.Version != SessionCheckpointProtocol.CurrentVersion) |
| | | 89 | | { |
| | | 90 | | throw new InvalidOperationException( |
| | | 91 | | $"Checkpoint protocol version {checkpoint.Protocol.Version} is not compatible with current runtime p |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | if (checkpoint.Protocol.CheckpointStorageKind != dataBoundary.CheckpointStorageKind) |
| | | 95 | | { |
| | | 96 | | throw new InvalidOperationException( |
| | | 97 | | $"Checkpoint storage kind {checkpoint.Protocol.CheckpointStorageKind} is not compatible with current |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | if (checkpoint.Protocol.PackedSnapshotFormatVersion != PackedFrameSnapshot.CurrentFormatVersion) |
| | | 101 | | { |
| | | 102 | | throw new InvalidOperationException( |
| | | 103 | | $"Checkpoint packed snapshot format version {checkpoint.Protocol.PackedSnapshotFormatVersion} is not |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | SessionInputProtocol.EnsureCompatible( |
| | | 107 | | checkpoint.Protocol.InputContract, |
| | | 108 | | SessionInputProtocol.CreateStamp(inputBoundary), |
| | | 109 | | nameof(SessionCheckpoint)); |
| | | 110 | | |
| | | 111 | | if (checkpoint.VerifiedSnapshot?.SchemaManifest.IsSpecified == true && |
| | | 112 | | checkpoint.Protocol.ComponentSchema.IsSpecified && |
| | | 113 | | !checkpoint.VerifiedSnapshot.SchemaManifest.Equals(checkpoint.Protocol.ComponentSchema)) |
| | | 114 | | { |
| | | 115 | | throw new InvalidOperationException("Checkpoint protocol schema manifest does not match its verified sna |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | if (checkpoint.PredictedSnapshot?.SchemaManifest.IsSpecified == true && |
| | | 119 | | checkpoint.Protocol.ComponentSchema.IsSpecified && |
| | | 120 | | !checkpoint.PredictedSnapshot.SchemaManifest.Equals(checkpoint.Protocol.ComponentSchema)) |
| | | 121 | | { |
| | | 122 | | throw new InvalidOperationException("Checkpoint protocol schema manifest does not match its predicted sn |
| | | 123 | | } |
| | | 124 | | } |
| | | 125 | | } |
| | | 126 | | |
| | 26 | 127 | | internal readonly record struct SessionCheckpointFrames(Frame? VerifiedFrame, Frame? PredictedFrame); |
| | | 128 | | } |