Decompiled source of TypeLoadExceptionFixer v1.0.4

patchers/com.github.Hamunii.TypeLoadExceptionFixer.dll

Decompiled 2 months ago
using System;
using System.Collections;
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.Logging;
using Microsoft.CodeAnalysis;
using Mono.Cecil;
using Mono.Cecil.Cil;
using MonoDetour.Cil;
using MonoMod.Cil;
using MonoMod.RuntimeDetour;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Unity.InputSystem")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("com.github.Hamunii.TypeLoadExceptionFixer")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.4.0")]
[assembly: AssemblyInformationalVersion("1.0.4+cbea511d427d6b82d22224bc2d3bc021fcd5b840")]
[assembly: AssemblyProduct("com.github.Hamunii.TypeLoadExceptionFixer")]
[assembly: AssemblyTitle("TypeLoadExceptionFixer")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.4.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.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 BepInEx
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class BepInAutoPluginAttribute : Attribute
	{
		public BepInAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace BepInEx.Preloader.Core.Patching
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class PatcherAutoPluginAttribute : Attribute
	{
		public PatcherAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace TypeLoadExceptionFixer
{
	internal static class Patcher
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static Func<Func<Assembly, Type[]>, Assembly, Type[]> <0>__Hook_Assembly_GetTypes;

			public static Manipulator <1>__ILHook_InputManager_RegisterCustomTypes_2;

			public static Action<TypeLoadException> <2>__HandleTypeLoadException;
		}

		internal static ManualLogSource Log = Logger.CreateLogSource("TypeLoadExceptionFixer");

		private static Hook hookAssemblyGetTypes = null;

		public static IEnumerable<string> TargetDLLs { get; } = new <>z__ReadOnlySingleElementList<string>("Unity.InputSystem.dll");


		public static void Initialize()
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Expected O, but got Unknown
			MethodInfo method = typeof(Assembly).GetMethod("GetTypes");
			hookAssemblyGetTypes = new Hook((MethodBase)method, (Delegate)new Func<Func<Assembly, Type[]>, Assembly, Type[]>(Hook_Assembly_GetTypes));
		}

		private static Type[] Hook_Assembly_GetTypes(Func<Assembly, Type[]> orig, Assembly self)
		{
			try
			{
				Type[] array = orig(self);
				for (int i = 0; i < array.Length; i++)
				{
					if (array[i] == null)
					{
						List<Type> list = new List<Type>();
						list.AddRange(array.Where((Type type) => (object)type != null));
						return list.ToArray();
					}
				}
				return array;
			}
			catch (ReflectionTypeLoadException ex)
			{
				List<Type> list2 = new List<Type>();
				list2.AddRange(ex.Types.Where((Type type) => (object)type != null));
				return list2.ToArray();
			}
		}

		public static void Patch(AssemblyDefinition assembly)
		{
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Expected O, but got Unknown
			if (!(((AssemblyNameReference)assembly.Name).Name != "Unity.InputSystem"))
			{
				ModuleDefinition mainModule = assembly.MainModule;
				TypeDefinition val = ((IEnumerable<TypeDefinition>)mainModule.Types).First((TypeDefinition t) => ((MemberReference)t).FullName == "UnityEngine.InputSystem.InputManager");
				MethodDefinition val2 = ((IEnumerable<MethodDefinition>)val.Methods).First((MethodDefinition m) => ((MemberReference)m).Name == "RegisterCustomTypes" && ((MethodReference)m).HasParameters);
				object obj = <>O.<1>__ILHook_InputManager_RegisterCustomTypes_2;
				if (obj == null)
				{
					Manipulator val3 = ILHook_InputManager_RegisterCustomTypes_2;
					<>O.<1>__ILHook_InputManager_RegisterCustomTypes_2 = val3;
					obj = (object)val3;
				}
				MethodDefinitionExtensions.ILWeave(val2, (Manipulator)obj);
			}
		}

		private static void ILHook_InputManager_RegisterCustomTypes_2(ILManipulationInfo info)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Expected O, but got Unknown
			ILWeaver w = new ILWeaver(info);
			Instruction tryStart = null;
			ILLabel afterTryEnd = null;
			ILLabel val5 = default(ILLabel);
			int num4 = default(int);
			int num3 = default(int);
			int num2 = default(int);
			int num = default(int);
			MethodReference val4 = default(MethodReference);
			ILWeaverResult val = w.MatchRelaxed(new Predicate<Instruction>[8]
			{
				(Instruction x) => ILPatternMatchingExt.MatchBr(x, ref val5),
				(Instruction x) => ILPatternMatchingExt.MatchLdloc(x, ref num4),
				(Instruction x) => ILPatternMatchingExt.MatchLdloc(x, ref num3),
				(Instruction x) => ILPatternMatchingExt.MatchLdelemRef(x),
				(Instruction x) => ILPatternMatchingExt.MatchStloc(x, ref num2),
				(Instruction x) => ILPatternMatchingExt.MatchLdloc(x, ref num) && w.SetInstructionTo(ref tryStart, x),
				(Instruction x) => ILPatternMatchingExt.MatchCallvirt(x, ref val4),
				(Instruction x) => ILPatternMatchingExt.MatchBrfalse(x, ref afterTryEnd)
			});
			if (!val.IsValid)
			{
				Log.LogError((object)val.FailureMessage);
				return;
			}
			Instruction val2 = w.CreateCall((Delegate)new Action<TypeLoadException>(HandleTypeLoadException));
			w.InsertBefore(afterTryEnd.Target, (IEnumerable<Instruction>)new <>z__ReadOnlySingleElementList<Instruction>(val2));
			WeaverExceptionCatchHandler val3 = default(WeaverExceptionCatchHandler);
			w.HandlerCreateCatch(typeof(TypeLoadException), ref val3).HandlerSetTryStart(tryStart, (IWeaverExceptionHandler)(object)val3).HandlerSetHandlerStart(val2, (IWeaverExceptionHandler)(object)val3)
				.HandlerSetHandlerEnd(val2, (IWeaverExceptionHandler)(object)val3)
				.HandlerApply((IWeaverExceptionHandler)(object)val3);
		}

		private static void HandleTypeLoadException(TypeLoadException ex)
		{
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}
[CompilerGenerated]
internal sealed class <>z__ReadOnlySingleElementList<T> : IEnumerable, ICollection, IList, IEnumerable<T>, IReadOnlyCollection<T>, IReadOnlyList<T>, ICollection<T>, IList<T>
{
	private sealed class Enumerator : IDisposable, IEnumerator, IEnumerator<T>
	{
		object IEnumerator.Current => _item;

		T IEnumerator<T>.Current => _item;

		public Enumerator(T item)
		{
			_item = item;
		}

		bool IEnumerator.MoveNext()
		{
			if (!_moveNextCalled)
			{
				return _moveNextCalled = true;
			}
			return false;
		}

		void IEnumerator.Reset()
		{
			_moveNextCalled = false;
		}

		void IDisposable.Dispose()
		{
		}
	}

	int ICollection.Count => 1;

	bool ICollection.IsSynchronized => false;

	object ICollection.SyncRoot => this;

	object IList.this[int index]
	{
		get
		{
			if (index != 0)
			{
				throw new IndexOutOfRangeException();
			}
			return _item;
		}
		set
		{
			throw new NotSupportedException();
		}
	}

	bool IList.IsFixedSize => true;

	bool IList.IsReadOnly => true;

	int IReadOnlyCollection<T>.Count => 1;

	T IReadOnlyList<T>.this[int index]
	{
		get
		{
			if (index != 0)
			{
				throw new IndexOutOfRangeException();
			}
			return _item;
		}
	}

	int ICollection<T>.Count => 1;

	bool ICollection<T>.IsReadOnly => true;

	T IList<T>.this[int index]
	{
		get
		{
			if (index != 0)
			{
				throw new IndexOutOfRangeException();
			}
			return _item;
		}
		set
		{
			throw new NotSupportedException();
		}
	}

	public <>z__ReadOnlySingleElementList(T item)
	{
		_item = item;
	}

	IEnumerator IEnumerable.GetEnumerator()
	{
		return new Enumerator(_item);
	}

	void ICollection.CopyTo(Array array, int index)
	{
		array.SetValue(_item, index);
	}

	int IList.Add(object value)
	{
		throw new NotSupportedException();
	}

	void IList.Clear()
	{
		throw new NotSupportedException();
	}

	bool IList.Contains(object value)
	{
		return EqualityComparer<T>.Default.Equals(_item, (T)value);
	}

	int IList.IndexOf(object value)
	{
		if (!EqualityComparer<T>.Default.Equals(_item, (T)value))
		{
			return -1;
		}
		return 0;
	}

	void IList.Insert(int index, object value)
	{
		throw new NotSupportedException();
	}

	void IList.Remove(object value)
	{
		throw new NotSupportedException();
	}

	void IList.RemoveAt(int index)
	{
		throw new NotSupportedException();
	}

	IEnumerator<T> IEnumerable<T>.GetEnumerator()
	{
		return new Enumerator(_item);
	}

	void ICollection<T>.Add(T item)
	{
		throw new NotSupportedException();
	}

	void ICollection<T>.Clear()
	{
		throw new NotSupportedException();
	}

	bool ICollection<T>.Contains(T item)
	{
		return EqualityComparer<T>.Default.Equals(_item, item);
	}

	void ICollection<T>.CopyTo(T[] array, int arrayIndex)
	{
		array[arrayIndex] = _item;
	}

	bool ICollection<T>.Remove(T item)
	{
		throw new NotSupportedException();
	}

	int IList<T>.IndexOf(T item)
	{
		if (!EqualityComparer<T>.Default.Equals(_item, item))
		{
			return -1;
		}
		return 0;
	}

	void IList<T>.Insert(int index, T item)
	{
		throw new NotSupportedException();
	}

	void IList<T>.RemoveAt(int index)
	{
		throw new NotSupportedException();
	}
}