diff --git a/OpenRA.Game/Game.cs b/OpenRA.Game/Game.cs index b5de90524b..848eaa2e4e 100644 --- a/OpenRA.Game/Game.cs +++ b/OpenRA.Game/Game.cs @@ -52,9 +52,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 93fa12e8f2..9b10113028 100644 --- a/OpenRA.Game/OpenRA.Game.csproj +++ b/OpenRA.Game/OpenRA.Game.csproj @@ -189,7 +189,7 @@ - + diff --git a/OpenRA.Game/World.cs b/OpenRA.Game/World.cs index 9e94843f07..1af06e23a5 100644 --- a/OpenRA.Game/World.cs +++ b/OpenRA.Game/World.cs @@ -213,7 +213,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); }