1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-12 16:02:55 +08:00

Merge pull request #21628 from peppy/go-hard-file-writes

Use hard links on windows when importing beatmaps from a legacy osu! install
This commit is contained in:
Dean Herbert 2022-12-16 14:52:57 +09:00 committed by GitHub
commit 232f590ba3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 263 additions and 60 deletions

View File

@ -564,7 +564,7 @@ namespace osu.Game.Tests.Database
var imported = await importer.Import( var imported = await importer.Import(
progressNotification, progressNotification,
new ImportTask(zipStream, string.Empty) new[] { new ImportTask(zipStream, string.Empty) }
); );
realm.Run(r => r.Refresh()); realm.Run(r => r.Refresh());
@ -1052,7 +1052,7 @@ namespace osu.Game.Tests.Database
{ {
string? temp = path ?? TestResources.GetTestBeatmapForImport(virtualTrack); string? temp = path ?? TestResources.GetTestBeatmapForImport(virtualTrack);
var importedSet = await importer.Import(new ImportTask(temp), batchImport); var importedSet = await importer.Import(new ImportTask(temp), new ImportParameters { Batch = batchImport });
Assert.NotNull(importedSet); Assert.NotNull(importedSet);
Debug.Assert(importedSet != null); Debug.Assert(importedSet != null);

View File

@ -226,12 +226,12 @@ namespace osu.Game.Tests.Online
this.testBeatmapManager = testBeatmapManager; this.testBeatmapManager = testBeatmapManager;
} }
public override Live<BeatmapSetInfo> ImportModel(BeatmapSetInfo item, ArchiveReader archive = null, bool batchImport = false, CancellationToken cancellationToken = default) public override Live<BeatmapSetInfo> ImportModel(BeatmapSetInfo item, ArchiveReader archive = null, ImportParameters parameters = default, CancellationToken cancellationToken = default)
{ {
if (!testBeatmapManager.AllowImport.Wait(TimeSpan.FromSeconds(10), cancellationToken)) if (!testBeatmapManager.AllowImport.Wait(TimeSpan.FromSeconds(10), cancellationToken))
throw new TimeoutException("Timeout waiting for import to be allowed."); throw new TimeoutException("Timeout waiting for import to be allowed.");
return (testBeatmapManager.CurrentImport = base.ImportModel(item, archive, batchImport, cancellationToken)); return (testBeatmapManager.CurrentImport = base.ImportModel(item, archive, parameters, cancellationToken));
} }
} }
} }

View File

@ -360,7 +360,7 @@ namespace osu.Game.Tests.Skins.IO
private async Task<Live<SkinInfo>> loadSkinIntoOsu(OsuGameBase osu, ImportTask import, bool batchImport = false) private async Task<Live<SkinInfo>> loadSkinIntoOsu(OsuGameBase osu, ImportTask import, bool batchImport = false)
{ {
var skinManager = osu.Dependencies.Get<SkinManager>(); var skinManager = osu.Dependencies.Get<SkinManager>();
return await skinManager.Import(import, batchImport); return await skinManager.Import(import, new ImportParameters { Batch = batchImport });
} }
} }
} }

View File

@ -44,7 +44,7 @@ namespace osu.Game.Beatmaps
public override async Task<Live<BeatmapSetInfo>?> ImportAsUpdate(ProgressNotification notification, ImportTask importTask, BeatmapSetInfo original) public override async Task<Live<BeatmapSetInfo>?> ImportAsUpdate(ProgressNotification notification, ImportTask importTask, BeatmapSetInfo original)
{ {
var imported = await Import(notification, importTask); var imported = await Import(notification, new[] { importTask });
if (!imported.Any()) if (!imported.Any())
return null; return null;
@ -203,10 +203,10 @@ namespace osu.Game.Beatmaps
} }
} }
protected override void PostImport(BeatmapSetInfo model, Realm realm, bool batchImport) protected override void PostImport(BeatmapSetInfo model, Realm realm, ImportParameters parameters)
{ {
base.PostImport(model, realm, batchImport); base.PostImport(model, realm, parameters);
ProcessBeatmap?.Invoke((model, batchImport)); ProcessBeatmap?.Invoke((model, parameters.Batch));
} }
private void validateOnlineIds(BeatmapSetInfo beatmapSet, Realm realm) private void validateOnlineIds(BeatmapSetInfo beatmapSet, Realm realm)

View File

@ -456,15 +456,15 @@ namespace osu.Game.Beatmaps
public Task Import(params string[] paths) => beatmapImporter.Import(paths); public Task Import(params string[] paths) => beatmapImporter.Import(paths);
public Task Import(params ImportTask[] tasks) => beatmapImporter.Import(tasks); public Task Import(ImportTask[] tasks, ImportParameters parameters = default) => beatmapImporter.Import(tasks, parameters);
public Task<IEnumerable<Live<BeatmapSetInfo>>> Import(ProgressNotification notification, params ImportTask[] tasks) => beatmapImporter.Import(notification, tasks); public Task<IEnumerable<Live<BeatmapSetInfo>>> Import(ProgressNotification notification, ImportTask[] tasks, ImportParameters parameters = default) => beatmapImporter.Import(notification, tasks, parameters);
public Task<Live<BeatmapSetInfo>?> Import(ImportTask task, bool batchImport = false, CancellationToken cancellationToken = default) => public Task<Live<BeatmapSetInfo>?> Import(ImportTask task, ImportParameters parameters = default, CancellationToken cancellationToken = default) =>
beatmapImporter.Import(task, batchImport, cancellationToken); beatmapImporter.Import(task, parameters, cancellationToken);
public Live<BeatmapSetInfo>? Import(BeatmapSetInfo item, ArchiveReader? archive = null, CancellationToken cancellationToken = default) => public Live<BeatmapSetInfo>? Import(BeatmapSetInfo item, ArchiveReader? archive = null, CancellationToken cancellationToken = default) =>
beatmapImporter.ImportModel(item, archive, false, cancellationToken); beatmapImporter.ImportModel(item, archive, default, cancellationToken);
public IEnumerable<string> HandledExtensions => beatmapImporter.HandledExtensions; public IEnumerable<string> HandledExtensions => beatmapImporter.HandledExtensions;

View File

@ -141,6 +141,9 @@ namespace osu.Game.Beatmaps
try try
{ {
string fileStorePath = BeatmapSetInfo.GetPathForFile(BeatmapInfo.Path); string fileStorePath = BeatmapSetInfo.GetPathForFile(BeatmapInfo.Path);
// TODO: check validity of file
var stream = GetStream(fileStorePath); var stream = GetStream(fileStorePath);
if (stream == null) if (stream == null)

View File

@ -31,7 +31,8 @@ namespace osu.Game.Database
/// This will post notifications tracking progress. /// This will post notifications tracking progress.
/// </remarks> /// </remarks>
/// <param name="tasks">The import tasks from which the files should be imported.</param> /// <param name="tasks">The import tasks from which the files should be imported.</param>
Task Import(params ImportTask[] tasks); /// <param name="parameters">Parameters to further configure the import process.</param>
Task Import(ImportTask[] tasks, ImportParameters parameters = default);
/// <summary> /// <summary>
/// An array of accepted file extensions (in the standard format of ".abc"). /// An array of accepted file extensions (in the standard format of ".abc").

View File

@ -20,8 +20,9 @@ namespace osu.Game.Database
/// </summary> /// </summary>
/// <param name="notification">The notification to update.</param> /// <param name="notification">The notification to update.</param>
/// <param name="tasks">The import tasks.</param> /// <param name="tasks">The import tasks.</param>
/// <param name="parameters">Parameters to further configure the import process.</param>
/// <returns>The imported models.</returns> /// <returns>The imported models.</returns>
Task<IEnumerable<Live<TModel>>> Import(ProgressNotification notification, params ImportTask[] tasks); Task<IEnumerable<Live<TModel>>> Import(ProgressNotification notification, ImportTask[] tasks, ImportParameters parameters = default);
/// <summary> /// <summary>
/// Process a single import as an update for an existing model. /// Process a single import as an update for an existing model.

View File

@ -0,0 +1,25 @@
// 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.
namespace osu.Game.Database
{
public struct ImportParameters
{
/// <summary>
/// Whether this import is part of a larger batch.
/// </summary>
/// <remarks>
/// May skip intensive pre-import checks in favour of faster processing.
///
/// More specifically, imports will be skipped before they begin, given an existing model matches on hash and filenames. Should generally only be used for large batch imports, as it may defy user expectations when updating an existing model.
///
/// Will also change scheduling behaviour to run at a lower priority.
/// </remarks>
public bool Batch { get; set; }
/// <summary>
/// Whether this import should use hard links rather than file copy operations if available.
/// </summary>
public bool PreferHardLinks { get; set; }
}
}

View File

@ -54,6 +54,19 @@ namespace osu.Game.Database
public void UpdateStorage(string stablePath) => cachedStorage = new StableStorage(stablePath, gameHost as DesktopGameHost); public void UpdateStorage(string stablePath) => cachedStorage = new StableStorage(stablePath, gameHost as DesktopGameHost);
public bool CheckHardLinkAvailability()
{
var stableStorage = GetCurrentStableStorage();
if (stableStorage == null || gameHost is not DesktopGameHost desktopGameHost)
return false;
string testExistingPath = stableStorage.GetFullPath(string.Empty);
string testDestinationPath = desktopGameHost.Storage.GetFullPath(string.Empty);
return HardLinkHelper.CheckAvailability(testDestinationPath, testExistingPath);
}
public virtual async Task<int> GetImportCount(StableContent content, CancellationToken cancellationToken) public virtual async Task<int> GetImportCount(StableContent content, CancellationToken cancellationToken)
{ {
var stableStorage = GetCurrentStableStorage(); var stableStorage = GetCurrentStableStorage();

View File

@ -57,7 +57,12 @@ namespace osu.Game.Database
return Task.CompletedTask; return Task.CompletedTask;
} }
return Task.Run(async () => await Importer.Import(GetStableImportPaths(storage).ToArray()).ConfigureAwait(false)); return Task.Run(async () =>
{
var tasks = GetStableImportPaths(storage).Select(p => new ImportTask(p)).ToArray();
await Importer.Import(tasks, new ImportParameters { Batch = true, PreferHardLinks = true }).ConfigureAwait(false);
});
} }
/// <summary> /// <summary>

View File

@ -73,7 +73,7 @@ namespace osu.Game.Database
if (originalModel != null) if (originalModel != null)
importSuccessful = (await importer.ImportAsUpdate(notification, new ImportTask(filename), originalModel)) != null; importSuccessful = (await importer.ImportAsUpdate(notification, new ImportTask(filename), originalModel)) != null;
else else
importSuccessful = (await importer.Import(notification, new ImportTask(filename))).Any(); importSuccessful = (await importer.Import(notification, new[] { new ImportTask(filename) })).Any();
// for now a failed import will be marked as a failed download for simplicity. // for now a failed import will be marked as a failed download for simplicity.
if (!importSuccessful) if (!importSuccessful)

View File

@ -81,16 +81,16 @@ namespace osu.Game.Database
public Task Import(params string[] paths) => Import(paths.Select(p => new ImportTask(p)).ToArray()); public Task Import(params string[] paths) => Import(paths.Select(p => new ImportTask(p)).ToArray());
public Task Import(params ImportTask[] tasks) public Task Import(ImportTask[] tasks, ImportParameters parameters = default)
{ {
var notification = new ProgressNotification { State = ProgressNotificationState.Active }; var notification = new ProgressNotification { State = ProgressNotificationState.Active };
PostNotification?.Invoke(notification); PostNotification?.Invoke(notification);
return Import(notification, tasks); return Import(notification, tasks, parameters);
} }
public async Task<IEnumerable<Live<TModel>>> Import(ProgressNotification notification, params ImportTask[] tasks) public async Task<IEnumerable<Live<TModel>>> Import(ProgressNotification notification, ImportTask[] tasks, ImportParameters parameters = default)
{ {
if (tasks.Length == 0) if (tasks.Length == 0)
{ {
@ -106,7 +106,7 @@ namespace osu.Game.Database
var imported = new List<Live<TModel>>(); var imported = new List<Live<TModel>>();
bool isBatchImport = tasks.Length >= minimum_items_considered_batch_import; parameters.Batch |= tasks.Length >= minimum_items_considered_batch_import;
await Task.WhenAll(tasks.Select(async task => await Task.WhenAll(tasks.Select(async task =>
{ {
@ -115,7 +115,7 @@ namespace osu.Game.Database
try try
{ {
var model = await Import(task, isBatchImport, notification.CancellationToken).ConfigureAwait(false); var model = await Import(task, parameters, notification.CancellationToken).ConfigureAwait(false);
lock (imported) lock (imported)
{ {
@ -176,16 +176,16 @@ namespace osu.Game.Database
/// Note that this bypasses the UI flow and should only be used for special cases or testing. /// Note that this bypasses the UI flow and should only be used for special cases or testing.
/// </summary> /// </summary>
/// <param name="task">The <see cref="ImportTask"/> containing data about the <typeparamref name="TModel"/> to import.</param> /// <param name="task">The <see cref="ImportTask"/> containing data about the <typeparamref name="TModel"/> to import.</param>
/// <param name="batchImport">Whether this import is part of a larger batch.</param> /// <param name="parameters">Parameters to further configure the import process.</param>
/// <param name="cancellationToken">An optional cancellation token.</param> /// <param name="cancellationToken">An optional cancellation token.</param>
/// <returns>The imported model, if successful.</returns> /// <returns>The imported model, if successful.</returns>
public async Task<Live<TModel>?> Import(ImportTask task, bool batchImport = false, CancellationToken cancellationToken = default) public async Task<Live<TModel>?> Import(ImportTask task, ImportParameters parameters = default, CancellationToken cancellationToken = default)
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
Live<TModel>? import; Live<TModel>? import;
using (ArchiveReader reader = task.GetReader()) using (ArchiveReader reader = task.GetReader())
import = await importFromArchive(reader, batchImport, cancellationToken).ConfigureAwait(false); import = await importFromArchive(reader, parameters, cancellationToken).ConfigureAwait(false);
// We may or may not want to delete the file depending on where it is stored. // We may or may not want to delete the file depending on where it is stored.
// e.g. reconstructing/repairing database with items from default storage. // e.g. reconstructing/repairing database with items from default storage.
@ -211,9 +211,9 @@ namespace osu.Game.Database
/// This method also handled queueing the import task on a relevant import thread pool. /// This method also handled queueing the import task on a relevant import thread pool.
/// </remarks> /// </remarks>
/// <param name="archive">The archive to be imported.</param> /// <param name="archive">The archive to be imported.</param>
/// <param name="batchImport">Whether this import is part of a larger batch.</param> /// <param name="parameters">Parameters to further configure the import process.</param>
/// <param name="cancellationToken">An optional cancellation token.</param> /// <param name="cancellationToken">An optional cancellation token.</param>
private async Task<Live<TModel>?> importFromArchive(ArchiveReader archive, bool batchImport = false, CancellationToken cancellationToken = default) private async Task<Live<TModel>?> importFromArchive(ArchiveReader archive, ImportParameters parameters = default, CancellationToken cancellationToken = default)
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
@ -236,10 +236,10 @@ namespace osu.Game.Database
return null; return null;
} }
var scheduledImport = Task.Factory.StartNew(() => ImportModel(model, archive, batchImport, cancellationToken), var scheduledImport = Task.Factory.StartNew(() => ImportModel(model, archive, parameters, cancellationToken),
cancellationToken, cancellationToken,
TaskCreationOptions.HideScheduler, TaskCreationOptions.HideScheduler,
batchImport ? import_scheduler_batch : import_scheduler); parameters.Batch ? import_scheduler_batch : import_scheduler);
return await scheduledImport.ConfigureAwait(false); return await scheduledImport.ConfigureAwait(false);
} }
@ -249,15 +249,15 @@ namespace osu.Game.Database
/// </summary> /// </summary>
/// <param name="item">The model to be imported.</param> /// <param name="item">The model to be imported.</param>
/// <param name="archive">An optional archive to use for model population.</param> /// <param name="archive">An optional archive to use for model population.</param>
/// <param name="batchImport">If <c>true</c>, imports will be skipped before they begin, given an existing model matches on hash and filenames. Should generally only be used for large batch imports, as it may defy user expectations when updating an existing model.</param> /// <param name="parameters">Parameters to further configure the import process.</param>
/// <param name="cancellationToken">An optional cancellation token.</param> /// <param name="cancellationToken">An optional cancellation token.</param>
public virtual Live<TModel>? ImportModel(TModel item, ArchiveReader? archive = null, bool batchImport = false, CancellationToken cancellationToken = default) => Realm.Run(realm => public virtual Live<TModel>? ImportModel(TModel item, ArchiveReader? archive = null, ImportParameters parameters = default, CancellationToken cancellationToken = default) => Realm.Run(realm =>
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
TModel? existing; TModel? existing;
if (batchImport && archive != null) if (parameters.Batch && archive != null)
{ {
// this is a fast bail condition to improve large import performance. // this is a fast bail condition to improve large import performance.
item.Hash = computeHashFast(archive); item.Hash = computeHashFast(archive);
@ -303,7 +303,7 @@ namespace osu.Game.Database
foreach (var filenames in getShortenedFilenames(archive)) foreach (var filenames in getShortenedFilenames(archive))
{ {
using (Stream s = archive.GetStream(filenames.original)) using (Stream s = archive.GetStream(filenames.original))
files.Add(new RealmNamedFileUsage(Files.Add(s, realm, false), filenames.shortened)); files.Add(new RealmNamedFileUsage(Files.Add(s, realm, false, parameters.PreferHardLinks), filenames.shortened));
} }
} }
@ -358,7 +358,7 @@ namespace osu.Game.Database
// import to store // import to store
realm.Add(item); realm.Add(item);
PostImport(item, realm, batchImport); PostImport(item, realm, parameters);
transaction.Commit(); transaction.Commit();
} }
@ -493,8 +493,8 @@ namespace osu.Game.Database
/// </summary> /// </summary>
/// <param name="model">The model prepared for import.</param> /// <param name="model">The model prepared for import.</param>
/// <param name="realm">The current realm context.</param> /// <param name="realm">The current realm context.</param>
/// <param name="batchImport">Whether the import was part of a batch.</param> /// <param name="parameters">Parameters to further configure the import process.</param>
protected virtual void PostImport(TModel model, Realm realm, bool batchImport) protected virtual void PostImport(TModel model, Realm realm, ImportParameters parameters)
{ {
} }

View File

@ -4,12 +4,14 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using osu.Framework;
using osu.Framework.Extensions; using osu.Framework.Extensions;
using osu.Framework.IO.Stores; using osu.Framework.IO.Stores;
using osu.Framework.Logging; using osu.Framework.Logging;
using osu.Framework.Platform; using osu.Framework.Platform;
using osu.Framework.Testing; using osu.Framework.Testing;
using osu.Game.Extensions; using osu.Game.Extensions;
using osu.Game.IO;
using osu.Game.Models; using osu.Game.Models;
using Realms; using Realms;
@ -41,7 +43,8 @@ namespace osu.Game.Database
/// <param name="data">The file data stream.</param> /// <param name="data">The file data stream.</param>
/// <param name="realm">The realm instance to add to. Should already be in a transaction.</param> /// <param name="realm">The realm instance to add to. Should already be in a transaction.</param>
/// <param name="addToRealm">Whether the <see cref="RealmFile"/> should immediately be added to the underlying realm. If <c>false</c> is provided here, the instance must be manually added.</param> /// <param name="addToRealm">Whether the <see cref="RealmFile"/> should immediately be added to the underlying realm. If <c>false</c> is provided here, the instance must be manually added.</param>
public RealmFile Add(Stream data, Realm realm, bool addToRealm = true) /// <param name="preferHardLinks">Whether this import should use hard links rather than file copy operations if available.</param>
public RealmFile Add(Stream data, Realm realm, bool addToRealm = true, bool preferHardLinks = false)
{ {
string hash = data.ComputeSHA2Hash(); string hash = data.ComputeSHA2Hash();
@ -50,7 +53,7 @@ namespace osu.Game.Database
var file = existing ?? new RealmFile { Hash = hash }; var file = existing ?? new RealmFile { Hash = hash };
if (!checkFileExistsAndMatchesHash(file)) if (!checkFileExistsAndMatchesHash(file))
copyToStore(file, data); copyToStore(file, data, preferHardLinks);
if (addToRealm && !file.IsManaged) if (addToRealm && !file.IsManaged)
realm.Add(file); realm.Add(file);
@ -58,8 +61,15 @@ namespace osu.Game.Database
return file; return file;
} }
private void copyToStore(RealmFile file, Stream data) private void copyToStore(RealmFile file, Stream data, bool preferHardLinks)
{ {
if (RuntimeInfo.OS == RuntimeInfo.Platform.Windows && data is FileStream fs && preferHardLinks)
{
// attempt to do a fast hard link rather than copy.
if (HardLinkHelper.CreateHardLink(Storage.GetFullPath(file.GetStoragePath(), true), fs.Name, IntPtr.Zero))
return;
}
data.Seek(0, SeekOrigin.Begin); data.Seek(0, SeekOrigin.Begin);
using (var output = Storage.CreateFileSafely(file.GetStoragePath())) using (var output = Storage.CreateFileSafely(file.GetStoragePath()))

View File

@ -0,0 +1,108 @@
// 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.
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using Microsoft.Win32.SafeHandles;
using osu.Framework;
namespace osu.Game.IO
{
internal static class HardLinkHelper
{
public static bool CheckAvailability(string testDestinationPath, string testSourcePath)
{
// We can support other operating systems quite easily in the future.
// Let's handle the most common one for now, though.
if (RuntimeInfo.OS != RuntimeInfo.Platform.Windows)
return false;
const string test_filename = "_hard_link_test";
testDestinationPath = Path.Combine(testDestinationPath, test_filename);
testSourcePath = Path.Combine(testSourcePath, test_filename);
cleanupFiles();
try
{
File.WriteAllText(testSourcePath, string.Empty);
// Test availability by creating an arbitrary hard link between the source and destination paths.
return CreateHardLink(testDestinationPath, testSourcePath, IntPtr.Zero);
}
catch
{
return false;
}
finally
{
cleanupFiles();
}
void cleanupFiles()
{
try
{
File.Delete(testDestinationPath);
File.Delete(testSourcePath);
}
catch
{
}
}
}
// For future use (to detect if a file is a hard link with other references existing on disk).
public static int GetFileLinkCount(string filePath)
{
int result = 0;
SafeFileHandle handle = CreateFile(filePath, FileAccess.Read, FileShare.Read, IntPtr.Zero, FileMode.Open, FileAttributes.Archive, IntPtr.Zero);
ByHandleFileInformation fileInfo;
if (GetFileInformationByHandle(handle, out fileInfo))
result = (int)fileInfo.NumberOfLinks;
CloseHandle(handle);
return result;
}
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool CreateHardLink(string lpFileName, string lpExistingFileName, IntPtr lpSecurityAttributes);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern SafeFileHandle CreateFile(
string lpFileName,
[MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
[MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
IntPtr lpSecurityAttributes,
[MarshalAs(UnmanagedType.U4)] FileMode dwCreationDisposition,
[MarshalAs(UnmanagedType.U4)] FileAttributes dwFlagsAndAttributes,
IntPtr hTemplateFile);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool GetFileInformationByHandle(SafeFileHandle handle, out ByHandleFileInformation lpFileInformation);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CloseHandle(SafeHandle hObject);
[StructLayout(LayoutKind.Sequential)]
private struct ByHandleFileInformation
{
public readonly uint FileAttributes;
public readonly FILETIME CreationTime;
public readonly FILETIME LastAccessTime;
public readonly FILETIME LastWriteTime;
public readonly uint VolumeSerialNumber;
public readonly uint FileSizeHigh;
public readonly uint FileSizeLow;
public readonly uint NumberOfLinks;
public readonly uint FileIndexHigh;
public readonly uint FileIndexLow;
}
}
}

View File

@ -15,10 +15,10 @@ namespace osu.Game.Localisation
public static LocalisableString Header => new TranslatableString(getKey(@"header"), @"Import"); public static LocalisableString Header => new TranslatableString(getKey(@"header"), @"Import");
/// <summary> /// <summary>
/// "If you have an installation of a previous osu! version, you can choose to migrate your existing content. Note that this will create a copy, and not affect your existing installation." /// "If you have an installation of a previous osu! version, you can choose to migrate your existing content. Note that this will not affect your existing installation's files in any way."
/// </summary> /// </summary>
public static LocalisableString Description => new TranslatableString(getKey(@"description"), public static LocalisableString Description => new TranslatableString(getKey(@"description"),
@"If you have an installation of a previous osu! version, you can choose to migrate your existing content. Note that this will create a copy, and not affect your existing installation."); @"If you have an installation of a previous osu! version, you can choose to migrate your existing content. Note that this will not affect your existing installation's files in any way.");
/// <summary> /// <summary>
/// "previous osu! install" /// "previous osu! install"

View File

@ -616,14 +616,14 @@ namespace osu.Game
}, validScreens: validScreens); }, validScreens: validScreens);
} }
public override Task Import(params ImportTask[] imports) public override Task Import(ImportTask[] imports, ImportParameters parameters = default)
{ {
// encapsulate task as we don't want to begin the import process until in a ready state. // encapsulate task as we don't want to begin the import process until in a ready state.
// ReSharper disable once AsyncVoidLambda // ReSharper disable once AsyncVoidLambda
// TODO: This is bad because `new Task` doesn't have a Func<Task?> override. // TODO: This is bad because `new Task` doesn't have a Func<Task?> override.
// Only used for android imports and a bit of a mess. Probably needs rethinking overall. // Only used for android imports and a bit of a mess. Probably needs rethinking overall.
var importTask = new Task(async () => await base.Import(imports).ConfigureAwait(false)); var importTask = new Task(async () => await base.Import(imports, parameters).ConfigureAwait(false));
waitForReady(() => this, _ => importTask.Start()); waitForReady(() => this, _ => importTask.Start());

View File

@ -44,13 +44,13 @@ namespace osu.Game
} }
} }
public virtual async Task Import(params ImportTask[] tasks) public virtual async Task Import(ImportTask[] tasks, ImportParameters parameters = default)
{ {
var tasksPerExtension = tasks.GroupBy(t => Path.GetExtension(t.Path).ToLowerInvariant()); var tasksPerExtension = tasks.GroupBy(t => Path.GetExtension(t.Path).ToLowerInvariant());
await Task.WhenAll(tasksPerExtension.Select(taskGroup => await Task.WhenAll(tasksPerExtension.Select(taskGroup =>
{ {
var importer = fileImporters.FirstOrDefault(i => i.HandledExtensions.Contains(taskGroup.Key)); var importer = fileImporters.FirstOrDefault(i => i.HandledExtensions.Contains(taskGroup.Key));
return importer?.Import(taskGroup.ToArray()) ?? Task.CompletedTask; return importer?.Import(taskGroup.ToArray(), parameters) ?? Task.CompletedTask;
})).ConfigureAwait(false); })).ConfigureAwait(false);
} }

View File

@ -7,6 +7,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using osu.Framework;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Extensions; using osu.Framework.Extensions;
@ -14,12 +15,15 @@ using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.UserInterface; using osu.Framework.Graphics.UserInterface;
using osu.Framework.Localisation; using osu.Framework.Localisation;
using osu.Framework.Logging;
using osu.Framework.Screens;
using osu.Game.Database; using osu.Game.Database;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Graphics.Containers; using osu.Game.Graphics.Containers;
using osu.Game.Graphics.UserInterfaceV2; using osu.Game.Graphics.UserInterfaceV2;
using osu.Game.Localisation; using osu.Game.Localisation;
using osu.Game.Overlays.Settings; using osu.Game.Overlays.Settings;
using osu.Game.Overlays.Settings.Sections.Maintenance;
using osu.Game.Screens.Edit.Setup; using osu.Game.Screens.Edit.Setup;
using osuTK; using osuTK;
@ -39,6 +43,8 @@ namespace osu.Game.Overlays.FirstRunSetup
private StableLocatorLabelledTextBox stableLocatorTextBox = null!; private StableLocatorLabelledTextBox stableLocatorTextBox = null!;
private LinkFlowContainer copyInformation = null!;
private IEnumerable<ImportCheckbox> contentCheckboxes => Content.Children.OfType<ImportCheckbox>(); private IEnumerable<ImportCheckbox> contentCheckboxes => Content.Children.OfType<ImportCheckbox>();
[BackgroundDependencyLoader(permitNulls: true)] [BackgroundDependencyLoader(permitNulls: true)]
@ -46,7 +52,7 @@ namespace osu.Game.Overlays.FirstRunSetup
{ {
Content.Children = new Drawable[] Content.Children = new Drawable[]
{ {
new OsuTextFlowContainer(cp => cp.Font = OsuFont.Default.With(size: CONTENT_FONT_SIZE)) new LinkFlowContainer(cp => cp.Font = OsuFont.Default.With(size: CONTENT_FONT_SIZE))
{ {
Colour = OverlayColourProvider.Content1, Colour = OverlayColourProvider.Content1,
Text = FirstRunOverlayImportFromStableScreenStrings.Description, Text = FirstRunOverlayImportFromStableScreenStrings.Description,
@ -62,6 +68,12 @@ namespace osu.Game.Overlays.FirstRunSetup
new ImportCheckbox(CommonStrings.Scores, StableContent.Scores), new ImportCheckbox(CommonStrings.Scores, StableContent.Scores),
new ImportCheckbox(CommonStrings.Skins, StableContent.Skins), new ImportCheckbox(CommonStrings.Skins, StableContent.Skins),
new ImportCheckbox(CommonStrings.Collections, StableContent.Collections), new ImportCheckbox(CommonStrings.Collections, StableContent.Collections),
copyInformation = new LinkFlowContainer(cp => cp.Font = OsuFont.Default.With(size: CONTENT_FONT_SIZE))
{
Colour = OverlayColourProvider.Content1,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y
},
importButton = new ProgressRoundedButton importButton = new ProgressRoundedButton
{ {
Size = button_size, Size = button_size,
@ -83,6 +95,9 @@ namespace osu.Game.Overlays.FirstRunSetup
stableLocatorTextBox.Current.BindValueChanged(_ => updateStablePath(), true); stableLocatorTextBox.Current.BindValueChanged(_ => updateStablePath(), true);
} }
[Resolved(canBeNull: true)]
private OsuGame? game { get; set; }
private void updateStablePath() private void updateStablePath()
{ {
var storage = legacyImportManager.GetCurrentStableStorage(); var storage = legacyImportManager.GetCurrentStableStorage();
@ -105,6 +120,25 @@ namespace osu.Game.Overlays.FirstRunSetup
toggleInteraction(true); toggleInteraction(true);
stableLocatorTextBox.Current.Value = storage.GetFullPath(string.Empty); stableLocatorTextBox.Current.Value = storage.GetFullPath(string.Empty);
importButton.Enabled.Value = true; importButton.Enabled.Value = true;
bool available = legacyImportManager.CheckHardLinkAvailability();
Logger.Log($"Hard link support is {available}");
if (available)
{
copyInformation.Text = "Data migration will use \"hard links\". No extra disk space will be used, and you can delete either data folder at any point without affecting the other installation.";
}
else if (RuntimeInfo.OS != RuntimeInfo.Platform.Windows)
copyInformation.Text = "Lightweight linking of files is not supported on your operating system yet, so a copy of all files will be made during import.";
else
{
copyInformation.Text =
"A second copy of all files will be made during import. To avoid this, please make sure the lazer data folder is on the same drive as your previous osu! install (and the file system is NTFS). ";
copyInformation.AddLink(GeneralSettingsStrings.ChangeFolderLocation, () =>
{
game?.PerformFromScreen(menu => menu.Push(new MigrationSelectScreen()));
});
}
} }
private void runImport() private void runImport()
@ -235,7 +269,7 @@ namespace osu.Game.Overlays.FirstRunSetup
return Task.CompletedTask; return Task.CompletedTask;
} }
Task ICanAcceptFiles.Import(params ImportTask[] tasks) => throw new NotImplementedException(); Task ICanAcceptFiles.Import(ImportTask[] tasks, ImportParameters parameters) => throw new NotImplementedException();
protected override void Dispose(bool isDisposing) protected override void Dispose(bool isDisposing)
{ {

View File

@ -145,9 +145,9 @@ namespace osu.Game.Scoring
#pragma warning restore CS0618 #pragma warning restore CS0618
} }
protected override void PostImport(ScoreInfo model, Realm realm, bool batchImport) protected override void PostImport(ScoreInfo model, Realm realm, ImportParameters parameters)
{ {
base.PostImport(model, realm, batchImport); base.PostImport(model, realm, parameters);
var userRequest = new GetUserRequest(model.RealmUser.Username); var userRequest = new GetUserRequest(model.RealmUser.Username);

View File

@ -169,18 +169,18 @@ namespace osu.Game.Scoring
public Task Import(params string[] paths) => scoreImporter.Import(paths); public Task Import(params string[] paths) => scoreImporter.Import(paths);
public Task Import(params ImportTask[] tasks) => scoreImporter.Import(tasks); public Task Import(ImportTask[] imports, ImportParameters parameters = default) => scoreImporter.Import(imports, parameters);
public override bool IsAvailableLocally(ScoreInfo model) => Realm.Run(realm => realm.All<ScoreInfo>().Any(s => s.OnlineID == model.OnlineID)); public override bool IsAvailableLocally(ScoreInfo model) => Realm.Run(realm => realm.All<ScoreInfo>().Any(s => s.OnlineID == model.OnlineID));
public IEnumerable<string> HandledExtensions => scoreImporter.HandledExtensions; public IEnumerable<string> HandledExtensions => scoreImporter.HandledExtensions;
public Task<IEnumerable<Live<ScoreInfo>>> Import(ProgressNotification notification, params ImportTask[] tasks) => scoreImporter.Import(notification, tasks); public Task<IEnumerable<Live<ScoreInfo>>> Import(ProgressNotification notification, ImportTask[] tasks, ImportParameters parameters = default) => scoreImporter.Import(notification, tasks);
public Task<Live<ScoreInfo>> ImportAsUpdate(ProgressNotification notification, ImportTask task, ScoreInfo original) => scoreImporter.ImportAsUpdate(notification, task, original); public Task<Live<ScoreInfo>> ImportAsUpdate(ProgressNotification notification, ImportTask task, ScoreInfo original) => scoreImporter.ImportAsUpdate(notification, task, original);
public Live<ScoreInfo> Import(ScoreInfo item, ArchiveReader archive = null, bool batchImport = false, CancellationToken cancellationToken = default) => public Live<ScoreInfo> Import(ScoreInfo item, ArchiveReader archive = null, ImportParameters parameters = default, CancellationToken cancellationToken = default) =>
scoreImporter.ImportModel(item, archive, batchImport, cancellationToken); scoreImporter.ImportModel(item, archive, parameters, cancellationToken);
/// <summary> /// <summary>
/// Populates the <see cref="ScoreInfo.MaximumStatistics"/> for a given <see cref="ScoreInfo"/>. /// Populates the <see cref="ScoreInfo.MaximumStatistics"/> for a given <see cref="ScoreInfo"/>.

View File

@ -91,7 +91,7 @@ namespace osu.Game.Screens.Edit.Setup
return Task.CompletedTask; return Task.CompletedTask;
} }
Task ICanAcceptFiles.Import(params ImportTask[] tasks) => throw new NotImplementedException(); Task ICanAcceptFiles.Import(ImportTask[] tasks, ImportParameters parameters) => throw new NotImplementedException();
protected override void Dispose(bool isDisposing) protected override void Dispose(bool isDisposing)
{ {

View File

@ -411,7 +411,7 @@ namespace osu.Game.Skinning.Editor
return Task.CompletedTask; return Task.CompletedTask;
} }
public Task Import(params ImportTask[] tasks) => throw new NotImplementedException(); Task ICanAcceptFiles.Import(ImportTask[] tasks, ImportParameters parameters) => throw new NotImplementedException();
public IEnumerable<string> HandledExtensions => new[] { ".jpg", ".jpeg", ".png" }; public IEnumerable<string> HandledExtensions => new[] { ".jpg", ".jpeg", ".png" };

View File

@ -270,15 +270,18 @@ namespace osu.Game.Skinning
public Task Import(params string[] paths) => skinImporter.Import(paths); public Task Import(params string[] paths) => skinImporter.Import(paths);
public Task Import(params ImportTask[] tasks) => skinImporter.Import(tasks); public Task Import(ImportTask[] imports, ImportParameters parameters = default) => skinImporter.Import(imports, parameters);
public IEnumerable<string> HandledExtensions => skinImporter.HandledExtensions; public IEnumerable<string> HandledExtensions => skinImporter.HandledExtensions;
public Task<IEnumerable<Live<SkinInfo>>> Import(ProgressNotification notification, params ImportTask[] tasks) => skinImporter.Import(notification, tasks); public Task<IEnumerable<Live<SkinInfo>>> Import(ProgressNotification notification, ImportTask[] tasks, ImportParameters parameters = default) =>
skinImporter.Import(notification, tasks, parameters);
public Task<Live<SkinInfo>> ImportAsUpdate(ProgressNotification notification, ImportTask task, SkinInfo original) => skinImporter.ImportAsUpdate(notification, task, original); public Task<Live<SkinInfo>> ImportAsUpdate(ProgressNotification notification, ImportTask task, SkinInfo original) =>
skinImporter.ImportAsUpdate(notification, task, original);
public Task<Live<SkinInfo>> Import(ImportTask task, bool batchImport = false, CancellationToken cancellationToken = default) => skinImporter.Import(task, batchImport, cancellationToken); public Task<Live<SkinInfo>> Import(ImportTask task, ImportParameters parameters = default, CancellationToken cancellationToken = default) =>
skinImporter.Import(task, parameters, cancellationToken);
#endregion #endregion