Decompiled source of FinallyCorrectKeysVRFix v1.0.0

FinallyCorrectKeysVRFix.dll

Decompiled 16 minutes ago
using System;
using System.Collections;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine.InputSystem;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[BepInPlugin("flamsey.finallycorrectkeys.vrfix", "FinallyCorrectKeys VR Fix", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class FinallyCorrectKeysVRFix : BaseUnityPlugin
{
	private static ManualLogSource Log;

	private static MethodInfo getInputAction;

	private static int suppressed;

	private void Awake()
	{
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Expected O, but got Unknown
		//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f1: Expected O, but got Unknown
		//IL_0176: Unknown result type (might be due to invalid IL or missing references)
		//IL_017d: Expected O, but got Unknown
		//IL_014b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0152: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		Assembly assembly = null;
		Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
		foreach (Assembly assembly2 in assemblies)
		{
			if (assembly2.GetName().Name == "Rminin.FinallyCorrectKeys")
			{
				assembly = assembly2;
				break;
			}
		}
		if (assembly == null)
		{
			Log.LogInfo((object)"FinallyCorrectKeys not loaded; nothing to fix.");
			return;
		}
		Harmony val = new Harmony("flamsey.finallycorrectkeys.vrfix");
		BindingFlags bindingAttr = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
		Type type = assembly.GetType("FinallyCorrectKeys.Util.ActionBindings");
		getInputAction = type?.GetMethod("GetInputAction", bindingAttr);
		MethodInfo methodInfo = type?.GetMethod("GetInputBindingString", bindingAttr);
		if (methodInfo != null)
		{
			MethodInfo methodInfo2 = methodInfo;
			HarmonyMethod val2 = new HarmonyMethod(typeof(FinallyCorrectKeysVRFix).GetMethod("BindingStringFinalizer", BindingFlags.Static | BindingFlags.NonPublic));
			val.Patch((MethodBase)methodInfo2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, val2, (HarmonyMethod)null);
		}
		MethodInfo methodInfo3 = assembly.GetType("FinallyCorrectKeys.Util.BindingReplacer")?.GetMethod("Replace", bindingAttr);
		if (methodInfo3 != null)
		{
			MethodInfo methodInfo2 = methodInfo3;
			HarmonyMethod val2 = new HarmonyMethod(typeof(FinallyCorrectKeysVRFix).GetMethod("ReplaceFinalizer", BindingFlags.Static | BindingFlags.NonPublic));
			val.Patch((MethodBase)methodInfo2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, val2, (HarmonyMethod)null);
		}
		HarmonyMethod val3 = new HarmonyMethod(typeof(FinallyCorrectKeysVRFix).GetMethod("SwallowFinalizer", BindingFlags.Static | BindingFlags.NonPublic));
		int num = 0;
		string[] array = new string[3] { "FinallyCorrectKeys.Patches.HUDManagerPatches", "FinallyCorrectKeys.Patches.PlayerControllerBPatches", "FinallyCorrectKeys.Patches.ShipBuildModeManagerPatches" };
		foreach (string name in array)
		{
			Type type2 = assembly.GetType(name);
			if (type2 == null)
			{
				continue;
			}
			MethodInfo[] methods = type2.GetMethods(bindingAttr);
			foreach (MethodInfo methodInfo4 in methods)
			{
				if (!methodInfo4.IsAbstract && !methodInfo4.IsGenericMethodDefinition && !(methodInfo4.Name == ".ctor") && !(methodInfo4.Name == ".cctor") && !(methodInfo4.ReturnType == typeof(IEnumerator)))
				{
					try
					{
						MethodInfo methodInfo2 = methodInfo4;
						HarmonyMethod val2 = val3;
						val.Patch((MethodBase)methodInfo2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, val2, (HarmonyMethod)null);
						num++;
					}
					catch (Exception ex)
					{
						Log.LogDebug((object)("Skipped " + methodInfo4.Name + ": " + ex.Message));
					}
				}
			}
		}
		Log.LogInfo((object)("Guarded FinallyCorrectKeys (" + num + " hooks)."));
	}

	private static Exception BindingStringFinalizer(Exception __exception, object[] __args, ref string __result)
	{
		if (__exception == null)
		{
			return null;
		}
		try
		{
			object? obj = (((object)getInputAction != null) ? getInputAction.Invoke(null, new object[1] { __args[0] }) : null);
			InputAction val = (InputAction)((obj is InputAction) ? obj : null);
			__result = ((val == null) ? string.Empty : InputActionRebindingExtensions.GetBindingDisplayString(val, (DisplayStringOptions)0, (string)null));
		}
		catch
		{
			__result = string.Empty;
		}
		Note(__exception);
		return null;
	}

	private static Exception ReplaceFinalizer(Exception __exception, object[] __args, ref string __result)
	{
		if (__exception == null)
		{
			return null;
		}
		__result = __args[0] as string;
		Note(__exception);
		return null;
	}

	private static Exception SwallowFinalizer(Exception __exception)
	{
		if (__exception != null)
		{
			Note(__exception);
		}
		return null;
	}

	private static void Note(Exception e)
	{
		suppressed++;
		if (suppressed <= 3)
		{
			Log.LogWarning((object)("Suppressed a FinallyCorrectKeys error (#" + suppressed + "): " + e.GetType().Name + ": " + e.Message));
		}
		else if (suppressed == 4)
		{
			Log.LogWarning((object)"Further FinallyCorrectKeys errors will be suppressed silently.");
		}
	}
}