Decompiled source of ClassicControllerInput v0.1.0

plugins/com.github.jie65535.ClassicControllerInput.dll

Decompiled 7 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Zorro.ControllerSupport;
using Zorro.Settings;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("com.github.jie65535.ClassicControllerInput")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("com.github.jie65535.ClassicControllerInput")]
[assembly: AssemblyTitle("ClassicControllerInput")]
[assembly: AssemblyVersion("0.1.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class IsUnmanagedAttribute : Attribute
	{
	}
	[CompilerGenerated]
	[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]
	[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]
	[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 ClassicControllerInput
{
	[HarmonyPatch]
	internal static class ControllerPatches
	{
		private static readonly FieldInfo ClimbingCharacter = AccessTools.Field(typeof(CharacterClimbing), "character");

		private static readonly FieldInfo MovementCharacter = AccessTools.Field(typeof(CharacterMovement), "character");

		private static readonly FieldInfo ClimbToggledOn = AccessTools.Field(typeof(CharacterClimbing), "climbToggledOn");

		private static readonly ConditionalWeakTable<Character, object> ClassicClimbs = new ConditionalWeakTable<Character, object>();

		private static readonly object ClassicClimb = new object();

		[HarmonyPatch(typeof(CharacterClimbing), "TryToStartWallClimb")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> ChooseClimbAtGrab(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Expected O, but got Unknown
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Expected O, but got Unknown
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Expected O, but got Unknown
			List<CodeInstruction> list = instructions.ToList();
			int num = FindSettingValue<ClimbInputSetting>(list);
			list.InsertRange(num + 1, (IEnumerable<CodeInstruction>)(object)new CodeInstruction[5]
			{
				new CodeInstruction(OpCodes.Ldarg_0, (object)null),
				new CodeInstruction(OpCodes.Ldfld, (object)ClimbingCharacter),
				new CodeInstruction(OpCodes.Ldarg_0, (object)null),
				new CodeInstruction(OpCodes.Ldflda, (object)ClimbToggledOn),
				CodeInstruction.Call(typeof(ControllerPatches), "GetClimbMode", (Type[])null, (Type[])null)
			});
			return list;
		}

		[HarmonyPatch(typeof(CharacterClimbing), "Update")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> PreserveClassicGrab(IEnumerable<CodeInstruction> instructions)
		{
			List<CodeInstruction> list = instructions.ToList();
			FilterButtonReads(list, "usePrimaryWasPressed", ClimbingCharacter, "AllowClimbButtonDetach");
			return list;
		}

		[HarmonyPatch(typeof(CharacterMovement), "SetMovementState")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> RestoreSprintToggle(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Expected O, but got Unknown
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			List<CodeInstruction> list = instructions.ToList();
			int num = FindSettingValue<SprintInputSetting>(list);
			list.InsertRange(num + 1, (IEnumerable<CodeInstruction>)(object)new CodeInstruction[3]
			{
				new CodeInstruction(OpCodes.Ldarg_0, (object)null),
				new CodeInstruction(OpCodes.Ldfld, (object)MovementCharacter),
				CodeInstruction.Call(typeof(ControllerPatches), "GetSprintMode", (Type[])null, (Type[])null)
			});
			FilterButtonReads(list, "sprintToggleIsPressed", MovementCharacter, "AllowSprintButtonHold");
			return list;
		}

		private static int FindSettingValue<TSetting>(List<CodeInstruction> code)
		{
			int[] array = (from entry in code.Select((CodeInstruction instruction, int index) => (instruction: instruction, index: index))
				where entry.instruction.operand is MethodInfo methodInfo2 && methodInfo2.DeclaringType == typeof(SettingsHandler) && methodInfo2.Name == "GetSetting" && methodInfo2.IsGenericMethod && methodInfo2.GetGenericArguments()[0] == typeof(TSetting)
				select entry.index + 1).ToArray();
			if (array.Length == 1 && array[0] + 2 < code.Count && code[array[0]].operand is MethodInfo { Name: "get_Value" } && code[array[0] + 1].opcode == OpCodes.Ldc_I4_1 && (code[array[0] + 2].opcode == OpCodes.Bne_Un_S || code[array[0] + 2].opcode == OpCodes.Bne_Un))
			{
				return array[0];
			}
			throw new InvalidOperationException("Could not locate the " + typeof(TSetting).Name + " comparison.");
		}

		private static void FilterButtonReads(List<CodeInstruction> code, string fieldName, FieldInfo character, string filter)
		{
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Expected O, but got Unknown
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Expected O, but got Unknown
			FieldInfo button = AccessTools.Field(typeof(CharacterInput), fieldName);
			int[] array = (from entry in code.Select((CodeInstruction instruction, int index) => (instruction: instruction, index: index))
				where CodeInstructionExtensions.LoadsField(entry.instruction, button, false)
				select entry.index).Reverse().ToArray();
			if (array.Length != 2)
			{
				throw new InvalidOperationException($"Expected two reads of {fieldName} and found {array.Length}.");
			}
			int[] array2 = array;
			foreach (int num2 in array2)
			{
				code.InsertRange(num2 + 1, (IEnumerable<CodeInstruction>)(object)new CodeInstruction[3]
				{
					new CodeInstruction(OpCodes.Ldarg_0, (object)null),
					new CodeInstruction(OpCodes.Ldfld, (object)character),
					CodeInstruction.Call(typeof(ControllerPatches), filter, (Type[])null, (Type[])null)
				});
			}
		}

		private static InputMode GetClimbMode(InputMode mode, Character character, ref bool climbToggledOn)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Invalid comparison between Unknown and I4
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Invalid comparison between Unknown and I4
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Invalid comparison between Unknown and I4
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			bool flag = (int)mode == 2;
			if (flag && character.IsLocal && (int)InputHandler.GetCurrentUsedInputScheme() == 1)
			{
				ClassicClimbs.GetValue(character, (Character _) => ClassicClimb);
				return (InputMode)1;
			}
			if (ClassicClimbs.Remove(character) && (int)mode != 1)
			{
				climbToggledOn = false;
			}
			if (!flag)
			{
				return mode;
			}
			return (InputMode)0;
		}

		private static bool AllowClimbButtonDetach(bool pressed, Character character)
		{
			object value;
			if (pressed)
			{
				return !ClassicClimbs.TryGetValue(character, out value);
			}
			return false;
		}

		private static InputMode GetSprintMode(InputMode mode, Character character)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Invalid comparison between Unknown and I4
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			if ((int)mode != 2)
			{
				return mode;
			}
			if (character.IsLocal && character.input.sprintToggleWasPressed)
			{
				return (InputMode)1;
			}
			return (InputMode)0;
		}

		private static bool AllowSprintButtonHold(bool held, Character character)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Invalid comparison between Unknown and I4
			if (held)
			{
				if (character.IsLocal)
				{
					return (int)((EnumSetting<InputMode>)(object)SettingsHandler.Instance.GetSetting<SprintInputSetting>()).Value != 2;
				}
				return true;
			}
			return false;
		}
	}
	[HarmonyPatch(/*Could not decode attribute arguments.*/)]
	internal static class InputSettingsPatch
	{
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> InstallInputSettings(IEnumerable<CodeInstruction> instructions)
		{
			int replacements = 0;
			foreach (CodeInstruction instruction in instructions)
			{
				if (instruction.opcode == OpCodes.Newobj && instruction.operand is ConstructorInfo constructorInfo)
				{
					if (constructorInfo.DeclaringType == typeof(ClimbInputSetting))
					{
						instruction.operand = AccessTools.Constructor(typeof(ClassicClimbInputSetting), (Type[])null, false);
						replacements++;
					}
					else if (constructorInfo.DeclaringType == typeof(SprintInputSetting))
					{
						instruction.operand = AccessTools.Constructor(typeof(ClassicSprintInputSetting), (Type[])null, false);
						replacements++;
					}
				}
				yield return instruction;
			}
			if (replacements != 2)
			{
				throw new InvalidOperationException("Could not locate the two control settings.");
			}
		}
	}
	internal sealed class ClassicClimbInputSetting : ClimbInputSetting
	{
		internal const InputMode Classic = (InputMode)2;

		protected override InputMode GetDefaultValue()
		{
			return (InputMode)2;
		}

		public override void Load(ISettingsSaveLoad loader)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			((EnumSetting<InputMode>)this).Value = ClassicInputOptions.Load<ClimbInputSetting, InputMode>(loader, ((EnumSetting<InputMode>)(object)this).GetDefaultValue());
		}

		public override void Save(ISettingsSaveLoad saver)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			ClassicInputOptions.Save<ClimbInputSetting, InputMode>(saver, ((EnumSetting<InputMode>)this).Value, (InputMode)2);
		}

		public override List<string> GetUnlocalizedChoices()
		{
			return ClassicInputOptions.AddChoice(((EnumSetting<InputMode>)this).GetUnlocalizedChoices(), localize: false);
		}

		public override List<string> GetCustomLocalizedChoices()
		{
			return ClassicInputOptions.AddChoice(((EnumSetting<InputMode>)this).GetUnlocalizedChoices(), localize: true);
		}
	}
	internal sealed class ClassicSprintInputSetting : SprintInputSetting
	{
		internal const InputMode Classic = (InputMode)2;

		protected override InputMode GetDefaultValue()
		{
			return (InputMode)2;
		}

		public override void Load(ISettingsSaveLoad loader)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			((EnumSetting<InputMode>)this).Value = ClassicInputOptions.Load<SprintInputSetting, InputMode>(loader, ((EnumSetting<InputMode>)(object)this).GetDefaultValue());
		}

		public override void Save(ISettingsSaveLoad saver)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			ClassicInputOptions.Save<SprintInputSetting, InputMode>(saver, ((EnumSetting<InputMode>)this).Value, (InputMode)2);
		}

		public override List<string> GetUnlocalizedChoices()
		{
			return ClassicInputOptions.AddChoice(((EnumSetting<InputMode>)this).GetUnlocalizedChoices(), localize: false);
		}

		public override List<string> GetCustomLocalizedChoices()
		{
			return ClassicInputOptions.AddChoice(((EnumSetting<InputMode>)this).GetUnlocalizedChoices(), localize: true);
		}
	}
	internal static class ClassicInputOptions
	{
		internal static TMode Load<TSetting, TMode>(ISettingsSaveLoad loader, TMode classic) where TSetting : EnumSetting<TMode> where TMode : unmanaged, Enum
		{
			Type typeFromHandle = typeof(TSetting);
			bool flag = default(bool);
			if (loader.TryLoadBool(GetClassicKey(typeFromHandle), ref flag) && flag)
			{
				return classic;
			}
			string value = default(string);
			if (loader.TryLoadString(typeFromHandle, ref value) && Enum.TryParse<TMode>(value, out var result) && Enum.IsDefined(typeof(TMode), result))
			{
				return result;
			}
			return classic;
		}

		internal static void Save<TSetting, TMode>(ISettingsSaveLoad saver, TMode mode, TMode classic) where TSetting : EnumSetting<TMode> where TMode : unmanaged, Enum
		{
			bool flag = EqualityComparer<TMode>.Default.Equals(mode, classic);
			if (!flag)
			{
				saver.SaveString(typeof(TSetting), mode.ToString());
			}
			saver.SaveBool(GetClassicKey(typeof(TSetting)), flag);
		}

		internal static List<string> AddChoice(List<string> choices, bool localize)
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Invalid comparison between Unknown and I4
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Invalid comparison between Unknown and I4
			string text = "Classic";
			if (localize)
			{
				for (int i = 0; i < choices.Count; i++)
				{
					choices[i] = LocalizedText.GetText(choices[i], true);
				}
				Language cURRENT_LANGUAGE = LocalizedText.CURRENT_LANGUAGE;
				string text2 = (((int)cURRENT_LANGUAGE == 9) ? "经典" : (((int)cURRENT_LANGUAGE != 10) ? text : "經典"));
				text = text2;
			}
			choices.Add(text);
			return choices;
		}

		private static string GetClassicKey(Type settingType)
		{
			return "com.github.jie65535.ClassicControllerInput." + settingType.Name;
		}
	}
	[BepInPlugin("com.github.jie65535.ClassicControllerInput", "ClassicControllerInput", "0.1.0")]
	public sealed class Plugin : BaseUnityPlugin
	{
		internal const string PluginGuid = "com.github.jie65535.ClassicControllerInput";

		private Harmony? harmony;

		private void Awake()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Expected O, but got Unknown
			harmony = new Harmony("com.github.jie65535.ClassicControllerInput");
			try
			{
				harmony.PatchAll(typeof(Plugin).Assembly);
				((BaseUnityPlugin)this).Logger.LogInfo((object)"ClassicControllerInput ready. Classic is available in the climb and sprint settings.");
			}
			catch (Exception arg)
			{
				harmony.UnpatchSelf();
				harmony = null;
				((BaseUnityPlugin)this).Logger.LogError((object)$"Could not apply the controller patches to this game build. {arg}");
			}
		}

		private void OnDestroy()
		{
			Harmony? obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
		}
	}
}