From ef419928ed0ea6031e6bfa4e8c1dfb5f165b9160 Mon Sep 17 00:00:00 2001 From: "ALIENJACK\\alien" Date: Tue, 6 May 2025 12:00:47 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4EmuCore=E5=9F=BA=E7=B1=BB?= =?UTF-8?q?=E4=B8=AD=E8=8E=B7=E5=8F=96=E8=BE=93=E5=85=A5=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=92=8C=E6=8E=A8=E9=80=81=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Assets/Plugins/AxiReplay/NetReplay.cs | 38 ++++++--------- .../Assets/Script/AppMain/EmuCore.cs | 48 +++++++++++-------- 2 files changed, 42 insertions(+), 44 deletions(-) diff --git a/AxibugEmuOnline.Client/Assets/Plugins/AxiReplay/NetReplay.cs b/AxibugEmuOnline.Client/Assets/Plugins/AxiReplay/NetReplay.cs index f7656a0e..ce7a74b7 100644 --- a/AxibugEmuOnline.Client/Assets/Plugins/AxiReplay/NetReplay.cs +++ b/AxibugEmuOnline.Client/Assets/Plugins/AxiReplay/NetReplay.cs @@ -65,7 +65,7 @@ namespace AxiReplay frameProfiler.InputHead(inputData.FrameStartID); } - public bool TryGetNextFrame(out ReplayStep data, out int frameDiff, out bool inputDiff) + public bool TryGetNextFrame(int targetFrame, bool indirectGet, out ReplayStep data, out int frameDiff, out bool inputDiff) { if (!bNetInit) { @@ -74,37 +74,27 @@ namespace AxiReplay inputDiff = false; return false; } - TakeFrame(1, out data, out frameDiff, out inputDiff); - return frameDiff > 0; + return TakeFrameToTargetFrame(targetFrame, indirectGet, out data, out frameDiff, out inputDiff); } - public bool TryGetNextFrame(int targetFrame, out ReplayStep data, out int frameDiff, out bool inputDiff) - { - if (!bNetInit) - { - data = default(ReplayStep); - frameDiff = default(int); - inputDiff = false; - return false; - } - return TakeFrameToTargetFrame(targetFrame, out data, out frameDiff, out inputDiff); + bool checkCanGetFrame(int targetFrame, bool indirectGet) + { + if (indirectGet) + { + return targetFrame == mNextReplay.FrameStartID && targetFrame <= mRemoteFrameIdx; + } + else + { + return targetFrame == mNextReplay.FrameStartID && targetFrame <= mRemoteFrameIdx && mNetReplayQueue.Count >= frameProfiler.TempFrameCount(mRemoteForwardCount); + } } - void TakeFrame(int addFrame, out ReplayStep data, out int bFrameDiff, out bool inputDiff) - { - int targetFrame = mCurrClientFrameIdx + addFrame; - TakeFrameToTargetFrame(targetFrame, out data, out bFrameDiff, out inputDiff); - } - - bool TakeFrameToTargetFrame(int targetFrame, out ReplayStep data, out int bFrameDiff, out bool inputDiff) + bool TakeFrameToTargetFrame(int targetFrame, bool indirectGet, out ReplayStep data, out int bFrameDiff, out bool inputDiff) { bool result; inputDiff = false; - - //if (targetFrame == mNextReplay.FrameStartID && targetFrame <= mRemoteFrameIdx && mNetReplayQueue.Count > 0) - //if (targetFrame == mNextReplay.FrameStartID && targetFrame <= mRemoteFrameIdx && mNetReplayQueue.Count >= mRemoteForwardCount) - if (targetFrame == mNextReplay.FrameStartID && targetFrame <= mRemoteFrameIdx && mNetReplayQueue.Count >= frameProfiler.TempFrameCount(mRemoteForwardCount)) + if (checkCanGetFrame(targetFrame, indirectGet)) { //当前帧追加 mCurrClientFrameIdx = targetFrame; diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/EmuCore.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/EmuCore.cs index 8ac2f053..ef893e45 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AppMain/EmuCore.cs +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/EmuCore.cs @@ -60,8 +60,11 @@ namespace AxibugEmuOnline.Client } } + /// 输入数据类型 public abstract class EmuCore : EmuCore { + protected virtual bool EnableRollbackNetCode => false; + public sealed override void PushEmulatorFrame() { if (!TryPushEmulatorFrame()) return; @@ -83,13 +86,31 @@ namespace AxibugEmuOnline.Client { if (SampleInputData(out var inputData)) { + if (IsNetPlay) SendLocalInput(); + return OnPushEmulatorFrame(inputData); } return false; } + private void SendLocalInput() + { + var localState = GetLocalInput(); + var rawData = InputDataToNet(localState); + App.roomMgr.SendRoomSingelPlayerInput(Frame, rawData); + + if (m_lastTestInput != rawData) + { + m_lastTestInput = rawData; + App.log.Debug($"{DateTime.Now.ToString("hh:mm:ss.fff")} Input F:{App.roomMgr.netReplay.mCurrClientFrameIdx} | I:{rawData}"); + } + } + ulong m_lastTestInput; + ReplayStep m_replayData; + int m_frameDiff; + bool m_inputDiff; protected bool SampleInputData(out INPUTDATA inputData) { bool result = false; @@ -97,34 +118,21 @@ namespace AxibugEmuOnline.Client if (IsNetPlay) { - ReplayStep replayData; - int frameDiff; - bool inputDiff; - - if (App.roomMgr.netReplay.TryGetNextFrame((int)Frame, out replayData, out frameDiff, out inputDiff)) - { - if (inputDiff) - { - App.log.Debug($"{DateTime.Now.ToString("hh:mm:ss.fff")} TryGetNextFrame remoteFrame->{App.roomMgr.netReplay.mRemoteFrameIdx} diff->{frameDiff} " + - $"frame=>{replayData.FrameStartID} InPut=>{replayData.InPut}"); + if (App.roomMgr.netReplay.TryGetNextFrame((int)Frame, EnableRollbackNetCode ? true : false, out m_replayData, out m_frameDiff, out m_inputDiff)) + { + if (m_inputDiff) + { + App.log.Debug($"{DateTime.Now.ToString("hh:mm:ss.fff")} TryGetNextFrame remoteFrame->{App.roomMgr.netReplay.mRemoteFrameIdx} diff->{m_frameDiff} " + + $"frame=>{m_replayData.FrameStartID} InPut=>{m_replayData.InPut}"); } - inputData = ConvertInputDataFromNet(replayData); + inputData = ConvertInputDataFromNet(m_replayData); result = true; } else { result = false; } - - var localState = GetLocalInput(); - var rawData = InputDataToNet(localState); - if (m_lastTestInput != rawData) - { - m_lastTestInput = rawData; - App.log.Debug($"{DateTime.Now.ToString("hh:mm:ss.fff")} Input F:{App.roomMgr.netReplay.mCurrClientFrameIdx} | I:{rawData}"); - } - App.roomMgr.SendRoomSingelPlayerInput(Frame, rawData); } else//单机模式 {