1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 09:27:29 +08:00

Make importing work properly.

Moves import code to BeatmapDatabase.
This commit is contained in:
Dean Herbert 2016-10-21 18:25:22 +09:00 committed by Drew DeVault
parent 0c9e26e546
commit d3a857edb9
10 changed files with 286 additions and 97 deletions

View File

@ -6,8 +6,10 @@ using System.IO;
using System.Linq;
using osu.Framework;
using osu.Framework.Desktop;
using osu.Framework.Desktop.Platform;
using osu.Framework.Platform;
using osu.Game;
using osu.Game.IPC;
namespace osu.Desktop
{
@ -16,19 +18,24 @@ namespace osu.Desktop
[STAThread]
public static int Main(string[] args)
{
BasicGameHost host = Host.GetSuitableHost(@"osu");
BaseGame osuGame = new OsuGame();
if (args.Length != 0 && args.All(File.Exists))
DesktopGameHost host = Host.GetSuitableHost(@"osu", true);
if (!host.IsPrimaryInstance)
{
host.Load(osuGame);
var beatmapIPC = new IpcChannel<OsuGame.ImportBeatmap>(host);
var importer = new BeatmapImporter(host);
foreach (var file in args)
beatmapIPC.SendMessage(new OsuGame.ImportBeatmap { Path = file }).Wait();
Console.WriteLine(@"Sent file to running instance");
return 0;
if (importer.Import(file).Wait(1000))
throw new TimeoutException(@"IPC took too long to send");
Console.WriteLine(@"Sent import requests to running instance");
}
host.Add(osuGame);
host.Run();
else
{
BaseGame osu = new OsuGame(args);
host.Add(osu);
host.Run();
}
return 0;
}
}

View File

@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
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
{
const string osz_path = @"../../../osu-resources/osu.Game.Resources/Beatmaps/241526 Soleily - Renatus.osz";
[TestFixtureSetUp]
public void SetUp()
{
}
[Test]
public void TestImportWhenClosed()
{
//unfortunately for the time being we need to reference osu.Framework.Desktop for a game host here.
HeadlessGameHost host = new HeadlessGameHost();
var osu = loadOsu(host);
osu.Beatmaps.Import(osz_path);
ensureLoaded(osu);
}
[Test]
public void TestImportOverIPC()
{
HeadlessGameHost host = new HeadlessGameHost(true);
HeadlessGameHost client = new HeadlessGameHost(true);
Assert.IsTrue(host.IsPrimaryInstance);
Assert.IsTrue(!client.IsPrimaryInstance);
var osu = loadOsu(host);
var importer = new BeatmapImporter(client);
if (!importer.Import(osz_path).Wait(1000))
Assert.Fail(@"IPC took too long to send");
ensureLoaded(osu, 10000);
}
private OsuGameBase loadOsu(BasicGameHost host)
{
var osu = new OsuGameBase();
host.Add(osu);
//reset beatmap database (sqlite and storage backing)
osu.Beatmaps.Reset();
return osu;
}
private void ensureLoaded(OsuGameBase osu, int timeout = 100)
{
IEnumerable<BeatmapSetInfo> resultSets = null;
Action waitAction = () =>
{
while ((resultSets = osu.Beatmaps.Query<BeatmapSetInfo>().Where(s => s.BeatmapSetID == 241526)).Count() != 1)
Thread.Sleep(1);
};
Assert.IsTrue(waitAction.BeginInvoke(null, null).AsyncWaitHandle.WaitOne(timeout),
@"BeatmapSet did not import to the database");
//ensure we were stored to beatmap database backing...
Assert.IsTrue(resultSets.Count() == 1);
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 = () =>
{
while ((resultBeatmaps = osu.Beatmaps.Query<BeatmapInfo>().Where(s => s.BeatmapSetID == 241526 && s.BaseDifficultyID > 0)).Count() != 12)
Thread.Sleep(1);
};
Assert.IsTrue(waitAction.BeginInvoke(null, null).AsyncWaitHandle.WaitOne(timeout),
@"Beatmaps did not import to the database");
//fetch children and check we can load from the post-storage path...
var set = osu.Beatmaps.GetChildren(resultSets.First());
Assert.IsTrue(set.Beatmaps.Count == resultBeatmaps.Count());
foreach (BeatmapInfo b in resultBeatmaps)
Assert.IsTrue(set.Beatmaps.Any(c => c.BeatmapID == b.BeatmapID));
Assert.IsTrue(set.Beatmaps.Count > 0);
var beatmap = osu.Beatmaps.GetBeatmap(set.Beatmaps[0]);
Assert.IsTrue(beatmap.HitObjects.Count > 0);
}
}
}

11
osu.Game.Tests/app.config Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@ -30,6 +30,21 @@
<Reference Include="nunit.framework, Version=3.5.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.5.0\lib\net45\nunit.framework.dll</HintPath>
<Private>True</Private>
<Reference Include="SQLite.Net, Version=3.0.5.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SQLite.Net-PCL.3.0.5\lib\net40\SQLite.Net.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="SQLite.Net.Platform.Generic, Version=3.0.5.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SQLite.Net-PCL.3.0.5\lib\net40\SQLite.Net.Platform.Generic.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="SQLite.Net.Platform.Win32, Version=3.0.5.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SQLite.Net-PCL.3.0.5\lib\net4\SQLite.Net.Platform.Win32.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="nunit.framework">
<HintPath>$(SolutionDir)\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="OpenTK, Version=2.0.0.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
<HintPath>..\packages\ppy.OpenTK.2.0.50727.1337\lib\net20\OpenTK.dll</HintPath>
@ -40,10 +55,19 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
<None Include="OpenTK.dll.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\osu-framework\osu.Framework.Desktop\osu.Framework.Desktop.csproj">
<Project>{65DC628F-A640-4111-AB35-3A5652BC1E17}</Project>
<Name>osu.Framework.Desktop</Name>
</ProjectReference>
<ProjectReference Include="..\osu-framework\osu.Framework\osu.Framework.csproj">
<Project>{c76bf5b3-985e-4d39-95fe-97c9c879b83a}</Project>
<Name>osu.Framework</Name>
</ProjectReference>
<ProjectReference Include="..\osu.Game\osu.Game.csproj">
<Project>{0D3FBF8A-7464-4CF7-8C90-3E7886DF2D4D}</Project>
<Name>osu.Game</Name>
@ -53,14 +77,10 @@
<Name>osu.Game.Resources</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\" />
<Folder Include="Beatmaps\" />
<Folder Include="Beatmaps\IO\" />
<Folder Include="Beatmaps\Formats\" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Compile Include="Beatmaps\IO\OszArchiveReaderTest.cs" />
<Compile Include="Beatmaps\IO\ImportBeatmapTest.cs" />
<Compile Include="Resources\Resource.cs" />
<Compile Include="Beatmaps\Formats\OsuLegacyDecoderTest.cs" />
</ItemGroup>

View File

@ -2,4 +2,5 @@
<packages>
<package id="NUnit" version="3.5.0" targetFramework="net45" />
<package id="ppy.OpenTK" version="2.0.50727.1337" targetFramework="net45" />
</packages>
<package id="SQLite.Net-PCL" version="3.0.5" targetFramework="net45" />
</packages>

View File

@ -9,6 +9,7 @@ using osu.Framework.Platform;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Formats;
using osu.Game.Beatmaps.IO;
using osu.Game.IPC;
using SQLite.Net;
using SQLiteNetExtensions.Extensions;
@ -19,10 +20,15 @@ namespace osu.Game.Database
private static SQLiteConnection connection { get; set; }
private BasicStorage storage;
public event Action<BeatmapSetInfo> BeatmapSetAdded;
public BeatmapDatabase(BasicStorage storage)
private BeatmapImporter ipc;
public BeatmapDatabase(BasicGameHost host)
{
this.storage = storage;
this.storage = host.Storage;
ipc = new BeatmapImporter(host, this);
if (connection == null)
{
connection = storage.GetDatabase(@"beatmaps");
@ -44,58 +50,63 @@ namespace osu.Game.Database
connection.DeleteAll<BeatmapInfo>();
}
public void Import(string path)
public void Import(params string[] paths)
{
string hash = null;
BeatmapMetadata metadata;
using (var reader = ArchiveReader.GetReader(storage, path))
metadata = reader.ReadMetadata();
if (connection.Table<BeatmapSetInfo>().Count(b => b.BeatmapSetID == metadata.BeatmapSetID) != 0)
return; // TODO: Update this beatmap instead
if (File.Exists(path)) // Not always the case, i.e. for LegacyFilesystemReader
foreach (string p in paths)
{
using (var md5 = MD5.Create())
using (var input = storage.GetStream(path))
var path = p;
string hash = null;
BeatmapMetadata metadata;
using (var reader = ArchiveReader.GetReader(storage, path))
metadata = reader.ReadMetadata();
if (connection.Table<BeatmapSetInfo>().Count(b => b.BeatmapSetID == metadata.BeatmapSetID) != 0)
return; // TODO: Update this beatmap instead
if (File.Exists(path)) // Not always the case, i.e. for LegacyFilesystemReader
{
hash = BitConverter.ToString(md5.ComputeHash(input)).Replace("-", "").ToLowerInvariant();
input.Seek(0, SeekOrigin.Begin);
path = Path.Combine(@"beatmaps", hash.Remove(1), hash.Remove(2), hash);
using (var output = storage.GetStream(path, FileAccess.Write))
input.CopyTo(output);
}
}
var beatmapSet = new BeatmapSetInfo
{
BeatmapSetID = metadata.BeatmapSetID,
Beatmaps = new List<BeatmapInfo>(),
Path = path,
Hash = hash,
Metadata = metadata
};
using (var reader = ArchiveReader.GetReader(storage, path))
{
string[] mapNames = reader.ReadBeatmaps();
foreach (var name in mapNames)
{
using (var stream = new StreamReader(reader.ReadFile(name)))
using (var md5 = MD5.Create())
using (var input = storage.GetStream(path))
{
var decoder = BeatmapDecoder.GetDecoder(stream);
Beatmap beatmap = decoder.Decode(stream);
beatmap.BeatmapInfo.Path = name;
// TODO: Diff beatmap metadata with set metadata and leave it here if necessary
beatmap.BeatmapInfo.Metadata = null;
beatmapSet.Beatmaps.Add(beatmap.BeatmapInfo);
hash = BitConverter.ToString(md5.ComputeHash(input)).Replace("-", "").ToLowerInvariant();
input.Seek(0, SeekOrigin.Begin);
path = Path.Combine(@"beatmaps", hash.Remove(1), hash.Remove(2), hash);
using (var output = storage.GetStream(path, FileAccess.Write))
input.CopyTo(output);
}
}
var beatmapSet = new BeatmapSetInfo
{
BeatmapSetID = metadata.BeatmapSetID,
Beatmaps = new List<BeatmapInfo>(),
Path = path,
Hash = hash,
Metadata = metadata
};
using (var reader = ArchiveReader.GetReader(storage, path))
{
string[] mapNames = reader.ReadBeatmaps();
foreach (var name in mapNames)
{
using (var stream = new StreamReader(reader.ReadFile(name)))
{
var decoder = BeatmapDecoder.GetDecoder(stream);
Beatmap beatmap = decoder.Decode(stream);
beatmap.BeatmapInfo.Path = name;
// TODO: Diff beatmap metadata with set metadata and leave it here if necessary
beatmap.BeatmapInfo.Metadata = null;
beatmapSet.Beatmaps.Add(beatmap.BeatmapInfo);
}
}
}
connection.InsertWithChildren(beatmapSet, true);
BeatmapSetAdded?.Invoke(beatmapSet);
}
connection.InsertWithChildren(beatmapSet, true);
BeatmapSetAdded?.Invoke(beatmapSet);
}
public ArchiveReader GetReader(BeatmapSetInfo beatmapSet)
@ -154,7 +165,7 @@ namespace osu.Game.Database
typeof(BeatmapMetadata),
typeof(BaseDifficulty),
};
public void Update<T>(T record, bool cascade = true) where T : class
{
if (!validTypes.Any(t => t == typeof(T)))

View File

@ -0,0 +1,46 @@
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System.Diagnostics;
using System.Threading.Tasks;
using osu.Framework.Platform;
using osu.Game.Database;
namespace osu.Game.IPC
{
public class BeatmapImporter
{
private IpcChannel<BeatmapImportMessage> channel;
private BeatmapDatabase beatmaps;
public BeatmapImporter(BasicGameHost host, BeatmapDatabase beatmaps = null)
{
this.beatmaps = beatmaps;
channel = new IpcChannel<BeatmapImportMessage>(host);
channel.MessageReceived += messageReceived;
}
public async Task Import(string path)
{
if (beatmaps != null)
beatmaps.Import(path);
else
{
await channel.SendMessage(new BeatmapImportMessage { Path = path });
}
}
private void messageReceived(BeatmapImportMessage msg)
{
Debug.Assert(beatmaps != null);
Import(msg.Path);
}
}
public class BeatmapImportMessage
{
public string Path;
}
}

View File

@ -9,38 +9,33 @@ using osu.Game.GameModes.Menu;
using OpenTK;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Platform;
using osu.Game.GameModes;
using osu.Game.Graphics.Background;
using osu.Game.GameModes.Play;
using osu.Game.Graphics.Containers;
using osu.Game.Overlays;
using osu.Framework;
using osu.Framework.Input;
using osu.Game.Input;
using OpenTK.Input;
using System.IO;
using osu.Game.Beatmaps.IO;
using osu.Framework.Logging;
namespace osu.Game
{
public class OsuGame : OsuGameBase
{
public class ImportBeatmap
{
public string Path;
}
public Toolbar Toolbar;
public ChatConsole Chat;
public MainMenu MainMenu => intro?.ChildGameMode as MainMenu;
private Intro intro;
private IpcChannel<ImportBeatmap> BeatmapIPC;
public Bindable<PlayMode> PlayMode;
string[] args;
public OsuGame(string[] args = null)
{
this.args = args;
}
public override void SetHost(BasicGameHost host)
{
base.SetHost(host);
@ -50,30 +45,17 @@ namespace osu.Game
public override void Load(BaseGame game)
{
BeatmapIPC = new IpcChannel<ImportBeatmap>(Host);
if (!Host.IsPrimaryInstance)
{
Logger.Log(@"osu! does not support multiple running instances.", LoggingTarget.Runtime, LogLevel.Error);
Environment.Exit(0);
}
BeatmapIPC.MessageReceived += message =>
{
try
{
Beatmaps.ImportBeatmap(message.Path);
// TODO: Switch to beatmap list and select the new song
}
catch (Exception ex)
{
// TODO: Show the user some info?
Logger.Log($@"Failed to import beatmap: {ex}", LoggingTarget.Runtime, LogLevel.Error);
}
};
base.Load(game);
if (args?.Length > 0)
Schedule(delegate { Beatmaps.Import(args); });
//attach our bindables to the audio subsystem.
Audio.Volume.Weld(Config.GetBindable<double>(OsuConfig.VolumeGlobal));
Audio.VolumeSample.Weld(Config.GetBindable<double>(OsuConfig.VolumeEffect));

View File

@ -10,6 +10,7 @@ using osu.Game.Configuration;
using osu.Game.Database;
using osu.Game.Graphics.Cursor;
using osu.Game.Graphics.Processing;
using osu.Game.IPC;
using osu.Game.Online.API;
using osu.Game.Overlays;
@ -18,7 +19,7 @@ namespace osu.Game
public class OsuGameBase : BaseGame
{
internal OsuConfigManager Config = new OsuConfigManager();
internal BeatmapDatabase Beatmaps { get; private set; }
public BeatmapDatabase Beatmaps { get; private set; }
protected override string MainResourceFile => @"osu.Game.Resources.dll";
@ -47,7 +48,7 @@ namespace osu.Game
base.Load(game);
OszArchiveReader.Register();
Beatmaps = new BeatmapDatabase(Host.Storage);
Beatmaps = new BeatmapDatabase(Host);
//this completely overrides the framework default. will need to change once we make a proper FontStore.
Fonts = new TextureStore() { ScaleAdjust = 0.01f };

View File

@ -156,6 +156,7 @@
<Compile Include="GameModes\Play\Catch\CatchComboCounter.cs" />
<Compile Include="GameModes\Play\Osu\OsuComboCounter.cs" />
<Compile Include="Graphics\UserInterface\StarCounter.cs" />
<Compile Include="IPC\BeatmapImporter.cs" />
<Compile Include="Online\API\APIAccess.cs" />
<Compile Include="Online\API\APIRequest.cs" />
<Compile Include="Online\API\OAuth.cs" />