// Copyright (c) 2007-2018 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; using NUnit.Framework; using osu.Framework.Allocation; using osu.Framework.Platform; using osu.Game.Beatmaps; using osu.Game.Rulesets.Mods; using osu.Game.Rulesets.Osu; using osu.Game.Rulesets.Osu.Mods; using osu.Game.Rulesets.Scoring; using osu.Game.Scoring; using osu.Game.Users; namespace osu.Game.Tests.Beatmaps.IO { public class ImportScoreTest { public const string TEST_OSZ_PATH = @"../../../../osu-resources/osu.Game.Resources/Beatmaps/241526 Soleily - Renatus.osz"; [Test] public void TestBasicImport() { using (HeadlessGameHost host = new CleanRunHeadlessGameHost("TestBasicImport")) { try { var osu = loadOsu(host); var toImport = new ScoreInfo { Rank = ScoreRank.B, TotalScore = 987654, Accuracy = 0.8, Health = 0.8, MaxCombo = 500, Combo = 250, User = new User { Username = "Test user" }, Date = DateTimeOffset.Now, OnlineScoreID = 12345, }; var imported = loadIntoOsu(osu, toImport); Assert.AreEqual(toImport.Rank, imported.Rank); Assert.AreEqual(toImport.TotalScore, imported.TotalScore); Assert.AreEqual(toImport.Accuracy, imported.Accuracy); Assert.AreEqual(toImport.Health, imported.Health); Assert.AreEqual(toImport.MaxCombo, imported.MaxCombo); Assert.AreEqual(toImport.Combo, imported.Combo); Assert.AreEqual(toImport.User.Username, imported.User.Username); Assert.AreEqual(toImport.Date, imported.Date); Assert.AreEqual(toImport.OnlineScoreID, imported.OnlineScoreID); } finally { host.Exit(); } } } [Test] public void TestImportMods() { using (HeadlessGameHost host = new CleanRunHeadlessGameHost("TestImportMods")) { try { var osu = loadOsu(host); var toImport = new ScoreInfo { Mods = new Mod[] { new OsuModHardRock(), new OsuModDoubleTime() }, }; var imported = loadIntoOsu(osu, toImport); Assert.IsTrue(imported.Mods.Any(m => m is OsuModHardRock)); Assert.IsTrue(imported.Mods.Any(m => m is OsuModDoubleTime)); } finally { host.Exit(); } } } [Test] public void TestImportStatistics() { using (HeadlessGameHost host = new CleanRunHeadlessGameHost("TestImportStatistics")) { try { var osu = loadOsu(host); var toImport = new ScoreInfo { Statistics = new Dictionary { { HitResult.Perfect, 100 }, { HitResult.Miss, 50 } } }; var imported = loadIntoOsu(osu, toImport); Assert.AreEqual(toImport.Statistics[HitResult.Perfect], imported.Statistics[HitResult.Perfect]); Assert.AreEqual(toImport.Statistics[HitResult.Miss], imported.Statistics[HitResult.Miss]); } finally { host.Exit(); } } } private ScoreInfo loadIntoOsu(OsuGameBase osu, ScoreInfo scoreInfo, string guidOverride = null) { scoreInfo.MD5Hash = guidOverride ?? Guid.NewGuid().ToString(); var beatmapManager = osu.Dependencies.Get(); scoreInfo.BeatmapInfo = beatmapManager.GetAllUsableBeatmapSets().First().Beatmaps.First(); scoreInfo.Ruleset = new OsuRuleset().RulesetInfo; var scoreManager = osu.Dependencies.Get(); scoreManager.Import(scoreInfo); var imported = scoreManager.Query(s => s.MD5Hash == scoreInfo.MD5Hash); return imported; } private string createTemporaryBeatmap() { var temp = Path.GetTempFileName() + ".osz"; File.Copy(TEST_OSZ_PATH, temp, true); Assert.IsTrue(File.Exists(temp)); return temp; } private OsuGameBase loadOsu(GameHost host) { var osu = new OsuGameBase(); Task.Run(() => host.Run(osu)); waitForOrAssert(() => osu.IsLoaded, @"osu! failed to start in a reasonable amount of time"); var beatmapFile = createTemporaryBeatmap(); var beatmapManager = osu.Dependencies.Get(); beatmapManager.Import(beatmapFile); return osu; } private void waitForOrAssert(Func result, string failureMessage, int timeout = 60000) { Task task = Task.Run(() => { while (!result()) Thread.Sleep(200); }); Assert.IsTrue(task.Wait(timeout), failureMessage); } } }