Decompiled source of CrystalLib v1.1.0

CrystalLib.dll

Decompiled 5 months 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.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.1.0.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_0022: Unknown result type (might be due to invalid IL or missing references)
			foreach (InputBinding sInstance in sInstances)
			{
				__instance.AddButton(sInstance.Name, sInstance.ConfigEntry.Value, 0f, 0f, true, false);
			}
		}
	}

	private static readonly List<InputBinding> sInstances;

	private static readonly Harmony sZInputHarmony;

	private static readonly Harmony sPlayerControllerHarmony;

	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_007b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0085: Expected O, but got Unknown
		//IL_008a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0094: Expected O, but got Unknown
		sInstances = new List<InputBinding>();
		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_004e: 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)
		{
			ZInput.instance.AddButton(Name, ConfigEntry.Value, 0f, 0f, true, false);
		}
	}

	~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 SetButton(string name, Key keyCode)
	{
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		((Dictionary<string, ButtonDef>)sButtonsField.GetValue(ZInput.instance))[name].m_key = keyCode;
	}
}
public class InputEventArgs : EventArgs
{
	public Player Player { get; }

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