1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-19 11:23:23 +08:00

Merge pull request #412 from revam/remove-source-on-import

Remove source on Import(IEnumerable<string> paths), Import(string path)
This commit is contained in:
Dean Herbert 2017-03-03 20:59:26 +09:00 committed by GitHub
commit 9fe86583f1
2 changed files with 100 additions and 8 deletions

View File

@ -2,6 +2,7 @@
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
@ -40,8 +41,16 @@ namespace osu.Game.Tests.Beatmaps.IO
using (HeadlessGameHost host = new HeadlessGameHost())
{
var osu = loadOsu(host);
osu.Dependencies.Get<BeatmapDatabase>().Import(osz_path);
var temp = prepareTempCopy(osz_path);
Assert.IsTrue(File.Exists(temp));
osu.Dependencies.Get<BeatmapDatabase>().Import(temp);
ensureLoaded(osu);
Assert.IsFalse(File.Exists(temp));
}
}
@ -56,14 +65,51 @@ namespace osu.Game.Tests.Beatmaps.IO
var osu = loadOsu(host);
var temp = prepareTempCopy(osz_path);
Assert.IsTrue(File.Exists(temp));
var importer = new BeatmapImporter(client);
if (!importer.ImportAsync(osz_path).Wait(1000))
if (!importer.ImportAsync(temp).Wait(1000))
Assert.Fail(@"IPC took too long to send");
ensureLoaded(osu);
Assert.IsFalse(File.Exists(temp));
}
}
[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())
{
var osu = loadOsu(host);
var temp = prepareTempCopy(osz_path);
Assert.IsTrue(File.Exists(temp));
using (FileStream stream = File.OpenRead(temp))
osu.Dependencies.Get<BeatmapDatabase>().Import(temp);
ensureLoaded(osu);
Assert.IsTrue(File.Exists(temp));
File.Delete(temp);
Assert.IsFalse(File.Exists(temp));
}
}
private string prepareTempCopy(string path)
{
var temp = Path.GetTempFileName();
return new FileInfo(osz_path).CopyTo(temp, true).FullName;
}
private OsuGameBase loadOsu(GameHost host)
{
var osu = new OsuGameBase();

View File

@ -116,13 +116,61 @@ namespace osu.Game.Database
connection.DeleteAll<BeatmapInfo>();
}
/// <summary>
/// Import multiple <see cref="BeatmapSetInfo"/> from <paramref name="paths"/>.
/// </summary>
/// <param name="paths">Multiple locations on disk</param>
public void Import(IEnumerable<string> paths)
{
Stack<BeatmapSetInfo> sets = new Stack<BeatmapSetInfo>();
foreach (string p in paths)
Import(p);
try
{
BeatmapSetInfo set = getBeatmapSet(p);
//If we have an ID then we already exist in the database.
if (set.ID == 0)
sets.Push(set);
// We may or may not want to delete the file depending on where it is stored.
// e.g. reconstructing/repairing database with beatmaps from default storage.
// Also, not always a single file, i.e. for LegacyFilesystemReader
// TODO: Add a check to prevent files from storage to be deleted.
try
{
File.Delete(p);
}
catch (Exception e)
{
Logger.Error(e, $@"Could not delete file at {p}");
}
}
catch (Exception e)
{
e = e.InnerException ?? e;
Logger.Error(e, $@"Could not import beatmap set");
}
// Batch commit with multiple sets to database
Import(sets);
}
/// <summary>
/// Import <see cref="BeatmapSetInfo"/> from <paramref name="path"/>.
/// </summary>
/// <param name="path">Location on disk</param>
public void Import(string path)
{
Import(new [] { path });
}
/// <summary>
/// Duplicates content from <paramref name="path"/> to storage and returns a representing <see cref="BeatmapSetInfo"/>.
/// </summary>
/// <param name="path">Content location</param>
/// <returns><see cref="BeatmapSetInfo"/></returns>
private BeatmapSetInfo getBeatmapSet(string path)
{
string hash = null;
@ -156,7 +204,7 @@ namespace osu.Game.Database
BeatmapSetAdded?.Invoke(existing);
}
return;
return existing;
}
var beatmapSet = new BeatmapSetInfo
@ -172,7 +220,6 @@ namespace osu.Game.Database
{
string[] mapNames = reader.BeatmapFilenames;
foreach (var name in mapNames)
{
using (var stream = new StreamReader(reader.GetStream(name)))
{
var decoder = BeatmapDecoder.GetDecoder(stream);
@ -184,11 +231,10 @@ namespace osu.Game.Database
beatmapSet.Beatmaps.Add(beatmap.BeatmapInfo);
}
beatmapSet.StoryboardFile = reader.StoryboardFilename;
}
beatmapSet.StoryboardFile = reader.StoryboardFilename;
}
Import(new[] { beatmapSet });
return beatmapSet;
}
public void Import(IEnumerable<BeatmapSetInfo> beatmapSets)