Skip to content

Commit af0a1ef

Browse files
committed
Add GetTypesSafe, fix crash on MonoField lookup
1 parent 32e6c7c commit af0a1ef

3 files changed

Lines changed: 22 additions & 9 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Reflection;
2+
3+
namespace Il2CppInterop.Common.Extensions;
4+
5+
internal static class AssemblyExtensions
6+
{
7+
public static Type[] GetTypesSafe(this Assembly assembly)
8+
{
9+
try
10+
{
11+
return assembly.GetTypes();
12+
}
13+
catch (ReflectionTypeLoadException ex)
14+
{
15+
return ex.Types.Where(t => t != null).ToArray();
16+
}
17+
}
18+
}

Il2CppInterop.Runtime/Injection/InjectorHelpers.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Runtime.InteropServices;
99
using System.Threading;
1010
using Il2CppInterop.Common;
11+
using Il2CppInterop.Common.Extensions;
1112
using Il2CppInterop.Common.XrefScans;
1213
using Il2CppInterop.Runtime.Runtime;
1314
using Il2CppInterop.Runtime.Runtime.VersionSpecific.Assembly;
@@ -340,7 +341,7 @@ private static d_ClassGetFieldDefaultValue FindClassGetFieldDefaultValue(bool fo
340341
if (forceICallMethod)
341342
{
342343
// MonoField isn't present on 2021.2.0+
343-
var monoFieldType = Il2CppMscorlib.GetTypes().SingleOrDefault((x) => x.Name is "MonoField");
344+
var monoFieldType = Il2CppMscorlib.GetTypesSafe().SingleOrDefault((x) => x.Name is "MonoField");
344345
if (monoFieldType == null)
345346
throw new Exception($"Unity {Il2CppInteropRuntime.Instance.UnityVersion} is not supported at the moment: MonoField isn't present in Il2Cppmscorlib.dll for unity version, unable to fetch icall");
346347

Il2CppInterop.Runtime/Runtime/UnityVersionHandler.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Reflection;
55
using Il2CppInterop.Common;
6+
using Il2CppInterop.Common.Extensions;
67
using Il2CppInterop.Runtime.Runtime.VersionSpecific.Assembly;
78
using Il2CppInterop.Runtime.Runtime.VersionSpecific.AssemblyName;
89
using Il2CppInterop.Runtime.Runtime.VersionSpecific.Class;
@@ -120,14 +121,7 @@ private static T GetHandler<T>()
120121

121122
private static Type[] GetAllTypesSafe()
122123
{
123-
try
124-
{
125-
return typeof(UnityVersionHandler).Assembly.GetTypes();
126-
}
127-
catch (ReflectionTypeLoadException re)
128-
{
129-
return re.Types.Where(t => t != null).ToArray();
130-
}
124+
return typeof(UnityVersionHandler).Assembly.GetTypesSafe();
131125
}
132126

133127
//Assemblies

0 commit comments

Comments
 (0)