1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-28 09:37:23 +08:00

Add more decoding (including full BeatmapMetadata)

This commit is contained in:
Drew DeVault 2016-10-07 15:36:20 -04:00
parent 222f9ade55
commit f776a54b9a
6 changed files with 206 additions and 106 deletions

View File

@ -2,6 +2,7 @@
using System.IO; using System.IO;
using NUnit.Framework; using NUnit.Framework;
using osu.Game.Beatmaps.IO; using osu.Game.Beatmaps.IO;
using osu.Game.GameModes.Play;
using osu.Game.Tests.Resources; using osu.Game.Tests.Resources;
namespace osu.Game.Tests.Beatmaps.IO namespace osu.Game.Tests.Beatmaps.IO
@ -40,6 +41,27 @@ namespace osu.Game.Tests.Beatmaps.IO
foreach (var map in expected) foreach (var map in expected)
Assert.Contains(map, maps); Assert.Contains(map, maps);
} }
}
[Test]
public void TestReadMetadata()
{
using (var osz = File.OpenRead(Resource.GetPath("241526 Soleily - Renatus.osz")))
{
var reader = new OszArchiveReader(osz);
var meta = reader.ReadMetadata();
Assert.AreEqual(241526, meta.BeatmapSetID);
Assert.AreEqual("Soleily", meta.Artist);
Assert.AreEqual("Soleily", meta.ArtistUnicode);
Assert.AreEqual("03. Renatus - Soleily 192kbps.mp3", meta.AudioFile);
Assert.AreEqual("Deif", meta.Author);
Assert.AreEqual("machinetop_background.jpg", meta.BackgroundFile);
Assert.AreEqual(164471, meta.PreviewTime);
Assert.AreEqual(string.Empty, meta.Source);
Assert.AreEqual("MBC7 Unisphere 地球ヤバイEP Chikyu Yabai", meta.Tags);
Assert.AreEqual("Renatus", meta.Title);
Assert.AreEqual("Renatus", meta.TitleUnicode);
}
} }
} }
} }

View File

@ -4,6 +4,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using osu.Game.Beatmaps.Objects; using osu.Game.Beatmaps.Objects;
using osu.Game.Beatmaps.Timing; using osu.Game.Beatmaps.Timing;
using osu.Game.GameModes.Play;
using osu.Game.Users; using osu.Game.Users;
using SQLite; using SQLite;
@ -26,6 +27,7 @@ namespace osu.Game.Beatmaps
public BeatmapMetadata Metadata { get; set; } public BeatmapMetadata Metadata { get; set; }
[Ignore] [Ignore]
public BaseDifficulty BaseDifficulty { get; set; } public BaseDifficulty BaseDifficulty { get; set; }
public PlayMode Mode { get; set; }
public string Version { get; set; } public string Version { get; set; }
} }
} }

View File

@ -19,7 +19,6 @@ namespace osu.Game.Beatmaps
public string Author { get; set; } public string Author { get; set; }
public string Source { get; set; } public string Source { get; set; }
public string Tags { get; set; } public string Tags { get; set; }
public PlayMode Mode { get; set; }
public int PreviewTime { get; set; } public int PreviewTime { get; set; }
public string AudioFile { get; set; } public string AudioFile { get; set; }
public string BackgroundFile { get; set; } public string BackgroundFile { get; set; }

View File

@ -0,0 +1,14 @@
using System;
namespace osu.Game.Beatmaps.Events
{
public enum EventType
{
Background = 0,
Video = 1,
Break = 2,
Colour = 3,
Sprite = 4,
Sample = 5,
Animation = 6
}
}

View File

@ -1,131 +1,192 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using osu.Game.Beatmaps.Events;
using osu.Game.Beatmaps.Objects; using osu.Game.Beatmaps.Objects;
using osu.Game.Beatmaps.Timing; using osu.Game.Beatmaps.Timing;
using osu.Game.GameModes.Play; using osu.Game.GameModes.Play;
namespace osu.Game.Beatmaps.Formats namespace osu.Game.Beatmaps.Formats
{ {
public class OsuLegacyDecoder : BeatmapDecoder public class OsuLegacyDecoder : BeatmapDecoder
{ {
public static void Register() public static void Register()
{ {
AddDecoder<OsuLegacyDecoder>("osu file format v14"); AddDecoder<OsuLegacyDecoder>("osu file format v14");
AddDecoder<OsuLegacyDecoder>("osu file format v13"); AddDecoder<OsuLegacyDecoder>("osu file format v13");
AddDecoder<OsuLegacyDecoder>("osu file format v12"); AddDecoder<OsuLegacyDecoder>("osu file format v12");
AddDecoder<OsuLegacyDecoder>("osu file format v11"); AddDecoder<OsuLegacyDecoder>("osu file format v11");
AddDecoder<OsuLegacyDecoder>("osu file format v10"); AddDecoder<OsuLegacyDecoder>("osu file format v10");
// TODO: Not sure how far back to go, or differences between versions // TODO: Not sure how far back to go, or differences between versions
} }
private enum Section
{ private enum Section
None, {
General, None,
Editor, General,
Metadata, Editor,
Difficulty, Metadata,
Events, Difficulty,
TimingPoints, Events,
Colours, TimingPoints,
HitObjects, Colours,
HitObjects,
} }
private void HandleGeneral(Beatmap beatmap, string key, string val)
{ private void HandleGeneral(Beatmap beatmap, string key, string val)
switch (key) {
{ switch (key)
case "AudioFilename": {
case "AudioFilename":
beatmap.Metadata.AudioFile = val; beatmap.Metadata.AudioFile = val;
break; break;
case "AudioLeadIn": case "AudioLeadIn":
// TODO // TODO
break; break;
case "PreviewTime": case "PreviewTime":
beatmap.Metadata.PreviewTime = int.Parse(val); beatmap.Metadata.PreviewTime = int.Parse(val);
break; break;
case "Countdown": case "Countdown":
// TODO
break;
case "SampleSet":
// TODO // TODO
break; break;
case "StackLeniency": case "SampleSet":
// TODO // TODO
break; break;
case "Mode": case "StackLeniency":
beatmap.Metadata.Mode = (PlayMode)int.Parse(val);
break;
case "LetterboxInBreaks":
// TODO // TODO
break; break;
case "SpecialStyle": case "Mode":
beatmap.Mode = (PlayMode)int.Parse(val);
break;
case "LetterboxInBreaks":
// TODO // TODO
break; break;
case "WidescreenStoryboard": case "SpecialStyle":
// TODO // TODO
break; break;
} case "WidescreenStoryboard":
// TODO
break;
}
}
private void HandleMetadata(Beatmap beatmap, string key, string val)
{
switch (key)
{
case "Title":
beatmap.Metadata.Title = val;
break;
case "TitleUnicode":
beatmap.Metadata.TitleUnicode = val;
break;
case "Artist":
beatmap.Metadata.Artist = val;
break;
case "ArtistUnicode":
beatmap.Metadata.ArtistUnicode = val;
break;
case "Creator":
beatmap.Metadata.Author = val;
break;
case "Version":
beatmap.Version = val;
break;
case "Source":
beatmap.Metadata.Source = val;
break;
case "Tags":
beatmap.Metadata.Tags = val;
break;
case "BeatmapID":
beatmap.BeatmapID = int.Parse(val);
break;
case "BeatmapSetID":
beatmap.BeatmapSetID = int.Parse(val);
beatmap.Metadata.BeatmapSetID = int.Parse(val);
break;
}
}
private void HandleEvents(Beatmap beatmap, string val)
{
if (val.StartsWith("//"))
return;
string[] split = val.Split(',');
EventType type;
int _type;
if (!int.TryParse(split[0], out _type))
{
if (!Enum.TryParse(split[0], out type))
throw new InvalidDataException($@"Unknown event type {split[0]}");
}
else
type = (EventType)_type;
// TODO: Parse and store the rest of the event
if (type == EventType.Background)
beatmap.Metadata.BackgroundFile = split[2].Trim('"');
} }
public override Beatmap Decode(TextReader stream) public override Beatmap Decode(TextReader stream)
{ {
var beatmap = new Beatmap var beatmap = new Beatmap
{ {
Metadata = new BeatmapMetadata(), Metadata = new BeatmapMetadata(),
HitObjects = new List<HitObject>(), BaseDifficulty = new BaseDifficulty(),
ControlPoints = new List<ControlPoint>(), HitObjects = new List<HitObject>(),
}; ControlPoints = new List<ControlPoint>(),
var section = Section.None; };
string line; var section = Section.None;
string line;
while (true) while (true)
{ {
line = stream.ReadLine(); line = stream.ReadLine();
if (line == null) if (line == null)
break; break;
line = line.Trim(); line = line.Trim();
if (string.IsNullOrEmpty(line)) if (string.IsNullOrEmpty(line))
continue; continue;
if (line.StartsWith("[") && line.EndsWith("]")) if (line.StartsWith("[") && line.EndsWith("]"))
{ {
if (!Enum.TryParse(line.Substring(1, line.Length - 2), out section)) if (!Enum.TryParse(line.Substring(1, line.Length - 2), out section))
throw new InvalidOperationException($@"Unknown osu section {line}"); throw new InvalidDataException($@"Unknown osu section {line}");
continue; continue;
} }
string val = line, key = null; string val = line, key = null;
if (section != Section.Events && section != Section.TimingPoints if (section != Section.Events && section != Section.TimingPoints
&& section != Section.HitObjects) && section != Section.HitObjects)
{ {
key = val.Remove(val.IndexOf(':')).Trim(); key = val.Remove(val.IndexOf(':')).Trim();
val = val.Substring(val.IndexOf(':') + 1).Trim(); val = val.Substring(val.IndexOf(':') + 1).Trim();
} }
switch (section) switch (section)
{ {
case Section.General: case Section.General:
HandleGeneral(beatmap, key, val); HandleGeneral(beatmap, key, val);
break; break;
case Section.Editor: case Section.Editor:
// TODO
break;
case Section.Metadata:
// TODO // TODO
break; break;
case Section.Difficulty: case Section.Metadata:
// TODO HandleMetadata(beatmap, key, val);
break; break;
case Section.Events: case Section.Difficulty:
// TODO // TODO
break; break;
case Section.TimingPoints: case Section.Events:
HandleEvents(beatmap, val);
break;
case Section.TimingPoints:
// TODO // TODO
break; break;
case Section.HitObjects: case Section.HitObjects:
// TODO // TODO
break; break;
} }
} }
return beatmap; return beatmap;
} }
} }
} }

View File

@ -154,6 +154,7 @@
<Compile Include="Beatmaps\Formats\OsuLegacyDecoder.cs" /> <Compile Include="Beatmaps\Formats\OsuLegacyDecoder.cs" />
<Compile Include="Beatmaps\IO\OszArchiveReader.cs" /> <Compile Include="Beatmaps\IO\OszArchiveReader.cs" />
<Compile Include="Beatmaps\BaseDifficulty.cs" /> <Compile Include="Beatmaps\BaseDifficulty.cs" />
<Compile Include="Beatmaps\Events\EventType.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\osu-framework\osu.Framework\osu.Framework.csproj"> <ProjectReference Include="..\osu-framework\osu.Framework\osu.Framework.csproj">
@ -176,6 +177,7 @@
<Folder Include="Database\" /> <Folder Include="Database\" />
<Folder Include="Beatmaps\Formats\" /> <Folder Include="Beatmaps\Formats\" />
<Folder Include="Beatmaps\IO\" /> <Folder Include="Beatmaps\IO\" />
<Folder Include="Beatmaps\Events\" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.