1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-12 17:23:22 +08:00

Remove nullability of Ruleset.CreateInstance

This commit is contained in:
Dean Herbert 2021-11-23 20:17:07 +09:00
parent 216c18b0bb
commit 8d69ebd7db
3 changed files with 34 additions and 19 deletions

View File

@ -60,5 +60,27 @@ namespace osu.Game.Models
InstantiationInfo = InstantiationInfo, InstantiationInfo = InstantiationInfo,
Available = Available Available = Available
}; };
public Ruleset CreateInstance()
{
if (!Available)
throw new RulesetLoadException(@"Ruleset not available");
var type = Type.GetType(InstantiationInfo);
if (type == null)
throw new RulesetLoadException(@"Type lookup failure");
var ruleset = Activator.CreateInstance(type) as Ruleset;
if (ruleset == null)
throw new RulesetLoadException(@"Instantiation failure");
// overwrite the pre-populated RulesetInfo with a potentially database attached copy.
// TODO: figure if we still want/need this after switching to realm.
// ruleset.RulesetInfo = this;
return ruleset;
}
} }
} }

View File

@ -1,7 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System;
using osu.Game.Database; using osu.Game.Database;
#nullable enable #nullable enable
@ -28,20 +27,6 @@ namespace osu.Game.Rulesets
/// </summary> /// </summary>
string InstantiationInfo { get; } string InstantiationInfo { get; }
Ruleset? CreateInstance() Ruleset CreateInstance();
{
var type = Type.GetType(InstantiationInfo);
if (type == null)
return null;
var ruleset = Activator.CreateInstance(type) as Ruleset;
// overwrite the pre-populated RulesetInfo with a potentially database attached copy.
// TODO: figure if we still want/need this after switching to realm.
// ruleset.RulesetInfo = this;
return ruleset;
}
} }
} }

View File

@ -4,7 +4,6 @@
using System; using System;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Newtonsoft.Json; using Newtonsoft.Json;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Testing; using osu.Framework.Testing;
namespace osu.Game.Rulesets namespace osu.Game.Rulesets
@ -26,9 +25,18 @@ namespace osu.Game.Rulesets
// TODO: this should probably be moved to RulesetStore. // TODO: this should probably be moved to RulesetStore.
public Ruleset CreateInstance() public Ruleset CreateInstance()
{ {
if (!Available) return null; if (!Available)
throw new RulesetLoadException(@"Ruleset not available");
var ruleset = (Ruleset)Activator.CreateInstance(Type.GetType(InstantiationInfo).AsNonNull()); var type = Type.GetType(InstantiationInfo);
if (type == null)
throw new RulesetLoadException(@"Type lookup failure");
var ruleset = Activator.CreateInstance(type) as Ruleset;
if (ruleset == null)
throw new RulesetLoadException(@"Instantiation failure");
// overwrite the pre-populated RulesetInfo with a potentially database attached copy. // overwrite the pre-populated RulesetInfo with a potentially database attached copy.
ruleset.RulesetInfo = this; ruleset.RulesetInfo = this;