From 155c74fc5f22cc2092d3980dfc30a980c8e9c6b7 Mon Sep 17 00:00:00 2001 From: Whinis Date: Thu, 24 Dec 2015 10:07:05 -0500 Subject: [PATCH] Reworking ReplayRecorderConnection to no longer be an IConnection but rather attached to IConnection --- OpenRA.Game/Game.cs | 4 +- OpenRA.Game/Network/Connection.cs | 19 ++++++++- ...ecorderConnection.cs => ReplayRecorder.cs} | 41 ++++++------------- OpenRA.Game/OpenRA.Game.csproj | 2 +- OpenRA.Game/World.cs | 4 +- 5 files changed, 37 insertions(+), 33 deletions(-) rename OpenRA.Game/Network/{ReplayRecorderConnection.cs => ReplayRecorder.cs} (66%) diff --git a/OpenRA.Game/Game.cs b/OpenRA.Game/Game.cs index 61b998c275..beafd8ce57 100644 --- a/OpenRA.Game/Game.cs +++ b/OpenRA.Game/Game.cs @@ -49,9 +49,9 @@ namespace OpenRA public static OrderManager JoinServer(string host, int port, string password, bool recordReplay = true) { - IConnection connection = new NetworkConnection(host, port); + var connection = new NetworkConnection(host, port); if (recordReplay) - connection = new ReplayRecorderConnection(connection, TimestampedFilename); + connection.StartRecording(TimestampedFilename); var om = new OrderManager(host, port, password, connection); JoinInner(om); diff --git a/OpenRA.Game/Network/Connection.cs b/OpenRA.Game/Network/Connection.cs index 8a6d02c155..ec413c0f4f 100644 --- a/OpenRA.Game/Network/Connection.cs +++ b/OpenRA.Game/Network/Connection.cs @@ -44,6 +44,7 @@ namespace OpenRA.Network } protected List receivedPackets = new List(); + public ReplayRecorder Recorder { get; private set; } public virtual int LocalClientId { @@ -99,10 +100,26 @@ namespace OpenRA.Network } foreach (var p in packets) + { packetFn(p.FromClient, p.Data); + if (Recorder != null) + Recorder.Receive(p.FromClient, p.Data); + } } - protected virtual void Dispose(bool disposing) { } + public void StartRecording(Func chooseFilename) + { + // If we have a previous recording then save/dispose it and start a new one. + if (Recorder != null) + Recorder.Dispose(); + Recorder = new ReplayRecorder(chooseFilename); + } + + protected virtual void Dispose(bool disposing) + { + if (disposing && Recorder != null) + Recorder.Dispose(); + } public void Dispose() { diff --git a/OpenRA.Game/Network/ReplayRecorderConnection.cs b/OpenRA.Game/Network/ReplayRecorder.cs similarity index 66% rename from OpenRA.Game/Network/ReplayRecorderConnection.cs rename to OpenRA.Game/Network/ReplayRecorder.cs index b42f07404e..a62d741324 100644 --- a/OpenRA.Game/Network/ReplayRecorderConnection.cs +++ b/OpenRA.Game/Network/ReplayRecorder.cs @@ -16,11 +16,9 @@ using OpenRA.FileFormats; namespace OpenRA.Network { - sealed class ReplayRecorderConnection : IConnection + sealed class ReplayRecorder { public ReplayMetadata Metadata; - - IConnection inner; BinaryWriter writer; Func chooseFilename; MemoryStream preStartBuffer = new MemoryStream(); @@ -36,10 +34,9 @@ namespace OpenRA.Network return frame == 0 && data.ToOrderList(null).Any(o => o.OrderString == "StartGame"); } - public ReplayRecorderConnection(IConnection inner, Func chooseFilename) + public ReplayRecorder(Func chooseFilename) { this.chooseFilename = chooseFilename; - this.inner = inner; writer = new BinaryWriter(preStartBuffer); } @@ -70,30 +67,19 @@ namespace OpenRA.Network this.writer = new BinaryWriter(file); } - public int LocalClientId { get { return inner.LocalClientId; } } - public ConnectionState ConnectionState { get { return inner.ConnectionState; } } - - public void Send(int frame, List orders) { inner.Send(frame, orders); } - public void SendImmediate(List orders) { inner.SendImmediate(orders); } - public void SendSync(int frame, byte[] syncData) { inner.SendSync(frame, syncData); } - - public void Receive(Action packetFn) + public void Receive(int clientID, byte[] data) { - inner.Receive((client, data) => - { - if (preStartBuffer != null && IsGameStart(data)) - { - writer.Flush(); - var preStartData = preStartBuffer.ToArray(); - preStartBuffer = null; - StartSavingReplay(preStartData); - } + if (preStartBuffer != null && IsGameStart(data)) + { + writer.Flush(); + var preStartData = preStartBuffer.ToArray(); + preStartBuffer = null; + StartSavingReplay(preStartData); + } - writer.Write(client); - writer.Write(data.Length); - writer.Write(data); - packetFn(client, data); - }); + writer.Write(clientID); + writer.Write(data.Length); + writer.Write(data); } bool disposed; @@ -114,7 +100,6 @@ namespace OpenRA.Network if (preStartBuffer != null) preStartBuffer.Dispose(); writer.Close(); - inner.Dispose(); } } } diff --git a/OpenRA.Game/OpenRA.Game.csproj b/OpenRA.Game/OpenRA.Game.csproj index 024f9c9287..38488e8e18 100644 --- a/OpenRA.Game/OpenRA.Game.csproj +++ b/OpenRA.Game/OpenRA.Game.csproj @@ -188,7 +188,7 @@ - + diff --git a/OpenRA.Game/World.cs b/OpenRA.Game/World.cs index 4cacd3392d..36954957ef 100644 --- a/OpenRA.Game/World.cs +++ b/OpenRA.Game/World.cs @@ -211,7 +211,9 @@ namespace OpenRA foreach (var player in Players) gameInfo.AddPlayer(player, OrderManager.LobbyInfo); - var rc = OrderManager.Connection as ReplayRecorderConnection; + var echo = OrderManager.Connection as EchoConnection; + var rc = echo != null ? echo.Recorder : null; + if (rc != null) rc.Metadata = new ReplayMetadata(gameInfo); }