1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-12 19:42:55 +08:00

Ignore possible nulls in Type.GetType() calls

They're mostly used in extensibility scenarios, so everything happens in
runtime. There is no better resolution than to crash with a null
reference exception.
This commit is contained in:
Bartłomiej Dach 2021-05-14 23:45:58 +02:00
parent 43c73f9583
commit 628e7a71ed
4 changed files with 8 additions and 4 deletions

View File

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using osu.Framework.Extensions.ObjectExtensions;
namespace osu.Game.IO.Serialization.Converters namespace osu.Game.IO.Serialization.Converters
{ {
@ -60,7 +61,7 @@ namespace osu.Game.IO.Serialization.Converters
throw new JsonException("Expected $type token."); throw new JsonException("Expected $type token.");
var typeName = lookupTable[(int)tok["$type"]]; var typeName = lookupTable[(int)tok["$type"]];
var instance = (T)Activator.CreateInstance(Type.GetType(typeName)); var instance = (T)Activator.CreateInstance(Type.GetType(typeName).AsNonNull());
serializer.Populate(itemReader, instance); serializer.Populate(itemReader, instance);
list.Add(instance); list.Add(instance);

View File

@ -4,6 +4,7 @@
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
@ -27,7 +28,7 @@ namespace osu.Game.Rulesets
{ {
if (!Available) return null; if (!Available) return null;
var ruleset = (Ruleset)Activator.CreateInstance(Type.GetType(InstantiationInfo)); var ruleset = (Ruleset)Activator.CreateInstance(Type.GetType(InstantiationInfo).AsNonNull());
// 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;

View File

@ -7,6 +7,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using osu.Framework; using osu.Framework;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Logging; using osu.Framework.Logging;
using osu.Framework.Platform; using osu.Framework.Platform;
using osu.Game.Database; using osu.Game.Database;
@ -111,7 +112,7 @@ namespace osu.Game.Rulesets
{ {
try try
{ {
var instanceInfo = ((Ruleset)Activator.CreateInstance(Type.GetType(r.InstantiationInfo))).RulesetInfo; var instanceInfo = ((Ruleset)Activator.CreateInstance(Type.GetType(r.InstantiationInfo).AsNonNull())).RulesetInfo;
r.Name = instanceInfo.Name; r.Name = instanceInfo.Name;
r.ShortName = instanceInfo.ShortName; r.ShortName = instanceInfo.ShortName;

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.IO.Stores; using osu.Framework.IO.Stores;
using osu.Game.Configuration; using osu.Game.Configuration;
using osu.Game.Database; using osu.Game.Database;
@ -32,7 +33,7 @@ namespace osu.Game.Skinning
var type = string.IsNullOrEmpty(InstantiationInfo) var type = string.IsNullOrEmpty(InstantiationInfo)
// handle the case of skins imported before InstantiationInfo was added. // handle the case of skins imported before InstantiationInfo was added.
? typeof(LegacySkin) ? typeof(LegacySkin)
: Type.GetType(InstantiationInfo); : Type.GetType(InstantiationInfo).AsNonNull();
if (type == typeof(DefaultLegacySkin)) if (type == typeof(DefaultLegacySkin))
return (Skin)Activator.CreateInstance(type, this, legacyDefaultResources, resources); return (Skin)Activator.CreateInstance(type, this, legacyDefaultResources, resources);