1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-28 06:52:55 +08:00

Merge pull request #15591 from peppy/fix-android-score-imports

Fix android score imports not working
This commit is contained in:
Dean Herbert 2021-11-12 21:11:27 +09:00 committed by GitHub
commit 936ec5fe8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 4 deletions

View File

@ -47,10 +47,30 @@ namespace osu.Game.Database
/// </summary> /// </summary>
public ArchiveReader GetReader() public ArchiveReader GetReader()
{ {
if (Stream != null) return Stream != null
return new ZipArchiveReader(Stream, Path); ? getReaderFrom(Stream)
: getReaderFrom(Path);
}
return getReaderFrom(Path); /// <summary>
/// Creates an <see cref="ArchiveReader"/> from a stream.
/// </summary>
/// <param name="stream">A seekable stream containing the archive content.</param>
/// <returns>A reader giving access to the archive's content.</returns>
private ArchiveReader getReaderFrom(Stream stream)
{
if (!(stream is MemoryStream memoryStream))
{
// This isn't used in any current path. May need to reconsider for performance reasons (ie. if we don't expect the incoming stream to be copied out).
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int)stream.Length);
memoryStream = new MemoryStream(buffer);
}
if (ZipUtils.IsZipArchive(memoryStream))
return new ZipArchiveReader(memoryStream, Path);
return new LegacyByteArrayReader(memoryStream.ToArray(), Path);
} }
/// <summary> /// <summary>

View File

@ -7,7 +7,7 @@ using System.IO;
namespace osu.Game.IO.Archives namespace osu.Game.IO.Archives
{ {
/// <summary> /// <summary>
/// Allows reading a single file from the provided stream. /// Allows reading a single file from the provided byte array.
/// </summary> /// </summary>
public class LegacyByteArrayReader : ArchiveReader public class LegacyByteArrayReader : ArchiveReader
{ {

View File

@ -9,6 +9,34 @@ namespace osu.Game.Utils
{ {
public static class ZipUtils public static class ZipUtils
{ {
public static bool IsZipArchive(MemoryStream stream)
{
try
{
stream.Seek(0, SeekOrigin.Begin);
using (var arc = ZipArchive.Open(stream))
{
foreach (var entry in arc.Entries)
{
using (entry.OpenEntryStream())
{
}
}
}
return true;
}
catch (Exception)
{
return false;
}
finally
{
stream.Seek(0, SeekOrigin.Begin);
}
}
public static bool IsZipArchive(string path) public static bool IsZipArchive(string path)
{ {
if (!File.Exists(path)) if (!File.Exists(path))