1
0
mirror of https://github.com/ppy/osu.git synced 2026-05-21 19:40:15 +08:00
Files
osu-lazer/osu.Game/IO/Archives/ZipArchiveReader.cs
T
Dean Herbert a996261304 Update lots of packages (#36996)
It's been a while.

Notes:

- `SharpCompress` usages changed a bit. Manually adjusted these, mostly
just renames or adjusted parameters.
- nUnit 3 -> 4 migrated using
https://gist.github.com/peppy/07994386d793a117350cb5f24b156585. there's
a mode in this script to update to the newer `Assert.That` syntax but it
requires fixes and couldn't really be bothered.
- DeepEqual nuked as the only usage was on a disabled test. The reason
it's disabled has been merged upstream, but it's failing for other
(realm) reasons which I don't think is worthwhile to investigate for
now.
- This bumps Moq. I think the author is back in a sensible headspace and
the new version has the stupid shit removed, so probably okay? Nice to
be on a level playing field with packages for once in a long time.
- Automapper is silly, but we've discussed this elsewhere.
- `TestRealmKeyBindingStore` failures are a wildcard, but fixed by using
a more standardised testing method. Dunno why, don't care.

---------

Co-authored-by: Bartłomiej Dach <dach.bartlomiej@gmail.com>
2026-03-17 03:58:02 +09:00

133 lines
4.7 KiB
C#

// 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.Buffers;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Toolkit.HighPerformance;
using osu.Framework.Extensions;
using osu.Framework.IO.Stores;
using SharpCompress.Archives;
using SharpCompress.Archives.Zip;
using SharpCompress.Common;
using SharpCompress.Readers;
using SixLabors.ImageSharp.Memory;
namespace osu.Game.IO.Archives
{
public sealed class ZipArchiveReader : ArchiveReader
{
/// <summary>
/// Archives created by osu!stable still write out as Shift-JIS.
/// We want to force this fallback rather than leave it up to the library/system.
/// In the future we may want to change exports to set the zip UTF-8 flag and use that instead.
/// </summary>
public static readonly ArchiveEncoding DEFAULT_ENCODING;
private readonly Stream archiveStream;
private readonly IWritableArchive archive;
static ZipArchiveReader()
{
// Required to support rare code pages.
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
DEFAULT_ENCODING = new ArchiveEncoding
{
Default = Encoding.GetEncoding(932),
Password = Encoding.GetEncoding(932),
};
}
public ZipArchiveReader(Stream archiveStream, string name = null)
: base(name)
{
this.archiveStream = archiveStream;
archive = ZipArchive.OpenArchive(archiveStream, new ReaderOptions
{
ArchiveEncoding = DEFAULT_ENCODING
});
}
public override Stream GetStream(string name)
{
IArchiveEntry entry = archive.Entries.SingleOrDefault(e => e.Key == name);
if (entry == null)
return null;
using (Stream s = entry.OpenEntryStream())
{
if (entry.Size > 0)
{
var owner = MemoryAllocator.Default.Allocate<byte>((int)entry.Size);
s.ReadExactly(owner.Memory.Span);
return new MemoryOwnerMemoryStream(owner);
}
// due to a sharpcompress bug (https://github.com/adamhathcock/sharpcompress/issues/88),
// in rare instances the `ZipArchiveEntry` will not contain a correct `Size` but instead report 0.
// this would lead to the block above reading nothing, and the game basically seeing an archive full of empty files.
// since the bug is years old now, and this is a rather rare situation anyways (reported once in years),
// work around this locally by falling back to reading as many bytes as possible and using a standard non-pooled memory stream.
return new MemoryStream(s.ReadAllRemainingBytesToArray());
}
}
public override void Dispose()
{
archive.Dispose();
archiveStream.Dispose();
}
public override IEnumerable<string> Filenames => archive.Entries.Where(e => !e.IsDirectory).Select(e => e.Key).ExcludeSystemFileNames();
private class MemoryOwnerMemoryStream : Stream
{
private readonly IMemoryOwner<byte> owner;
private readonly Stream stream;
public MemoryOwnerMemoryStream(IMemoryOwner<byte> owner)
{
this.owner = owner;
stream = owner.Memory.AsStream();
}
protected override void Dispose(bool disposing)
{
owner?.Dispose();
base.Dispose(disposing);
}
public override void Flush() => stream.Flush();
public override int Read(byte[] buffer, int offset, int count) => stream.Read(buffer, offset, count);
public override long Seek(long offset, SeekOrigin origin) => stream.Seek(offset, origin);
public override void SetLength(long value) => stream.SetLength(value);
public override void Write(byte[] buffer, int offset, int count) => stream.Write(buffer, offset, count);
public override bool CanRead => stream.CanRead;
public override bool CanSeek => stream.CanSeek;
public override bool CanWrite => stream.CanWrite;
public override long Length => stream.Length;
public override long Position
{
get => stream.Position;
set => stream.Position = value;
}
}
}
}