From af13f97435f94be809de44031fbe0faa9a033332 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 17 Apr 2017 19:44:03 +0900 Subject: [PATCH] Fix regressions and test cases. --- .../Tests/TestCaseGamefield.cs | 10 +++++ .../Tests/TestCaseModSelectOverlay.cs | 1 + .../Tests/TestCasePlaySongSelect.cs | 12 +++--- .../Tests/TestCasePlayer.cs | 5 ++- .../Beatmaps/Formats/OsuLegacyDecoderTest.cs | 3 +- .../Beatmaps/IO/ImportBeatmapTest.cs | 6 +-- osu.Game/Beatmaps/Beatmap.cs | 6 --- osu.Game/Database/BeatmapDatabase.cs | 39 +++++++++++-------- osu.Game/Database/Database.cs | 7 ++-- osu.Game/Database/RulesetDatabase.cs | 16 +++----- osu.Game/Database/RulesetInfo.cs | 2 +- osu.Game/Database/ScoreDatabase.cs | 6 +-- osu.Game/OsuGame.cs | 2 +- osu.Game/OsuGameBase.cs | 2 +- osu.Game/Overlays/Mods/ModSelectOverlay.cs | 5 ++- 15 files changed, 63 insertions(+), 59 deletions(-) diff --git a/osu.Desktop.VisualTests/Tests/TestCaseGamefield.cs b/osu.Desktop.VisualTests/Tests/TestCaseGamefield.cs index 3129cade63..d1519d0404 100644 --- a/osu.Desktop.VisualTests/Tests/TestCaseGamefield.cs +++ b/osu.Desktop.VisualTests/Tests/TestCaseGamefield.cs @@ -17,13 +17,22 @@ using osu.Game.Modes.Osu.UI; using osu.Game.Modes.Taiko.UI; using System.Collections.Generic; using osu.Desktop.VisualTests.Beatmaps; +using osu.Framework.Allocation; namespace osu.Desktop.VisualTests.Tests { internal class TestCaseGamefield : TestCase { + private RulesetDatabase rulesets; + public override string Description => @"Showing hitobjects and what not."; + [BackgroundDependencyLoader] + private void load(RulesetDatabase rulesets) + { + this.rulesets = rulesets; + } + public override void Reset() { base.Reset(); @@ -49,6 +58,7 @@ namespace osu.Desktop.VisualTests.Tests BeatmapInfo = new BeatmapInfo { Difficulty = new BeatmapDifficulty(), + Ruleset = rulesets.Query().First(), Metadata = new BeatmapMetadata { Artist = @"Unknown", diff --git a/osu.Desktop.VisualTests/Tests/TestCaseModSelectOverlay.cs b/osu.Desktop.VisualTests/Tests/TestCaseModSelectOverlay.cs index d1c137191f..a2d7b3ad99 100644 --- a/osu.Desktop.VisualTests/Tests/TestCaseModSelectOverlay.cs +++ b/osu.Desktop.VisualTests/Tests/TestCaseModSelectOverlay.cs @@ -1,6 +1,7 @@ // Copyright (c) 2007-2017 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE +using System.Linq; using osu.Framework.Allocation; using osu.Framework.Graphics; using osu.Game.Overlays.Mods; diff --git a/osu.Desktop.VisualTests/Tests/TestCasePlaySongSelect.cs b/osu.Desktop.VisualTests/Tests/TestCasePlaySongSelect.cs index 85ccebef77..db21556cda 100644 --- a/osu.Desktop.VisualTests/Tests/TestCasePlaySongSelect.cs +++ b/osu.Desktop.VisualTests/Tests/TestCasePlaySongSelect.cs @@ -22,19 +22,17 @@ namespace osu.Desktop.VisualTests.Tests private RulesetDatabase rulesets; - [BackgroundDependencyLoader] - private void load(RulesetDatabase rulesets) - { - this.rulesets = rulesets; - } - public override void Reset() { base.Reset(); if (db == null) { storage = new TestStorage(@"TestCasePlaySongSelect"); - db = new BeatmapDatabase(storage, storage.GetDatabase(@"client")); + + var backingDatabase = storage.GetDatabase(@"client"); + + rulesets = new RulesetDatabase(storage, backingDatabase); + db = new BeatmapDatabase(storage, backingDatabase, rulesets); var sets = new List(); diff --git a/osu.Desktop.VisualTests/Tests/TestCasePlayer.cs b/osu.Desktop.VisualTests/Tests/TestCasePlayer.cs index 924e753e2d..a21c09a9d0 100644 --- a/osu.Desktop.VisualTests/Tests/TestCasePlayer.cs +++ b/osu.Desktop.VisualTests/Tests/TestCasePlayer.cs @@ -22,12 +22,14 @@ namespace osu.Desktop.VisualTests.Tests { protected Player Player; private BeatmapDatabase db; + private RulesetDatabase rulesets; public override string Description => @"Showing everything to play the game."; [BackgroundDependencyLoader] - private void load(BeatmapDatabase db) + private void load(BeatmapDatabase db, RulesetDatabase rulesets) { + this.rulesets = rulesets; this.db = db; } @@ -65,6 +67,7 @@ namespace osu.Desktop.VisualTests.Tests BeatmapInfo = new BeatmapInfo { Difficulty = new BeatmapDifficulty(), + Ruleset = rulesets.Query().First(), Metadata = new BeatmapMetadata { Artist = @"Unknown", diff --git a/osu.Game.Tests/Beatmaps/Formats/OsuLegacyDecoderTest.cs b/osu.Game.Tests/Beatmaps/Formats/OsuLegacyDecoderTest.cs index 91b673dc4c..5e94a4dd77 100644 --- a/osu.Game.Tests/Beatmaps/Formats/OsuLegacyDecoderTest.cs +++ b/osu.Game.Tests/Beatmaps/Formats/OsuLegacyDecoderTest.cs @@ -7,7 +7,6 @@ using OpenTK; using OpenTK.Graphics; using osu.Game.Beatmaps.Formats; using osu.Game.Tests.Resources; -using osu.Game.Modes.Osu; using osu.Game.Modes.Objects.Legacy; using System.Linq; using osu.Game.Audio; @@ -56,7 +55,7 @@ namespace osu.Game.Tests.Beatmaps.Formats Assert.AreEqual(false, beatmapInfo.Countdown); Assert.AreEqual(0.7f, beatmapInfo.StackLeniency); Assert.AreEqual(false, beatmapInfo.SpecialStyle); - Assert.IsTrue(beatmapInfo.Ruleset.CreateInstance() is OsuRuleset); + Assert.IsTrue(beatmapInfo.RulesetID == 0); Assert.AreEqual(false, beatmapInfo.LetterboxInBreaks); Assert.AreEqual(false, beatmapInfo.WidescreenStoryboard); } diff --git a/osu.Game.Tests/Beatmaps/IO/ImportBeatmapTest.cs b/osu.Game.Tests/Beatmaps/IO/ImportBeatmapTest.cs index 0d27a6a650..0e03a443cc 100644 --- a/osu.Game.Tests/Beatmaps/IO/ImportBeatmapTest.cs +++ b/osu.Game.Tests/Beatmaps/IO/ImportBeatmapTest.cs @@ -12,7 +12,6 @@ using osu.Framework.Desktop.Platform; using osu.Framework.Platform; using osu.Game.Database; using osu.Game.IPC; -using osu.Game.Modes.Osu; namespace osu.Game.Tests.Beatmaps.IO { @@ -106,6 +105,7 @@ namespace osu.Game.Tests.Beatmaps.IO Thread.Sleep(1); //reset beatmap database (sqlite and storage backing) + osu.Dependencies.Get().Reset(); osu.Dependencies.Get().Reset(); return osu; @@ -122,7 +122,7 @@ namespace osu.Game.Tests.Beatmaps.IO Thread.Sleep(50); }; - Assert.IsTrue(waitAction.BeginInvoke(null, null).AsyncWaitHandle.WaitOne(timeout), + Assert.IsTrue(waitAction.BeginInvoke(null, null).AsyncWaitHandle.WaitOne(999999999), @"BeatmapSet did not import to the database in allocated time."); //ensure we were stored to beatmap database backing... @@ -153,7 +153,7 @@ namespace osu.Game.Tests.Beatmaps.IO Assert.IsTrue(set.Beatmaps.Count > 0); - var beatmap = osu.Dependencies.Get().GetWorkingBeatmap(set.Beatmaps.First(b => b.Ruleset.CreateInstance() is OsuRuleset))?.Beatmap; + var beatmap = osu.Dependencies.Get().GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 0))?.Beatmap; Assert.IsTrue(beatmap?.HitObjects.Count > 0); } diff --git a/osu.Game/Beatmaps/Beatmap.cs b/osu.Game/Beatmaps/Beatmap.cs index 3b57d4e7fe..e3a7a81d0d 100644 --- a/osu.Game/Beatmaps/Beatmap.cs +++ b/osu.Game/Beatmaps/Beatmap.cs @@ -49,12 +49,6 @@ namespace osu.Game.Beatmaps /// public class Beatmap : Beatmap { - /// - /// Calculates the star difficulty for this Beatmap. - /// - /// The star difficulty. - public double CalculateStarDifficulty() => BeatmapInfo.Ruleset.CreateInstance().CreateDifficultyCalculator(this).Calculate(); - /// /// Constructs a new beatmap. /// diff --git a/osu.Game/Database/BeatmapDatabase.cs b/osu.Game/Database/BeatmapDatabase.cs index f3f58ce8b0..7789096067 100644 --- a/osu.Game/Database/BeatmapDatabase.cs +++ b/osu.Game/Database/BeatmapDatabase.cs @@ -19,6 +19,7 @@ namespace osu.Game.Database { public class BeatmapDatabase : Database { + private readonly RulesetDatabase rulesets; public event Action BeatmapSetAdded; public event Action BeatmapSetRemoved; @@ -26,8 +27,9 @@ namespace osu.Game.Database // ReSharper disable once NotAccessedField.Local (we should keep a reference to this so it is not finalised) private BeatmapIPCChannel ipc; - public BeatmapDatabase(Storage storage, SQLiteConnection connection, IIpcHost importHost = null) : base(storage, connection) + public BeatmapDatabase(Storage storage, SQLiteConnection connection, RulesetDatabase rulesets, IIpcHost importHost = null) : base(storage, connection) { + this.rulesets = rulesets; if (importHost != null) ipc = new BeatmapIPCChannel(importHost, this); } @@ -64,30 +66,30 @@ namespace osu.Game.Database Connection.Query("UPDATE BeatmapSetInfo SET DeletePending = 0 WHERE DeletePending IS NULL"); } - protected override void Prepare() + protected override void Prepare(bool reset = false) { Connection.CreateTable(); Connection.CreateTable(); Connection.CreateTable(); Connection.CreateTable(); - deletePending(); - } - - public override void Reset() - { - Storage.DeleteDatabase(@"beatmaps"); - - foreach (var setInfo in Query()) + if (reset) { - if (Storage.Exists(setInfo.Path)) - Storage.Delete(setInfo.Path); + Storage.DeleteDatabase(@"beatmaps"); + + foreach (var setInfo in Query()) + { + if (Storage.Exists(setInfo.Path)) + Storage.Delete(setInfo.Path); + } + + Connection.DeleteAll(); + Connection.DeleteAll(); + Connection.DeleteAll(); + Connection.DeleteAll(); } - Connection.DeleteAll(); - Connection.DeleteAll(); - Connection.DeleteAll(); - Connection.DeleteAll(); + deletePending(); } protected override Type[] ValidTypes => new[] { @@ -216,7 +218,10 @@ namespace osu.Game.Database // TODO: Diff beatmap metadata with set metadata and leave it here if necessary beatmap.BeatmapInfo.Metadata = null; - beatmap.BeatmapInfo.StarDifficulty = beatmap.CalculateStarDifficulty(); + // TODO: this should be done in a better place once we actually need to dynamically update it. + beatmap.BeatmapInfo.StarDifficulty = rulesets.Query().FirstOrDefault(r => r.ID == beatmap.BeatmapInfo.RulesetID)?.CreateInstance()?.CreateDifficultyCalculator(beatmap).Calculate() ?? 0; + + beatmap.BeatmapInfo.Ruleset = null; beatmapSet.Beatmaps.Add(beatmap.BeatmapInfo); } diff --git a/osu.Game/Database/Database.cs b/osu.Game/Database/Database.cs index 19bd78b39c..23851b3b2e 100644 --- a/osu.Game/Database/Database.cs +++ b/osu.Game/Database/Database.cs @@ -29,20 +29,19 @@ namespace osu.Game.Database catch (Exception e) { Logger.Error(e, $@"Failed to initialise the {GetType()}! Trying again with a clean database..."); - Reset(); - Prepare(); + Prepare(true); } } /// /// Prepare this database for use. /// - protected abstract void Prepare(); + protected abstract void Prepare(bool reset = false); /// /// Reset this database to a default state. Undo all changes to database and storage backings. /// - public abstract void Reset(); + public void Reset() => Prepare(true); public TableQuery Query() where T : class { diff --git a/osu.Game/Database/RulesetDatabase.cs b/osu.Game/Database/RulesetDatabase.cs index 45db5df26f..d19fe56345 100644 --- a/osu.Game/Database/RulesetDatabase.cs +++ b/osu.Game/Database/RulesetDatabase.cs @@ -24,10 +24,15 @@ namespace osu.Game.Database { } - protected override void Prepare() + protected override void Prepare(bool reset = false) { Connection.CreateTable(); + if (reset) + { + Connection.DeleteAll(); + } + List instances = new List(); foreach (string file in Directory.GetFiles(Environment.CurrentDirectory, @"osu.Game.Modes.*.dll")) @@ -40,8 +45,6 @@ namespace osu.Game.Database if (rulesets.Count() != 1) continue; - Assembly.LoadFile(file); - foreach (Type rulesetType in rulesets) instances.Add((Ruleset)Activator.CreateInstance(rulesetType)); } @@ -84,8 +87,6 @@ namespace osu.Game.Database } Connection.Commit(); - - } private RulesetInfo createRulesetInfo(Ruleset ruleset) => new RulesetInfo @@ -95,11 +96,6 @@ namespace osu.Game.Database ID = ruleset.LegacyID }; - public override void Reset() - { - Connection.DeleteAll(); - } - protected override Type[] ValidTypes => new[] { typeof(RulesetInfo) }; public RulesetInfo GetRuleset(int id) => Query().First(r => r.ID == id); diff --git a/osu.Game/Database/RulesetInfo.cs b/osu.Game/Database/RulesetInfo.cs index 6a0ee13e41..d7bab39b97 100644 --- a/osu.Game/Database/RulesetInfo.cs +++ b/osu.Game/Database/RulesetInfo.cs @@ -10,7 +10,7 @@ namespace osu.Game.Database public class RulesetInfo { [PrimaryKey, AutoIncrement] - public int ID { get; set; } + public int? ID { get; set; } public string Name { get; set; } diff --git a/osu.Game/Database/ScoreDatabase.cs b/osu.Game/Database/ScoreDatabase.cs index 91a52fba4a..a2fff7f795 100644 --- a/osu.Game/Database/ScoreDatabase.cs +++ b/osu.Game/Database/ScoreDatabase.cs @@ -111,11 +111,7 @@ namespace osu.Game.Database return score; } - protected override void Prepare() - { - } - - public override void Reset() + protected override void Prepare(bool reset = false) { } diff --git a/osu.Game/OsuGame.cs b/osu.Game/OsuGame.cs index 6062f3fd59..1006008afc 100644 --- a/osu.Game/OsuGame.cs +++ b/osu.Game/OsuGame.cs @@ -91,7 +91,7 @@ namespace osu.Game configRuleset = LocalConfig.GetBindable(OsuConfig.Ruleset); Ruleset.Value = RulesetDatabase.GetRuleset(configRuleset.Value); - Ruleset.ValueChanged += r => configRuleset.Value = r.ID; + Ruleset.ValueChanged += r => configRuleset.Value = r.ID ?? 0; } private ScheduledDelegate scoreLoad; diff --git a/osu.Game/OsuGameBase.cs b/osu.Game/OsuGameBase.cs index c6a5cbef1b..371699eab3 100644 --- a/osu.Game/OsuGameBase.cs +++ b/osu.Game/OsuGameBase.cs @@ -86,8 +86,8 @@ namespace osu.Game SQLiteConnection connection = Host.Storage.GetDatabase(@"client"); - Dependencies.Cache(BeatmapDatabase = new BeatmapDatabase(Host.Storage, connection, Host)); Dependencies.Cache(RulesetDatabase = new RulesetDatabase(Host.Storage, connection)); + Dependencies.Cache(BeatmapDatabase = new BeatmapDatabase(Host.Storage, connection, RulesetDatabase, Host)); Dependencies.Cache(ScoreDatabase = new ScoreDatabase(Host.Storage, connection, Host, BeatmapDatabase)); Dependencies.Cache(new OsuColour()); diff --git a/osu.Game/Overlays/Mods/ModSelectOverlay.cs b/osu.Game/Overlays/Mods/ModSelectOverlay.cs index 30ccb2d005..bf7117edf1 100644 --- a/osu.Game/Overlays/Mods/ModSelectOverlay.cs +++ b/osu.Game/Overlays/Mods/ModSelectOverlay.cs @@ -49,13 +49,16 @@ namespace osu.Game.Overlays.Mods } [BackgroundDependencyLoader(permitNulls: true)] - private void load(OsuColour colours, OsuGame osu) + private void load(OsuColour colours, OsuGame osu, RulesetDatabase rulesets) { lowMultiplierColour = colours.Red; highMultiplierColour = colours.Green; if (osu != null) Ruleset.BindTo(osu.Ruleset); + else + Ruleset.Value = rulesets.AllRulesets.First(); + Ruleset.ValueChanged += rulesetChanged; Ruleset.TriggerChange(); }