1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 05:37:24 +08:00
osu-lazer/osu.Game/IO/Archives/ArchiveReader.cs

56 lines
1.6 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
2018-04-13 17:19:50 +08:00
using System.Collections.Generic;
using System.IO;
using System.Threading;
2018-08-27 16:05:58 +08:00
using System.Threading.Tasks;
using osu.Framework.Extensions;
2018-04-13 17:19:50 +08:00
using osu.Framework.IO.Stores;
namespace osu.Game.IO.Archives
{
2018-04-21 17:15:27 +08:00
public abstract class ArchiveReader : IResourceStore<byte[]>
2018-04-13 17:19:50 +08:00
{
/// <summary>
/// Opens a stream for reading a specific file from this archive.
/// </summary>
public abstract Stream GetStream(string name);
public IEnumerable<string> GetAvailableResources() => Filenames;
2018-04-13 17:19:50 +08:00
public abstract void Dispose();
/// <summary>
/// The name of this archive (usually the containing filename).
/// </summary>
public readonly string Name;
protected ArchiveReader(string name)
{
Name = name;
}
public abstract IEnumerable<string> Filenames { get; }
public virtual byte[] Get(string name)
{
using (Stream input = GetStream(name))
return input?.ReadAllBytesToArray();
}
2018-08-27 16:05:58 +08:00
public async Task<byte[]> GetAsync(string name, CancellationToken cancellationToken = default)
2018-04-13 17:19:50 +08:00
{
using (Stream input = GetStream(name))
{
if (input == null)
return null;
return await input.ReadAllBytesToArrayAsync(cancellationToken).ConfigureAwait(false);
2018-04-13 17:19:50 +08:00
}
}
}
}