2022-12-04 19:19:07 +02:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using Unity.Collections;
|
|
|
|
|
|
using UnityGGPO;
|
|
|
|
|
|
|
2022-12-08 03:28:59 +02:00
|
|
|
|
namespace SharedGame
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
|
2022-12-08 03:28:59 +02:00
|
|
|
|
public class RollbackRunner : IGameRunner
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
private NativeArray<byte> buffer = new NativeArray<byte>();
|
|
|
|
|
|
|
|
|
|
|
|
private NativeArray<byte>[] buffers = new NativeArray<byte>[8];
|
|
|
|
|
|
private long[][] inputss = new long[8][];
|
|
|
|
|
|
|
|
|
|
|
|
private int targetFrame = 0;
|
|
|
|
|
|
|
|
|
|
|
|
public IGame Game { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
public GameInfo GameInfo { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
private Stopwatch frameWatch = new Stopwatch();
|
|
|
|
|
|
private Stopwatch idleWatch = new Stopwatch();
|
|
|
|
|
|
|
2022-12-08 03:28:59 +02:00
|
|
|
|
public void Idle(int ms)
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
idleWatch.Start();
|
|
|
|
|
|
Utils.Sleep(ms);
|
|
|
|
|
|
idleWatch.Stop();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-08 03:28:59 +02:00
|
|
|
|
public void RunFrame()
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
++targetFrame;
|
|
|
|
|
|
frameWatch.Start();
|
|
|
|
|
|
var inputs = new long[GameInfo.players.Length];
|
2022-12-08 03:28:59 +02:00
|
|
|
|
for (int i = 0; i < inputs.Length; ++i)
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
inputs[i] = Game.ReadInputs(GameInfo.players[i].controllerId);
|
|
|
|
|
|
}
|
|
|
|
|
|
Game.Update(inputs, 0);
|
|
|
|
|
|
frameWatch.Stop();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-08 03:28:59 +02:00
|
|
|
|
public void OnTestSave()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (buffer.IsCreated)
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
buffer.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
buffer = Game.ToBytes();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-08 03:28:59 +02:00
|
|
|
|
public void OnTestLoad()
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
Game.FromBytes(buffer);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-08 03:28:59 +02:00
|
|
|
|
private void Rotate()
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
buffers[0].Dispose();
|
2022-12-08 03:28:59 +02:00
|
|
|
|
for (int i = 1; i < buffers.Length; ++i)
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
buffers[i - 1] = buffers[i];
|
|
|
|
|
|
}
|
|
|
|
|
|
buffers[buffers.Length - 1] = default;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-08 03:28:59 +02:00
|
|
|
|
private void Rollback(int frames)
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
int cf = targetFrame;
|
|
|
|
|
|
int f = targetFrame - frames;
|
|
|
|
|
|
|
2022-12-08 03:28:59 +02:00
|
|
|
|
for (int i = 0; i < frames; ++i)
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
var inputs = this.inputss[i];
|
|
|
|
|
|
Game.FromBytes(buffers[i]);
|
|
|
|
|
|
Game.Update(inputs, 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-08 03:28:59 +02:00
|
|
|
|
public RollbackRunner(IGame game)
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
Game = game;
|
|
|
|
|
|
GameInfo = new GameInfo();
|
|
|
|
|
|
int handle = 1;
|
|
|
|
|
|
int controllerId = 0;
|
|
|
|
|
|
GameInfo.players = new PlayerConnectionInfo[2];
|
2022-12-08 03:28:59 +02:00
|
|
|
|
GameInfo.players[0] = new PlayerConnectionInfo
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
handle = handle,
|
|
|
|
|
|
type = GGPOPlayerType.GGPO_PLAYERTYPE_LOCAL,
|
|
|
|
|
|
connect_progress = 100,
|
|
|
|
|
|
controllerId = controllerId
|
|
|
|
|
|
};
|
|
|
|
|
|
GameInfo.SetConnectState(handle, PlayerConnectState.Connecting);
|
|
|
|
|
|
++handle;
|
|
|
|
|
|
++controllerId;
|
2022-12-08 03:28:59 +02:00
|
|
|
|
GameInfo.players[1] = new PlayerConnectionInfo
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
handle = handle,
|
|
|
|
|
|
type = GGPOPlayerType.GGPO_PLAYERTYPE_LOCAL,
|
|
|
|
|
|
connect_progress = 100,
|
|
|
|
|
|
controllerId = controllerId++
|
|
|
|
|
|
};
|
|
|
|
|
|
GameInfo.SetConnectState(handle, PlayerConnectState.Connecting);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-08 03:28:59 +02:00
|
|
|
|
public StatusInfo GetStatus(Stopwatch updateWatch)
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
var status = new StatusInfo();
|
|
|
|
|
|
status.idlePerc = (float)idleWatch.ElapsedMilliseconds / (float)updateWatch.ElapsedMilliseconds;
|
|
|
|
|
|
status.updatePerc = (float)frameWatch.ElapsedMilliseconds / (float)updateWatch.ElapsedMilliseconds;
|
|
|
|
|
|
status.periodic = GameInfo.periodic;
|
|
|
|
|
|
status.now = GameInfo.now;
|
|
|
|
|
|
return status;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-08 03:28:59 +02:00
|
|
|
|
public void DisconnectPlayer(int player)
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-08 03:28:59 +02:00
|
|
|
|
public void Shutdown()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (buffer.IsCreated)
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
buffer.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-08 03:28:59 +02:00
|
|
|
|
public class LocalRunner : IGameRunner
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
private NativeArray<byte> buffer;
|
|
|
|
|
|
|
|
|
|
|
|
public IGame Game { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
public GameInfo GameInfo { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
private Stopwatch frameWatch = new Stopwatch();
|
|
|
|
|
|
private Stopwatch idleWatch = new Stopwatch();
|
|
|
|
|
|
|
2022-12-08 03:28:59 +02:00
|
|
|
|
public void Idle(int ms)
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
idleWatch.Start();
|
|
|
|
|
|
Utils.Sleep(ms);
|
|
|
|
|
|
idleWatch.Stop();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-08 03:28:59 +02:00
|
|
|
|
public void RunFrame()
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
frameWatch.Start();
|
|
|
|
|
|
var inputs = new long[GameInfo.players.Length];
|
2022-12-08 03:28:59 +02:00
|
|
|
|
for (int i = 0; i < inputs.Length; ++i)
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
inputs[i] = Game.ReadInputs(GameInfo.players[i].controllerId);
|
|
|
|
|
|
}
|
|
|
|
|
|
Game.Update(inputs, 0);
|
|
|
|
|
|
frameWatch.Stop();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-08 03:28:59 +02:00
|
|
|
|
public void OnTestSave()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (buffer.IsCreated)
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
buffer.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
buffer = Game.ToBytes();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-08 03:28:59 +02:00
|
|
|
|
public void OnTestLoad()
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
Game.FromBytes(buffer);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-08 03:28:59 +02:00
|
|
|
|
public LocalRunner(IGame game)
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
Game = game;
|
|
|
|
|
|
GameInfo = new GameInfo();
|
|
|
|
|
|
int handle = 1;
|
|
|
|
|
|
int controllerId = 0;
|
|
|
|
|
|
GameInfo.players = new PlayerConnectionInfo[2];
|
2022-12-08 03:28:59 +02:00
|
|
|
|
GameInfo.players[0] = new PlayerConnectionInfo
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
handle = handle,
|
|
|
|
|
|
type = GGPOPlayerType.GGPO_PLAYERTYPE_LOCAL,
|
|
|
|
|
|
connect_progress = 100,
|
|
|
|
|
|
controllerId = controllerId
|
|
|
|
|
|
};
|
|
|
|
|
|
GameInfo.SetConnectState(handle, PlayerConnectState.Connecting);
|
|
|
|
|
|
++handle;
|
|
|
|
|
|
++controllerId;
|
2022-12-08 03:28:59 +02:00
|
|
|
|
GameInfo.players[1] = new PlayerConnectionInfo
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
handle = handle,
|
|
|
|
|
|
type = GGPOPlayerType.GGPO_PLAYERTYPE_LOCAL,
|
|
|
|
|
|
connect_progress = 100,
|
|
|
|
|
|
controllerId = controllerId++
|
|
|
|
|
|
};
|
|
|
|
|
|
GameInfo.SetConnectState(handle, PlayerConnectState.Connecting);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-08 03:28:59 +02:00
|
|
|
|
public StatusInfo GetStatus(Stopwatch updateWatch)
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
var status = new StatusInfo();
|
|
|
|
|
|
status.idlePerc = (float)idleWatch.ElapsedMilliseconds / (float)updateWatch.ElapsedMilliseconds;
|
|
|
|
|
|
status.updatePerc = (float)frameWatch.ElapsedMilliseconds / (float)updateWatch.ElapsedMilliseconds;
|
|
|
|
|
|
status.periodic = GameInfo.periodic;
|
|
|
|
|
|
status.now = GameInfo.now;
|
|
|
|
|
|
return status;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-08 03:28:59 +02:00
|
|
|
|
public void DisconnectPlayer(int player)
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-08 03:28:59 +02:00
|
|
|
|
public void Shutdown()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (buffer.IsCreated)
|
|
|
|
|
|
{
|
2022-12-04 19:19:07 +02:00
|
|
|
|
buffer.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|