using System; using System.Collections.Generic; using System.IO; using osu.Framework.IO.Stores; using osu.Framework.Platform; using osu.Game.Database; namespace osu.Game.Beatmaps.IO { public abstract class ArchiveReader : IDisposable, IResourceStore { private class Reader { public Func Test { get; set; } public Type Type { get; set; } } private static List readers { get; } = new List(); 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(Func test) where T : ArchiveReader { readers.Add(new Reader { Test = test, Type = typeof(T) }); } /// /// Reads the beatmap metadata from this archive. /// public abstract BeatmapMetadata ReadMetadata(); /// /// Gets a list of beatmap file names. /// public abstract string[] ReadBeatmaps(); /// /// Opens a stream for reading a specific file from this archive. /// public abstract Stream GetStream(string name); public abstract void Dispose(); 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(); } } } } }