1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 08:07:24 +08:00

Merge pull request #1873 from peppy/rulesetinfo-refactor

Allow rulesets to create their own instantiation info
This commit is contained in:
Dean Herbert 2018-01-10 17:34:01 +09:00 committed by GitHub
commit 5b6ddb984f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 54 additions and 58 deletions

View File

@ -101,7 +101,7 @@ namespace osu.Game.Rulesets.Catch
public override int LegacyID => 2; public override int LegacyID => 2;
public CatchRuleset(RulesetInfo rulesetInfo) public CatchRuleset(RulesetInfo rulesetInfo = null)
: base(rulesetInfo) : base(rulesetInfo)
{ {
} }

View File

@ -9,7 +9,7 @@ namespace osu.Game.Rulesets.Catch.Tests
[Ignore("getting CI working")] [Ignore("getting CI working")]
public class TestCaseCatchPlayer : Game.Tests.Visual.TestCasePlayer public class TestCaseCatchPlayer : Game.Tests.Visual.TestCasePlayer
{ {
public TestCaseCatchPlayer() : base(typeof(CatchRuleset)) public TestCaseCatchPlayer() : base(new CatchRuleset())
{ {
} }
} }

View File

@ -12,7 +12,7 @@ namespace osu.Game.Rulesets.Catch.Tests
public class TestCaseCatchStacker : Game.Tests.Visual.TestCasePlayer public class TestCaseCatchStacker : Game.Tests.Visual.TestCasePlayer
{ {
public TestCaseCatchStacker() public TestCaseCatchStacker()
: base(typeof(CatchRuleset)) : base(new CatchRuleset())
{ {
} }

View File

@ -12,7 +12,7 @@ namespace osu.Game.Rulesets.Catch.Tests
public class TestCaseHyperdash : Game.Tests.Visual.TestCasePlayer public class TestCaseHyperdash : Game.Tests.Visual.TestCasePlayer
{ {
public TestCaseHyperdash() public TestCaseHyperdash()
: base(typeof(CatchRuleset)) : base(new CatchRuleset())
{ {
} }

View File

@ -9,7 +9,7 @@ namespace osu.Game.Rulesets.Catch.Tests
public class TestCasePerformancePoints : Game.Tests.Visual.TestCasePerformancePoints public class TestCasePerformancePoints : Game.Tests.Visual.TestCasePerformancePoints
{ {
public TestCasePerformancePoints() public TestCasePerformancePoints()
: base(new CatchRuleset(new RulesetInfo())) : base(new CatchRuleset())
{ {
} }
} }

View File

@ -113,7 +113,7 @@ namespace osu.Game.Rulesets.Mania
public override int LegacyID => 3; public override int LegacyID => 3;
public ManiaRuleset(RulesetInfo rulesetInfo) public ManiaRuleset(RulesetInfo rulesetInfo = null)
: base(rulesetInfo) : base(rulesetInfo)
{ {
} }

View File

@ -9,7 +9,7 @@ namespace osu.Game.Rulesets.Mania.Tests
public class TestCasePerformancePoints : Game.Tests.Visual.TestCasePerformancePoints public class TestCasePerformancePoints : Game.Tests.Visual.TestCasePerformancePoints
{ {
public TestCasePerformancePoints() public TestCasePerformancePoints()
: base(new ManiaRuleset(new RulesetInfo())) : base(new ManiaRuleset())
{ {
} }
} }

View File

@ -145,7 +145,7 @@ namespace osu.Game.Rulesets.Osu
public override int LegacyID => 0; public override int LegacyID => 0;
public OsuRuleset(RulesetInfo rulesetInfo) public OsuRuleset(RulesetInfo rulesetInfo = null)
: base(rulesetInfo) : base(rulesetInfo)
{ {
} }

View File

@ -9,7 +9,7 @@ namespace osu.Game.Rulesets.Osu.Tests
public class TestCasePerformancePoints : Game.Tests.Visual.TestCasePerformancePoints public class TestCasePerformancePoints : Game.Tests.Visual.TestCasePerformancePoints
{ {
public TestCasePerformancePoints() public TestCasePerformancePoints()
: base(new OsuRuleset(new RulesetInfo())) : base(new OsuRuleset())
{ {
} }
} }

View File

@ -103,7 +103,7 @@ namespace osu.Game.Rulesets.Taiko
public override int LegacyID => 1; public override int LegacyID => 1;
public TaikoRuleset(RulesetInfo rulesetInfo) public TaikoRuleset(RulesetInfo rulesetInfo = null)
: base(rulesetInfo) : base(rulesetInfo)
{ {
} }

View File

@ -9,7 +9,7 @@ namespace osu.Game.Rulesets.Taiko.Tests
public class TestCasePerformancePoints : Game.Tests.Visual.TestCasePerformancePoints public class TestCasePerformancePoints : Game.Tests.Visual.TestCasePerformancePoints
{ {
public TestCasePerformancePoints() public TestCasePerformancePoints()
: base(new TaikoRuleset(new RulesetInfo())) : base(new TaikoRuleset())
{ {
} }
} }

View File

@ -56,25 +56,25 @@ namespace osu.Game.Tests.Visual
Clock = new FramedClock(), Clock = new FramedClock(),
Children = new Drawable[] Children = new Drawable[]
{ {
new OsuRulesetContainer(new OsuRuleset(new RulesetInfo()), beatmap, false) new OsuRulesetContainer(new OsuRuleset(), beatmap, false)
{ {
Scale = new Vector2(0.5f), Scale = new Vector2(0.5f),
Anchor = Anchor.TopLeft, Anchor = Anchor.TopLeft,
Origin = Anchor.TopLeft Origin = Anchor.TopLeft
}, },
new TaikoRulesetContainer(new TaikoRuleset(new RulesetInfo()),beatmap, false) new TaikoRulesetContainer(new TaikoRuleset(),beatmap, false)
{ {
Scale = new Vector2(0.5f), Scale = new Vector2(0.5f),
Anchor = Anchor.TopRight, Anchor = Anchor.TopRight,
Origin = Anchor.TopRight Origin = Anchor.TopRight
}, },
new CatchRulesetContainer(new CatchRuleset(new RulesetInfo()),beatmap, false) new CatchRulesetContainer(new CatchRuleset(),beatmap, false)
{ {
Scale = new Vector2(0.5f), Scale = new Vector2(0.5f),
Anchor = Anchor.BottomLeft, Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft Origin = Anchor.BottomLeft
}, },
new ManiaRulesetContainer(new ManiaRuleset(new RulesetInfo()),beatmap, false) new ManiaRulesetContainer(new ManiaRuleset(),beatmap, false)
{ {
Scale = new Vector2(0.5f), Scale = new Vector2(0.5f),
Anchor = Anchor.BottomRight, Anchor = Anchor.BottomRight,

View File

@ -64,7 +64,7 @@ namespace osu.Game.Beatmaps
public override string ShortName => "dummy"; public override string ShortName => "dummy";
public DummyRuleset(RulesetInfo rulesetInfo) public DummyRuleset(RulesetInfo rulesetInfo = null)
: base(rulesetInfo) : base(rulesetInfo)
{ {
} }

View File

@ -34,9 +34,9 @@ namespace osu.Game.Rulesets
public Mod GetAutoplayMod() => GetAllMods().First(mod => mod is ModAutoplay); public Mod GetAutoplayMod() => GetAllMods().First(mod => mod is ModAutoplay);
protected Ruleset(RulesetInfo rulesetInfo) protected Ruleset(RulesetInfo rulesetInfo = null)
{ {
RulesetInfo = rulesetInfo; RulesetInfo = rulesetInfo ?? createRulesetInfo();
} }
/// <summary> /// <summary>
@ -88,5 +88,17 @@ namespace osu.Game.Rulesets
/// <param name="variant">The variant.</param> /// <param name="variant">The variant.</param>
/// <returns>A descriptive name of the variant.</returns> /// <returns>A descriptive name of the variant.</returns>
public virtual string GetVariantName(int variant) => string.Empty; public virtual string GetVariantName(int variant) => string.Empty;
/// <summary>
/// Create a ruleset info based on this ruleset.
/// </summary>
/// <returns>A filled <see cref="RulesetInfo"/>.</returns>
private RulesetInfo createRulesetInfo() => new RulesetInfo
{
Name = Description,
ShortName = ShortName,
InstantiationInfo = GetType().AssemblyQualifiedName,
ID = LegacyID
};
} }
} }

View File

@ -58,30 +58,21 @@ namespace osu.Game.Rulesets
{ {
var context = GetContext(); var context = GetContext();
var instances = loaded_assemblies.Values.Select(r => (Ruleset)Activator.CreateInstance(r, new RulesetInfo())).ToList(); var instances = loaded_assemblies.Values.Select(r => (Ruleset)Activator.CreateInstance(r, (RulesetInfo)null)).ToList();
//add all legacy modes in correct order //add all legacy modes in correct order
foreach (var r in instances.Where(r => r.LegacyID >= 0).OrderBy(r => r.LegacyID)) foreach (var r in instances.Where(r => r.LegacyID >= 0).OrderBy(r => r.LegacyID))
{ {
var rulesetInfo = createRulesetInfo(r); if (context.RulesetInfo.SingleOrDefault(rsi => rsi.ID == r.RulesetInfo.ID) == null)
if (context.RulesetInfo.SingleOrDefault(rsi => rsi.ID == rulesetInfo.ID) == null) context.RulesetInfo.Add(r.RulesetInfo);
{
context.RulesetInfo.Add(rulesetInfo);
}
} }
context.SaveChanges(); context.SaveChanges();
//add any other modes //add any other modes
foreach (var r in instances.Where(r => r.LegacyID < 0)) foreach (var r in instances.Where(r => r.LegacyID < 0))
{ if (context.RulesetInfo.FirstOrDefault(ri => ri.InstantiationInfo == r.RulesetInfo.InstantiationInfo) == null)
var us = createRulesetInfo(r); context.RulesetInfo.Add(r.RulesetInfo);
var existing = context.RulesetInfo.FirstOrDefault(ri => ri.InstantiationInfo == us.InstantiationInfo);
if (existing == null)
context.RulesetInfo.Add(us);
}
context.SaveChanges(); context.SaveChanges();
@ -124,13 +115,5 @@ namespace osu.Game.Rulesets
{ {
} }
} }
private RulesetInfo createRulesetInfo(Ruleset ruleset) => new RulesetInfo
{
Name = ruleset.Description,
ShortName = ruleset.ShortName,
InstantiationInfo = ruleset.GetType().AssemblyQualifiedName,
ID = ruleset.LegacyID
};
} }
} }

View File

@ -1,7 +1,6 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>. // Copyright (c) 2007-2018 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
using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -18,24 +17,19 @@ namespace osu.Game.Tests.Visual
{ {
public abstract class TestCasePlayer : ScreenTestCase public abstract class TestCasePlayer : ScreenTestCase
{ {
private readonly Type ruleset; private readonly Ruleset ruleset;
protected Player Player; protected Player Player;
private TestWorkingBeatmap working; private TestWorkingBeatmap working;
/// <summary> protected TestCasePlayer(Ruleset ruleset)
/// Create a TestCase which runs through the Player screen.
/// </summary>
/// <param name="ruleset">An optional ruleset type which we want to target. If not provided we'll allow all rulesets to be tested.</param>
protected TestCasePlayer(Type ruleset)
{ {
this.ruleset = ruleset; this.ruleset = ruleset;
} }
protected TestCasePlayer() protected TestCasePlayer()
{ {
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
@ -48,15 +42,22 @@ namespace osu.Game.Tests.Visual
Depth = int.MaxValue Depth = int.MaxValue
}); });
string instantiation = ruleset?.AssemblyQualifiedName; if (ruleset != null)
{
foreach (var r in rulesets.AvailableRulesets.Where(rs => instantiation == null || rs.InstantiationInfo == instantiation)) Player p = null;
AddStep(ruleset.RulesetInfo.Name, () => p = loadPlayerFor(ruleset));
AddUntilStep(() => p.IsLoaded);
}
else
{
foreach (var r in rulesets.AvailableRulesets)
{ {
Player p = null; Player p = null;
AddStep(r.Name, () => p = loadPlayerFor(r)); AddStep(r.Name, () => p = loadPlayerFor(r));
AddUntilStep(() => p.IsLoaded); AddUntilStep(() => p.IsLoaded);
} }
} }
}
protected virtual Beatmap CreateBeatmap() protected virtual Beatmap CreateBeatmap()
{ {
@ -69,21 +70,21 @@ namespace osu.Game.Tests.Visual
return beatmap; return beatmap;
} }
private Player loadPlayerFor(RulesetInfo r) private Player loadPlayerFor(RulesetInfo ri) => loadPlayerFor(ri.CreateInstance());
private Player loadPlayerFor(Ruleset r)
{ {
var beatmap = CreateBeatmap(); var beatmap = CreateBeatmap();
beatmap.BeatmapInfo.Ruleset = r; beatmap.BeatmapInfo.Ruleset = r.RulesetInfo;
var instance = r.CreateInstance();
working = new TestWorkingBeatmap(beatmap); working = new TestWorkingBeatmap(beatmap);
working.Mods.Value = new[] { instance.GetAllMods().First(m => m is ModNoFail) }; working.Mods.Value = new[] { r.GetAllMods().First(m => m is ModNoFail) };
if (Player != null) if (Player != null)
Remove(Player); Remove(Player);
var player = CreatePlayer(working, instance); var player = CreatePlayer(working, r);
LoadComponentAsync(player, LoadScreen); LoadComponentAsync(player, LoadScreen);