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

128 lines
4.0 KiB
C#
Raw Normal View History

2024-11-12 14:55:45 +08:00
using System;
using System.Collections.Generic;
namespace AxiReplay
{
public class NetReplay
{
2024-11-11 13:58:27 +08:00
/// <summary>
/// 客户端当前帧
/// </summary>
2024-11-12 12:49:58 +08:00
public int mCurrClientFrameIdx = int.MinValue;
2024-09-13 13:52:37 +08:00
/// <summary>
/// 服务器远端当前帧
/// </summary>
2024-11-12 09:59:09 +08:00
public int mRemoteFrameIdx { get; private set; }
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;
2024-11-12 12:49:58 +08:00
bool bNetInit = false;
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
mCurrReplay = default(ReplayStep);
2024-11-11 16:39:41 +08:00
mCurrReplay.FrameStartID = int.MinValue;
2024-11-12 12:49:58 +08:00
bNetInit = false;
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-12 12:49:58 +08:00
if (!bNetInit)
{
bNetInit = true;
mNextReplay = mNetReplayQueue.Dequeue();
}
}
2024-11-11 13:58:27 +08:00
public bool TryGetNextFrame(out ReplayStep data, out int frameDiff, out bool inputDiff)
{
2024-11-12 12:49:58 +08:00
if (!bNetInit)
{
data = default(ReplayStep);
frameDiff = default;
inputDiff = false;
return false;
}
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)
{
2024-11-12 12:49:58 +08:00
if (!bNetInit)
{
data = default(ReplayStep);
frameDiff = default;
inputDiff = false;
return false;
}
return TakeFrameToTargetFrame(targetFrame, out data, out frameDiff, out inputDiff);
2024-11-12 09:56:07 +08:00
}
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);
}
2024-11-12 12:49:58 +08:00
bool TakeFrameToTargetFrame(int targetFrame, out ReplayStep data, out int bFrameDiff, out bool inputDiff)
2024-11-12 09:56:07 +08:00
{
2024-11-12 12:49:58 +08:00
bool result;
2024-11-12 09:56:07 +08:00
inputDiff = false;
2024-11-12 12:49:58 +08:00
if (targetFrame == mNextReplay.FrameStartID && targetFrame <= mRemoteFrameIdx && mNetReplayQueue.Count > 0)
{
2024-11-11 13:58:27 +08:00
//当前帧追加
2024-11-12 12:49:58 +08:00
mCurrClientFrameIdx = targetFrame;
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-12 12:49:58 +08:00
result = true;
}
2024-11-12 12:49:58 +08:00
else
result = false;
2024-11-11 19:21:35 +08:00
2024-11-11 13:58:27 +08:00
bFrameDiff = mRemoteFrameIdx - mCurrClientFrameIdx;
data = mCurrReplay;
2024-11-12 12:49:58 +08:00
return result;
2024-11-12 14:55:45 +08:00
}
public int GetSkipFrameCount()
{
var frameGap = mDiffFrameCount;
if (frameGap > 10000) return 0;
int skip = 0;
if (frameGap <= 2) skip = 0;
if (frameGap > 2 && frameGap < 6) skip = 1 + 1;
else if (frameGap > 7 && frameGap < 12) skip = 2 + 1;
else if (frameGap > 13 && frameGap < 20) skip = 3 + 1;
else skip = frameGap - 2;
return skip;
}
}
}