Decompiled source of CrystalLib v1.1.1

CrystalLib.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
using UnityEngine.InputSystem;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("CrystalLib")]
[assembly: AssemblyDescription("CrystalLib")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Crystal")]
[assembly: AssemblyProduct("CrystalLib")]
[assembly: AssemblyCopyright("Copyright © 2023 Crystal Ferrai")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("1b09bd80-a4c4-4706-9b13-2d7fc263faa1")]
[assembly: AssemblyFileVersion("1.1.1.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.1.1.0")]
namespace CrystalLib;

public class InputBinding : IDisposable
{
	[HarmonyPatch(typeof(PlayerController))]
	private static class PlayerController_Patches
	{
		[HarmonyPatch("FixedUpdate")]
		[HarmonyPostfix]
		private static void FixedUpdate_Postfix(PlayerController __instance)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Expected O, but got Unknown
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Expected O, but got Unknown
			ZNetView val = (ZNetView)sViewField.GetValue(__instance);
			if ((Object.op_Implicit((Object)(object)val) && !val.IsOwner()) || !(bool)sTakeInputMethod.Invoke(__instance, new object[1] { false }))
			{
				return;
			}
			foreach (InputBinding sInstance in sInstances)
			{
				if (ZInput.GetButtonDown(sInstance.Name))
				{
					Player player = (Player)sCharacterField.GetValue(__instance);
					sInstance.InputPressed?.Invoke(sInstance, new InputEventArgs(player));
				}
			}
		}
	}

	[HarmonyPatch(typeof(ZInput))]
	private static class ZInput_Patches
	{
		[HarmonyPatch("Reset")]
		[HarmonyPostfix]
		private static void Reset_Postfix(ZInput __instance)
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			foreach (InputBinding sInstance in sInstances)
			{
				AddButton(sInstance.Name, sInstance.ConfigEntry.Value, __instance);
			}
		}
	}

	private static readonly List<InputBinding> sInstances;

	private static readonly Harmony sZInputHarmony;

	private static readonly Harmony sPlayerControllerHarmony;

	private static readonly MethodInfo sAddButtonMethod;

	private static readonly MethodInfo sKeyToPathMethod;

	private static readonly MethodInfo sTakeInputMethod;

	private static readonly FieldInfo sCharacterField;

	private static readonly FieldInfo sViewField;

	private static readonly FieldInfo sButtonsField;

	public string Name { get; }

	public ConfigEntry<Key> ConfigEntry { get; }

	public event EventHandler<InputEventArgs> InputPressed;

	static InputBinding()
	{
		//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bb: Expected O, but got Unknown
		//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ca: Expected O, but got Unknown
		sInstances = new List<InputBinding>();
		sAddButtonMethod = typeof(ZInput).GetMethod("AddButton", BindingFlags.Instance | BindingFlags.NonPublic);
		sKeyToPathMethod = typeof(ZInput).GetMethod("KeyToPath", BindingFlags.Static | BindingFlags.NonPublic);
		sTakeInputMethod = typeof(PlayerController).GetMethod("TakeInput", BindingFlags.Instance | BindingFlags.NonPublic);
		sCharacterField = typeof(PlayerController).GetField("m_character", BindingFlags.Instance | BindingFlags.NonPublic);
		sViewField = typeof(PlayerController).GetField("m_nview", BindingFlags.Instance | BindingFlags.NonPublic);
		sButtonsField = typeof(ZInput).GetField("m_buttons", BindingFlags.Instance | BindingFlags.NonPublic);
		sZInputHarmony = new Harmony("CrystalLib_KeyBind_ZInput");
		sPlayerControllerHarmony = new Harmony("CrystalLib_KeyBind_PlayerController");
		sZInputHarmony.PatchAll(typeof(ZInput_Patches));
		sPlayerControllerHarmony.PatchAll(typeof(PlayerController_Patches));
	}

	public InputBinding(string name, ConfigEntry<Key> configEntry)
	{
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		Name = name;
		ConfigEntry = configEntry;
		ConfigEntry.SettingChanged += ConfigEntry_SettingChanged;
		sInstances.Add(this);
		if (ZInput.instance != null)
		{
			AddButton(Name, ConfigEntry.Value);
		}
	}

	~InputBinding()
	{
		Dispose(disposing: false);
	}

	public void Dispose()
	{
		GC.SuppressFinalize(this);
		Dispose(disposing: true);
	}

	private void Dispose(bool disposing)
	{
		if (disposing)
		{
			ConfigEntry.SettingChanged -= ConfigEntry_SettingChanged;
			sInstances.Remove(this);
		}
	}

	private void ConfigEntry_SettingChanged(object sender, EventArgs e)
	{
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		if (ZInput.instance != null)
		{
			SetButton(Name, ConfigEntry.Value);
		}
	}

	private static void AddButton(string name, Key keyCode, ZInput instance = null)
	{
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		if (instance == null)
		{
			instance = ZInput.instance;
		}
		if (instance != null)
		{
			string text = (string)sKeyToPathMethod.Invoke(null, new object[1] { keyCode });
			sAddButtonMethod.Invoke(instance, new object[7] { name, text, false, true, false, 0f, 0f });
		}
	}

	private static void SetButton(string name, Key keyCode)
	{
		//IL_0012: 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_0039: Expected O, but got Unknown
		string text = (string)sKeyToPathMethod.Invoke(ZInput.instance, new object[1] { keyCode });
		ButtonDef value = new ButtonDef(name, text, false, true, false, 0f, 0f);
		((Dictionary<string, ButtonDef>)sButtonsField.GetValue(ZInput.instance))[name] = value;
	}
}
public class InputEventArgs : EventArgs
{
	public Player Player { get; }

	public InputEventArgs(Player player)
	{
		Player = player;
	}
}