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
|
|
|
|
|
2016-10-05 04:29:08 +08:00
|
|
|
|
using System.IO;
|
2016-10-05 05:08:43 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using Ionic.Zip;
|
|
|
|
|
using osu.Game.Beatmaps.Formats;
|
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
|
|
|
|
|
{
|
2017-03-14 10:46:34 +08:00
|
|
|
|
public sealed class OszArchiveReader : ArchiveReader
|
2016-10-14 11:33:58 +08:00
|
|
|
|
{
|
|
|
|
|
public static void Register()
|
|
|
|
|
{
|
2016-10-05 04:29:08 +08:00
|
|
|
|
AddReader<OszArchiveReader>((storage, path) =>
|
2016-10-14 11:33:58 +08:00
|
|
|
|
{
|
|
|
|
|
using (var stream = storage.GetStream(path))
|
2017-03-14 10:46:34 +08:00
|
|
|
|
return ZipFile.IsZipFile(stream, false);
|
2016-10-14 11:33:58 +08:00
|
|
|
|
});
|
|
|
|
|
OsuLegacyDecoder.Register();
|
|
|
|
|
}
|
2016-10-19 23:00:11 +08:00
|
|
|
|
|
|
|
|
|
private Stream archiveStream;
|
|
|
|
|
private ZipFile archive;
|
|
|
|
|
private Beatmap firstMap;
|
2017-02-09 22:09:48 +08:00
|
|
|
|
|
2016-10-14 11:33:58 +08:00
|
|
|
|
public OszArchiveReader(Stream archiveStream)
|
|
|
|
|
{
|
2016-10-19 23:00:11 +08:00
|
|
|
|
this.archiveStream = archiveStream;
|
2016-10-14 11:33:58 +08:00
|
|
|
|
archive = ZipFile.Read(archiveStream);
|
2017-02-13 17:30:47 +08:00
|
|
|
|
BeatmapFilenames = archive.Entries.Where(e => e.FileName.EndsWith(@".osu"))
|
2016-10-14 11:33:58 +08:00
|
|
|
|
.Select(e => e.FileName).ToArray();
|
2017-02-13 17:30:47 +08:00
|
|
|
|
if (BeatmapFilenames.Length == 0)
|
2016-10-14 11:33:58 +08:00
|
|
|
|
throw new FileNotFoundException(@"This directory contains no beatmaps");
|
2017-02-13 17:30:47 +08:00
|
|
|
|
StoryboardFilename = archive.Entries.Where(e => e.FileName.EndsWith(@".osb"))
|
2017-02-09 22:09:48 +08:00
|
|
|
|
.Select(e => e.FileName).FirstOrDefault();
|
2017-02-13 17:30:47 +08:00
|
|
|
|
using (var stream = new StreamReader(GetStream(BeatmapFilenames[0])))
|
2016-10-14 11:33:58 +08:00
|
|
|
|
{
|
|
|
|
|
var decoder = BeatmapDecoder.GetDecoder(stream);
|
2016-10-19 01:35:01 +08:00
|
|
|
|
firstMap = decoder.Decode(stream);
|
2016-10-14 11:33:58 +08:00
|
|
|
|
}
|
2016-10-05 04:29:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-05 19:00:14 +08:00
|
|
|
|
public override Stream GetStream(string name)
|
2016-10-05 04:29:08 +08:00
|
|
|
|
{
|
2016-10-14 11:33:58 +08:00
|
|
|
|
ZipEntry entry = archive.Entries.SingleOrDefault(e => e.FileName == name);
|
|
|
|
|
if (entry == null)
|
|
|
|
|
throw new FileNotFoundException();
|
2016-10-05 05:08:43 +08:00
|
|
|
|
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-19 01:35:01 +08:00
|
|
|
|
return firstMap.BeatmapInfo.Metadata;
|
2016-10-05 04:29:08 +08:00
|
|
|
|
}
|
2016-10-19 01:35:01 +08:00
|
|
|
|
|
|
|
|
|
public override void Dispose()
|
2016-10-14 11:33:58 +08:00
|
|
|
|
{
|
|
|
|
|
archive.Dispose();
|
2016-10-19 23:00:11 +08:00
|
|
|
|
archiveStream.Dispose();
|
2016-10-10 21:26:34 +08:00
|
|
|
|
}
|
2016-10-14 11:33:58 +08:00
|
|
|
|
}
|
2016-10-05 04:29:08 +08:00
|
|
|
|
}
|