1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 16:07:24 +08:00

create a task to export to avoid block main thread

Code quality and remove some #nullable disable
This commit is contained in:
cdwcgt 2022-11-17 00:01:29 +09:00
parent 335e4e8ec5
commit e1a21e0cf9
No known key found for this signature in database
GPG Key ID: 144396D01095C3A2
11 changed files with 78 additions and 36 deletions

View File

@ -15,6 +15,7 @@ using osu.Framework.Platform;
using osu.Game.Database;
using osu.Game.Extensions;
using osu.Game.IO;
using osu.Game.Overlays;
using osu.Game.Skinning;
using SharpCompress.Archives.Zip;
@ -122,7 +123,7 @@ namespace osu.Game.Tests.Skins.IO
import1.PerformRead(s =>
{
new LegacySkinExporter(osu.Dependencies.Get<Storage>()).ExportModelTo(s, exportStream);
new LegacySkinExporter(osu.Dependencies.Get<Storage>(), osu.Dependencies.Get<INotificationOverlay>()).ExportModelTo(s, exportStream);
});
string exportFilename = import1.GetDisplayString();
@ -204,7 +205,7 @@ namespace osu.Game.Tests.Skins.IO
Assert.IsFalse(s.Protected);
Assert.AreEqual(typeof(ArgonSkin), s.CreateInstance(skinManager).GetType());
new LegacySkinExporter(osu.Dependencies.Get<Storage>()).ExportModelTo(s, exportStream);
new LegacySkinExporter(osu.Dependencies.Get<Storage>(), osu.Dependencies.Get<INotificationOverlay>()).ExportModelTo(s, exportStream);
Assert.Greater(exportStream.Length, 0);
});
@ -239,7 +240,7 @@ namespace osu.Game.Tests.Skins.IO
Assert.IsFalse(s.Protected);
Assert.AreEqual(typeof(DefaultLegacySkin), s.CreateInstance(skinManager).GetType());
new LegacySkinExporter(osu.Dependencies.Get<Storage>()).ExportModelTo(s, exportStream);
new LegacySkinExporter(osu.Dependencies.Get<Storage>(), osu.Dependencies.Get<INotificationOverlay>()).ExportModelTo(s, exportStream);
Assert.Greater(exportStream.Length, 0);
});

View File

@ -1,10 +1,9 @@
// 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 osu.Framework.Platform;
using osu.Game.Beatmaps;
using osu.Game.Overlays;
namespace osu.Game.Database
{
@ -12,8 +11,8 @@ namespace osu.Game.Database
{
protected override string FileExtension => ".osz";
public LegacyBeatmapExporter(Storage storage)
: base(storage)
public LegacyBeatmapExporter(Storage storage, INotificationOverlay? notificationOverlay)
: base(storage, notificationOverlay)
{
}
}

View File

@ -1,11 +1,12 @@
// 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.IO;
using System.Threading.Tasks;
using osu.Framework.Platform;
using osu.Game.Extensions;
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
using SharpCompress.Archives.Zip;
namespace osu.Game.Database
@ -25,10 +26,17 @@ namespace osu.Game.Database
private readonly Storage exportStorage;
protected LegacyExporter(Storage storage)
private readonly INotificationOverlay? notificationOverlay;
protected ProgressNotification Notification = null!;
private string filename = null!;
protected LegacyExporter(Storage storage, INotificationOverlay? notificationOverlay)
{
exportStorage = storage.GetStorageForDirectory(@"exports");
UserFileStorage = storage.GetStorageForDirectory(@"files");
this.notificationOverlay = notificationOverlay;
}
/// <summary>
@ -37,12 +45,25 @@ namespace osu.Game.Database
/// <param name="item">The item to export.</param>
public void Export(TModel item)
{
string filename = $"{item.GetDisplayString().GetValidFilename()}{FileExtension}";
filename = $"{item.GetDisplayString().GetValidFilename()}{FileExtension}";
using (var stream = exportStorage.CreateFileSafely(filename))
ExportModelTo(item, stream);
Stream stream = exportStorage.CreateFileSafely(filename);
exportStorage.PresentFileExternally(filename);
Notification = new ProgressNotification
{
State = ProgressNotificationState.Active,
Text = "Exporting...",
CompletionText = "Export completed"
};
Notification.CompletionClickAction += () => exportStorage.PresentFileExternally(filename);
Notification.CancelRequested += () =>
{
stream.Dispose();
return true;
};
ExportModelTo(item, stream);
notificationOverlay?.Post(Notification);
}
/// <summary>
@ -57,7 +78,24 @@ namespace osu.Game.Database
foreach (var file in model.Files)
archive.AddEntry(file.Filename, UserFileStorage.GetStream(file.File.GetStoragePath()));
archive.SaveTo(outputStream);
Task.Factory.StartNew(() =>
{
archive.SaveTo(outputStream);
}, Notification.CancellationToken).ContinueWith(t =>
{
if (t.IsCompletedSuccessfully)
{
outputStream.Dispose();
Notification.State = ProgressNotificationState.Completed;
}
else
{
if (Notification.State == ProgressNotificationState.Cancelled) return;
Notification.State = ProgressNotificationState.Cancelled;
Notification.Text = "Export Failed";
}
});
}
}
}

View File

@ -1,12 +1,12 @@
// 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.IO;
using System.Linq;
using osu.Framework.Platform;
using osu.Game.Extensions;
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
using osu.Game.Scoring;
namespace osu.Game.Database
@ -15,8 +15,8 @@ namespace osu.Game.Database
{
protected override string FileExtension => ".osr";
public LegacyScoreExporter(Storage storage)
: base(storage)
public LegacyScoreExporter(Storage storage, INotificationOverlay? notificationOverlay)
: base(storage, notificationOverlay)
{
}
@ -28,6 +28,9 @@ namespace osu.Game.Database
using (var inputStream = UserFileStorage.GetStream(file.File.GetStoragePath()))
inputStream.CopyTo(outputStream);
Notification.State = ProgressNotificationState.Completed;
outputStream.Dispose();
}
}
}

View File

@ -1,8 +1,6 @@
// 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;
using System.Collections.Generic;
using System.IO;

View File

@ -1,9 +1,8 @@
// 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 osu.Framework.Platform;
using osu.Game.Overlays;
using osu.Game.Skinning;
namespace osu.Game.Database
@ -12,8 +11,8 @@ namespace osu.Game.Database
{
protected override string FileExtension => ".osk";
public LegacySkinExporter(Storage storage)
: base(storage)
public LegacySkinExporter(Storage storage, INotificationOverlay? notificationOverlay)
: base(storage, notificationOverlay)
{
}
}

View File

@ -1,8 +1,6 @@
// 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 osu.Game.Skinning;
namespace osu.Game.Database

View File

@ -173,11 +173,6 @@ namespace osu.Game.Database
if (!Filename.EndsWith(realm_extension, StringComparison.Ordinal))
Filename += realm_extension;
#if DEBUG
if (!DebugUtils.IsNUnitRunning)
applyFilenameSchemaSuffix(ref Filename);
#endif
string newerVersionFilename = $"{Filename.Replace(realm_extension, string.Empty)}_newer_version{realm_extension}";
// Attempt to recover a newer database version if available.

View File

@ -75,6 +75,9 @@ namespace osu.Game.Online.Leaderboards
[Resolved]
private Storage storage { get; set; }
[Resolved]
private INotificationOverlay notificationOverlay { get; set; }
public ITooltip<ScoreInfo> GetCustomTooltip() => new LeaderboardScoreTooltip();
public virtual ScoreInfo TooltipContent => Score;
@ -427,7 +430,7 @@ namespace osu.Game.Online.Leaderboards
if (Score.Files.Count > 0)
{
items.Add(new OsuMenuItem("Export", MenuItemType.Standard, () => new LegacyScoreExporter(storage).Export(Score)));
items.Add(new OsuMenuItem("Export", MenuItemType.Standard, () => new LegacyScoreExporter(storage, notificationOverlay).Export(Score)));
items.Add(new OsuMenuItem(CommonStrings.ButtonsDelete, MenuItemType.Destructive, () => dialogOverlay?.Push(new LocalScoreDeleteDialog(Score))));
}

View File

@ -141,11 +141,16 @@ namespace osu.Game.Overlays.Settings.Sections
[Resolved]
private Storage storage { get; set; }
[CanBeNull]
private INotificationOverlay notificationOverlay;
private Bindable<Skin> currentSkin;
[BackgroundDependencyLoader]
private void load()
private void load(INotificationOverlay notificationOverlay)
{
this.notificationOverlay = notificationOverlay;
Text = SkinSettingsStrings.ExportSkinButton;
Action = export;
}
@ -162,7 +167,7 @@ namespace osu.Game.Overlays.Settings.Sections
{
try
{
currentSkin.Value.SkinInfo.PerformRead(s => new LegacySkinExporter(storage).Export(s));
currentSkin.Value.SkinInfo.PerformRead(s => new LegacySkinExporter(storage, notificationOverlay).Export(s));
}
catch (Exception e)
{

View File

@ -93,6 +93,9 @@ namespace osu.Game.Screens.Edit
[Resolved]
private Storage storage { get; set; }
[Resolved]
private INotificationOverlay notificationOverlay { get; set; }
[Resolved(canBeNull: true)]
private IDialogOverlay dialogOverlay { get; set; }
@ -938,7 +941,7 @@ namespace osu.Game.Screens.Edit
private void exportBeatmap()
{
Save();
new LegacyBeatmapExporter(storage).Export(Beatmap.Value.BeatmapSetInfo);
new LegacyBeatmapExporter(storage, notificationOverlay).Export(Beatmap.Value.BeatmapSetInfo);
}
/// <summary>