1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 03:07:51 +08:00
osu-lazer/osu.Game/IO/ArchiveReader.cs

59 lines
1.8 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2016-12-06 17:56:20 +08:00
using System;
using System.Collections.Generic;
using System.IO;
2016-11-05 19:00:14 +08:00
using osu.Framework.IO.Stores;
2016-10-10 21:20:06 +08:00
using osu.Framework.Platform;
namespace osu.Game.IO
{
2016-11-05 19:00:14 +08:00
public abstract class ArchiveReader : IDisposable, IResourceStore<byte[]>
{
protected class Reader
{
2017-02-23 14:38:17 +08:00
public Func<Storage, string, bool> Test { get; set; }
public Type Type { get; set; }
}
protected static List<Reader> Readers { get; } = new List<Reader>();
2017-02-23 14:38:17 +08:00
public static ArchiveReader GetReader(Storage storage, string path)
{
foreach (var reader in Readers)
{
if (reader.Test(storage, path))
return (ArchiveReader)Activator.CreateInstance(reader.Type, storage.GetStream(path));
}
throw new IOException(@"Unknown file format");
}
2017-02-23 14:38:17 +08:00
protected static void AddReader<T>(Func<Storage, string, bool> test) where T : ArchiveReader
{
Readers.Add(new Reader { Test = test, Type = typeof(T) });
}
/// <summary>
/// Opens a stream for reading a specific file from this archive.
/// </summary>
2016-11-05 19:00:14 +08:00
public abstract Stream GetStream(string name);
public abstract void Dispose();
2016-11-05 19:00:14 +08:00
public virtual byte[] Get(string name)
{
using (Stream input = GetStream(name))
{
if (input == null)
return null;
using (MemoryStream ms = new MemoryStream())
{
input.CopyTo(ms);
return ms.ToArray();
}
}
}
}
}