1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-12 18:13:16 +08:00

Implement OszArchiveReader

This commit is contained in:
Drew DeVault 2016-10-04 17:08:43 -04:00
parent 5dd57e4be2
commit 87f3e2e1db
4 changed files with 35 additions and 11 deletions

View File

@ -19,15 +19,16 @@ namespace osu.Desktop.Beatmaps.IO
} }
private string BasePath { get; set; } private string BasePath { get; set; }
private string[] Beatmaps { get; set; }
private Beatmap FirstMap { get; set; } private Beatmap FirstMap { get; set; }
public LegacyFilesystemReader(string path) public LegacyFilesystemReader(string path)
{ {
BasePath = path; BasePath = path;
var maps = ReadBeatmaps(); Beatmaps = Directory.GetFiles(BasePath, "*.osu").Select(f => Path.GetFileName(f)).ToArray();
if (maps.Length == 0) if (Beatmaps.Length == 0)
throw new FileNotFoundException("This directory contains no beatmaps"); throw new FileNotFoundException("This directory contains no beatmaps");
using (var stream = new StreamReader(ReadFile(maps[0]))) using (var stream = new StreamReader(ReadFile(Beatmaps[0])))
{ {
var decoder = BeatmapDecoder.GetDecoder(stream); var decoder = BeatmapDecoder.GetDecoder(stream);
FirstMap = decoder.Decode(stream); FirstMap = decoder.Decode(stream);
@ -36,7 +37,7 @@ namespace osu.Desktop.Beatmaps.IO
public override string[] ReadBeatmaps() public override string[] ReadBeatmaps()
{ {
return Directory.GetFiles(BasePath, "*.osu").Select(f => Path.GetFileName(f)).ToArray(); return Beatmaps;
} }
public override Stream ReadFile(string name) public override Stream ReadFile(string name)

View File

@ -1,5 +1,8 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using Ionic.Zip;
using osu.Game.Beatmaps.Formats;
namespace osu.Game.Beatmaps.IO namespace osu.Game.Beatmaps.IO
{ {
@ -11,32 +14,48 @@ namespace osu.Game.Beatmaps.IO
{ {
using (var stream = storage.GetStream(path)) using (var stream = storage.GetStream(path))
{ {
// TODO: detect if osz if (!ZipFile.IsZipFile(stream, false))
return false; return false;
using (ZipFile zip = ZipFile.Read(stream))
return zip.Entries.Any(e => e.FileName.EndsWith(".osu"));
} }
}); });
} }
private Stream Archive { get; set; } private ZipFile Archive { get; set; }
private string[] Beatmaps { get; set; }
private Beatmap FirstMap { get; set; }
public OszArchiveReader(Stream archive) public OszArchiveReader(Stream archive)
{ {
Archive = archive; 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() public override string[] ReadBeatmaps()
{ {
throw new NotImplementedException(); return Beatmaps;
} }
public override Stream ReadFile(string name) public override Stream ReadFile(string name)
{ {
throw new NotImplementedException(); ZipEntry entry = Archive.Entries.SingleOrDefault(e => e.FileName == name);
if (entry == null)
throw new FileNotFoundException();
return entry.OpenReader();
} }
public override Metadata ReadMetadata() public override Metadata ReadMetadata()
{ {
throw new NotImplementedException(); return FirstMap.Metadata;
} }
} }
} }

View File

@ -55,6 +55,9 @@
<Reference Include="SQLite-net"> <Reference Include="SQLite-net">
<HintPath>..\packages\sqlite-net-pcl.1.2.0\lib\portable-net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10\SQLite-net.dll</HintPath> <HintPath>..\packages\sqlite-net-pcl.1.2.0\lib\portable-net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10\SQLite-net.dll</HintPath>
</Reference> </Reference>
<Reference Include="DotNetZip">
<HintPath>..\packages\DotNetZip.1.10.1\lib\net20\DotNetZip.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Beatmaps\Beatmap.cs" /> <Compile Include="Beatmaps\Beatmap.cs" />

View File

@ -4,6 +4,7 @@ Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
--> -->
<packages> <packages>
<package id="DotNetZip" version="1.10.1" targetFramework="net45" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" /> <package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />
<package id="ppy.OpenTK" version="1.1.2225.3" targetFramework="net45" /> <package id="ppy.OpenTK" version="1.1.2225.3" targetFramework="net45" />
<package id="sqlite-net-pcl" version="1.2.0" targetFramework="net45" /> <package id="sqlite-net-pcl" version="1.2.0" targetFramework="net45" />