protected override void OnUpdate() { NetworkSnapshotAckComponent ack = GetSingleton <NetworkSnapshotAckComponent>(); // 有新快照数据,可以进行回滚. if (_lastRollbackTick < ack.LastReceivedSnapshotByLocal) { Rollback(); _lastRollbackTick = ack.LastReceivedSnapshotByLocal; } base.OnUpdate(); }
protected override void OnUpdate() { if (_isServer) { this.PredictingTick = World.GetExistingSystem <ServerSimulationSystemGroup>().Tick; NetDebug.ServerTick = this.PredictingTick; base.OnUpdate(); return; } //==================================================================================== IsFixError = false; NetworkSnapshotAckComponent ack = GetSingleton <NetworkSnapshotAckComponent>(); uint targetTick = _tickSimulationSystemGroup.ServerTick; if (!SequenceHelpers.IsNewer(targetTick, ack.LastReceivedSnapshotByLocal)) { return; } if (!SequenceHelpers.IsNewer(ack.LastReceivedSnapshotByLocal, LastAppliedSnapshotTick)) { LitUpdate(targetTick); return; } LastAppliedSnapshotTick = ack.LastReceivedSnapshotByLocal; if (targetTick - LastAppliedSnapshotTick > GlobalConstants.CommandDataMaxSize) { LastAppliedSnapshotTick = targetTick - GlobalConstants.CommandDataMaxSize; } IsRewind = true; // Debug.Log($"开始回滚:{LastAppliedSnapshotTick + 1} to {targetTick}, PredictingTick={PredictingTick}"); for (uint i = LastAppliedSnapshotTick + 1; i != targetTick; ++i) { LitUpdate(i); } IsRewind = false; // Debug.Log($"回滚结束:{PredictingTick}"); IsFixError = true; LitUpdate(targetTick); }
public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex) { var entities = chunk.GetNativeArray(entityType); var commandTargets = chunk.GetNativeArray(commandTargetType); var snapshotAckComponents = chunk.GetNativeArray(snapshotAckType); var inBuffer = chunk.GetBufferAccessor(inBufferType); for (int i = 0; i < entities.Length; i++) { if (commandTargets[i].Target == Entity.Null) { continue; } DynamicBuffer <TCommandData> cmdBuf = commandDatas[commandTargets[i].Target]; NetworkSnapshotAckComponent snapshotAck = snapshotAckComponents[i]; DataStreamReader reader = inBuffer[i].AsDataStreamReader(); while (reader.GetBytesRead() < reader.Length) { uint tick = reader.ReadUInt(); var serilaizer = default(TCommandSerializer); TCommandData cmd = default; cmd.Tick = tick; serilaizer.Deserialize(ref reader, ref cmd); int age = (int)(serverTick - cmd.Tick); age *= 256; snapshotAck.ServerCommandAge = (snapshotAck.ServerCommandAge * 7 + age) / 8; cmdBuf.AddCommandData(cmd); // 读取冗余输入 for (uint j = 1; j <= GlobalConstants.InputBufferSendSize; ++j) { cmd.Tick = tick - j; serilaizer.Deserialize(ref reader, ref cmd); cmdBuf.AddCommandData(cmd); } } snapshotAckComponents[i] = snapshotAck; inBuffer[i].Clear(); } }