diff --git a/osu.Game/Beatmaps/BeatmapManager.cs b/osu.Game/Beatmaps/BeatmapManager.cs index e4c53cec94..985d2c7a15 100644 --- a/osu.Game/Beatmaps/BeatmapManager.cs +++ b/osu.Game/Beatmaps/BeatmapManager.cs @@ -474,10 +474,11 @@ namespace osu.Game.Beatmaps // TODO: Diff beatmap metadata with set metadata and leave it here if necessary beatmap.BeatmapInfo.Metadata = null; + RulesetInfo ruleset = rulesets.GetRuleset(beatmap.BeatmapInfo.RulesetID); + // TODO: this should be done in a better place once we actually need to dynamically update it. - beatmap.BeatmapInfo.Ruleset = rulesets.QueryRulesetInfo(r => r.ID == beatmap.BeatmapInfo.RulesetID); - beatmap.BeatmapInfo.StarDifficulty = rulesets.QueryRulesetInfo(r => r.ID == beatmap.BeatmapInfo.RulesetID)?.CreateInstance()?.CreateDifficultyCalculator(beatmap) - .Calculate() ?? 0; + beatmap.BeatmapInfo.Ruleset = ruleset; + beatmap.BeatmapInfo.StarDifficulty = ruleset?.CreateInstance()?.CreateDifficultyCalculator(beatmap).Calculate() ?? 0; beatmapSet.Beatmaps.Add(beatmap.BeatmapInfo); } diff --git a/osu.Game/Input/KeyBindingStore.cs b/osu.Game/Input/KeyBindingStore.cs index 088e3bd75b..5cc018ff2c 100644 --- a/osu.Game/Input/KeyBindingStore.cs +++ b/osu.Game/Input/KeyBindingStore.cs @@ -17,7 +17,7 @@ namespace osu.Game.Input public KeyBindingStore(OsuDbContext connection, RulesetStore rulesets, Storage storage = null) : base(connection, storage) { - foreach (var info in rulesets.AllRulesets) + foreach (var info in rulesets.AvailableRulesets) { var ruleset = info.CreateInstance(); foreach (var variant in ruleset.AvailableVariants) diff --git a/osu.Game/Overlays/Direct/FilterControl.cs b/osu.Game/Overlays/Direct/FilterControl.cs index 28d26d0641..9b52cfd367 100644 --- a/osu.Game/Overlays/Direct/FilterControl.cs +++ b/osu.Game/Overlays/Direct/FilterControl.cs @@ -39,7 +39,7 @@ namespace osu.Game.Overlays.Direct DisplayStyleControl.Dropdown.AccentColour = colours.BlueDark; Ruleset.BindTo(game?.Ruleset ?? new Bindable { Value = rulesets.GetRuleset(0) }); - foreach (var r in rulesets.AllRulesets) + foreach (var r in rulesets.AvailableRulesets) { modeButtons.Add(new RulesetToggleButton(Ruleset, r)); } diff --git a/osu.Game/Overlays/KeyBindingOverlay.cs b/osu.Game/Overlays/KeyBindingOverlay.cs index 72c653030c..4394d0fec0 100644 --- a/osu.Game/Overlays/KeyBindingOverlay.cs +++ b/osu.Game/Overlays/KeyBindingOverlay.cs @@ -19,7 +19,7 @@ namespace osu.Game.Overlays { AddSection(new GlobalKeyBindingsSection(global)); - foreach (var ruleset in rulesets.AllRulesets) + foreach (var ruleset in rulesets.AvailableRulesets) AddSection(new RulesetBindingsSection(ruleset)); } diff --git a/osu.Game/Overlays/Mods/ModSelectOverlay.cs b/osu.Game/Overlays/Mods/ModSelectOverlay.cs index eb643f390f..9ff21dfdd4 100644 --- a/osu.Game/Overlays/Mods/ModSelectOverlay.cs +++ b/osu.Game/Overlays/Mods/ModSelectOverlay.cs @@ -56,7 +56,7 @@ namespace osu.Game.Overlays.Mods if (osu != null) Ruleset.BindTo(osu.Ruleset); else - Ruleset.Value = rulesets.AllRulesets.First(); + Ruleset.Value = rulesets.AvailableRulesets.First(); Ruleset.ValueChanged += rulesetChanged; Ruleset.TriggerChange(); diff --git a/osu.Game/Overlays/Settings/Sections/GameplaySection.cs b/osu.Game/Overlays/Settings/Sections/GameplaySection.cs index 326cb582e2..035a3c7a13 100644 --- a/osu.Game/Overlays/Settings/Sections/GameplaySection.cs +++ b/osu.Game/Overlays/Settings/Sections/GameplaySection.cs @@ -27,7 +27,7 @@ namespace osu.Game.Overlays.Settings.Sections [BackgroundDependencyLoader] private void load(RulesetStore rulesets) { - foreach(Ruleset ruleset in rulesets.AllRulesets.Select(info => info.CreateInstance())) + foreach(Ruleset ruleset in rulesets.AvailableRulesets.Select(info => info.CreateInstance())) { SettingsSubsection section = ruleset.CreateSettings(); if (section != null) diff --git a/osu.Game/Overlays/Settings/SettingsFooter.cs b/osu.Game/Overlays/Settings/SettingsFooter.cs index cb1c861ee7..bf417a2fac 100644 --- a/osu.Game/Overlays/Settings/SettingsFooter.cs +++ b/osu.Game/Overlays/Settings/SettingsFooter.cs @@ -27,7 +27,7 @@ namespace osu.Game.Overlays.Settings var modes = new List(); - foreach (var ruleset in rulesets.AllRulesets) + foreach (var ruleset in rulesets.AvailableRulesets) { var icon = new ConstrainedIconContainer { diff --git a/osu.Game/Overlays/Toolbar/ToolbarModeSelector.cs b/osu.Game/Overlays/Toolbar/ToolbarModeSelector.cs index 60c1261190..da72ae0347 100644 --- a/osu.Game/Overlays/Toolbar/ToolbarModeSelector.cs +++ b/osu.Game/Overlays/Toolbar/ToolbarModeSelector.cs @@ -67,7 +67,7 @@ namespace osu.Game.Overlays.Toolbar [BackgroundDependencyLoader] private void load(RulesetStore rulesets, OsuGame game) { - foreach (var r in rulesets.AllRulesets) + foreach (var r in rulesets.AvailableRulesets) { modeButtons.Add(new ToolbarModeButton { diff --git a/osu.Game/Rulesets/RulesetStore.cs b/osu.Game/Rulesets/RulesetStore.cs index fe9dfd7a11..c39312205c 100644 --- a/osu.Game/Rulesets/RulesetStore.cs +++ b/osu.Game/Rulesets/RulesetStore.cs @@ -18,13 +18,6 @@ namespace osu.Game.Rulesets { private static readonly Dictionary loaded_assemblies = new Dictionary(); - public IEnumerable AllRulesets => Connection.RulesetInfo.Where(r => r.Available); - - public RulesetStore(OsuDbContext connection) - : base(connection) - { - } - static RulesetStore() { AppDomain.CurrentDomain.AssemblyResolve += currentDomain_AssemblyResolve; @@ -33,6 +26,23 @@ namespace osu.Game.Rulesets loadRulesetFromFile(file); } + public RulesetStore(OsuDbContext connection) + : base(connection) + { + } + + /// + /// Retrieve a ruleset using a known ID. + /// + /// The ruleset's internal ID. + /// A ruleset, if available, else null. + public RulesetInfo GetRuleset(int id) => AvailableRulesets.FirstOrDefault(r => r.ID == id); + + /// + /// All available rulesets. + /// + public IEnumerable AvailableRulesets => Connection.RulesetInfo.Where(r => r.Available); + private static Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args) => loaded_assemblies.Keys.FirstOrDefault(a => a.FullName == args.Name); private const string ruleset_library_prefix = "osu.Game.Rulesets"; @@ -44,7 +54,7 @@ namespace osu.Game.Rulesets Connection.Database.ExecuteSqlCommand("DELETE FROM RulesetInfo"); } - var instances = loaded_assemblies.Values.Select(r => (Ruleset)Activator.CreateInstance(r, new RulesetInfo())); + var instances = loaded_assemblies.Values.Select(r => (Ruleset)Activator.CreateInstance(r, new RulesetInfo())).ToList(); //add all legacy modes in correct order foreach (var r in instances.Where(r => r.LegacyID >= 0).OrderBy(r => r.LegacyID)) @@ -55,6 +65,7 @@ namespace osu.Game.Rulesets Connection.RulesetInfo.Add(rulesetInfo); } } + Connection.SaveChanges(); //add any other modes @@ -67,6 +78,7 @@ namespace osu.Game.Rulesets if (existing == null) Connection.RulesetInfo.Add(us); } + Connection.SaveChanges(); //perform a consistency check @@ -82,6 +94,7 @@ namespace osu.Game.Rulesets r.Available = false; } } + Connection.SaveChanges(); } @@ -108,20 +121,5 @@ namespace osu.Game.Rulesets InstantiationInfo = ruleset.GetType().AssemblyQualifiedName, ID = ruleset.LegacyID }; - - public RulesetInfo GetRuleset(int id) => Connection.RulesetInfo.First(r => r.ID == id); - - public RulesetInfo QueryRulesetInfo(Func query) - { - return Connection.RulesetInfo.FirstOrDefault(query); - } - - public List QueryRulesets(Func query = null) - { - var rulesets = Connection.RulesetInfo; - if (query != null) - return rulesets.Where(query).ToList(); - return rulesets.ToList(); - } } } diff --git a/osu.Game/Tests/Visual/TestCaseMods.cs b/osu.Game/Tests/Visual/TestCaseMods.cs index ef250edcc3..0447d6582d 100644 --- a/osu.Game/Tests/Visual/TestCaseMods.cs +++ b/osu.Game/Tests/Visual/TestCaseMods.cs @@ -49,7 +49,7 @@ namespace osu.Game.Tests.Visual AddStep("Toggle", modSelect.ToggleVisibility); - foreach (var ruleset in rulesets.AllRulesets) + foreach (var ruleset in rulesets.AvailableRulesets) AddStep(ruleset.CreateInstance().Description, () => modSelect.Ruleset.Value = ruleset); } } diff --git a/osu.Game/Tests/Visual/TestCasePlaySongSelect.cs b/osu.Game/Tests/Visual/TestCasePlaySongSelect.cs index 1b7f2def4d..a8a00e9a0d 100644 --- a/osu.Game/Tests/Visual/TestCasePlaySongSelect.cs +++ b/osu.Game/Tests/Visual/TestCasePlaySongSelect.cs @@ -75,7 +75,7 @@ namespace osu.Game.Tests.Visual new BeatmapInfo { OnlineBeatmapID = 1234 + i, - Ruleset = rulesets.QueryRulesets().First(), + Ruleset = rulesets.AvailableRulesets.First(), Path = "normal.osu", Version = "Normal", Difficulty = new BeatmapDifficulty @@ -86,7 +86,7 @@ namespace osu.Game.Tests.Visual new BeatmapInfo { OnlineBeatmapID = 1235 + i, - Ruleset = rulesets.QueryRulesets().First(), + Ruleset = rulesets.AvailableRulesets.First(), Path = "hard.osu", Version = "Hard", Difficulty = new BeatmapDifficulty @@ -97,7 +97,7 @@ namespace osu.Game.Tests.Visual new BeatmapInfo { OnlineBeatmapID = 1236 + i, - Ruleset = rulesets.QueryRulesets().First(), + Ruleset = rulesets.AvailableRulesets.First(), Path = "insane.osu", Version = "Insane", Difficulty = new BeatmapDifficulty diff --git a/osu.Game/Tests/Visual/TestCasePlayer.cs b/osu.Game/Tests/Visual/TestCasePlayer.cs index 811e240cee..dfbdaf1d9d 100644 --- a/osu.Game/Tests/Visual/TestCasePlayer.cs +++ b/osu.Game/Tests/Visual/TestCasePlayer.cs @@ -50,7 +50,7 @@ namespace osu.Game.Tests.Visual string instantiation = ruleset?.AssemblyQualifiedName; - foreach (var r in rulesets.QueryRulesets(rs => rs.Available && (instantiation == null || rs.InstantiationInfo == instantiation))) + foreach (var r in rulesets.AvailableRulesets.Where(rs => instantiation == null || rs.InstantiationInfo == instantiation)) AddStep(r.Name, () => loadPlayerFor(r)); }