1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 23:07:44 +08:00
osu-lazer/osu.Game/OsuGameBase_Importing.cs
2022-06-17 16:37:17 +09:00

60 lines
2.2 KiB
C#

// 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.
#nullable disable
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using osu.Game.Database;
namespace osu.Game
{
public partial class OsuGameBase
{
private readonly List<ICanAcceptFiles> fileImporters = new List<ICanAcceptFiles>();
/// <summary>
/// Register a global handler for file imports. Most recently registered will have precedence.
/// </summary>
/// <param name="handler">The handler to register.</param>
public void RegisterImportHandler(ICanAcceptFiles handler) => fileImporters.Insert(0, handler);
/// <summary>
/// Unregister a global handler for file imports.
/// </summary>
/// <param name="handler">The previously registered handler.</param>
public void UnregisterImportHandler(ICanAcceptFiles handler) => fileImporters.Remove(handler);
public async Task Import(params string[] paths)
{
if (paths.Length == 0)
return;
var filesPerExtension = paths.GroupBy(p => Path.GetExtension(p).ToLowerInvariant());
foreach (var groups in filesPerExtension)
{
foreach (var importer in fileImporters)
{
if (importer.HandledExtensions.Contains(groups.Key))
await importer.Import(groups.ToArray()).ConfigureAwait(false);
}
}
}
public virtual async Task Import(params ImportTask[] tasks)
{
var tasksPerExtension = tasks.GroupBy(t => Path.GetExtension(t.Path).ToLowerInvariant());
await Task.WhenAll(tasksPerExtension.Select(taskGroup =>
{
var importer = fileImporters.FirstOrDefault(i => i.HandledExtensions.Contains(taskGroup.Key));
return importer?.Import(taskGroup.ToArray()) ?? Task.CompletedTask;
})).ConfigureAwait(false);
}
public IEnumerable<string> HandledExtensions => fileImporters.SelectMany(i => i.HandledExtensions);
}
}