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

61 lines
1.9 KiB
C#
Raw Normal View History

using System;
using System.IO;
2016-10-05 05:08:43 +08:00
using System.Linq;
using Ionic.Zip;
using osu.Game.Beatmaps.Formats;
namespace osu.Game.Beatmaps.IO
{
public class OszArchiveReader : ArchiveReader
{
static OszArchiveReader()
{
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 05:08:43 +08:00
private ZipFile Archive { get; set; }
private string[] Beatmaps { get; set; }
private Beatmap FirstMap { get; set; }
public OszArchiveReader(Stream archive)
{
2016-10-05 05:08:43 +08:00
Archive = ZipFile.Read(archive);
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");
using (var stream = new StreamReader(ReadFile(Beatmaps[0])))
{
var decoder = BeatmapDecoder.GetDecoder(stream);
FirstMap = decoder.Decode(stream);
}
}
public override string[] ReadBeatmaps()
{
2016-10-05 05:08:43 +08:00
return Beatmaps;
}
public override Stream ReadFile(string name)
{
2016-10-05 05:08:43 +08:00
ZipEntry entry = Archive.Entries.SingleOrDefault(e => e.FileName == name);
if (entry == null)
throw new FileNotFoundException();
return entry.OpenReader();
}
public override Metadata ReadMetadata()
{
2016-10-05 05:08:43 +08:00
return FirstMap.Metadata;
}
}
}