1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 10:33:30 +08:00

Improve beatmap loading and hydration

This commit is contained in:
Drew DeVault 2016-10-13 10:29:30 -04:00
parent fd10e6e582
commit 25d08c8e2c
7 changed files with 50 additions and 24 deletions

View File

@ -31,7 +31,8 @@ namespace osu.Desktop.Beatmaps.IO
using (var stream = new StreamReader(ReadFile(beatmaps[0])))
{
var decoder = BeatmapDecoder.GetDecoder(stream);
firstMap = decoder.Decode(stream);
firstMap = new Beatmap();
decoder.Decode(stream, firstMap);
}
}

View File

@ -3,6 +3,7 @@ using System.IO;
using NUnit.Framework;
using OpenTK;
using OpenTK.Graphics;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Formats;
using osu.Game.Beatmaps.Objects.Osu;
using osu.Game.Beatmaps.Samples;
@ -25,7 +26,8 @@ namespace osu.Game.Tests.Beatmaps.Formats
var decoder = new OsuLegacyDecoder();
using (var stream = Resource.OpenResource("Soleily - Renatus (Gamu) [Insane].osu"))
{
var beatmap = decoder.Decode(new StreamReader(stream));
Beatmap beatmap = new Beatmap();
decoder.Decode(new StreamReader(stream), beatmap);
var meta = beatmap.Metadata;
Assert.AreEqual(241526, meta.BeatmapSetID);
Assert.AreEqual("Soleily", meta.Artist);
@ -47,7 +49,8 @@ namespace osu.Game.Tests.Beatmaps.Formats
var decoder = new OsuLegacyDecoder();
using (var stream = Resource.OpenResource("Soleily - Renatus (Gamu) [Insane].osu"))
{
var beatmap = decoder.Decode(new StreamReader(stream));
Beatmap beatmap = new Beatmap();
decoder.Decode(new StreamReader(stream), beatmap);
Assert.AreEqual(0, beatmap.AudioLeadIn);
Assert.AreEqual(false, beatmap.Countdown);
Assert.AreEqual(SampleSet.Soft, beatmap.SampleSet);
@ -65,7 +68,8 @@ namespace osu.Game.Tests.Beatmaps.Formats
var decoder = new OsuLegacyDecoder();
using (var stream = Resource.OpenResource("Soleily - Renatus (Gamu) [Insane].osu"))
{
var beatmap = decoder.Decode(new StreamReader(stream));
Beatmap beatmap = new Beatmap();
decoder.Decode(new StreamReader(stream), beatmap);
int[] expectedBookmarks =
{
11505, 22054, 32604, 43153, 53703, 64252, 74802, 85351,
@ -88,7 +92,8 @@ namespace osu.Game.Tests.Beatmaps.Formats
var decoder = new OsuLegacyDecoder();
using (var stream = Resource.OpenResource("Soleily - Renatus (Gamu) [Insane].osu"))
{
var beatmap = decoder.Decode(new StreamReader(stream));
Beatmap beatmap = new Beatmap();
decoder.Decode(new StreamReader(stream), beatmap);
var difficulty = beatmap.BaseDifficulty;
Assert.AreEqual(6.5f, difficulty.DrainRate);
Assert.AreEqual(4, difficulty.CircleSize);
@ -105,7 +110,8 @@ namespace osu.Game.Tests.Beatmaps.Formats
var decoder = new OsuLegacyDecoder();
using (var stream = Resource.OpenResource("Soleily - Renatus (Gamu) [Insane].osu"))
{
var beatmap = decoder.Decode(new StreamReader(stream));
Beatmap beatmap = new Beatmap();
decoder.Decode(new StreamReader(stream), beatmap);
Color4[] expected =
{
new Color4(142, 199, 255, 255),
@ -126,7 +132,8 @@ namespace osu.Game.Tests.Beatmaps.Formats
var decoder = new OsuLegacyDecoder();
using (var stream = Resource.OpenResource("Soleily - Renatus (Gamu) [Insane].osu"))
{
var beatmap = decoder.Decode(new StreamReader(stream));
Beatmap beatmap = new Beatmap();
decoder.Decode(new StreamReader(stream), beatmap);
var slider = beatmap.HitObjects[0] as Slider;
Assert.IsNotNull(slider);
Assert.AreEqual(new Vector2(192, 168), slider.Position);

View File

@ -17,7 +17,7 @@ namespace osu.Game.Beatmaps
[NotNull, Indexed]
public int BeatmapMetadataID { get; set; }
[Ignore]
public List<Beatmap> Beatmaps { get; protected set; }
public List<Beatmap> Beatmaps { get; protected set; } = new List<Beatmap>();
[Ignore]
public BeatmapMetadata Metadata { get; set; }
[Ignore]

View File

@ -20,6 +20,6 @@ namespace osu.Game.Beatmaps.Formats
decoders[magic] = typeof(T);
}
public abstract Beatmap Decode(TextReader stream);
public abstract void Decode(TextReader stream, Beatmap beatmap);
}
}

View File

@ -202,16 +202,16 @@ namespace osu.Game.Beatmaps.Formats
});
}
public override Beatmap Decode(TextReader stream)
public override void Decode(TextReader stream, Beatmap beatmap)
{
var beatmap = new Beatmap
{
Metadata = new BeatmapMetadata(),
BaseDifficulty = new BaseDifficulty(),
HitObjects = new List<HitObject>(),
ControlPoints = new List<ControlPoint>(),
ComboColors = new List<Color4>(),
};
// We don't overwrite these two because they're DB bound
if (beatmap.Metadata == null) beatmap.Metadata = new BeatmapMetadata();
if (beatmap.BaseDifficulty == null) beatmap.BaseDifficulty = new BaseDifficulty();
// These are fine though
beatmap.HitObjects = new List<HitObject>();
beatmap.ControlPoints = new List<ControlPoint>();
beatmap.ComboColors = new List<Color4>();
var section = Section.None;
string line;
while (true)
@ -266,7 +266,6 @@ namespace osu.Game.Beatmaps.Formats
break;
}
}
return beatmap;
}
}
}

View File

@ -33,7 +33,8 @@ namespace osu.Game.Beatmaps.IO
using (var stream = new StreamReader(ReadFile(beatmaps[0])))
{
var decoder = BeatmapDecoder.GetDecoder(stream);
firstMap = decoder.Decode(stream);
firstMap = new Beatmap();
decoder.Decode(stream, firstMap);
}
}

View File

@ -48,7 +48,7 @@ namespace osu.Game.Database
reader = ArchiveReader.GetReader(storage, path);
var metadata = reader.ReadMetadata();
if (connection.Table<BeatmapSet>().Count(b => b.BeatmapSetID == metadata.BeatmapSetID) != 0)
return;
return; // TODO: Update this beatmap instead
string[] mapNames = reader.ReadBeatmaps();
var beatmapSet = new BeatmapSet
{
@ -62,7 +62,8 @@ namespace osu.Game.Database
using (var stream = new StreamReader(reader.ReadFile(name)))
{
var decoder = BeatmapDecoder.GetDecoder(stream);
var beatmap = decoder.Decode(stream);
Beatmap beatmap = new Beatmap();
decoder.Decode(stream, beatmap);
maps.Add(beatmap);
beatmap.BaseDifficultyID = connection.Insert(beatmap.BaseDifficulty);
}
@ -71,12 +72,29 @@ namespace osu.Game.Database
connection.Insert(beatmapSet);
connection.InsertAll(maps);
}
public ArchiveReader GetReader(BeatmapSet beatmapSet)
{
return ArchiveReader.GetReader(storage, beatmapSet.Path);
}
/// <summary>
/// Given a BeatmapSet pulled from the database, loads the rest of its data from disk.
/// </summary> public void PopulateBeatmap(BeatmapSet beatmap)
/// </summary> public void PopulateBeatmap(BeatmapSet beatmapSet)
{
// TODO
using (var reader = GetReader(beatmapSet))
{
string[] mapNames = reader.ReadBeatmaps();
foreach (var name in mapNames)
{
using (var stream = new StreamReader(reader.ReadFile(name)))
{
var decoder = BeatmapDecoder.GetDecoder(stream);
Beatmap beatmap = new Beatmap();
decoder.Decode(stream, beatmap);
beatmapSet.Beatmaps.Add(beatmap);
}
}
}
}
}
}