Skip to content

Commit

Permalink
Merge pull request ppy#15591 from peppy/fix-android-score-imports
Browse files Browse the repository at this point in the history
Fix android score imports not working
  • Loading branch information
peppy authored Nov 12, 2021
2 parents b0dcab8 + e66b637 commit 936ec5f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
26 changes: 23 additions & 3 deletions osu.Game/Database/ImportTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,30 @@ public ImportTask(Stream stream, string filename)
/// </summary>
public ArchiveReader GetReader()
{
if (Stream != null)
return new ZipArchiveReader(Stream, Path);
return Stream != null
? getReaderFrom(Stream)
: 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 getReaderFrom(Path);
return new LegacyByteArrayReader(memoryStream.ToArray(), Path);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion osu.Game/IO/Archives/LegacyByteArrayReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace osu.Game.IO.Archives
{
/// <summary>
/// Allows reading a single file from the provided stream.
/// Allows reading a single file from the provided byte array.
/// </summary>
public class LegacyByteArrayReader : ArchiveReader
{
Expand Down
28 changes: 28 additions & 0 deletions osu.Game/Utils/ZipUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,34 @@ namespace osu.Game.Utils
{
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)
{
if (!File.Exists(path))
Expand Down

0 comments on commit 936ec5f

Please sign in to comment.