From 59bfc7abad4c043fcae479017ad46c258e897018 Mon Sep 17 00:00:00 2001 From: smoogipooo Date: Sat, 15 Apr 2017 04:53:37 +0900 Subject: [PATCH] Move statics to RulesetCollection to avoid conflicts later on. --- osu.Desktop.Tests/VisualTests.cs | 8 ++-- osu.Desktop.VisualTests/Program.cs | 8 ++-- osu.Desktop/Program.cs | 8 ++-- .../Beatmaps/Formats/OsuLegacyDecoderTest.cs | 2 +- .../Beatmaps/IO/ImportBeatmapTest.cs | 8 ++-- .../Beatmaps/IO/OszArchiveReaderTest.cs | 2 +- osu.Game/Beatmaps/Beatmap.cs | 2 +- osu.Game/Beatmaps/Drawables/DifficultyIcon.cs | 2 +- osu.Game/Database/ScoreDatabase.cs | 2 +- osu.Game/Modes/Ruleset.cs | 17 -------- osu.Game/Modes/RulesetCollection.cs | 41 +++++++++++++++++++ osu.Game/Overlays/Mods/ModSelectOverlay.cs | 2 +- osu.Game/Overlays/Options/OptionsFooter.cs | 2 +- osu.Game/Screens/Play/Player.cs | 2 +- osu.Game/Screens/Select/BeatmapInfoWedge.cs | 2 +- osu.Game/osu.Game.csproj | 1 + 16 files changed, 67 insertions(+), 42 deletions(-) create mode 100644 osu.Game/Modes/RulesetCollection.cs diff --git a/osu.Desktop.Tests/VisualTests.cs b/osu.Desktop.Tests/VisualTests.cs index 6519dbe917..36dce18b05 100644 --- a/osu.Desktop.Tests/VisualTests.cs +++ b/osu.Desktop.Tests/VisualTests.cs @@ -20,10 +20,10 @@ public void TestVisualTests() { using (var host = new HeadlessGameHost()) { - Ruleset.Register(new OsuRuleset()); - Ruleset.Register(new TaikoRuleset()); - Ruleset.Register(new ManiaRuleset()); - Ruleset.Register(new CatchRuleset()); + RulesetCollection.Register(typeof(OsuRuleset)); + RulesetCollection.Register(typeof(TaikoRuleset)); + RulesetCollection.Register(typeof(ManiaRuleset)); + RulesetCollection.Register(typeof(CatchRuleset)); host.Run(new AutomatedVisualTestGame()); } diff --git a/osu.Desktop.VisualTests/Program.cs b/osu.Desktop.VisualTests/Program.cs index fe1cdfd7f0..912034a927 100644 --- a/osu.Desktop.VisualTests/Program.cs +++ b/osu.Desktop.VisualTests/Program.cs @@ -21,10 +21,10 @@ public static void Main(string[] args) using (GameHost host = Host.GetSuitableHost(@"osu")) { - Ruleset.Register(new OsuRuleset()); - Ruleset.Register(new TaikoRuleset()); - Ruleset.Register(new ManiaRuleset()); - Ruleset.Register(new CatchRuleset()); + RulesetCollection.Register(typeof(OsuRuleset)); + RulesetCollection.Register(typeof(TaikoRuleset)); + RulesetCollection.Register(typeof(ManiaRuleset)); + RulesetCollection.Register(typeof(CatchRuleset)); if (benchmark) host.Run(new AutomatedVisualTestGame()); diff --git a/osu.Desktop/Program.cs b/osu.Desktop/Program.cs index ddf58ac363..e9117cf533 100644 --- a/osu.Desktop/Program.cs +++ b/osu.Desktop/Program.cs @@ -41,10 +41,10 @@ public static int Main(string[] args) } else { - Ruleset.Register(new OsuRuleset()); - Ruleset.Register(new TaikoRuleset()); - Ruleset.Register(new ManiaRuleset()); - Ruleset.Register(new CatchRuleset()); + RulesetCollection.Register(typeof(OsuRuleset)); + RulesetCollection.Register(typeof(TaikoRuleset)); + RulesetCollection.Register(typeof(ManiaRuleset)); + RulesetCollection.Register(typeof(CatchRuleset)); host.Run(new OsuGameDesktop(args)); } diff --git a/osu.Game.Tests/Beatmaps/Formats/OsuLegacyDecoderTest.cs b/osu.Game.Tests/Beatmaps/Formats/OsuLegacyDecoderTest.cs index 8183bc952e..59a5790bea 100644 --- a/osu.Game.Tests/Beatmaps/Formats/OsuLegacyDecoderTest.cs +++ b/osu.Game.Tests/Beatmaps/Formats/OsuLegacyDecoderTest.cs @@ -22,7 +22,7 @@ public class OsuLegacyDecoderTest public void SetUp() { OsuLegacyDecoder.Register(); - Ruleset.Register(new OsuRuleset()); + RulesetCollection.Register(typeof(OsuRuleset)); } [Test] diff --git a/osu.Game.Tests/Beatmaps/IO/ImportBeatmapTest.cs b/osu.Game.Tests/Beatmaps/IO/ImportBeatmapTest.cs index 5d15b43761..66ed27adbb 100644 --- a/osu.Game.Tests/Beatmaps/IO/ImportBeatmapTest.cs +++ b/osu.Game.Tests/Beatmaps/IO/ImportBeatmapTest.cs @@ -28,10 +28,10 @@ public class ImportBeatmapTest [OneTimeSetUp] public void SetUp() { - Ruleset.Register(new OsuRuleset()); - Ruleset.Register(new TaikoRuleset()); - Ruleset.Register(new ManiaRuleset()); - Ruleset.Register(new CatchRuleset()); + RulesetCollection.Register(typeof(OsuRuleset)); + RulesetCollection.Register(typeof(TaikoRuleset)); + RulesetCollection.Register(typeof(ManiaRuleset)); + RulesetCollection.Register(typeof(CatchRuleset)); } [Test] diff --git a/osu.Game.Tests/Beatmaps/IO/OszArchiveReaderTest.cs b/osu.Game.Tests/Beatmaps/IO/OszArchiveReaderTest.cs index b9c4cf780a..add00d8f4d 100644 --- a/osu.Game.Tests/Beatmaps/IO/OszArchiveReaderTest.cs +++ b/osu.Game.Tests/Beatmaps/IO/OszArchiveReaderTest.cs @@ -19,7 +19,7 @@ public class OszArchiveReaderTest public void SetUp() { OszArchiveReader.Register(); - Ruleset.Register(new OsuRuleset()); + RulesetCollection.Register(typeof(OsuRuleset)); } [Test] diff --git a/osu.Game/Beatmaps/Beatmap.cs b/osu.Game/Beatmaps/Beatmap.cs index 5709bdc8c5..420b23eb62 100644 --- a/osu.Game/Beatmaps/Beatmap.cs +++ b/osu.Game/Beatmaps/Beatmap.cs @@ -54,7 +54,7 @@ public class Beatmap : Beatmap /// Calculates the star difficulty for this Beatmap. /// /// The star difficulty. - public double CalculateStarDifficulty() => Ruleset.GetRuleset(BeatmapInfo.Mode).CreateDifficultyCalculator(this).Calculate(); + public double CalculateStarDifficulty() => RulesetCollection.GetRuleset(BeatmapInfo.Mode).CreateDifficultyCalculator(this).Calculate(); /// /// Constructs a new beatmap. diff --git a/osu.Game/Beatmaps/Drawables/DifficultyIcon.cs b/osu.Game/Beatmaps/Drawables/DifficultyIcon.cs index 47ae4d7985..3fb5194382 100644 --- a/osu.Game/Beatmaps/Drawables/DifficultyIcon.cs +++ b/osu.Game/Beatmaps/Drawables/DifficultyIcon.cs @@ -45,7 +45,7 @@ private void load(OsuColour palette) Origin = Anchor.Centre, TextSize = Size.X, Colour = Color4.White, - Icon = Ruleset.GetRuleset(beatmap.Mode).Icon + Icon = RulesetCollection.GetRuleset((int)beatmap.Mode).Icon } }; } diff --git a/osu.Game/Database/ScoreDatabase.cs b/osu.Game/Database/ScoreDatabase.cs index 642bb4aff6..e602b2373e 100644 --- a/osu.Game/Database/ScoreDatabase.cs +++ b/osu.Game/Database/ScoreDatabase.cs @@ -39,7 +39,7 @@ public Score ReadReplayFile(string replayFilename) using (Stream s = storage.GetStream(Path.Combine(replay_folder, replayFilename))) using (SerializationReader sr = new SerializationReader(s)) { - var ruleset = Ruleset.GetRuleset((PlayMode)sr.ReadByte()); + var ruleset = RulesetCollection.GetRuleset((PlayMode)sr.ReadByte()); score = ruleset.CreateScoreProcessor().CreateScore(); /* score.Pass = true;*/ diff --git a/osu.Game/Modes/Ruleset.cs b/osu.Game/Modes/Ruleset.cs index c97420fbe3..42f2fe789f 100644 --- a/osu.Game/Modes/Ruleset.cs +++ b/osu.Game/Modes/Ruleset.cs @@ -22,10 +22,6 @@ public class BeatmapStatistic public abstract class Ruleset { - private static readonly ConcurrentDictionary available_rulesets = new ConcurrentDictionary(); - - public static IEnumerable PlayModes => available_rulesets.Keys; - public virtual IEnumerable GetBeatmapStatistics(WorkingBeatmap beatmap) => new BeatmapStatistic[] { }; public abstract IEnumerable GetModsFor(ModType type); @@ -36,8 +32,6 @@ public abstract class Ruleset public abstract ScoreProcessor CreateScoreProcessor(); - public static void Register(Ruleset ruleset) => available_rulesets.TryAdd(ruleset.PlayMode, ruleset.GetType()); - protected abstract PlayMode PlayMode { get; } public virtual FontAwesome Icon => FontAwesome.fa_question_circle; @@ -45,16 +39,5 @@ public abstract class Ruleset public abstract string Description { get; } public abstract IEnumerable CreateGameplayKeys(); - - public static Ruleset GetRuleset(PlayMode mode) - { - Type type; - - if (!available_rulesets.TryGetValue(mode, out type)) - return null; - - return Activator.CreateInstance(type) as Ruleset; - } - } } diff --git a/osu.Game/Modes/RulesetCollection.cs b/osu.Game/Modes/RulesetCollection.cs new file mode 100644 index 0000000000..95d0df4e6a --- /dev/null +++ b/osu.Game/Modes/RulesetCollection.cs @@ -0,0 +1,41 @@ +// Copyright (c) 2007-2017 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; + +namespace osu.Game.Modes +{ + public static class RulesetCollection + { + private static readonly ConcurrentDictionary available_rulesets = new ConcurrentDictionary(); + + public static void Register(Type type) + { + Ruleset ruleset = Activator.CreateInstance(type) as Ruleset; + + if (ruleset == null) + return; + + available_rulesets.TryAdd(available_rulesets.Count, ruleset); + } + + public static Ruleset GetRuleset(PlayMode playMode) + { + return GetRuleset((int)playMode); + } + + public static Ruleset GetRuleset(int rulesetId) + { + Ruleset ruleset; + + if (!available_rulesets.TryGetValue(rulesetId, out ruleset)) + throw new InvalidOperationException($"Ruleset id {rulesetId} doesn't exist. How did you trigger this?"); + + return ruleset; + } + + public static IEnumerable AllRulesets => available_rulesets.Values; + } +} diff --git a/osu.Game/Overlays/Mods/ModSelectOverlay.cs b/osu.Game/Overlays/Mods/ModSelectOverlay.cs index 2b9f8e86a9..7a21b36da9 100644 --- a/osu.Game/Overlays/Mods/ModSelectOverlay.cs +++ b/osu.Game/Overlays/Mods/ModSelectOverlay.cs @@ -41,7 +41,7 @@ public class ModSelectOverlay : WaveOverlayContainer private void modeChanged(PlayMode newMode) { - var ruleset = Ruleset.GetRuleset(newMode); + var ruleset = RulesetCollection.GetRuleset(newMode); foreach (ModSection section in modSectionsContainer.Children) section.Buttons = ruleset.GetModsFor(section.ModType).Select(m => new ModButton(m)).ToArray(); refreshSelectedMods(); diff --git a/osu.Game/Overlays/Options/OptionsFooter.cs b/osu.Game/Overlays/Options/OptionsFooter.cs index c42fe42428..fde4d8a925 100644 --- a/osu.Game/Overlays/Options/OptionsFooter.cs +++ b/osu.Game/Overlays/Options/OptionsFooter.cs @@ -30,7 +30,7 @@ private void load(OsuGameBase game, OsuColour colours) foreach (PlayMode m in Enum.GetValues(typeof(PlayMode))) modes.Add(new TextAwesome { - Icon = Ruleset.GetRuleset(m).Icon, + Icon = RulesetCollection.GetRuleset(m).Icon, Colour = Color4.Gray, TextSize = 20 }); diff --git a/osu.Game/Screens/Play/Player.cs b/osu.Game/Screens/Play/Player.cs index fa564cdd61..e43eb6cd73 100644 --- a/osu.Game/Screens/Play/Player.cs +++ b/osu.Game/Screens/Play/Player.cs @@ -109,7 +109,7 @@ private void load(AudioManager audio, BeatmapDatabase beatmaps, OsuConfigManager sourceClock.Reset(); }); - ruleset = Ruleset.GetRuleset(Beatmap.PlayMode); + ruleset = RulesetCollection.GetRuleset(Beatmap.PlayMode); HitRenderer = ruleset.CreateHitRendererWith(Beatmap); scoreProcessor = HitRenderer.CreateScoreProcessor(); diff --git a/osu.Game/Screens/Select/BeatmapInfoWedge.cs b/osu.Game/Screens/Select/BeatmapInfoWedge.cs index 768cef4645..586100c47d 100644 --- a/osu.Game/Screens/Select/BeatmapInfoWedge.cs +++ b/osu.Game/Screens/Select/BeatmapInfoWedge.cs @@ -100,7 +100,7 @@ public void UpdateBeatmap(WorkingBeatmap beatmap) })); //get statistics fromt he current ruleset. - labels.AddRange(Ruleset.GetRuleset(beatmap.BeatmapInfo.Mode).GetBeatmapStatistics(beatmap).Select(s => new InfoLabel(s))); + labels.AddRange(RulesetCollection.GetRuleset(beatmap.BeatmapInfo.Mode).GetBeatmapStatistics(beatmap).Select(s => new InfoLabel(s))); } AlwaysPresent = true; diff --git a/osu.Game/osu.Game.csproj b/osu.Game/osu.Game.csproj index f810eeec96..b89f7a3b65 100644 --- a/osu.Game/osu.Game.csproj +++ b/osu.Game/osu.Game.csproj @@ -131,6 +131,7 @@ +