From ef26b0ba8aba42209ddbae7c2de2ef8cd6f2334e Mon Sep 17 00:00:00 2001
From: Dean Herbert <pe@ppy.sh>
Date: Thu, 28 Oct 2021 15:00:30 +0900
Subject: [PATCH 1/2] Fix serlialisation failure during ladder saving causing
 all existing file content to be deleted

---
 osu.Game.Tournament/TournamentGameBase.cs | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/osu.Game.Tournament/TournamentGameBase.cs b/osu.Game.Tournament/TournamentGameBase.cs
index 978be720df..22cc07ad8d 100644
--- a/osu.Game.Tournament/TournamentGameBase.cs
+++ b/osu.Game.Tournament/TournamentGameBase.cs
@@ -269,17 +269,19 @@ namespace osu.Game.Tournament
                                             ladder.Matches.Where(p => p.LosersProgression.Value != null).Select(p => new TournamentProgression(p.ID, p.LosersProgression.Value.ID, true)))
                                         .ToList();
 
+            string serialisedLadder = JsonConvert.SerializeObject(ladder,
+                new JsonSerializerSettings
+                {
+                    Formatting = Formatting.Indented,
+                    NullValueHandling = NullValueHandling.Ignore,
+                    DefaultValueHandling = DefaultValueHandling.Ignore,
+                    Converters = new JsonConverter[] { new JsonPointConverter() }
+                });
+
             using (var stream = storage.GetStream(bracket_filename, FileAccess.Write, FileMode.Create))
             using (var sw = new StreamWriter(stream))
             {
-                sw.Write(JsonConvert.SerializeObject(ladder,
-                    new JsonSerializerSettings
-                    {
-                        Formatting = Formatting.Indented,
-                        NullValueHandling = NullValueHandling.Ignore,
-                        DefaultValueHandling = DefaultValueHandling.Ignore,
-                        Converters = new JsonConverter[] { new JsonPointConverter() }
-                    }));
+                sw.Write(serialisedLadder);
             }
         }
 

From a6669a3892744152b7c4628303325263e45499e1 Mon Sep 17 00:00:00 2001
From: Dean Herbert <pe@ppy.sh>
Date: Thu, 28 Oct 2021 15:04:09 +0900
Subject: [PATCH 2/2] Add mention of why the serialisation happens outside
 stream usage

---
 osu.Game.Tournament/TournamentGameBase.cs | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/osu.Game.Tournament/TournamentGameBase.cs b/osu.Game.Tournament/TournamentGameBase.cs
index 22cc07ad8d..0e8646d15c 100644
--- a/osu.Game.Tournament/TournamentGameBase.cs
+++ b/osu.Game.Tournament/TournamentGameBase.cs
@@ -269,6 +269,7 @@ namespace osu.Game.Tournament
                                             ladder.Matches.Where(p => p.LosersProgression.Value != null).Select(p => new TournamentProgression(p.ID, p.LosersProgression.Value.ID, true)))
                                         .ToList();
 
+            // Serialise before opening stream for writing, so if there's a failure it will leave the file in the previous state.
             string serialisedLadder = JsonConvert.SerializeObject(ladder,
                 new JsonSerializerSettings
                 {
@@ -280,9 +281,7 @@ namespace osu.Game.Tournament
 
             using (var stream = storage.GetStream(bracket_filename, FileAccess.Write, FileMode.Create))
             using (var sw = new StreamWriter(stream))
-            {
                 sw.Write(serialisedLadder);
-            }
         }
 
         protected override UserInputManager CreateUserInputManager() => new TournamentInputManager();