2019-01-24 08:43:03 +00:00
|
|
|
|
// 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
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2018-08-27 08:05:58 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Framework.IO.Stores;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.IO.Archives
|
|
|
|
|
{
|
2018-04-21 09:15:27 +00:00
|
|
|
|
public abstract class ArchiveReader : IResourceStore<byte[]>
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Opens a stream for reading a specific file from this archive.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public abstract Stream GetStream(string name);
|
|
|
|
|
|
2019-05-31 05:33:18 +00:00
|
|
|
|
public IEnumerable<string> GetAvailableResources() => Filenames;
|
|
|
|
|
|
2018-04-13 09:19:50 +00: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; }
|
|
|
|
|
|
2018-08-27 08:05:58 +00:00
|
|
|
|
public virtual byte[] Get(string name) => GetAsync(name).Result;
|
|
|
|
|
|
|
|
|
|
public async Task<byte[]> GetAsync(string name)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
using (Stream input = GetStream(name))
|
|
|
|
|
{
|
|
|
|
|
if (input == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
byte[] buffer = new byte[input.Length];
|
2021-03-08 03:57:16 +00:00
|
|
|
|
await input.ReadAsync(buffer).ConfigureAwait(false);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|