1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-13 05:22:54 +08:00

Crentalise all import logic

This commit is contained in:
Dean Herbert 2018-02-15 14:19:16 +09:00
parent d3dd31dadb
commit a0a65abcac
8 changed files with 45 additions and 23 deletions

View File

@ -115,21 +115,7 @@ namespace osu.Desktop
if (filePaths.Any(f => Path.GetExtension(f) != firstExtension)) return;
switch (firstExtension)
{
case ".osz":
Task.Factory.StartNew(() => BeatmapManager.Import(filePaths), TaskCreationOptions.LongRunning);
return;
case ".osr":
Task.Run(() =>
{
var score = ScoreStore.ReadReplayFile(filePaths.First());
Schedule(() => LoadScore(score));
});
return;
}
Task.Factory.StartNew(() => Import(filePaths), TaskCreationOptions.LongRunning);
}
private static readonly string[] allowed_extensions = { @".osz", @".osr" };
}
}

View File

@ -20,7 +20,7 @@ namespace osu.Game.Database
/// </summary>
/// <typeparam name="TModel">The model type.</typeparam>
/// <typeparam name="TFileModel">The associated file join type.</typeparam>
public abstract class ArchiveModelManager<TModel, TFileModel> : ICanImportArchives
public abstract class ArchiveModelManager<TModel, TFileModel> : ICanAcceptFiles
where TModel : class, IHasFiles<TFileModel>, IHasPrimaryKey, ISoftDelete
where TFileModel : INamedFileInfo, new()
{

View File

@ -1,6 +1,6 @@
namespace osu.Game.Database
{
public interface ICanImportArchives
public interface ICanAcceptFiles
{
void Import(params string[] paths);

View File

@ -12,9 +12,9 @@ namespace osu.Game.IPC
{
public class ArchiveImportIPCChannel : IpcChannel<ArchiveImportMessage>
{
private readonly ICanImportArchives importer;
private readonly ICanAcceptFiles importer;
public ArchiveImportIPCChannel(IIpcHost host, ICanImportArchives importer = null)
public ArchiveImportIPCChannel(IIpcHost host, ICanAcceptFiles importer = null)
: base(host)
{
this.importer = importer;

View File

@ -105,6 +105,8 @@ namespace osu.Game
{
this.frameworkConfig = frameworkConfig;
ScoreStore.ScoreImported += score => Schedule(() => LoadScore(score));
if (!Host.IsPrimaryInstance)
{
Logger.Log(@"osu! does not support multiple running instances.", LoggingTarget.Runtime, LogLevel.Error);
@ -114,7 +116,8 @@ namespace osu.Game
if (args?.Length > 0)
{
var paths = args.Where(a => !a.StartsWith(@"-"));
Task.Run(() => BeatmapManager.Import(paths.ToArray()));
Task.Run(() => Import(paths.ToArray()));
}
dependencies.CacheAs(this);

View File

@ -2,7 +2,10 @@
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using osu.Framework.Allocation;
using osu.Framework.Audio;
@ -30,7 +33,7 @@ using osu.Game.Rulesets.Scoring;
namespace osu.Game
{
public class OsuGameBase : Framework.Game, IOnlineComponent
public class OsuGameBase : Framework.Game, IOnlineComponent, ICanAcceptFiles
{
protected OsuConfigManager LocalConfig;
@ -114,6 +117,8 @@ namespace osu.Game
dependencies.Cache(SettingsStore = new SettingsStore(contextFactory));
dependencies.Cache(new OsuColour());
fileImporters.Add(BeatmapManager);
//this completely overrides the framework default. will need to change once we make a proper FontStore.
dependencies.Cache(Fonts = new FontStore { ScaleAdjust = 100 });
@ -257,5 +262,17 @@ namespace osu.Game
base.Dispose(isDisposing);
}
private readonly List<ICanAcceptFiles> fileImporters = new List<ICanAcceptFiles>();
public void Import(params string[] paths)
{
var extension = Path.GetExtension(paths.First());
foreach (var importer in fileImporters)
if (importer.HandledExtensions.Contains(extension)) importer.Import(paths);
}
public string[] HandledExtensions => fileImporters.SelectMany(i => i.HandledExtensions).ToArray();
}
}

View File

@ -1,6 +1,7 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using System.IO;
using osu.Framework.Platform;
@ -14,7 +15,7 @@ using SharpCompress.Compressors.LZMA;
namespace osu.Game.Rulesets.Scoring
{
public class ScoreStore : DatabaseBackedStore
public class ScoreStore : DatabaseBackedStore, ICanAcceptFiles
{
private readonly Storage storage;
@ -23,6 +24,8 @@ namespace osu.Game.Rulesets.Scoring
private const string replay_folder = @"replays";
public event Action<Score> ScoreImported;
// ReSharper disable once NotAccessedField.Local (we should keep a reference to this so it is not finalised)
private ScoreIPCChannel ipc;
@ -36,6 +39,18 @@ namespace osu.Game.Rulesets.Scoring
ipc = new ScoreIPCChannel(importHost, this);
}
public string[] HandledExtensions => new[] { ".osr" };
public void Import(params string[] paths)
{
foreach (var path in paths)
{
var score = ReadReplayFile(path);
if (score != null)
ScoreImported?.Invoke(score);
}
}
public Score ReadReplayFile(string replayFilename)
{
Score score;
@ -159,5 +174,6 @@ namespace osu.Game.Rulesets.Scoring
return new Replay { Frames = frames };
}
}
}

View File

@ -277,7 +277,7 @@
<Compile Include="Database\ArchiveModelManager.cs" />
<Compile Include="Database\DatabaseContextFactory.cs" />
<Compile Include="Database\DatabaseWriteUsage.cs" />
<Compile Include="Database\ICanImportArchives.cs" />
<Compile Include="Database\ICanAcceptFiles.cs" />
<Compile Include="Database\IDatabaseContextFactory.cs" />
<Compile Include="Database\IHasPrimaryKey.cs" />
<Compile Include="Database\INamedFileInfo.cs" />