2016-10-14 11:33:58 +08:00
|
|
|
|
using System;
|
2016-10-05 04:29:08 +08:00
|
|
|
|
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;
|
2016-10-19 01:35:01 +08:00
|
|
|
|
using osu.Game.Database;
|
2016-10-05 04:29:08 +08:00
|
|
|
|
|
2016-10-14 11:33:58 +08:00
|
|
|
|
namespace osu.Game.Beatmaps.IO
|
|
|
|
|
{
|
2016-11-05 19:00:14 +08:00
|
|
|
|
public abstract class ArchiveReader : IDisposable, IResourceStore<byte[]>
|
2016-10-14 11:33:58 +08:00
|
|
|
|
{
|
|
|
|
|
private class Reader
|
|
|
|
|
{
|
|
|
|
|
public Func<BasicStorage, string, bool> Test { get; set; }
|
|
|
|
|
public Type Type { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static List<Reader> readers { get; } = new List<Reader>();
|
|
|
|
|
|
|
|
|
|
public static ArchiveReader GetReader(BasicStorage 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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static void AddReader<T>(Func<BasicStorage, string, bool> test) where T : ArchiveReader
|
|
|
|
|
{
|
|
|
|
|
readers.Add(new Reader { Test = test, Type = typeof(T) });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads the beatmap metadata from this archive.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public abstract BeatmapMetadata ReadMetadata();
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a list of beatmap file names.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public abstract string[] ReadBeatmaps();
|
|
|
|
|
/// <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);
|
2016-10-10 21:26:34 +08:00
|
|
|
|
|
2016-10-14 11:33:58 +08:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-14 11:33:58 +08:00
|
|
|
|
}
|
2016-10-05 04:29:08 +08:00
|
|
|
|
}
|