Introduce concept of HitObjectParsers, fix tests and stop using reflection (unreliable due to lazy loading).

This commit is contained in:
Dean Herbert 2016-11-14 22:03:39 +09:00
parent 0011d7f720
commit d3f810f72f
19 changed files with 146 additions and 59 deletions

View File

@ -10,6 +10,11 @@ using osu.Framework.Desktop.Platform;
using osu.Framework.Platform;
using osu.Game;
using osu.Game.IPC;
using osu.Game.Modes;
using osu.Game.Modes.Catch;
using osu.Game.Modes.Mania;
using osu.Game.Modes.Osu;
using osu.Game.Modes.Taiko;
namespace osu.Desktop
{
@ -31,6 +36,11 @@ namespace osu.Desktop
}
else
{
Ruleset.Register(new OsuRuleset());
Ruleset.Register(new TaikoRuleset());
Ruleset.Register(new ManiaRuleset());
Ruleset.Register(new CatchRuleset());
BaseGame osu = new OsuGame(args);
host.Add(osu);
host.Run();

View File

@ -134,6 +134,18 @@
<Project>{c92a607b-1fdd-4954-9f92-03ff547d9080}</Project>
<Name>osu.Game.Modes.Osu</Name>
</ProjectReference>
<ProjectReference Include="..\osu.Game.Modes.Catch\osu.Game.Modes.Catch.csproj">
<Project>{58f6c80c-1253-4a0e-a465-b8c85ebeadf3}</Project>
<Name>osu.Game.Modes.Catch</Name>
</ProjectReference>
<ProjectReference Include="..\osu.Game.Modes.Mania\osu.Game.Modes.Mania.csproj">
<Project>{48f4582b-7687-4621-9cbe-5c24197cb536}</Project>
<Name>osu.Game.Modes.Mania</Name>
</ProjectReference>
<ProjectReference Include="..\osu.Game.Modes.Taiko\osu.Game.Modes.Taiko.csproj">
<Project>{f167e17a-7de6-4af5-b920-a5112296c695}</Project>
<Name>osu.Game.Modes.Taiko</Name>
</ProjectReference>
<ProjectReference Include="..\osu.Game\osu.Game.csproj">
<Project>{0d3fbf8a-7464-4cf7-8c90-3e7886df2d4d}</Project>
<Name>osu.Game</Name>

View File

@ -13,7 +13,7 @@ namespace osu.Game.Modes.Osu.Objects
public Vector2 Position { get; set; }
[Flags]
private enum HitObjectType
internal enum HitObjectType
{
Circle = 1,
Slider = 2,
@ -25,35 +25,5 @@ namespace osu.Game.Modes.Osu.Objects
Hold = 128,
ManiaLong = 128,
}
public static OsuBaseHit Parse(string val)
{
string[] split = val.Split(',');
var type = (HitObjectType)int.Parse(split[3]);
bool combo = type.HasFlag(HitObjectType.NewCombo);
type &= (HitObjectType)0xF;
type &= ~HitObjectType.NewCombo;
OsuBaseHit result;
switch (type)
{
case HitObjectType.Circle:
result = new Circle();
break;
case HitObjectType.Slider:
result = new Slider();
break;
case HitObjectType.Spinner:
result = new Spinner();
break;
default:
throw new InvalidOperationException($@"Unknown hit object type {type}");
}
result.Position = new Vector2(int.Parse(split[0]), int.Parse(split[1]));
result.StartTime = double.Parse(split[2]);
result.Sample = new HitSampleInfo { Type = (SampleType)int.Parse(split[4]) };
result.NewCombo = combo;
// TODO: "addition" field
return result;
}
}
}

View File

@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using osu.Game.Beatmaps.Samples;
using osu.Game.Modes.Objects;
using OpenTK;
namespace osu.Game.Modes.Osu.Objects
{
public class OsuHitObjectParser : HitObjectParser
{
public override HitObject Parse(string text)
{
string[] split = text.Split(',');
var type = (OsuBaseHit.HitObjectType)int.Parse(split[3]);
bool combo = type.HasFlag(OsuBaseHit.HitObjectType.NewCombo);
type &= (OsuBaseHit.HitObjectType)0xF;
type &= ~OsuBaseHit.HitObjectType.NewCombo;
OsuBaseHit result;
switch (type)
{
case OsuBaseHit.HitObjectType.Circle:
result = new Circle();
break;
case OsuBaseHit.HitObjectType.Slider:
result = new Slider();
break;
case OsuBaseHit.HitObjectType.Spinner:
result = new Spinner();
break;
default:
//throw new InvalidOperationException($@"Unknown hit object type {type}");
return null;
}
result.Position = new Vector2(int.Parse(split[0]), int.Parse(split[1]));
result.StartTime = double.Parse(split[2]);
result.Sample = new HitSampleInfo { Type = (SampleType)int.Parse(split[4]) };
result.NewCombo = combo;
// TODO: "addition" field
return result;
}
}
}

View File

@ -3,6 +3,7 @@
using System.Collections.Generic;
using osu.Game.Modes.Objects;
using osu.Game.Modes.Osu.Objects;
using osu.Game.Modes.Osu.UI;
using osu.Game.Modes.UI;
@ -13,5 +14,7 @@ namespace osu.Game.Modes.Osu
public override ScoreOverlay CreateScoreOverlay() => new OsuScoreOverlay();
public override HitRenderer CreateHitRendererWith(List<HitObject> objects) => new UI.OsuHitRenderer { Objects = objects };
public override HitObjectParser CreateHitObjectParser() => new OsuHitObjectParser();
}
}

View File

@ -41,6 +41,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Objects\OsuHitObjectParser.cs" />
<Compile Include="UI\OsuComboCounter.cs" />
<Compile Include="UI\OsuHitRenderer.cs" />
<Compile Include="UI\OsuPlayfield.cs" />

View File

@ -4,15 +4,18 @@
using System.Collections.Generic;
using osu.Game.Modes.Catch.UI;
using osu.Game.Modes.Objects;
using osu.Game.Modes.Osu.Objects;
using osu.Game.Modes.Osu.UI;
using osu.Game.Modes.UI;
namespace osu.Game.Modes.Catch
{
class CatchRuleset : Ruleset
public class CatchRuleset : Ruleset
{
public override ScoreOverlay CreateScoreOverlay() => new OsuScoreOverlay();
public override HitRenderer CreateHitRendererWith(List<HitObject> objects) => new CatchHitRenderer { Objects = objects };
public override HitObjectParser CreateHitObjectParser() => new OsuHitObjectParser();
}
}

View File

@ -5,15 +5,18 @@ using System.Collections.Generic;
using osu.Game.Modes.Mania.UI;
using osu.Game.Modes.Objects;
using osu.Game.Modes.Osu;
using osu.Game.Modes.Osu.Objects;
using osu.Game.Modes.Osu.UI;
using osu.Game.Modes.UI;
namespace osu.Game.Modes.Mania
{
class ManiaRuleset : Ruleset
public class ManiaRuleset : Ruleset
{
public override ScoreOverlay CreateScoreOverlay() => new OsuScoreOverlay();
public override HitRenderer CreateHitRendererWith(List<HitObject> objects) => new ManiaHitRenderer { Objects = objects };
public override HitObjectParser CreateHitObjectParser() => new OsuHitObjectParser();
}
}

View File

@ -3,16 +3,19 @@
using System.Collections.Generic;
using osu.Game.Modes.Objects;
using osu.Game.Modes.Osu.Objects;
using osu.Game.Modes.Osu.UI;
using osu.Game.Modes.Taiko.UI;
using osu.Game.Modes.UI;
namespace osu.Game.Modes.Taiko
{
class TaikoRuleset : Ruleset
public class TaikoRuleset : Ruleset
{
public override ScoreOverlay CreateScoreOverlay() => new OsuScoreOverlay();
public override HitRenderer CreateHitRendererWith(List<HitObject> objects) => new TaikoHitRenderer { Objects = objects };
public override HitObjectParser CreateHitObjectParser() => new OsuHitObjectParser();
}
}

View File

@ -7,6 +7,7 @@ using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Formats;
using osu.Game.Beatmaps.Samples;
using osu.Game.Modes;
using osu.Game.Modes.Osu;
using osu.Game.Modes.Osu.Objects;
using osu.Game.Screens.Play;
using osu.Game.Tests.Resources;
@ -20,6 +21,7 @@ namespace osu.Game.Tests.Beatmaps.Formats
public void SetUp()
{
OsuLegacyDecoder.Register();
Ruleset.Register(new OsuRuleset());
}
[Test]
public void TestDecodeMetadata()

View File

@ -9,6 +9,10 @@ using osu.Framework.Platform;
using osu.Game.Database;
using osu.Game.IPC;
using osu.Game.Modes;
using osu.Game.Modes.Catch;
using osu.Game.Modes.Mania;
using osu.Game.Modes.Osu;
using osu.Game.Modes.Taiko;
using osu.Game.Screens.Play;
namespace osu.Game.Tests.Beatmaps.IO
@ -21,6 +25,10 @@ namespace osu.Game.Tests.Beatmaps.IO
[OneTimeSetUp]
public void SetUp()
{
Ruleset.Register(new OsuRuleset());
Ruleset.Register(new TaikoRuleset());
Ruleset.Register(new ManiaRuleset());
Ruleset.Register(new CatchRuleset());
}
[Test]

View File

@ -2,6 +2,8 @@
using System.IO;
using NUnit.Framework;
using osu.Game.Beatmaps.IO;
using osu.Game.Modes;
using osu.Game.Modes.Osu;
using osu.Game.Tests.Resources;
namespace osu.Game.Tests.Beatmaps.IO
@ -13,8 +15,9 @@ namespace osu.Game.Tests.Beatmaps.IO
public void SetUp()
{
OszArchiveReader.Register();
Ruleset.Register(new OsuRuleset());
}
[Test]
public void TestReadBeatmaps()
{

View File

@ -70,6 +70,18 @@
<Project>{c92a607b-1fdd-4954-9f92-03ff547d9080}</Project>
<Name>osu.Game.Modes.Osu</Name>
</ProjectReference>
<ProjectReference Include="..\osu.Game.Modes.Catch\osu.Game.Modes.Catch.csproj">
<Project>{58f6c80c-1253-4a0e-a465-b8c85ebeadf3}</Project>
<Name>osu.Game.Modes.Catch</Name>
</ProjectReference>
<ProjectReference Include="..\osu.Game.Modes.Mania\osu.Game.Modes.Mania.csproj">
<Project>{48f4582b-7687-4621-9cbe-5c24197cb536}</Project>
<Name>osu.Game.Modes.Mania</Name>
</ProjectReference>
<ProjectReference Include="..\osu.Game.Modes.Taiko\osu.Game.Modes.Taiko.csproj">
<Project>{f167e17a-7de6-4af5-b920-a5112296c695}</Project>
<Name>osu.Game.Modes.Taiko</Name>
</ProjectReference>
<ProjectReference Include="..\osu.Game\osu.Game.csproj">
<Project>{0D3FBF8A-7464-4CF7-8C90-3E7886DF2D4D}</Project>
<Name>osu.Game</Name>

View File

@ -48,6 +48,8 @@ namespace osu.Game.Beatmaps.Formats
new Color4(121,9,13, 255),
};
if (colours.Count == 0) return;
int i = -1;
foreach (HitObject h in b.HitObjects)

View File

@ -221,7 +221,9 @@ namespace osu.Game.Beatmaps.Formats
BaseDifficulty = new BaseDifficulty(),
},
};
HitObjectParser parser = null;
var section = Section.None;
string line;
while (true)
@ -233,14 +235,14 @@ namespace osu.Game.Beatmaps.Formats
continue;
if (line.StartsWith(@"osu file format v"))
continue;
if (line.StartsWith(@"[") && line.EndsWith(@"]"))
{
if (!Enum.TryParse(line.Substring(1, line.Length - 2), out section))
throw new InvalidDataException($@"Unknown osu section {line}");
continue;
}
string val = line, key = null;
if (section != Section.Events && section != Section.TimingPoints && section != Section.HitObjects)
{
@ -251,6 +253,7 @@ namespace osu.Game.Beatmaps.Formats
{
case Section.General:
handleGeneral(beatmap, key, val);
parser = Ruleset.GetRuleset(beatmap.BeatmapInfo.Mode).CreateHitObjectParser();
break;
case Section.Editor:
handleEditor(beatmap, key, val);
@ -271,13 +274,13 @@ namespace osu.Game.Beatmaps.Formats
handleColours(beatmap, key, val);
break;
case Section.HitObjects:
var h = HitObject.Parse(beatmap.BeatmapInfo.Mode, val);
if (h != null)
beatmap.HitObjects.Add(h);
var obj = parser?.Parse(val);
if (obj != null)
beatmap.HitObjects.Add(obj);
break;
}
}
return beatmap;
}
}

View File

@ -21,17 +21,5 @@ namespace osu.Game.Modes.Objects
public double Duration => EndTime - StartTime;
public HitSampleInfo Sample;
public static HitObject Parse(PlayMode mode, string val)
{
//TODO: move to modular HitObjectParser system rather than static parsing. (https://github.com/ppy/osu/pull/60/files#r83135780)
switch (mode)
{
case PlayMode.Osu:
return null; //return OsuBaseHit.Parse(val);
default:
return null;
}
}
}
}

View File

@ -0,0 +1,16 @@
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace osu.Game.Modes.Objects
{
public abstract class HitObjectParser
{
public abstract HitObject Parse(string text);
}
}

View File

@ -13,17 +13,19 @@ namespace osu.Game.Modes
{
public abstract class Ruleset
{
private static List<Type> availableRulesets = new List<Type>();
public abstract ScoreOverlay CreateScoreOverlay();
public abstract HitRenderer CreateHitRendererWith(List<HitObject> objects);
public abstract HitObjectParser CreateHitObjectParser();
public static void Register(Ruleset ruleset) => availableRulesets.Add(ruleset.GetType());
public static Ruleset GetRuleset(PlayMode mode)
{
Type type = AppDomain.CurrentDomain
.GetAssemblies()
.Where(a => a.FullName.Contains($@"osu.Game.Modes.{mode}"))
.SelectMany(a => a.GetTypes())
.FirstOrDefault(t => t.Name == $@"{mode}Ruleset");
Type type = availableRulesets.FirstOrDefault(t => t.Name == $@"{mode}Ruleset");
if (type == null)
return null;

View File

@ -63,6 +63,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Modes\Objects\HitObjectParser.cs" />
<Compile Include="Overlays\DragBar.cs" />
<Compile Include="Overlays\MusicController.cs" />
<Compile Include="Beatmaps\Beatmap.cs" />