demo recording stuff p2
This commit is contained in:
92
Source/Game/PlayerInputDemo.cs
Normal file
92
Source/Game/PlayerInputDemo.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Runtime.InteropServices;
|
||||
using FlaxEngine;
|
||||
using Console = Cabrito.Console;
|
||||
|
||||
namespace Game
|
||||
{
|
||||
public class PlayerInputDemo : PlayerInput
|
||||
{
|
||||
protected List<PlayerInputState> buffer = new List<PlayerInputState>();
|
||||
protected IEnumerator<PlayerInputState> bufferEnumerable;
|
||||
|
||||
public PlayerInputDemo(string demoPath)
|
||||
{
|
||||
Console.Print("demo?");
|
||||
if (!File.Exists(demoPath))
|
||||
return;
|
||||
|
||||
var expectedPlayerInputStateSize = Marshal.SizeOf(typeof(PlayerInputState));
|
||||
|
||||
var stream = File.OpenRead(demoPath);
|
||||
var ver = (int)stream.ReadByte();
|
||||
var inputStateSize = (int)stream.ReadByte();
|
||||
if (ver != DemoVer && inputStateSize != expectedPlayerInputStateSize)
|
||||
{
|
||||
Console.Print("demover mismatch: version " + ver + " != " + DemoVer + ", inputStateSize " + inputStateSize + " != " + Marshal.SizeOf(typeof(PlayerInputState)));
|
||||
stream.Close();
|
||||
return;
|
||||
}
|
||||
|
||||
T RawDeserialize<T>(byte[] rawData, int position)
|
||||
{
|
||||
int rawsize = Marshal.SizeOf(typeof(T));
|
||||
if (rawsize > rawData.Length - position)
|
||||
throw new ArgumentException("Not enough data to fill struct. Array length from position: "+(rawData.Length-position) + ", Struct length: "+rawsize);
|
||||
IntPtr buffer = Marshal.AllocHGlobal(rawsize);
|
||||
Marshal.Copy(rawData, position, buffer, rawsize);
|
||||
T retobj = (T)Marshal.PtrToStructure(buffer, typeof(T));
|
||||
Marshal.FreeHGlobal(buffer);
|
||||
return retobj;
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
byte[] b = new byte[expectedPlayerInputStateSize];
|
||||
var readBytes = stream.Read(b, 0, b.Length);
|
||||
if (readBytes < expectedPlayerInputStateSize)
|
||||
break;
|
||||
|
||||
buffer.Add(RawDeserialize<PlayerInputState>(b, 0));
|
||||
}
|
||||
|
||||
bufferEnumerable = buffer.GetEnumerator();
|
||||
|
||||
Console.Print("demo numstates: " + buffer.Count);
|
||||
}
|
||||
|
||||
public override void OnUpdate()
|
||||
{
|
||||
lastState = currentState;
|
||||
}
|
||||
|
||||
public override void OnFixedUpdate()
|
||||
{
|
||||
}
|
||||
|
||||
public override void OnEndFrame()
|
||||
{
|
||||
// TODO: check if the current state frame matches the current frame number before advancing
|
||||
|
||||
if (bufferEnumerable == null || !bufferEnumerable.MoveNext())
|
||||
{
|
||||
if (buffer.Any())
|
||||
{
|
||||
bufferEnumerable.Dispose();
|
||||
bufferEnumerable = null;
|
||||
buffer.Clear();
|
||||
Console.Print("Demo ended");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
//var actorState = currentState.actor;
|
||||
currentState.input = bufferEnumerable.Current;
|
||||
//currentState.actor = actorState;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user