Decompiled source of UncaughtExceptionCatcher v0.0.2

UncaughtExceptionCatcher.dll

Decompiled 8 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using HarmonyLib.Public.Patching;
using Microsoft.CodeAnalysis;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: IgnoresAccessChecksTo("Unity.Netcode.Runtime")]
[assembly: AssemblyCompany("UncaughtExceptionCatcher")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Catches all uncaught exceptions thrown by mods, fixing the issue of broken mods breaking other mods")]
[assembly: AssemblyFileVersion("0.0.1.0")]
[assembly: AssemblyInformationalVersion("0.0.1")]
[assembly: AssemblyProduct("UncaughtExceptionCatcher")]
[assembly: AssemblyTitle("UncaughtExceptionCatcher")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.1.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace UncaughtExceptionCatcher
{
	[BepInPlugin("DBJ.UncaughtExceptionCatcher", "UncaughtExceptionCatcher", "0.0.2")]
	public class UncaughtExceptionCatcherBase : BaseUnityPlugin
	{
		internal const string modGUID = "DBJ.UncaughtExceptionCatcher";

		internal const string modName = "UncaughtExceptionCatcher";

		internal const string modVersion = "0.0.2";

		internal static readonly Harmony harmony = new Harmony("DBJ.UncaughtExceptionCatcher");

		internal static readonly ManualLogSource mls = Logger.CreateLogSource("DBJ.UncaughtExceptionCatcher");

		internal static readonly MethodInfo finalizer = AccessTools.Method(typeof(UncaughtExceptionCatcherBase), "Finalizer", (Type[])null, (Type[])null);

		private void Awake()
		{
			mls.LogInfo((object)"[Awake 1 / 3] Attempting to wrap all existing patches...");
			PatchAllMethods();
			mls.LogInfo((object)"[Awake 2 / 3] Attempting to wrap all future patches...");
			harmony.PatchAll();
			mls.LogInfo((object)"[Awake 3 / 3] All existing and future patches have been wrapped!");
		}

		internal static void PatchAllMethods()
		{
			IEnumerable<MethodBase> allPatchedMethods = Harmony.GetAllPatchedMethods();
			foreach (MethodBase item in allPatchedMethods)
			{
				if (item == null)
				{
					continue;
				}
				PatchInfo val = PatchManager.ToPatchInfo(item);
				Patch[][] array = new Patch[2][]
				{
					val?.prefixes,
					val?.postfixes
				};
				Patch[][] array2 = array;
				foreach (Patch[] array3 in array2)
				{
					if (array3 == null || array3.Length == 0)
					{
						continue;
					}
					Patch[] array4 = array3;
					foreach (Patch val2 in array4)
					{
						if (!(((val2 != null) ? val2.PatchMethod : null) == null))
						{
							PatchMethodIfNoFinalizer(val2.PatchMethod, PatchManager.ToPatchInfo((MethodBase)val2.PatchMethod), val2.owner);
						}
					}
				}
				PatchMethodIfNoFinalizer(item, val, harmony.Id);
			}
		}

		internal static Exception Finalizer(ref Exception __exception)
		{
			if (__exception != null)
			{
				mls.LogFatal((object)"An uncaught exception has been caught from a mod's patcher! Details below:");
				mls.LogError((object)__exception.ToString());
			}
			__exception = null;
			return null;
		}

		internal static void PatchMethodIfNoFinalizer(MethodBase methodBase, PatchInfo patchInfo, string owner)
		{
			if (methodBase == null)
			{
				return;
			}
			if (patchInfo == null)
			{
				mls.LogError((object)$"Could not patch {methodBase} due to null PatchInfo!");
				return;
			}
			if (patchInfo.finalizers != null && patchInfo.finalizers.Any((Patch patch) => patch.PatchMethod == finalizer))
			{
				if (patchInfo.finalizers.Last().PatchMethod == finalizer)
				{
					return;
				}
				mls.LogDebug((object)$"Re-patching {methodBase}...");
			}
			else
			{
				mls.LogDebug((object)$"Patching {methodBase}...");
			}
			try
			{
				patchInfo.RemoveFinalizer(owner);
				patchInfo.AddFinalizer(finalizer, owner, -32768, new string[0], new string[0], false);
			}
			catch (Exception ex)
			{
				string text = ((ex != null) ? ex.ToString() : "[could not obtain error message]");
				mls.LogFatal((object)$"Patching of {methodBase} failed due to a Harmony exception! Details below:");
				mls.LogError((object)text);
			}
		}
	}
	[HarmonyPatch(typeof(PatchProcessor))]
	public class PatchProcessorPatches
	{
		[HarmonyPatch("Patch")]
		[HarmonyPostfix]
		public static void PatchPatcher()
		{
			UncaughtExceptionCatcherBase.PatchAllMethods();
		}
	}
	[HarmonyPatch(typeof(PatchClassProcessor))]
	public class PatchClassProcessorPatches
	{
		[HarmonyPatch("Patch")]
		[HarmonyPostfix]
		public static void PatchPatcher()
		{
			UncaughtExceptionCatcherBase.PatchAllMethods();
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "UncaughtExceptionCatcher";

		public const string PLUGIN_NAME = "UncaughtExceptionCatcher";

		public const string PLUGIN_VERSION = "0.0.1";
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}