Skip to content

Commit

Permalink
Add input recording file selector, wait for network ready
Browse files Browse the repository at this point in the history
  • Loading branch information
jeickhoff committed Jul 31, 2023
1 parent 4fa6f02 commit db241d0
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Assets/Scenes/MainScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 1d8c509f1d3f4dcaab72eb2f27f779b7, type: 3}
m_Name:
m_EditorClassIdentifier:
inputFile: recordedInputs.csv
filename: recordedInputs.inputtrace
inputRecorder: {fileID: 955198409}
emulationType: 2
--- !u!114 &955198407
Expand Down Expand Up @@ -904,7 +904,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 8bf58c69bf424fe194d229f05ce07072, type: 3}
m_Name:
m_EditorClassIdentifier:
editorArgs: -streaming_type host -emulation disabled
editorArgs: -streaming_type host -emulation none
--- !u!1 &2101193216
GameObject:
m_ObjectHideFlags: 0
Expand Down
1 change: 1 addition & 0 deletions Assets/Scripts/CmdArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static StreamingRole ClientStreamingRole

public static bool DebugEnabled = false;
public static bool EmulationEnabled = false;
public static string EmulationFile = "";
public static EmulationType emulationType = EmulationType.None;
public static int seed = -1;

Expand Down
9 changes: 9 additions & 0 deletions Assets/Scripts/CmdArgsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ void OnEnable()
}
}

if (args.TryGetValue("-emulation_file", out string emulationFile))
{
CmdArgs.EmulationFile = emulationFile;
}
else
{
CmdArgs.EmulationFile= Application.persistentDataPath + '\\' +"recordedInputs.inputtrace";
}

// Seed
if (args.TryGetValue("-seed", out string seed))
{
Expand Down
21 changes: 15 additions & 6 deletions Assets/Scripts/Player/Emulated/Emulation.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System;
using System.IO;
using Opencraft.Player.Emulated.InputPlayback;
using UnityEngine;

namespace Opencraft.Player.Emulated
{
public class Emulation : MonoBehaviour
{
public string inputFile = "recordedInputs.inputtrace";
public string filename = "recordedInputs.inputtrace";
public InputRecorder inputRecorder;
public EmulationType emulationType = EmulationType.None;

Expand All @@ -19,9 +20,17 @@ public class Emulation : MonoBehaviour

public void initializePlayback()
{
Debug.Log("Starting input playback!");
//inputRecorder.gameObject.SetActive(true);
inputRecorder.LoadCaptureFromFile(Application.persistentDataPath + '/' +inputFile);
try
{
inputRecorder.LoadCaptureFromFile(CmdArgs.EmulationFile);
}
catch (Exception ex)
{
Debug.Log("Failed to load input playback file with error:");
Debug.LogError(ex);
return;
}
Debug.Log($"Starting input playback from {CmdArgs.EmulationFile}");
inputRecorder.StartReplay();
}

Expand All @@ -36,9 +45,9 @@ private void OnApplicationQuit()
{
if (emulationType == EmulationType.RecordInput)
{
Debug.Log("Saving capture file!");
Debug.Log($"Saving capture file to {Application.persistentDataPath + '/' +filename}");
inputRecorder.StopCapture();
inputRecorder.SaveCaptureToFile(Application.persistentDataPath + '/' + inputFile);
inputRecorder.SaveCaptureToFile(Application.persistentDataPath + '/' + filename);
}

if (emulationType == EmulationType.InputPlayback)
Expand Down
33 changes: 29 additions & 4 deletions Assets/Scripts/Player/Emulated/EmulationInitSystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Unity.Entities;
using Opencraft.Player.Multiplay;
using Unity.Collections;
using Unity.Entities;
using Unity.NetCode;
using Unity.VisualScripting;
using UnityEngine;

Expand All @@ -9,22 +12,44 @@ namespace Opencraft.Player.Emulated
[UpdateInGroup(typeof(InitializationSystemGroup))]
public partial class EmulationInitSystem : SystemBase
{
private EntityQuery connections;
protected override void OnCreate()
{
var builder = new EntityQueryBuilder(Allocator.Temp)
.WithAll<NetworkId, NetworkStreamInGame>();
connections = GetEntityQuery(builder);
}

protected override void OnUpdate()
{
// todo use coroutines
Emulation emulation = EmulationSingleton.Instance;
if (emulation.IsUnityNull())
Multiplay.Multiplay multiplay = MultiplaySingleton.Instance;
if (emulation.IsUnityNull() || multiplay.IsUnityNull())
return;
// Wait for either clientworld to be connected to the server, or for this guest client to be connected
if (CmdArgs.ClientStreamingRole == CmdArgs.StreamingRole.Guest)
{
if(!multiplay.IsGuestConnected())
return;
}
else
{
if(connections.IsEmpty)
return;
}
Enabled = false;

emulation.emulationType = CmdArgs.emulationType;
Debug.Log($"Emulation type is {emulation.emulationType}");

// Multiplay guest emulation only supports input playback
if (CmdArgs.ClientStreamingRole == CmdArgs.StreamingRole.Guest && emulation.emulationType == EmulationType.BehaviourProfile)
{
Debug.LogWarning("Multiplay guest emulation only supports input playback, switching to it.");
emulation.emulationType = EmulationType.InputPlayback;
}

switch (emulation.emulationType)
{
case EmulationType.RecordInput:
Expand Down
10 changes: 9 additions & 1 deletion Assets/Scripts/Player/Multiplay/Multiplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class Multiplay : SignalingHandlerBase,
public StatsUI statsUI;
public GameObject defaultCamera;

private bool guestConnected = false;

private List<string> connectionIds = new List<string>();
private List<Component> streams = new List<Component>();
public HashSet<string> disconnectedIds = new HashSet<string>();
Expand All @@ -41,6 +43,10 @@ void Awake()

public override IEnumerable<Component> Streams => streams;

public bool IsGuestConnected()
{
return guestConnected;
}

// On delete or disconnect, simply mark this connection for destruction. Handle destruction in MultiplayPlayerSystem
public void OnDeletedConnection(SignalingEventData eventData)
Expand Down Expand Up @@ -166,6 +172,7 @@ IEnumerator ConnectGuest()
renderStreaming.SetSignalingSettings(settings.SignalingSettings);
renderStreaming.Run(handlers: new SignalingHandlerBase[] { handler });


// Enable the video output
videoImage.gameObject.SetActive(true);
var receiveVideoViewer = guestPlayer.GetComponent<VideoStreamReceiver>();
Expand All @@ -176,9 +183,10 @@ IEnumerator ConnectGuest()

//todo hacky wait for the signalling server to connect
yield return new WaitForSeconds(1f);

handler.CreateConnection(connectionId);
yield return new WaitUntil(() => handler.IsConnected(connectionId));
guestConnected = true;
}
}

Expand Down
17 changes: 10 additions & 7 deletions Assets/Scripts/Player/Multiplay/MultiplayPlayerSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,17 @@ protected override void OnUpdate()
var playerController = multiplay.connectionPlayerObjects[connectionId]
.GetComponent<MultiplayPlayerController>();

Debug.Log($"Creating DestroyPlayer RPC for entity {playerController.playerEntity} on {connectionId}");
foreach (var (_, entity) in SystemAPI.Query<RefRO<NetworkId>>().WithAll<NetworkStreamInGame>().WithEntityAccess())
if (playerController.playerEntityExists)
{
var req = commandBuffer.CreateEntity();
DestroyPlayerRequest destroyPlayerRequest = new DestroyPlayerRequest
{ Player = playerController.playerEntity };
commandBuffer.AddComponent(req, destroyPlayerRequest);
commandBuffer.AddComponent(req, new SendRpcCommandRequest { TargetConnection = entity });
Debug.Log($"Creating DestroyPlayer RPC for entity {playerController.playerEntity} on {connectionId}");
foreach (var (_, entity) in SystemAPI.Query<RefRO<NetworkId>>().WithAll<NetworkStreamInGame>().WithEntityAccess())
{
var req = commandBuffer.CreateEntity();
DestroyPlayerRequest destroyPlayerRequest = new DestroyPlayerRequest
{ Player = playerController.playerEntity };
commandBuffer.AddComponent(req, destroyPlayerRequest);
commandBuffer.AddComponent(req, new SendRpcCommandRequest { TargetConnection = entity });
}
}
multiplay.DestroyMultiplayConnection(connectionId);
}
Expand Down
4 changes: 2 additions & 2 deletions ProjectSettings/EntitiesClientSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ MonoBehaviour:
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: e2ea235c1fcfe29488ed97c467a0da53, type: 3}
m_Script: {fileID: 0}
m_Name:
m_EditorClassIdentifier:
m_EditorClassIdentifier: Unity.Entities.Build:Unity.Entities.Build:EntitiesClientSettings
FilterSettings:
ExcludedBakingSystemAssemblies: []

0 comments on commit db241d0

Please sign in to comment.