mirror of
https://github.com/ppy/osu.git
synced 2024-11-11 11:28:00 +08:00
Merge pull request #15569 from peppy/avoid-throwing-unnecessary-exception
Gracefully handle missing type rather than triggering `ArgumentNullException`
This commit is contained in:
commit
1540352b4d
15
osu.Game/Rulesets/RulesetLoadException.cs
Normal file
15
osu.Game/Rulesets/RulesetLoadException.cs
Normal file
@ -0,0 +1,15 @@
|
||||
// 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.
|
||||
|
||||
using System;
|
||||
|
||||
namespace osu.Game.Rulesets
|
||||
{
|
||||
public class RulesetLoadException : Exception
|
||||
{
|
||||
public RulesetLoadException(string message)
|
||||
: base(@$"Ruleset could not be loaded ({message})")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -7,7 +7,6 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using osu.Framework;
|
||||
using osu.Framework.Extensions.ObjectExtensions;
|
||||
using osu.Framework.Logging;
|
||||
using osu.Framework.Platform;
|
||||
using osu.Game.Database;
|
||||
@ -129,12 +128,15 @@ namespace osu.Game.Rulesets
|
||||
{
|
||||
try
|
||||
{
|
||||
var instanceInfo = ((Ruleset)Activator.CreateInstance(Type.GetType(r.InstantiationInfo).AsNonNull())).RulesetInfo;
|
||||
var resolvedType = Type.GetType(r.InstantiationInfo)
|
||||
?? throw new RulesetLoadException(@"Type could not be resolved");
|
||||
|
||||
var instanceInfo = (Activator.CreateInstance(resolvedType) as Ruleset)?.RulesetInfo
|
||||
?? throw new RulesetLoadException(@"Instantiation failure");
|
||||
|
||||
r.Name = instanceInfo.Name;
|
||||
r.ShortName = instanceInfo.ShortName;
|
||||
r.InstantiationInfo = instanceInfo.InstantiationInfo;
|
||||
|
||||
r.Available = true;
|
||||
}
|
||||
catch
|
||||
|
@ -147,19 +147,15 @@ namespace osu.Game.Stores
|
||||
{
|
||||
try
|
||||
{
|
||||
var type = Type.GetType(r.InstantiationInfo);
|
||||
var resolvedType = Type.GetType(r.InstantiationInfo)
|
||||
?? throw new RulesetLoadException(@"Type could not be resolved");
|
||||
|
||||
if (type == null)
|
||||
throw new InvalidOperationException(@"Type resolution failure.");
|
||||
var instanceInfo = (Activator.CreateInstance(resolvedType) as Ruleset)?.RulesetInfo
|
||||
?? throw new RulesetLoadException(@"Instantiation failure");
|
||||
|
||||
var rInstance = (Activator.CreateInstance(type) as Ruleset)?.RulesetInfo;
|
||||
|
||||
if (rInstance == null)
|
||||
throw new InvalidOperationException(@"Instantiation failure.");
|
||||
|
||||
r.Name = rInstance.Name;
|
||||
r.ShortName = rInstance.ShortName;
|
||||
r.InstantiationInfo = rInstance.InstantiationInfo;
|
||||
r.Name = instanceInfo.Name;
|
||||
r.ShortName = instanceInfo.ShortName;
|
||||
r.InstantiationInfo = instanceInfo.InstantiationInfo;
|
||||
r.Available = true;
|
||||
|
||||
detachedRulesets.Add(r.Clone());
|
||||
|
Loading…
Reference in New Issue
Block a user