Skip to content

Commit

Permalink
Handle ReflectionTypeLoadException (#399)
Browse files Browse the repository at this point in the history
* Handle assemblies which could throw ReflectionTypeLoadException when types cant be all retrieved

* Returns false on error for GetTypesSafe
  • Loading branch information
xiaoxiao921 authored May 21, 2022
1 parent 55f01bc commit b6af95d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
6 changes: 4 additions & 2 deletions R2API/ContentManagement/R2APIContentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ public static void AddPreExistingSerializableContentPack(R2APISerializableConten
try {
Assembly assembly = Assembly.GetCallingAssembly();
if (!AssemblyToBepInModName.ContainsKey(assembly)) {
Type mainClass = assembly.GetTypes()
_ = Reflection.GetTypesSafe(assembly, out var types);
Type mainClass = types
.Where(t => t.GetCustomAttribute<BepInPlugin>() != null)
.FirstOrDefault();

Expand Down Expand Up @@ -397,7 +398,8 @@ internal static R2APISerializableContentPack GetOrCreateSerializableContentPack(
R2API.Logger.LogWarning($"The assembly {assembly.FullName} is not a loaded BepInEx plugin, falling back to looking for attribute in assembly");

try {
var infoAttribute = assembly.GetTypes().Select(x => x.GetCustomAttribute<BepInPlugin>()).First(x => x != null);
_ = Reflection.GetTypesSafe(assembly, out var types);
var infoAttribute = types.Select(x => x.GetCustomAttribute<BepInPlugin>()).First(x => x != null);
modName = infoAttribute.GUID;
}
catch {
Expand Down
3 changes: 2 additions & 1 deletion R2API/Utils/APISubmodule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ void AddModuleToSet(IEnumerable<CustomAttributeArgument> arguments) {
}

void CallWhenAssembliesAreScanned() {
var moduleTypes = Assembly.GetExecutingAssembly().GetTypes().Where(APISubmoduleFilter).ToList();
_ = Reflection.GetTypesSafe(Assembly.GetExecutingAssembly(), out var types);
var moduleTypes = types.Where(APISubmoduleFilter).ToList();

foreach (var moduleType in moduleTypes) {
R2API.Logger.LogInfo($"Enabling R2API Submodule: {moduleType.Name}");
Expand Down
11 changes: 8 additions & 3 deletions R2API/Utils/CommandHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ private static void HandleCommandsConvars() {
}

private static void RegisterCommands(Assembly assembly) {
var types = assembly?.GetTypes();
if (types == null) {
if (assembly == null) {
return;
}
_ = Reflection.GetTypesSafe(assembly, out var types);

try {
var catalog = _console.concommandCatalog;
Expand Down Expand Up @@ -112,9 +112,14 @@ private static void RegisterCommands(Assembly assembly) {
}

private static void RegisterConVars(Assembly assembly) {
if (assembly == null) {
return;
}
_ = Reflection.GetTypesSafe(assembly, out var types);

try {
var customVars = new List<BaseConVar>();
foreach (var type in assembly.GetTypes()) {
foreach (var type in types) {
foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)) {
if (field.FieldType.IsSubclassOf(typeof(BaseConVar))) {
if (field.IsStatic) {
Expand Down
18 changes: 18 additions & 0 deletions R2API/Utils/Reflection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,24 @@ public static byte ReadLocalIndex(OpCode opCode, object? operand) {

#endregion Fast Reflection

/// <summary>
///
/// </summary>
/// <param name="assembly"></param>
/// <param name="assemblyTypes"></param>
/// <returns>false if a ReflectionTypeLoadException was caught</returns>
public static bool GetTypesSafe(Assembly assembly, out Type[] assemblyTypes) {
try {
assemblyTypes = assembly.GetTypes();
}
catch (ReflectionTypeLoadException re) {
assemblyTypes = re.Types.Where(t => t != null).ToArray();
return false;
}

return true;
}

public static System.Reflection.FieldInfo GetNestedField(Type type, string fieldName) {
var nestedTypes = type.GetNestedTypes(AllFlags);
foreach (Type nestedType in nestedTypes) {
Expand Down

0 comments on commit b6af95d

Please sign in to comment.