demo recording stuff p2
This commit is contained in:
@@ -10,6 +10,7 @@ namespace Game
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct PlayerInputState
|
||||
{
|
||||
public ulong frame;
|
||||
public float viewDeltaX, viewDeltaY;
|
||||
public float moveForward;
|
||||
public float moveRight;
|
||||
@@ -37,106 +38,20 @@ namespace Game
|
||||
{
|
||||
public PlayerState lastState;
|
||||
public PlayerState currentState;
|
||||
public int frame;
|
||||
public ulong frame;
|
||||
|
||||
private const byte DemoVer = 1;
|
||||
public const byte DemoVer = 1;
|
||||
|
||||
private List<PlayerInputState> buffer = new List<PlayerInputState>();
|
||||
|
||||
private bool demoPlayback = false;
|
||||
private bool demoRecording = true;
|
||||
|
||||
private string demoPath = @"C:\dev\GoakeFlax\testdemo.gdem";
|
||||
|
||||
public void OpenDemo()
|
||||
public virtual void OnUpdate()
|
||||
{
|
||||
if (!File.Exists(demoPath))
|
||||
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;
|
||||
}
|
||||
|
||||
var stream = File.OpenRead(demoPath);
|
||||
var ver = (int)stream.ReadByte();
|
||||
if (ver != DemoVer)
|
||||
{
|
||||
Console.WriteLine("demover doesn't match: " + ver + " != " + DemoVer);
|
||||
stream.Close();
|
||||
return;
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
var expectedSize = Marshal.SizeOf(typeof(PlayerInputState));
|
||||
byte[] b = new byte[expectedSize];
|
||||
var readBytes = stream.Read(b, 0, b.Length);
|
||||
if (readBytes < expectedSize)
|
||||
break;
|
||||
|
||||
buffer.Add(RawDeserialize<PlayerInputState>(b, 0));
|
||||
}
|
||||
|
||||
Console.WriteLine("demo numstates: " + buffer.Count);
|
||||
|
||||
demoPlayback = true;
|
||||
demoRecording = false;
|
||||
}
|
||||
|
||||
public void OnUpdate()
|
||||
public virtual void OnFixedUpdate()
|
||||
{
|
||||
lastState = currentState;
|
||||
|
||||
// Record camera angles here?
|
||||
if (!demoPlayback)
|
||||
{
|
||||
currentState.input.viewDeltaX = InputManager.GetAxisRaw("Mouse X");
|
||||
currentState.input.viewDeltaY = InputManager.GetAxisRaw("Mouse Y");
|
||||
}
|
||||
}
|
||||
|
||||
public void OnFixedUpdate()
|
||||
public virtual void OnEndFrame()
|
||||
{
|
||||
// Record intent here
|
||||
|
||||
if (!demoPlayback)
|
||||
{
|
||||
currentState.input.moveForward = InputManager.GetAxis("Vertical");
|
||||
currentState.input.moveRight = InputManager.GetAxis("Horizontal");
|
||||
currentState.input.attacking = InputManager.GetAction("Attack");
|
||||
currentState.input.jumping = InputManager.GetAction("Jump");
|
||||
}
|
||||
}
|
||||
|
||||
public void OnEndFrame()
|
||||
{
|
||||
if (demoRecording)
|
||||
buffer.Add(currentState.input);
|
||||
|
||||
frame++;
|
||||
|
||||
if (demoPlayback)
|
||||
{
|
||||
if (frame < buffer.Count)
|
||||
{
|
||||
//var actorState = currentState.actor;
|
||||
currentState.input = buffer[frame];
|
||||
//currentState.actor = actorState;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("demo ended");
|
||||
demoPlayback = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void RecordCurrentActorState(PlayerActorState actorState)
|
||||
@@ -153,37 +68,5 @@ namespace Game
|
||||
{
|
||||
return currentState.actor;
|
||||
}
|
||||
|
||||
public void Flush()
|
||||
{
|
||||
if (!demoRecording)
|
||||
return;
|
||||
|
||||
var stream = File.Open(demoPath, FileMode.Create, FileAccess.Write);
|
||||
//stream.Position = 0;
|
||||
//stream.SetLength(0);
|
||||
stream.WriteByte(DemoVer);
|
||||
|
||||
byte[] RawSerialize(object anything)
|
||||
{
|
||||
int rawSize = Marshal.SizeOf(anything);
|
||||
IntPtr buffer = Marshal.AllocHGlobal(rawSize);
|
||||
Marshal.StructureToPtr(anything, buffer, false);
|
||||
byte[] rawDatas = new byte[rawSize];
|
||||
Marshal.Copy(buffer, rawDatas, 0, rawSize);
|
||||
Marshal.FreeHGlobal(buffer);
|
||||
return rawDatas;
|
||||
}
|
||||
|
||||
foreach (var state in buffer)
|
||||
{
|
||||
var bytes = RawSerialize(state);
|
||||
stream.Write(bytes, 0, bytes.Length * sizeof(byte));
|
||||
}
|
||||
|
||||
stream.Close();
|
||||
|
||||
Debug.Write(LogType.Info, "demo, wrote states: " + buffer.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
95
Source/Game/PlayerInputLocal.cs
Normal file
95
Source/Game/PlayerInputLocal.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Runtime.InteropServices;
|
||||
using FlaxEngine;
|
||||
|
||||
namespace Game
|
||||
{
|
||||
public class PlayerInputLocal : PlayerInput
|
||||
{
|
||||
protected List<PlayerInputState> buffer = new List<PlayerInputState>();
|
||||
protected FileStream demoFileStream;
|
||||
|
||||
public bool IsRecording { get { return demoFileStream != null; } }
|
||||
|
||||
public PlayerInputLocal()
|
||||
{
|
||||
}
|
||||
|
||||
public PlayerInputLocal(string demoPath)
|
||||
{
|
||||
demoFileStream = File.Open(demoPath, FileMode.Create, FileAccess.Write);
|
||||
//stream.Position = 0;
|
||||
//stream.SetLength(0);
|
||||
demoFileStream.WriteByte(DemoVer);
|
||||
demoFileStream.WriteByte((byte)Marshal.SizeOf(typeof(PlayerInputState)));
|
||||
}
|
||||
|
||||
public override void OnUpdate()
|
||||
{
|
||||
lastState = currentState;
|
||||
|
||||
// Record camera angles here?
|
||||
currentState.input.viewDeltaX = InputManager.GetAxisRaw("Mouse X");
|
||||
currentState.input.viewDeltaY = InputManager.GetAxisRaw("Mouse Y");
|
||||
}
|
||||
|
||||
public override void OnFixedUpdate()
|
||||
{
|
||||
// Record intent here
|
||||
currentState.input.moveForward = InputManager.GetAxis("Vertical");
|
||||
currentState.input.moveRight = InputManager.GetAxis("Horizontal");
|
||||
currentState.input.attacking = InputManager.GetAction("Attack");
|
||||
currentState.input.jumping = InputManager.GetAction("Jump");
|
||||
}
|
||||
|
||||
public override void OnEndFrame()
|
||||
{
|
||||
if (IsRecording)
|
||||
{
|
||||
currentState.input.frame = frame;
|
||||
buffer.Add(currentState.input);
|
||||
}
|
||||
frame++;
|
||||
}
|
||||
|
||||
public void FlushDemo()
|
||||
{
|
||||
if (!IsRecording)
|
||||
return;
|
||||
|
||||
byte[] RawSerialize(object anything)
|
||||
{
|
||||
int rawSize = Marshal.SizeOf(anything);
|
||||
IntPtr buffer = Marshal.AllocHGlobal(rawSize);
|
||||
Marshal.StructureToPtr(anything, buffer, false);
|
||||
byte[] rawDatas = new byte[rawSize];
|
||||
Marshal.Copy(buffer, rawDatas, 0, rawSize);
|
||||
Marshal.FreeHGlobal(buffer);
|
||||
return rawDatas;
|
||||
}
|
||||
|
||||
foreach (var state in buffer)
|
||||
{
|
||||
var bytes = RawSerialize(state);
|
||||
demoFileStream.Write(bytes, 0, bytes.Length * sizeof(byte));
|
||||
}
|
||||
|
||||
buffer.Clear();
|
||||
}
|
||||
|
||||
public void StopRecording()
|
||||
{
|
||||
if (!IsRecording)
|
||||
return;
|
||||
|
||||
FlushDemo();
|
||||
demoFileStream.Close();
|
||||
demoFileStream = null;
|
||||
|
||||
Debug.Write(LogType.Info, "demo, wrote states: " + buffer.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Source/Game/PlayerInputNetwork.cs
Normal file
14
Source/Game/PlayerInputNetwork.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Runtime.InteropServices;
|
||||
using FlaxEngine;
|
||||
|
||||
namespace Game
|
||||
{
|
||||
public class PlayerInputNetwork : PlayerInput
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -52,7 +52,8 @@ namespace Game
|
||||
{
|
||||
base.OnAwake();
|
||||
|
||||
input = new PlayerInput();
|
||||
//input = new PlayerInputLocal(@"C:\dev\GoakeFlax\testdemo.gdem");
|
||||
input = new PlayerInputDemo(@"C:\dev\GoakeFlax\testdemo.gdem");
|
||||
|
||||
onExit.Triggered += () =>
|
||||
{
|
||||
@@ -66,16 +67,14 @@ namespace Game
|
||||
//rigidBody.CollisionEnter += OnCollisionEnter;
|
||||
//rigidBody.TriggerEnter += OnTriggerEnter;
|
||||
//rigidBody.TriggerExit += OnTriggerExit;
|
||||
|
||||
input.OpenDemo();
|
||||
}
|
||||
|
||||
public override void OnDisable()
|
||||
{
|
||||
base.OnDisable();
|
||||
|
||||
if (input != null)
|
||||
input.Flush();
|
||||
if (input != null && input is PlayerInputLocal) // FIXME
|
||||
(input as PlayerInputLocal).StopRecording();
|
||||
}
|
||||
|
||||
private List<PhysicsColliderActor> touchingActors = new List<PhysicsColliderActor>();
|
||||
|
||||
Reference in New Issue
Block a user