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