2019-01-24 16:43:03 +08:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2021-11-22 14:59:12 +08:00
|
|
|
|
using System.IO;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using osu.Framework.Allocation;
|
2020-06-03 17:30:27 +08:00
|
|
|
|
using osu.Framework.Extensions;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Game.Beatmaps;
|
2020-12-07 17:00:45 +08:00
|
|
|
|
using osu.Game.Database;
|
2019-01-28 17:19:57 +08:00
|
|
|
|
using osu.Game.Tests.Resources;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.Beatmaps.IO
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
2020-09-18 17:05:33 +08:00
|
|
|
|
public class ImportBeatmapTest : ImportTest
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2021-11-22 14:59:12 +08:00
|
|
|
|
public static async Task<BeatmapSetInfo> LoadQuickOszIntoOsu(OsuGameBase osu)
|
2021-02-22 13:48:04 +08:00
|
|
|
|
{
|
2021-10-27 12:04:41 +08:00
|
|
|
|
string temp = TestResources.GetQuickTestBeatmapForImport();
|
2021-02-22 13:48:04 +08:00
|
|
|
|
|
|
|
|
|
var manager = osu.Dependencies.Get<BeatmapManager>();
|
|
|
|
|
|
2022-01-06 21:54:43 +08:00
|
|
|
|
var importedSet = manager.Import(new ImportTask(temp)).GetResultSafely();
|
2021-02-22 13:48:04 +08:00
|
|
|
|
|
2022-01-03 16:51:56 +08:00
|
|
|
|
ensureLoaded(osu).WaitSafely();
|
2021-02-22 13:48:04 +08:00
|
|
|
|
|
|
|
|
|
waitForOrAssert(() => !File.Exists(temp), "Temporary file still exists after standard import", 5000);
|
|
|
|
|
|
2021-09-30 18:33:12 +08:00
|
|
|
|
return manager.GetAllUsableBeatmapSets().Find(beatmapSet => beatmapSet.ID == importedSet.Value.ID);
|
2022-01-03 16:51:56 +08:00
|
|
|
|
}, TaskCreationOptions.LongRunning);
|
2021-02-22 13:48:04 +08:00
|
|
|
|
|
2022-01-03 16:51:56 +08:00
|
|
|
|
public static Task<BeatmapSetInfo> LoadOszIntoOsu(OsuGameBase osu, string path = null, bool virtualTrack = false) => Task.Factory.StartNew(() =>
|
2018-05-29 10:57:11 +08:00
|
|
|
|
{
|
2021-10-27 12:04:41 +08:00
|
|
|
|
string temp = path ?? TestResources.GetTestBeatmapForImport(virtualTrack);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
var manager = osu.Dependencies.Get<BeatmapManager>();
|
|
|
|
|
|
2022-01-06 21:54:43 +08:00
|
|
|
|
var importedSet = manager.Import(new ImportTask(temp)).GetResultSafely();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2022-01-03 16:51:56 +08:00
|
|
|
|
ensureLoaded(osu).WaitSafely();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
waitForOrAssert(() => !File.Exists(temp), "Temporary file still exists after standard import", 5000);
|
|
|
|
|
|
2021-09-30 18:33:12 +08:00
|
|
|
|
return manager.GetAllUsableBeatmapSets().Find(beatmapSet => beatmapSet.ID == importedSet.Value.ID);
|
2022-01-03 16:51:56 +08:00
|
|
|
|
}, TaskCreationOptions.LongRunning);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
private void deleteBeatmapSet(BeatmapSetInfo imported, OsuGameBase osu)
|
|
|
|
|
{
|
|
|
|
|
var manager = osu.Dependencies.Get<BeatmapManager>();
|
|
|
|
|
manager.Delete(imported);
|
|
|
|
|
|
2019-06-10 17:35:23 +08:00
|
|
|
|
checkBeatmapSetCount(osu, 0);
|
|
|
|
|
checkBeatmapSetCount(osu, 1, true);
|
|
|
|
|
checkSingleReferencedFileCount(osu, 0);
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
Assert.IsTrue(manager.QueryBeatmapSets(_ => true).First().DeletePending);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 16:37:24 +08:00
|
|
|
|
private static void checkBeatmapSetCount(OsuGameBase osu, int expected, bool includeDeletePending = false)
|
2019-06-10 17:35:23 +08:00
|
|
|
|
{
|
|
|
|
|
var manager = osu.Dependencies.Get<BeatmapManager>();
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(expected, includeDeletePending
|
|
|
|
|
? manager.QueryBeatmapSets(_ => true).ToList().Count
|
|
|
|
|
: manager.GetAllUsableBeatmapSets().Count);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 16:37:24 +08:00
|
|
|
|
private static string hashFile(string filename)
|
2021-06-23 16:19:18 +08:00
|
|
|
|
{
|
|
|
|
|
using (var s = File.OpenRead(filename))
|
|
|
|
|
return s.ComputeMD5Hash();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 16:37:24 +08:00
|
|
|
|
private static void checkBeatmapCount(OsuGameBase osu, int expected)
|
2019-06-10 17:35:23 +08:00
|
|
|
|
{
|
|
|
|
|
Assert.AreEqual(expected, osu.Dependencies.Get<BeatmapManager>().QueryBeatmaps(_ => true).ToList().Count);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 16:37:24 +08:00
|
|
|
|
private static void checkSingleReferencedFileCount(OsuGameBase osu, int expected)
|
2019-06-10 17:35:23 +08:00
|
|
|
|
{
|
2021-11-25 13:06:02 +08:00
|
|
|
|
Assert.AreEqual(expected, osu.Dependencies.Get<DatabaseContextFactory>().Get().FileInfo.Count(f => f.ReferenceCount == 1));
|
2019-06-10 17:35:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 16:51:56 +08:00
|
|
|
|
private static Task ensureLoaded(OsuGameBase osu, int timeout = 60000) => Task.Factory.StartNew(() =>
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
IEnumerable<BeatmapSetInfo> resultSets = null;
|
|
|
|
|
var store = osu.Dependencies.Get<BeatmapManager>();
|
2021-11-12 16:50:31 +08:00
|
|
|
|
waitForOrAssert(() => (resultSets = store.QueryBeatmapSets(s => s.OnlineID == 241526)).Any(),
|
2018-04-13 17:19:50 +08:00
|
|
|
|
@"BeatmapSet did not import to the database in allocated time.", timeout);
|
|
|
|
|
|
2020-05-05 09:31:11 +08:00
|
|
|
|
// ensure we were stored to beatmap database backing...
|
2018-04-13 17:19:50 +08:00
|
|
|
|
Assert.IsTrue(resultSets.Count() == 1, $@"Incorrect result count found ({resultSets.Count()} but should be 1).");
|
2021-11-22 14:59:12 +08:00
|
|
|
|
IEnumerable<BeatmapInfo> queryBeatmaps() => store.QueryBeatmaps(s => s.BeatmapSet.OnlineID == 241526);
|
2021-11-12 16:50:31 +08:00
|
|
|
|
IEnumerable<BeatmapSetInfo> queryBeatmapSets() => store.QueryBeatmapSets(s => s.OnlineID == 241526);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2020-05-05 09:31:11 +08:00
|
|
|
|
// if we don't re-check here, the set will be inserted but the beatmaps won't be present yet.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
waitForOrAssert(() => queryBeatmaps().Count() == 12,
|
|
|
|
|
@"Beatmaps did not import to the database in allocated time", timeout);
|
|
|
|
|
waitForOrAssert(() => queryBeatmapSets().Count() == 1,
|
|
|
|
|
@"BeatmapSet did not import to the database in allocated time", timeout);
|
|
|
|
|
int countBeatmapSetBeatmaps = 0;
|
|
|
|
|
int countBeatmaps = 0;
|
|
|
|
|
waitForOrAssert(() =>
|
|
|
|
|
(countBeatmapSetBeatmaps = queryBeatmapSets().First().Beatmaps.Count) ==
|
|
|
|
|
(countBeatmaps = queryBeatmaps().Count()),
|
|
|
|
|
$@"Incorrect database beatmap count post-import ({countBeatmaps} but should be {countBeatmapSetBeatmaps}).", timeout);
|
|
|
|
|
|
|
|
|
|
var set = queryBeatmapSets().First();
|
|
|
|
|
foreach (BeatmapInfo b in set.Beatmaps)
|
2021-11-12 16:45:05 +08:00
|
|
|
|
Assert.IsTrue(set.Beatmaps.Any(c => c.OnlineID == b.OnlineID));
|
2018-04-13 17:19:50 +08:00
|
|
|
|
Assert.IsTrue(set.Beatmaps.Count > 0);
|
|
|
|
|
var beatmap = store.GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 0))?.Beatmap;
|
2018-04-19 19:44:38 +08:00
|
|
|
|
Assert.IsTrue(beatmap?.HitObjects.Any() == true);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
beatmap = store.GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 1))?.Beatmap;
|
2018-04-19 19:44:38 +08:00
|
|
|
|
Assert.IsTrue(beatmap?.HitObjects.Any() == true);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
beatmap = store.GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 2))?.Beatmap;
|
2018-04-19 19:44:38 +08:00
|
|
|
|
Assert.IsTrue(beatmap?.HitObjects.Any() == true);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
beatmap = store.GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 3))?.Beatmap;
|
2018-04-19 19:44:38 +08:00
|
|
|
|
Assert.IsTrue(beatmap?.HitObjects.Any() == true);
|
2022-01-03 16:51:56 +08:00
|
|
|
|
}, TaskCreationOptions.LongRunning);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-12-25 18:17:21 +08:00
|
|
|
|
private static void waitForOrAssert(Func<bool> result, string failureMessage, int timeout = 60000)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2022-01-03 16:51:56 +08:00
|
|
|
|
Task task = Task.Factory.StartNew(() =>
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
while (!result()) Thread.Sleep(200);
|
2022-01-03 16:51:56 +08:00
|
|
|
|
}, TaskCreationOptions.LongRunning);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-06-04 21:25:18 +08:00
|
|
|
|
Assert.IsTrue(task.Wait(timeout), failureMessage);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|