2016-10-05 04:29:08 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2016-10-05 05:08:43 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using Ionic.Zip;
|
|
|
|
|
using osu.Game.Beatmaps.Formats;
|
2016-10-05 04:29:08 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Beatmaps.IO
|
|
|
|
|
{
|
2016-10-08 03:09:52 +08:00
|
|
|
|
public sealed class OszArchiveReader : ArchiveReader
|
2016-10-05 04:29:08 +08:00
|
|
|
|
{
|
2016-10-08 03:09:52 +08:00
|
|
|
|
public static void Register()
|
2016-10-05 04:29:08 +08:00
|
|
|
|
{
|
|
|
|
|
AddReader<OszArchiveReader>((storage, path) =>
|
|
|
|
|
{
|
|
|
|
|
using (var stream = storage.GetStream(path))
|
|
|
|
|
{
|
2016-10-05 05:08:43 +08:00
|
|
|
|
if (!ZipFile.IsZipFile(stream, false))
|
|
|
|
|
return false;
|
|
|
|
|
using (ZipFile zip = ZipFile.Read(stream))
|
|
|
|
|
return zip.Entries.Any(e => e.FileName.EndsWith(".osu"));
|
2016-10-05 04:29:08 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
2016-10-08 03:09:52 +08:00
|
|
|
|
OsuLegacyDecoder.Register();
|
2016-10-05 04:29:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-11 02:00:33 +08:00
|
|
|
|
private ZipFile archive { get; set; }
|
|
|
|
|
private string[] beatmaps { get; set; }
|
|
|
|
|
private Beatmap firstMap { get; set; }
|
2016-10-05 04:29:08 +08:00
|
|
|
|
|
2016-10-11 02:00:33 +08:00
|
|
|
|
public OszArchiveReader(Stream archiveStream)
|
2016-10-05 04:29:08 +08:00
|
|
|
|
{
|
2016-10-11 02:00:33 +08:00
|
|
|
|
archive = ZipFile.Read(archiveStream);
|
|
|
|
|
beatmaps = archive.Entries.Where(e => e.FileName.EndsWith(".osu"))
|
2016-10-05 05:08:43 +08:00
|
|
|
|
.Select(e => e.FileName).ToArray();
|
2016-10-11 02:00:33 +08:00
|
|
|
|
if (beatmaps.Length == 0)
|
2016-10-05 05:08:43 +08:00
|
|
|
|
throw new FileNotFoundException("This directory contains no beatmaps");
|
2016-10-11 02:00:33 +08:00
|
|
|
|
using (var stream = new StreamReader(ReadFile(beatmaps[0])))
|
2016-10-05 05:08:43 +08:00
|
|
|
|
{
|
|
|
|
|
var decoder = BeatmapDecoder.GetDecoder(stream);
|
2016-10-11 02:00:33 +08:00
|
|
|
|
firstMap = decoder.Decode(stream);
|
2016-10-05 05:08:43 +08:00
|
|
|
|
}
|
2016-10-05 04:29:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string[] ReadBeatmaps()
|
|
|
|
|
{
|
2016-10-11 02:00:33 +08:00
|
|
|
|
return beatmaps;
|
2016-10-05 04:29:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Stream ReadFile(string name)
|
|
|
|
|
{
|
2016-10-11 02:00:33 +08:00
|
|
|
|
ZipEntry entry = archive.Entries.SingleOrDefault(e => e.FileName == name);
|
2016-10-05 05:08:43 +08:00
|
|
|
|
if (entry == null)
|
|
|
|
|
throw new FileNotFoundException();
|
|
|
|
|
return entry.OpenReader();
|
2016-10-05 04:29:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-08 01:50:34 +08:00
|
|
|
|
public override BeatmapMetadata ReadMetadata()
|
2016-10-05 04:29:08 +08:00
|
|
|
|
{
|
2016-10-11 02:00:33 +08:00
|
|
|
|
return firstMap.Metadata;
|
2016-10-05 04:29:08 +08:00
|
|
|
|
}
|
2016-10-10 21:26:34 +08:00
|
|
|
|
public override void Dispose()
|
|
|
|
|
{
|
2016-10-11 02:00:33 +08:00
|
|
|
|
archive.Dispose();
|
2016-10-10 21:26:34 +08:00
|
|
|
|
}
|
2016-10-05 04:29:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|