2017-02-14 15:13:25 +08:00
|
|
|
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
|
|
using System;
|
2017-02-28 21:36:11 +08:00
|
|
|
|
using System.IO;
|
2016-10-21 17:25:22 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
2017-02-26 19:45:41 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2016-10-21 17:25:22 +08:00
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using osu.Framework.Desktop.Platform;
|
|
|
|
|
using osu.Framework.Platform;
|
|
|
|
|
using osu.Game.Database;
|
|
|
|
|
using osu.Game.IPC;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.Beatmaps.IO
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class ImportBeatmapTest
|
|
|
|
|
{
|
2017-03-07 09:59:19 +08:00
|
|
|
|
private const string osz_path = @"../../../osu-resources/osu.Game.Resources/Beatmaps/241526 Soleily - Renatus.osz";
|
2016-10-21 17:25:22 +08:00
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestImportWhenClosed()
|
|
|
|
|
{
|
|
|
|
|
//unfortunately for the time being we need to reference osu.Framework.Desktop for a game host here.
|
2016-11-21 11:34:03 +08:00
|
|
|
|
using (HeadlessGameHost host = new HeadlessGameHost())
|
|
|
|
|
{
|
2017-05-02 14:32:47 +08:00
|
|
|
|
loadOsu(host);
|
2017-02-28 21:36:11 +08:00
|
|
|
|
|
|
|
|
|
var temp = prepareTempCopy(osz_path);
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(File.Exists(temp));
|
|
|
|
|
|
2017-05-02 14:32:47 +08:00
|
|
|
|
host.Dependencies.Get<BeatmapDatabase>().Import(temp);
|
2017-02-28 21:36:11 +08:00
|
|
|
|
|
2017-05-02 14:32:47 +08:00
|
|
|
|
ensureLoaded(host);
|
2017-02-28 21:36:11 +08:00
|
|
|
|
|
|
|
|
|
Assert.IsFalse(File.Exists(temp));
|
2016-11-21 11:34:03 +08:00
|
|
|
|
}
|
2016-10-21 17:25:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestImportOverIPC()
|
|
|
|
|
{
|
2016-11-21 11:34:03 +08:00
|
|
|
|
using (HeadlessGameHost host = new HeadlessGameHost("host", true))
|
|
|
|
|
using (HeadlessGameHost client = new HeadlessGameHost("client", true))
|
|
|
|
|
{
|
|
|
|
|
Assert.IsTrue(host.IsPrimaryInstance);
|
|
|
|
|
Assert.IsTrue(!client.IsPrimaryInstance);
|
2016-10-21 17:25:22 +08:00
|
|
|
|
|
2017-05-02 14:32:47 +08:00
|
|
|
|
loadOsu(host);
|
2016-10-21 17:25:22 +08:00
|
|
|
|
|
2017-02-28 21:36:11 +08:00
|
|
|
|
var temp = prepareTempCopy(osz_path);
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(File.Exists(temp));
|
|
|
|
|
|
2017-03-04 17:51:16 +08:00
|
|
|
|
var importer = new BeatmapIPCChannel(client);
|
2017-05-31 14:51:21 +08:00
|
|
|
|
if (!importer.ImportAsync(temp).Wait(10000))
|
2016-11-21 11:34:03 +08:00
|
|
|
|
Assert.Fail(@"IPC took too long to send");
|
2016-10-21 17:25:22 +08:00
|
|
|
|
|
2017-05-02 14:32:47 +08:00
|
|
|
|
ensureLoaded(host);
|
2017-02-28 21:36:11 +08:00
|
|
|
|
|
|
|
|
|
Assert.IsFalse(File.Exists(temp));
|
2016-11-21 11:34:03 +08:00
|
|
|
|
}
|
2016-10-21 17:25:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-02 22:24:12 +08:00
|
|
|
|
[Test]
|
|
|
|
|
public void TestImportWhenFileOpen()
|
|
|
|
|
{
|
|
|
|
|
//unfortunately for the time being we need to reference osu.Framework.Desktop for a game host here.
|
|
|
|
|
using (HeadlessGameHost host = new HeadlessGameHost())
|
|
|
|
|
{
|
2017-05-02 14:32:47 +08:00
|
|
|
|
loadOsu(host);
|
2017-03-02 22:24:12 +08:00
|
|
|
|
|
|
|
|
|
var temp = prepareTempCopy(osz_path);
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(File.Exists(temp));
|
|
|
|
|
|
2017-03-07 12:30:36 +08:00
|
|
|
|
using (File.OpenRead(temp))
|
2017-05-02 14:32:47 +08:00
|
|
|
|
host.Dependencies.Get<BeatmapDatabase>().Import(temp);
|
2017-03-02 22:24:12 +08:00
|
|
|
|
|
2017-05-02 14:32:47 +08:00
|
|
|
|
ensureLoaded(host);
|
2017-03-02 22:24:12 +08:00
|
|
|
|
|
|
|
|
|
Assert.IsTrue(File.Exists(temp));
|
2017-04-06 16:21:18 +08:00
|
|
|
|
|
2017-03-02 22:24:12 +08:00
|
|
|
|
File.Delete(temp);
|
|
|
|
|
|
|
|
|
|
Assert.IsFalse(File.Exists(temp));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-28 21:36:11 +08:00
|
|
|
|
private string prepareTempCopy(string path)
|
|
|
|
|
{
|
2017-03-14 10:46:34 +08:00
|
|
|
|
var temp = Path.GetTempFileName();
|
2017-03-07 09:59:19 +08:00
|
|
|
|
return new FileInfo(path).CopyTo(temp, true).FullName;
|
2017-02-28 21:36:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-23 14:38:17 +08:00
|
|
|
|
private OsuGameBase loadOsu(GameHost host)
|
2016-10-21 17:25:22 +08:00
|
|
|
|
{
|
|
|
|
|
var osu = new OsuGameBase();
|
2017-02-26 19:45:41 +08:00
|
|
|
|
Task.Run(() => host.Run(osu));
|
2016-10-21 17:25:22 +08:00
|
|
|
|
|
2016-11-03 15:59:16 +08:00
|
|
|
|
while (!osu.IsLoaded)
|
|
|
|
|
Thread.Sleep(1);
|
|
|
|
|
|
2016-10-21 17:25:22 +08:00
|
|
|
|
//reset beatmap database (sqlite and storage backing)
|
2017-05-02 14:32:47 +08:00
|
|
|
|
host.Dependencies.Get<RulesetDatabase>().Reset();
|
|
|
|
|
host.Dependencies.Get<BeatmapDatabase>().Reset();
|
2016-10-21 17:25:22 +08:00
|
|
|
|
|
|
|
|
|
return osu;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-02 14:32:47 +08:00
|
|
|
|
private void ensureLoaded(GameHost host, int timeout = 10000)
|
2016-10-21 17:25:22 +08:00
|
|
|
|
{
|
|
|
|
|
IEnumerable<BeatmapSetInfo> resultSets = null;
|
|
|
|
|
|
|
|
|
|
Action waitAction = () =>
|
|
|
|
|
{
|
2017-05-02 14:32:47 +08:00
|
|
|
|
while (!(resultSets = host.Dependencies.Get<BeatmapDatabase>()
|
2017-05-06 16:06:38 +08:00
|
|
|
|
.Query<BeatmapSetInfo>().Where(s => s.OnlineBeatmapSetID == 241526)).Any())
|
2017-02-25 10:46:04 +08:00
|
|
|
|
Thread.Sleep(50);
|
2016-10-21 17:25:22 +08:00
|
|
|
|
};
|
|
|
|
|
|
2017-04-17 19:10:51 +08:00
|
|
|
|
Assert.IsTrue(waitAction.BeginInvoke(null, null).AsyncWaitHandle.WaitOne(timeout),
|
2017-03-07 09:59:19 +08:00
|
|
|
|
@"BeatmapSet did not import to the database in allocated time.");
|
2016-10-21 17:25:22 +08:00
|
|
|
|
|
|
|
|
|
//ensure we were stored to beatmap database backing...
|
2016-11-05 21:48:32 +08:00
|
|
|
|
|
2017-02-25 10:46:04 +08:00
|
|
|
|
Assert.IsTrue(resultSets.Count() == 1, $@"Incorrect result count found ({resultSets.Count()} but should be 1).");
|
2016-10-21 17:25:22 +08:00
|
|
|
|
|
|
|
|
|
IEnumerable<BeatmapInfo> resultBeatmaps = null;
|
|
|
|
|
|
|
|
|
|
//if we don't re-check here, the set will be inserted but the beatmaps won't be present yet.
|
|
|
|
|
waitAction = () =>
|
|
|
|
|
{
|
2017-05-02 14:32:47 +08:00
|
|
|
|
while ((resultBeatmaps = host.Dependencies.Get<BeatmapDatabase>()
|
2017-05-06 15:37:53 +08:00
|
|
|
|
.GetAllWithChildren<BeatmapInfo>(s => s.OnlineBeatmapSetID == 241526 && s.BaseDifficultyID > 0)).Count() != 12)
|
2017-02-25 10:55:30 +08:00
|
|
|
|
Thread.Sleep(50);
|
2016-10-21 17:25:22 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(waitAction.BeginInvoke(null, null).AsyncWaitHandle.WaitOne(timeout),
|
2017-02-25 10:55:30 +08:00
|
|
|
|
@"Beatmaps did not import to the database in allocated time");
|
2016-10-21 17:25:22 +08:00
|
|
|
|
|
2017-05-11 17:14:48 +08:00
|
|
|
|
var set = host.Dependencies.Get<BeatmapDatabase>().GetChildren(resultSets.First());
|
2016-10-21 17:25:22 +08:00
|
|
|
|
|
2017-02-25 10:55:30 +08:00
|
|
|
|
Assert.IsTrue(set.Beatmaps.Count == resultBeatmaps.Count(),
|
|
|
|
|
$@"Incorrect database beatmap count post-import ({resultBeatmaps.Count()} but should be {set.Beatmaps.Count}).");
|
2016-10-21 17:25:22 +08:00
|
|
|
|
|
|
|
|
|
foreach (BeatmapInfo b in resultBeatmaps)
|
2017-01-09 21:05:01 +08:00
|
|
|
|
Assert.IsTrue(set.Beatmaps.Any(c => c.OnlineBeatmapID == b.OnlineBeatmapID));
|
2016-10-21 17:25:22 +08:00
|
|
|
|
|
|
|
|
|
Assert.IsTrue(set.Beatmaps.Count > 0);
|
|
|
|
|
|
2017-05-02 14:32:47 +08:00
|
|
|
|
var beatmap = host.Dependencies.Get<BeatmapDatabase>().GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 0))?.Beatmap;
|
2017-04-18 08:46:14 +08:00
|
|
|
|
Assert.IsTrue(beatmap?.HitObjects.Count > 0);
|
|
|
|
|
|
2017-05-02 14:32:47 +08:00
|
|
|
|
beatmap = host.Dependencies.Get<BeatmapDatabase>().GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 1))?.Beatmap;
|
2017-04-18 08:46:14 +08:00
|
|
|
|
Assert.IsTrue(beatmap?.HitObjects.Count > 0);
|
|
|
|
|
|
2017-05-02 14:32:47 +08:00
|
|
|
|
beatmap = host.Dependencies.Get<BeatmapDatabase>().GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 2))?.Beatmap;
|
2017-04-18 08:46:14 +08:00
|
|
|
|
Assert.IsTrue(beatmap?.HitObjects.Count > 0);
|
2016-10-21 17:25:22 +08:00
|
|
|
|
|
2017-05-02 14:32:47 +08:00
|
|
|
|
beatmap = host.Dependencies.Get<BeatmapDatabase>().GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 3))?.Beatmap;
|
2017-03-07 09:59:19 +08:00
|
|
|
|
Assert.IsTrue(beatmap?.HitObjects.Count > 0);
|
2016-10-21 17:25:22 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|