From b5bd6191c27eed510ab417beed3b6d7ab91bed57 Mon Sep 17 00:00:00 2001 From: Scott_NZ Date: Mon, 25 Feb 2013 21:12:14 +1300 Subject: [PATCH] Don't crash (due to replay file being in use) if we have two instances of the game playing --- OpenRA.Game/Game.cs | 2 +- .../Network/ReplayRecorderConnection.cs | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/OpenRA.Game/Game.cs b/OpenRA.Game/Game.cs index b4af0f25d4..8f33d9500e 100755 --- a/OpenRA.Game/Game.cs +++ b/OpenRA.Game/Game.cs @@ -58,7 +58,7 @@ namespace OpenRA static string ChooseReplayFilename() { - return DateTime.UtcNow.ToString("OpenRA-yyyy-MM-ddTHHmmssZ.rep"); + return DateTime.UtcNow.ToString("OpenRA-yyyy-MM-ddTHHmmssZ"); } static void JoinInner(OrderManager om) diff --git a/OpenRA.Game/Network/ReplayRecorderConnection.cs b/OpenRA.Game/Network/ReplayRecorderConnection.cs index c7aba98b6b..249574ac1c 100644 --- a/OpenRA.Game/Network/ReplayRecorderConnection.cs +++ b/OpenRA.Game/Network/ReplayRecorderConnection.cs @@ -34,12 +34,23 @@ namespace OpenRA.Network void StartSavingReplay(byte[] initialContent) { var filename = chooseFilename(); - var replayPath = Path.Combine(Platform.SupportDir, "Replays"); + var replaysDirectory = Path.Combine(Platform.SupportDir, "Replays"); - if (!Directory.Exists(replayPath)) - Directory.CreateDirectory(replayPath); + if (!Directory.Exists(replaysDirectory)) + Directory.CreateDirectory(replaysDirectory); - var file = File.Create(Path.Combine(replayPath, filename)); + string fullFilename; + var id = -1; + do + { + fullFilename = Path.Combine(replaysDirectory, id < 0 + ? "{0}.rep".F(filename) + : "{0}-{1}.rep".F(filename, id)); + id++; + } + while (File.Exists(fullFilename)); + + var file = File.Create(fullFilename); file.Write(initialContent); this.writer = new BinaryWriter(file); }