2017-02-07 12:59:30 +08:00
|
|
|
|
// 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;
|
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-05 04:29:08 +08:00
|
|
|
|
|
2017-03-14 10:46:34 +08:00
|
|
|
|
namespace osu.Game.Beatmaps.IO
|
2016-10-14 11:33:58 +08:00
|
|
|
|
{
|
2016-11-05 19:00:14 +08:00
|
|
|
|
public abstract class ArchiveReader : IDisposable, IResourceStore<byte[]>
|
2016-10-14 11:33:58 +08:00
|
|
|
|
{
|
2017-03-14 10:46:34 +08:00
|
|
|
|
private class Reader
|
2016-10-14 11:33:58 +08:00
|
|
|
|
{
|
2017-05-08 18:56:04 +08:00
|
|
|
|
public Func<Storage, string, bool> Test;
|
|
|
|
|
public Type Type;
|
2016-10-14 11:33:58 +08:00
|
|
|
|
}
|
2016-11-22 13:53:23 +08:00
|
|
|
|
|
2017-05-08 18:56:04 +08:00
|
|
|
|
private static readonly List<Reader> readers = new List<Reader>();
|
2017-02-13 17:30:47 +08:00
|
|
|
|
|
2017-02-23 14:38:17 +08:00
|
|
|
|
public static ArchiveReader GetReader(Storage storage, string path)
|
2016-10-14 11:33:58 +08:00
|
|
|
|
{
|
2017-03-14 10:46:34 +08:00
|
|
|
|
foreach (var reader in readers)
|
2016-10-14 11:33:58 +08:00
|
|
|
|
{
|
|
|
|
|
if (reader.Test(storage, path))
|
|
|
|
|
return (ArchiveReader)Activator.CreateInstance(reader.Type, storage.GetStream(path));
|
|
|
|
|
}
|
|
|
|
|
throw new IOException(@"Unknown file format");
|
|
|
|
|
}
|
2017-02-13 17:30:47 +08:00
|
|
|
|
|
2017-02-23 14:38:17 +08:00
|
|
|
|
protected static void AddReader<T>(Func<Storage, string, bool> test) where T : ArchiveReader
|
2016-10-14 11:33:58 +08:00
|
|
|
|
{
|
2017-03-14 10:46:34 +08:00
|
|
|
|
readers.Add(new Reader { Test = test, Type = typeof(T) });
|
2016-10-14 11:33:58 +08:00
|
|
|
|
}
|
2017-02-13 17:30:47 +08:00
|
|
|
|
|
2017-03-14 10:46:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a list of beatmap file names.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string[] BeatmapFilenames { get; protected set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The storyboard filename. Null if no storyboard is present.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string StoryboardFilename { get; protected set; }
|
|
|
|
|
|
2017-02-09 22:09:48 +08:00
|
|
|
|
/// <summary>
|
2016-10-14 11:33:58 +08:00
|
|
|
|
/// 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;
|
|
|
|
|
|
2017-05-08 19:41:22 +08:00
|
|
|
|
byte[] buffer = new byte[input.Length];
|
|
|
|
|
input.Read(buffer, 0, buffer.Length);
|
|
|
|
|
return buffer;
|
2016-11-05 19:00:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-14 11:33:58 +08:00
|
|
|
|
}
|
2016-10-05 04:29:08 +08:00
|
|
|
|
}
|