AxibugEmuOnline/AxibugEmuOnline.Client/Assets/Plugins/AxiReplay/NetReplay.cs

85 lines
2.9 KiB
C#
Raw Normal View History

using System.Collections.Generic;
namespace AxiReplay
{
public class NetReplay
{
2024-11-11 13:58:27 +08:00
/// <summary>
/// 客户端当前帧
/// </summary>
2024-11-11 15:40:21 +08:00
public int mCurrClientFrameIdx => mCurrReplay.FrameStartID;
2024-09-13 13:52:37 +08:00
/// <summary>
/// 服务器远端当前帧
/// </summary>
2024-11-11 16:31:19 +08:00
public int mRemoteFrameIdx { get; private set; } = int.MinValue;
2024-11-11 20:09:54 +08:00
/// <summary>
/// Remote 2 Client Frame Gap
/// </summary>
public int mDiffFrameCount => mRemoteFrameIdx - mCurrClientFrameIdx;
2024-11-11 13:58:27 +08:00
/// <summary>
/// 网络数据队列
/// </summary>
Queue<ReplayStep> mNetReplayQueue = new Queue<ReplayStep>();
2024-09-13 13:52:37 +08:00
/// <summary>
2024-11-11 13:58:27 +08:00
/// 当前数据
2024-09-13 13:52:37 +08:00
/// </summary>
2024-11-11 13:58:27 +08:00
ReplayStep mCurrReplay;
2024-11-11 15:40:21 +08:00
/// <summary>
/// 下一个数据数据
/// </summary>
ReplayStep mNextReplay;
public NetReplay()
{
2024-11-11 13:58:27 +08:00
ResetData();
}
2024-09-14 17:43:08 +08:00
public void ResetData()
{
2024-11-11 13:58:27 +08:00
mNetReplayQueue.Clear();
2024-09-14 17:43:08 +08:00
mRemoteFrameIdx = 0;
mCurrReplay = default(ReplayStep);
2024-11-11 16:39:41 +08:00
mCurrReplay.FrameStartID = int.MinValue;
2024-11-11 15:40:21 +08:00
mNextReplay = default(ReplayStep);
2024-11-11 16:31:19 +08:00
mNextReplay.FrameStartID = 0;
2024-09-14 17:43:08 +08:00
}
2024-11-11 11:39:38 +08:00
public void InData(ReplayStep inputData, int ServerFrameIdx)
{
2024-11-11 13:58:27 +08:00
mNetReplayQueue.Enqueue(inputData);
2024-09-14 17:43:08 +08:00
mRemoteFrameIdx = inputData.FrameStartID;
}
2024-11-11 13:58:27 +08:00
public bool TryGetNextFrame(out ReplayStep data, out int frameDiff, out bool inputDiff)
{
2024-11-11 13:58:27 +08:00
TakeFrame(1, out data, out frameDiff, out inputDiff);
return frameDiff > 0;
}
2024-11-12 09:56:07 +08:00
public bool TryGetNextFrame(int targetFrame, out ReplayStep data, out int frameDiff, out bool inputDiff)
{
TakeFrameToTargetFrame(targetFrame, out data, out frameDiff, out inputDiff);
return frameDiff > 0;
}
2024-11-11 13:58:27 +08:00
void TakeFrame(int addFrame, out ReplayStep data, out int bFrameDiff, out bool inputDiff)
{
2024-11-11 15:40:21 +08:00
int targetFrame = mCurrClientFrameIdx + addFrame;
2024-11-12 09:56:07 +08:00
TakeFrameToTargetFrame(targetFrame, out data, out bFrameDiff, out inputDiff);
}
void TakeFrameToTargetFrame(int targetFrame, out ReplayStep data, out int bFrameDiff, out bool inputDiff)
{
inputDiff = false;
2024-11-11 16:39:41 +08:00
if (targetFrame <= mNextReplay.FrameStartID + 1 && targetFrame <= mRemoteFrameIdx && mNetReplayQueue.Count > 0)
{
2024-11-11 13:58:27 +08:00
//当前帧追加
ulong oldInput = mCurrReplay.InPut;
2024-11-11 15:40:21 +08:00
mCurrReplay = mNextReplay;
2024-11-11 13:58:27 +08:00
if (oldInput != mCurrReplay.InPut)
inputDiff = true;
2024-11-11 15:40:21 +08:00
mNextReplay = mNetReplayQueue.Dequeue();
}
2024-11-11 19:21:35 +08:00
2024-11-11 13:58:27 +08:00
bFrameDiff = mRemoteFrameIdx - mCurrClientFrameIdx;
data = mCurrReplay;
}
}
}