mirror of https://github.com/ppy/osu
Fix importing archives which are nested in a single folder within a zip
This commit is contained in:
parent
0640b2eb35
commit
ddff9882cf
|
@ -15,7 +15,10 @@
|
|||
using osu.Game.Beatmaps;
|
||||
using osu.Game.IO;
|
||||
using osu.Game.Tests.Resources;
|
||||
using SharpCompress.Archives;
|
||||
using SharpCompress.Archives.Zip;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.Writers.Zip;
|
||||
|
||||
namespace osu.Game.Tests.Beatmaps.IO
|
||||
{
|
||||
|
@ -135,7 +138,7 @@ public async Task TestRollbackOnFailure()
|
|||
using (var zip = ZipArchive.Open(brokenOsz))
|
||||
{
|
||||
zip.AddEntry("broken.osu", brokenOsu, false);
|
||||
zip.SaveTo(outStream, SharpCompress.Common.CompressionType.Deflate);
|
||||
zip.SaveTo(outStream, CompressionType.Deflate);
|
||||
}
|
||||
|
||||
// this will trigger purging of the existing beatmap (online set id match) but should rollback due to broken osu.
|
||||
|
@ -366,6 +369,51 @@ public async Task TestImportWhenFileOpen()
|
|||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TestImportNestedStructure()
|
||||
{
|
||||
using (HeadlessGameHost host = new CleanRunHeadlessGameHost("TestImportNestedStructure"))
|
||||
{
|
||||
try
|
||||
{
|
||||
var osu = loadOsu(host);
|
||||
|
||||
var temp = TestResources.GetTestBeatmapForImport();
|
||||
|
||||
string extractedFolder = $"{temp}_extracted";
|
||||
string subfolder = Path.Combine(extractedFolder, "subfolder");
|
||||
|
||||
Directory.CreateDirectory(subfolder);
|
||||
|
||||
try
|
||||
{
|
||||
using (var zip = ZipArchive.Open(temp))
|
||||
zip.WriteToDirectory(subfolder);
|
||||
|
||||
using (var zip = ZipArchive.Create())
|
||||
{
|
||||
zip.AddAllFromDirectory(extractedFolder);
|
||||
zip.SaveTo(temp, new ZipWriterOptions(CompressionType.Deflate));
|
||||
}
|
||||
|
||||
var imported = await osu.Dependencies.Get<BeatmapManager>().Import(temp);
|
||||
|
||||
ensureLoaded(osu);
|
||||
|
||||
Assert.IsFalse(imported.Files.Any(f => f.Filename.Contains("subfolder")), "Files contain common subfolder");
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(extractedFolder, true);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
host.Exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<BeatmapSetInfo> LoadOszIntoOsu(OsuGameBase osu, string path = null)
|
||||
{
|
||||
var temp = path ?? TestResources.GetTestBeatmapForImport();
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using osu.Framework;
|
||||
using osu.Framework.Extensions;
|
||||
using osu.Framework.Extensions.IEnumerableExtensions;
|
||||
using osu.Framework.IO.File;
|
||||
using osu.Framework.Logging;
|
||||
using osu.Framework.Platform;
|
||||
|
@ -481,12 +482,16 @@ private List<TFileModel> createFileInfos(ArchiveReader reader, FileStore files)
|
|||
{
|
||||
var fileInfos = new List<TFileModel>();
|
||||
|
||||
string prefix = reader.Filenames.GetCommonPrefix();
|
||||
if (!(prefix.EndsWith("/") || prefix.EndsWith("\\")))
|
||||
prefix = string.Empty;
|
||||
|
||||
// import files to manager
|
||||
foreach (string file in reader.Filenames)
|
||||
using (Stream s = reader.GetStream(file))
|
||||
fileInfos.Add(new TFileModel
|
||||
{
|
||||
Filename = FileSafety.PathStandardise(file),
|
||||
Filename = FileSafety.PathStandardise(file.Substring(prefix.Length)),
|
||||
FileInfo = files.Add(s)
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue