From a20bd5cc3d3b07ec3dfb461a3b0c6b33c2a25d74 Mon Sep 17 00:00:00 2001 From: smallketchup82 Date: Mon, 23 Sep 2024 09:47:33 -0400 Subject: [PATCH] Abstract out logic to SkinImporter --- osu.Game/Overlays/SkinEditor/SkinEditor.cs | 28 +------------------- osu.Game/Skinning/SkinImporter.cs | 30 ++++++++++++++++++++-- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/osu.Game/Overlays/SkinEditor/SkinEditor.cs b/osu.Game/Overlays/SkinEditor/SkinEditor.cs index a2f64d0a9c..56529de88d 100644 --- a/osu.Game/Overlays/SkinEditor/SkinEditor.cs +++ b/osu.Game/Overlays/SkinEditor/SkinEditor.cs @@ -318,32 +318,7 @@ namespace osu.Game.Overlays.SkinEditor mountMenuItem.Action.Disabled = true; - await Task.Run(() => - { - currentSkin.Value.SkinInfo.PerformWrite(skinInfo => - { - // Clear files in the skin - skinInfo.Files.Clear(); - - // Get all the files in the mounted directory and add them to the skin - string[] filesInMounted = Directory.EnumerateFiles(externalEditOperation.MountedPath, "*.*", SearchOption.AllDirectories).Select(f => Path.GetRelativePath(externalEditOperation.MountedPath, f)).ToArray(); - - foreach (string file in filesInMounted) - { - using var stream = File.OpenRead(Path.Combine(externalEditOperation.MountedPath, file)); - - // The GetFile call in this method is really expensive, and we are certain that the file does not exist in the skin yet. - // Consider adding a method to add a file without checking if it exists. Or add the file directly to the skin. - skins.AddFile(skinInfo, stream, file); - } - }); - - try - { - Directory.Delete(externalEditOperation.MountedPath, true); - } - catch { } - }).ConfigureAwait(false); + await externalEditOperation.Finish().ConfigureAwait(false); Schedule(() => { @@ -354,7 +329,6 @@ namespace osu.Game.Overlays.SkinEditor // If there's a better way to reload the skin, this should be replaced with it. currentSkin.Value = newSkinInfo.CreateInstance(skins); - // Dispose the old skin to ensure it's no longer used oldskin.Dispose(); mountMenuItem.Action.Disabled = false; diff --git a/osu.Game/Skinning/SkinImporter.cs b/osu.Game/Skinning/SkinImporter.cs index 9d9b197ac2..8147287eec 100644 --- a/osu.Game/Skinning/SkinImporter.cs +++ b/osu.Game/Skinning/SkinImporter.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -45,9 +46,34 @@ namespace osu.Game.Skinning private const string unknown_creator_string = @"Unknown"; - public override async Task?> ImportAsUpdate(ProgressNotification notification, ImportTask task, SkinInfo original) + /// + /// Update an existing skin with the contents of a path + /// + /// The progress notification + /// The to update the with + /// The to update + /// + public override Task?> ImportAsUpdate(ProgressNotification notification, ImportTask task, SkinInfo original) { - throw new NotImplementedException(); + var skinInfoLive = original.ToLive(Realm); + + skinInfoLive.PerformWrite(skinInfo => + { + skinInfo.Files.Clear(); + + string[] filesInMountedDirectory = Directory.EnumerateFiles(task.Path, "*.*", SearchOption.AllDirectories).Select(f => Path.GetRelativePath(task.Path, f)).ToArray(); + + foreach (string file in filesInMountedDirectory) + { + using var stream = File.OpenRead(Path.Combine(task.Path, file)); + + // The GetFile call in this method is *really* expensive, and we are certain that the file does not exist in the skin yet. + // Consider adding a method to add a file without checking if it already exists. Or add the file directly to the skin. + modelManager.AddFile(original, stream, file); + } + }); + + return Task.FromResult(skinInfoLive)!; } protected override void Populate(SkinInfo model, ArchiveReader? archive, Realm realm, CancellationToken cancellationToken = default)