Skip to content

Commit

Permalink
Added BepInEx.SuppressGetTypesErrorsPatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed May 12, 2021
1 parent 3a08eab commit 7967ac7
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{F2A30C40-4073-45AC-8325-E255BD2B011D}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>BepInEx</RootNamespace>
<AssemblyName>BepInEx.SuppressGetTypesErrorsPatcher</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>embedded</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\bin\BepInEx\patchers\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>embedded</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\bin\BepInEx\patchers\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>..\lib\0Harmony.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="BepInEx">
<HintPath>..\lib\BepInEx.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Mono.Cecil">
<HintPath>..\lib\Mono.Cecil.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="UnityEngine">
<HintPath>..\lib\UnityEngine.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="SuppressGetTypesErrorsPatcher.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
15 changes: 15 additions & 0 deletions BepInEx.SuppressGetTypesErrors/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Reflection;
using System.Runtime.InteropServices;
using static BepInEx.SuppressGetTypesErrorsPatcher;

[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("https://github.com/BepInEx/BepInEx.Utility")]
[assembly: AssemblyCopyright("Copyright 2021")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: AssemblyTitle(GUID)]
[assembly: AssemblyProduct(GUID)]
[assembly: AssemblyDescription(PluginName)]
[assembly: AssemblyVersion(Version)]
[assembly: AssemblyFileVersion(Version)]
43 changes: 43 additions & 0 deletions BepInEx.SuppressGetTypesErrors/SuppressGetTypesErrorsPatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using BepInEx.Bootstrap;
using HarmonyLib;
using Mono.Cecil;

namespace BepInEx
{
public static class SuppressGetTypesErrorsPatcher
{
public const string GUID = "BepInEx.SuppressGetTypesErrorsPatcher";
public const string PluginName = "Suppress Type.GetTypes Errors";
public const string Version = "1.0";

// Needed to be a valid patcher
public static IEnumerable<string> TargetDLLs { get; } = Enumerable.Empty<string>();

// Needed to be a valid patcher
public static void Patch(AssemblyDefinition assembly) { }

public static void Finish()
{
// Need to run this in finalizer after all assemblies are patched or we might patch an assembly that gets replaced later
Harmony.CreateAndPatchAll(typeof(SuppressGetTypesErrorsPatcher), GUID);
}

[HarmonyPatch(typeof(Assembly), nameof(Assembly.GetTypes), new Type[0])]
[HarmonyFinalizer]
public static void HandleReflectionTypeLoad(ref Exception __exception, ref Type[] __result)
{
if (__exception == null)
return;
if (__exception is ReflectionTypeLoadException re)
{
__exception = null;
__result = re.Types.Where(t => t != null).ToArray();
UnityEngine.Debug.Log($"Encountered ReflectionTypeLoadException which was suppressed. Full error: \n${TypeLoader.TypeLoadExceptionToString(re)}");
}
}
}
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ Prevents plugin hotkeys from triggering while typing in an input field.
## MessageCenter
A simple plugin that shows any log entries marked as "Message" on screen. Plugins generally use the "Message" log level for things that they want the user to read.

## SuppressGetTypesErrorsPatcher
A patcher that hooks Assembly.GetTypes() and handles ReflectionTypeLoadException. Useful when game code is using Assembly.GetTypes() without handling the exception, and it crashes on plugin assemblies that have types that can't be loaded.

#### How to make my mod compatible?
Use the `Logger` of your plugin and call its `LogMessage` method or `Log` method and pass in `LogLevel.Message` as a parameter. You don't have to reference this plugin, and everything will work fine if this plugin doesn't exist.

Expand Down

0 comments on commit 7967ac7

Please sign in to comment.