diff --git a/AxibugEmuOnline.Client/Assets/Plugins/AxiReplay/NetReplay.cs b/AxibugEmuOnline.Client/Assets/Plugins/AxiReplay/NetReplay.cs index 1670fc6..a94ea03 100644 --- a/AxibugEmuOnline.Client/Assets/Plugins/AxiReplay/NetReplay.cs +++ b/AxibugEmuOnline.Client/Assets/Plugins/AxiReplay/NetReplay.cs @@ -4,89 +4,57 @@ namespace AxiReplay { public class NetReplay { - public int mCurrPlayFrame = -1; - Queue mQueueReplay; - ReplayStep mNextReplay; - ReplayStep mCurrReplay; - int byFrameIdx = 0; + /// + /// 客户端当前帧 + /// + public int mCurrClientFrameIdx { get; private set; } = -1; /// /// 服务器远端当前帧 /// - public int mRemoteFrameIdx { get; private set; } + public int mRemoteFrameIdx { get; private set; } = -1; /// - /// 当前帧和服务器帧相差数量 + /// 网络数据队列 /// - public int remoteFrameDiff => mRemoteFrameIdx - mCurrPlayFrame; + Queue mNetReplayQueue = new Queue(); + /// + /// 当前数据 + /// + ReplayStep mCurrReplay; public NetReplay() { - mQueueReplay = new Queue(); + ResetData(); } - public void ResetData() { - mQueueReplay.Clear(); + mNetReplayQueue.Clear(); mRemoteFrameIdx = 0; - byFrameIdx = 0; - mNextReplay = default(ReplayStep); mCurrReplay = default(ReplayStep); } - public void InData(ReplayStep inputData, int ServerFrameIdx) { - mQueueReplay.Enqueue(inputData); + mNetReplayQueue.Enqueue(inputData); mRemoteFrameIdx = inputData.FrameStartID; } - - public bool NextFrame(out ReplayStep data, out int FrameDiff) + public bool TryGetNextFrame(out ReplayStep data, out int frameDiff, out bool inputDiff) { - return TakeFrame(0, out data, out FrameDiff); + TakeFrame(1, out data, out frameDiff, out inputDiff); + return frameDiff > 0; } - /// - /// 往前推进帧的,指定帧下标 - /// - public bool NextFramebyFrameIdx(int FrameID, out ReplayStep data, out int FrameDiff) + void TakeFrame(int addFrame, out ReplayStep data, out int bFrameDiff, out bool inputDiff) { - bool res = TakeFrame(FrameID - byFrameIdx, out data, out FrameDiff); - byFrameIdx = FrameID; - return res; - } - public bool TakeFrame(int addFrame, out ReplayStep data, out int FrameDiff) - { - bool Changed = false; - mCurrPlayFrame += addFrame; - if (mCurrPlayFrame >= mNextReplay.FrameStartID) + inputDiff = false; + int targetFrame = mCurrClientFrameIdx += addFrame; + if (targetFrame <= mRemoteFrameIdx && mNetReplayQueue.Count > 0) { - Changed = mCurrReplay.InPut != mNextReplay.InPut; - mCurrReplay = mNextReplay; - data = mCurrReplay; - UpdateNextFrame(mCurrPlayFrame, out FrameDiff); - } - else - { - data = mCurrReplay; - FrameDiff = mRemoteFrameIdx - mCurrPlayFrame; - } - return Changed; - } - void UpdateNextFrame(int targetFrame, out int FrameDiff) - { - FrameDiff = mRemoteFrameIdx - targetFrame; - //如果已经超过 - while (targetFrame > mNextReplay.FrameStartID) - { - if (mNextReplay.FrameStartID >= mRemoteFrameIdx) - { - //TODO - //bEnd = true; - break; - } - - if (mQueueReplay.Count > 0) - { - mNextReplay = mQueueReplay.Dequeue(); - } - targetFrame++; + //当前帧追加 + mCurrClientFrameIdx = targetFrame; + ulong oldInput = mCurrReplay.InPut; + mCurrReplay = mNetReplayQueue.Dequeue(); + if (oldInput != mCurrReplay.InPut) + inputDiff = true; } + bFrameDiff = mRemoteFrameIdx - mCurrClientFrameIdx; + data = mCurrReplay; } } } diff --git a/AxibugEmuOnline.Client/Assets/Script/NesEmulator/CoreSupporter.cs b/AxibugEmuOnline.Client/Assets/Script/NesEmulator/CoreSupporter.cs index 29f527f..6ef8c4b 100644 --- a/AxibugEmuOnline.Client/Assets/Script/NesEmulator/CoreSupporter.cs +++ b/AxibugEmuOnline.Client/Assets/Script/NesEmulator/CoreSupporter.cs @@ -102,12 +102,12 @@ namespace AxibugEmuOnline.Client { if (InGameUI.Instance.IsNetPlay) { - if (App.roomMgr.netReplay.NextFrame(out var replayData, out int _)) + if (App.roomMgr.netReplay.TryGetNextFrame(out var replayData, out int frameDiff, out bool inputDiff)) { m_sampledState = FromNet(replayData); var localState = NesControllerMapper.Get().CreateState(); var rawData = ToNet(localState); - App.roomMgr.SendRoomSingelPlayerInput((uint)App.roomMgr.netReplay.mCurrPlayFrame, rawData); + App.roomMgr.SendRoomSingelPlayerInput((uint)App.roomMgr.netReplay.mCurrClientFrameIdx, rawData); } else {