mirror of
https://github.com/ppy/osu.git
synced 2025-01-12 17:43:05 +08:00
Refactor ArchiveReader for extensibility
This commit is contained in:
parent
f0126e8c76
commit
fd53d10264
@ -13,7 +13,7 @@ namespace osu.Desktop.Beatmaps.IO
|
||||
/// <summary>
|
||||
/// Reads an extracted legacy beatmap from disk.
|
||||
/// </summary>
|
||||
public class LegacyFilesystemReader : ArchiveReader
|
||||
public class LegacyFilesystemReader : BeatmapArchiveReader
|
||||
{
|
||||
public static void Register() => AddReader<LegacyFilesystemReader>((storage, path) => Directory.Exists(path));
|
||||
|
||||
|
@ -6,23 +6,22 @@ using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using osu.Framework.IO.Stores;
|
||||
using osu.Framework.Platform;
|
||||
using osu.Game.Database;
|
||||
|
||||
namespace osu.Game.Beatmaps.IO
|
||||
{
|
||||
public abstract class ArchiveReader : IDisposable, IResourceStore<byte[]>
|
||||
{
|
||||
private class Reader
|
||||
protected class Reader
|
||||
{
|
||||
public Func<Storage, string, bool> Test { get; set; }
|
||||
public Type Type { get; set; }
|
||||
}
|
||||
|
||||
private static List<Reader> readers { get; } = new List<Reader>();
|
||||
protected static List<Reader> Readers { get; } = new List<Reader>();
|
||||
|
||||
public static ArchiveReader GetReader(Storage storage, string path)
|
||||
{
|
||||
foreach (var reader in readers)
|
||||
foreach (var reader in Readers)
|
||||
{
|
||||
if (reader.Test(storage, path))
|
||||
return (ArchiveReader)Activator.CreateInstance(reader.Type, storage.GetStream(path));
|
||||
@ -32,24 +31,9 @@ namespace osu.Game.Beatmaps.IO
|
||||
|
||||
protected static void AddReader<T>(Func<Storage, string, bool> test) where T : ArchiveReader
|
||||
{
|
||||
readers.Add(new Reader { Test = test, Type = typeof(T) });
|
||||
Readers.Add(new Reader { Test = test, Type = typeof(T) });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the beatmap metadata from this archive.
|
||||
/// </summary>
|
||||
public abstract BeatmapMetadata ReadMetadata();
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of beatmap file names.
|
||||
/// </summary>
|
||||
public string[] BeatmapFilenames { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// The storyboard filename. Null if no storyboard is present.
|
||||
/// </summary>
|
||||
public string StoryboardFilename { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// Opens a stream for reading a specific file from this archive.
|
||||
/// </summary>
|
||||
|
39
osu.Game/Beatmaps/IO/BeatmapArchiveReader.cs
Normal file
39
osu.Game/Beatmaps/IO/BeatmapArchiveReader.cs
Normal file
@ -0,0 +1,39 @@
|
||||
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
||||
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using osu.Framework.Platform;
|
||||
using osu.Game.Database;
|
||||
|
||||
namespace osu.Game.Beatmaps.IO
|
||||
{
|
||||
public abstract class BeatmapArchiveReader : ArchiveReader
|
||||
{
|
||||
|
||||
public static BeatmapArchiveReader GetBeatmapArchiveReader(Storage storage, string path)
|
||||
{
|
||||
foreach (var reader in Readers)
|
||||
{
|
||||
if (reader.Test(storage, path))
|
||||
return (BeatmapArchiveReader)Activator.CreateInstance(reader.Type, storage.GetStream(path));
|
||||
}
|
||||
throw new IOException(@"Unknown file format");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the beatmap metadata from this archive.
|
||||
/// </summary>
|
||||
public abstract BeatmapMetadata ReadMetadata();
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of beatmap file names.
|
||||
/// </summary>
|
||||
public string[] BeatmapFilenames { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// The storyboard filename. Null if no storyboard is present.
|
||||
/// </summary>
|
||||
public string StoryboardFilename { get; protected set; }
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@ using osu.Game.Database;
|
||||
|
||||
namespace osu.Game.Beatmaps.IO
|
||||
{
|
||||
public sealed class OszArchiveReader : ArchiveReader
|
||||
public sealed class OszArchiveReader : BeatmapArchiveReader
|
||||
{
|
||||
public static void Register()
|
||||
{
|
||||
|
@ -19,7 +19,7 @@ namespace osu.Game.Beatmaps
|
||||
|
||||
public readonly bool WithStoryboard;
|
||||
|
||||
protected abstract ArchiveReader GetReader();
|
||||
protected abstract BeatmapArchiveReader GetReader();
|
||||
|
||||
protected WorkingBeatmap(BeatmapInfo beatmapInfo, BeatmapSetInfo beatmapSetInfo, bool withStoryboard = false)
|
||||
{
|
||||
@ -87,7 +87,7 @@ namespace osu.Game.Beatmaps
|
||||
set { lock (beatmapLock) beatmap = value; }
|
||||
}
|
||||
|
||||
private ArchiveReader trackReader;
|
||||
private BeatmapArchiveReader trackReader;
|
||||
private Track track;
|
||||
private object trackLock = new object();
|
||||
public Track Track
|
||||
|
@ -176,7 +176,7 @@ namespace osu.Game.Database
|
||||
|
||||
BeatmapMetadata metadata;
|
||||
|
||||
using (var reader = ArchiveReader.GetReader(storage, path))
|
||||
using (var reader = BeatmapArchiveReader.GetBeatmapArchiveReader(storage, path))
|
||||
metadata = reader.ReadMetadata();
|
||||
|
||||
if (File.Exists(path)) // Not always the case, i.e. for LegacyFilesystemReader
|
||||
@ -216,7 +216,7 @@ namespace osu.Game.Database
|
||||
Metadata = metadata
|
||||
};
|
||||
|
||||
using (var reader = ArchiveReader.GetReader(storage, path))
|
||||
using (var reader = BeatmapArchiveReader.GetBeatmapArchiveReader(storage, path))
|
||||
{
|
||||
string[] mapNames = reader.BeatmapFilenames;
|
||||
foreach (var name in mapNames)
|
||||
@ -261,12 +261,12 @@ namespace osu.Game.Database
|
||||
BeatmapSetRemoved?.Invoke(beatmapSet);
|
||||
}
|
||||
|
||||
public ArchiveReader GetReader(BeatmapSetInfo beatmapSet)
|
||||
public BeatmapArchiveReader GetReader(BeatmapSetInfo beatmapSet)
|
||||
{
|
||||
if (string.IsNullOrEmpty(beatmapSet.Path))
|
||||
return null;
|
||||
|
||||
return ArchiveReader.GetReader(storage, beatmapSet.Path);
|
||||
return BeatmapArchiveReader.GetBeatmapArchiveReader(storage, beatmapSet.Path);
|
||||
}
|
||||
|
||||
public BeatmapSetInfo GetBeatmapSet(int id)
|
||||
@ -348,7 +348,7 @@ namespace osu.Game.Database
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
protected override ArchiveReader GetReader() => database?.GetReader(BeatmapSetInfo);
|
||||
protected override BeatmapArchiveReader GetReader() => database?.GetReader(BeatmapSetInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -223,7 +223,7 @@
|
||||
<Compile Include="Users\User.cs" />
|
||||
<Compile Include="Graphics\UserInterface\Volume\VolumeControl.cs" />
|
||||
<Compile Include="Database\BeatmapDatabase.cs" />
|
||||
<Compile Include="Beatmaps\IO\ArchiveReader.cs" />
|
||||
<Compile Include="Beatmaps\IO\BeatmapArchiveReader.cs" />
|
||||
<Compile Include="Beatmaps\Formats\BeatmapDecoder.cs" />
|
||||
<Compile Include="Beatmaps\Formats\OsuLegacyDecoder.cs" />
|
||||
<Compile Include="Beatmaps\IO\OszArchiveReader.cs" />
|
||||
@ -311,6 +311,7 @@
|
||||
<Compile Include="Screens\Select\Options\BeatmapOptionsEditButton.cs" />
|
||||
<Compile Include="Screens\Select\Options\BeatmapOptionsOverlay.cs" />
|
||||
<Compile Include="Screens\Select\Options\BeatmapOptionsRemoveFromUnplayedButton.cs" />
|
||||
<Compile Include="Beatmaps\IO\ArchiveReader.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="$(SolutionDir)\osu-framework\osu.Framework\osu.Framework.csproj">
|
||||
|
Loading…
Reference in New Issue
Block a user