From 1d1c08125bb26f5a440a2e39bcfa98dec7c3a2e7 Mon Sep 17 00:00:00 2001 From: smoogipooo Date: Fri, 3 Mar 2017 15:59:33 +0900 Subject: [PATCH] Further safety checking. --- osu.Game/Screens/Tournament/Drawings.cs | 66 +++++++++++++------ osu.Game/Screens/Tournament/Group.cs | 2 +- .../Tournament/ScrollingTeamContainer.cs | 9 +++ 3 files changed, 56 insertions(+), 21 deletions(-) diff --git a/osu.Game/Screens/Tournament/Drawings.cs b/osu.Game/Screens/Tournament/Drawings.cs index 67bd96cbb3..d2fbf6c432 100644 --- a/osu.Game/Screens/Tournament/Drawings.cs +++ b/osu.Game/Screens/Tournament/Drawings.cs @@ -24,6 +24,7 @@ using osu.Framework.Configuration; using osu.Framework.Graphics.Textures; using osu.Framework.IO.Stores; using osu.Framework.Platform; +using osu.Framework.Logging; namespace osu.Game.Screens.Tournament { @@ -250,11 +251,18 @@ namespace osu.Game.Screens.Tournament { Action writeAction = () => { - // Write to drawings_results - using (Stream stream = storage.GetStream(results_filename, FileAccess.Write, FileMode.Create)) - using (StreamWriter sw = new StreamWriter(stream)) + try { - sw.Write(text); + // Write to drawings_results + using (Stream stream = storage.GetStream(results_filename, FileAccess.Write, FileMode.Create)) + using (StreamWriter sw = new StreamWriter(stream)) + { + sw.Write(text); + } + } + catch (Exception ex) + { + Logger.Error(ex, "Failed to write results."); } }; @@ -269,6 +277,8 @@ namespace osu.Game.Screens.Tournament teamsContainer.ClearTeams(); allTeams.Clear(); + List newTeams = new List(); + try { using (Stream stream = storage.GetStream(teams_filename, FileAccess.Read, FileMode.Open)) @@ -276,32 +286,46 @@ namespace osu.Game.Screens.Tournament { while (sr.Peek() != -1) { - string line = sr.ReadLine(); + string line = sr.ReadLine().Trim(); + + if (string.IsNullOrEmpty(line)) + continue; string[] split = line.Split(':'); + + if (split.Length < 2) + { + Logger.Log($"Invalid team definition: {line}. Expected \"flag_name : team_name : team_acronym\"."); + continue; + } + string flagName = split[0].Trim(); - string name = split[1].Trim(); + string teamName = split[1].Trim(); - string acronym = name.Substring(0, Math.Min(3, name.Length)); - if (split.Length >= 3) - acronym = split[2].Trim(); + string acronym = split.Length >= 3 ? split[2].Trim() : teamName; + acronym = acronym.Substring(0, Math.Min(3, acronym.Length)); - if (groupsContainer.ContainsTeam(name)) + if (groupsContainer.ContainsTeam(teamName)) continue; Team t = new Team() { FlagName = flagName, - FullName = name, + FullName = teamName, Acronym = acronym }; - allTeams.Add(t); - teamsContainer.AddTeam(t); + newTeams.Add(t); } } + + allTeams = newTeams; + teamsContainer.AddTeams(allTeams); + } + catch (Exception ex) + { + Logger.Error(ex, "Failed to read teams."); } - catch { } } private void reset(bool loadLastResults = false) @@ -310,9 +334,9 @@ namespace osu.Game.Screens.Tournament reloadTeams(); - try + if (loadLastResults) { - if (loadLastResults) + try { // Read from drawings_results using (Stream stream = storage.GetStream(results_filename, FileAccess.Read, FileMode.Open)) @@ -320,7 +344,7 @@ namespace osu.Game.Screens.Tournament { while (sr.Peek() != -1) { - string line = sr.ReadLine(); + string line = sr.ReadLine().Trim(); if (string.IsNullOrEmpty(line)) continue; @@ -338,13 +362,15 @@ namespace osu.Game.Screens.Tournament } } } - else + catch (Exception ex) { - writeResults(string.Empty); + Logger.Error(ex, "Failed to read last drawings results."); } + } - catch + else { + writeResults(string.Empty); } } } diff --git a/osu.Game/Screens/Tournament/Group.cs b/osu.Game/Screens/Tournament/Group.cs index f0aa79edc8..ff8f2dee67 100644 --- a/osu.Game/Screens/Tournament/Group.cs +++ b/osu.Game/Screens/Tournament/Group.cs @@ -20,7 +20,7 @@ namespace osu.Game.Screens.Tournament { public class Group : Container { - public string GroupName; + public readonly string GroupName; public int TeamsCount { get; private set; } diff --git a/osu.Game/Screens/Tournament/ScrollingTeamContainer.cs b/osu.Game/Screens/Tournament/ScrollingTeamContainer.cs index 9a92289698..10a047325a 100644 --- a/osu.Game/Screens/Tournament/ScrollingTeamContainer.cs +++ b/osu.Game/Screens/Tournament/ScrollingTeamContainer.cs @@ -163,6 +163,15 @@ namespace osu.Game.Screens.Tournament scrollState = ScrollState.Idle; } + public void AddTeams(IEnumerable teams) + { + if (teams == null) + return; + + foreach (Team t in teams) + AddTeam(t); + } + public void ClearTeams() { availableTeams.Clear();