1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 06:57:39 +08:00
osu-lazer/osu.Game/Beatmaps/IO/OszArchiveReader.cs

70 lines
2.2 KiB
C#
Raw Normal View History

2016-12-06 17:56:20 +08:00
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.IO;
2016-10-05 05:08:43 +08:00
using System.Linq;
using System.Security.Cryptography;
2016-10-05 05:08:43 +08:00
using Ionic.Zip;
using osu.Game.Beatmaps.Formats;
2016-10-19 01:35:01 +08:00
using osu.Game.Database;
namespace osu.Game.Beatmaps.IO
{
public sealed class OszArchiveReader : ArchiveReader
{
public static void Register()
{
AddReader<OszArchiveReader>((storage, path) =>
{
using (var stream = storage.GetStream(path))
return ZipFile.IsZipFile(stream, false);
});
OsuLegacyDecoder.Register();
}
private Stream archiveStream;
private ZipFile archive;
private string[] beatmaps;
private Beatmap firstMap;
public OszArchiveReader(Stream archiveStream)
{
this.archiveStream = archiveStream;
archive = ZipFile.Read(archiveStream);
beatmaps = archive.Entries.Where(e => e.FileName.EndsWith(@".osu"))
.Select(e => e.FileName).ToArray();
if (beatmaps.Length == 0)
throw new FileNotFoundException(@"This directory contains no beatmaps");
2016-11-05 19:00:14 +08:00
using (var stream = new StreamReader(GetStream(beatmaps[0])))
{
var decoder = BeatmapDecoder.GetDecoder(stream);
2016-10-19 01:35:01 +08:00
firstMap = decoder.Decode(stream);
}
}
public override string[] ReadBeatmaps()
{
2016-10-11 02:00:33 +08:00
return beatmaps;
}
2016-11-05 19:00:14 +08:00
public override Stream GetStream(string name)
{
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();
}
public override BeatmapMetadata ReadMetadata()
{
2016-10-19 01:35:01 +08:00
return firstMap.BeatmapInfo.Metadata;
}
2016-10-19 01:35:01 +08:00
public override void Dispose()
{
archive.Dispose();
archiveStream.Dispose();
}
}
}