osu/osu.Game/IO/Archives/ArchiveReader.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

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