NetReplay

This commit is contained in:
sin365 2024-11-11 13:58:27 +08:00
parent 960aad4831
commit ea2e63cb58
2 changed files with 31 additions and 63 deletions

View File

@ -4,89 +4,57 @@ namespace AxiReplay
{ {
public class NetReplay public class NetReplay
{ {
public int mCurrPlayFrame = -1; /// <summary>
Queue<ReplayStep> mQueueReplay; /// 客户端当前帧
ReplayStep mNextReplay; /// </summary>
ReplayStep mCurrReplay; public int mCurrClientFrameIdx { get; private set; } = -1;
int byFrameIdx = 0;
/// <summary> /// <summary>
/// 服务器远端当前帧 /// 服务器远端当前帧
/// </summary> /// </summary>
public int mRemoteFrameIdx { get; private set; } public int mRemoteFrameIdx { get; private set; } = -1;
/// <summary> /// <summary>
/// 当前帧和服务器帧相差数量 /// 网络数据队列
/// </summary> /// </summary>
public int remoteFrameDiff => mRemoteFrameIdx - mCurrPlayFrame; Queue<ReplayStep> mNetReplayQueue = new Queue<ReplayStep>();
/// <summary>
/// 当前数据
/// </summary>
ReplayStep mCurrReplay;
public NetReplay() public NetReplay()
{ {
mQueueReplay = new Queue<ReplayStep>(); ResetData();
} }
public void ResetData() public void ResetData()
{ {
mQueueReplay.Clear(); mNetReplayQueue.Clear();
mRemoteFrameIdx = 0; mRemoteFrameIdx = 0;
byFrameIdx = 0;
mNextReplay = default(ReplayStep);
mCurrReplay = default(ReplayStep); mCurrReplay = default(ReplayStep);
} }
public void InData(ReplayStep inputData, int ServerFrameIdx) public void InData(ReplayStep inputData, int ServerFrameIdx)
{ {
mQueueReplay.Enqueue(inputData); mNetReplayQueue.Enqueue(inputData);
mRemoteFrameIdx = inputData.FrameStartID; mRemoteFrameIdx = inputData.FrameStartID;
} }
public bool TryGetNextFrame(out ReplayStep data, out int frameDiff, out bool inputDiff)
public bool NextFrame(out ReplayStep data, out int FrameDiff)
{ {
return TakeFrame(0, out data, out FrameDiff); TakeFrame(1, out data, out frameDiff, out inputDiff);
return frameDiff > 0;
} }
/// <summary> void TakeFrame(int addFrame, out ReplayStep data, out int bFrameDiff, out bool inputDiff)
/// 往前推进帧的,指定帧下标
/// </summary>
public bool NextFramebyFrameIdx(int FrameID, out ReplayStep data, out int FrameDiff)
{ {
bool res = TakeFrame(FrameID - byFrameIdx, out data, out FrameDiff); inputDiff = false;
byFrameIdx = FrameID; int targetFrame = mCurrClientFrameIdx += addFrame;
return res; if (targetFrame <= mRemoteFrameIdx && mNetReplayQueue.Count > 0)
}
public bool TakeFrame(int addFrame, out ReplayStep data, out int FrameDiff)
{
bool Changed = false;
mCurrPlayFrame += addFrame;
if (mCurrPlayFrame >= mNextReplay.FrameStartID)
{ {
Changed = mCurrReplay.InPut != mNextReplay.InPut; //当前帧追加
mCurrReplay = mNextReplay; mCurrClientFrameIdx = targetFrame;
data = mCurrReplay; ulong oldInput = mCurrReplay.InPut;
UpdateNextFrame(mCurrPlayFrame, out FrameDiff); mCurrReplay = mNetReplayQueue.Dequeue();
} if (oldInput != mCurrReplay.InPut)
else inputDiff = true;
{
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++;
} }
bFrameDiff = mRemoteFrameIdx - mCurrClientFrameIdx;
data = mCurrReplay;
} }
} }
} }

View File

@ -102,12 +102,12 @@ namespace AxibugEmuOnline.Client
{ {
if (InGameUI.Instance.IsNetPlay) 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); m_sampledState = FromNet(replayData);
var localState = NesControllerMapper.Get().CreateState(); var localState = NesControllerMapper.Get().CreateState();
var rawData = ToNet(localState); var rawData = ToNet(localState);
App.roomMgr.SendRoomSingelPlayerInput((uint)App.roomMgr.netReplay.mCurrPlayFrame, rawData); App.roomMgr.SendRoomSingelPlayerInput((uint)App.roomMgr.netReplay.mCurrClientFrameIdx, rawData);
} }
else else
{ {