Decompiled source of PlayerActionsHelper v0.0.1

PlayerActionsHelper.dll

Decompiled a year ago
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using InControl;
using PlayerActionsHelper.Extensions;
using UnityEngine;

[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("PlayerActionsHelper")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("PlayerActionsHelper")]
[assembly: AssemblyTitle("PlayerActionsHelper")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace PlayerActionsHelper
{
	[BepInPlugin("com.rounds.willuwontu.ActionHelper", "Action Helper", "0.0.1")]
	[BepInProcess("Rounds.exe")]
	internal class ActionHelper : BaseUnityPlugin
	{
		private const string ModId = "com.rounds.willuwontu.ActionHelper";

		private const string ModName = "Action Helper";

		public const string Version = "0.0.1";

		public static ActionHelper instance { get; private set; }

		private void Awake()
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			instance = this;
			new Harmony("com.rounds.willuwontu.ActionHelper").PatchAll();
		}

		private void Start()
		{
		}
	}
	public static class PlayerActionManager
	{
		private static Dictionary<string, ActionInfo> registeredActions = new Dictionary<string, ActionInfo>();

		public static ReadOnlyDictionary<string, ActionInfo> RegisteredActions => new ReadOnlyDictionary<string, ActionInfo>(registeredActions);

		public static void RegisterPlayerAction(ActionInfo actionInfo)
		{
			if (registeredActions.ContainsKey(actionInfo.name))
			{
				Debug.LogWarning((object)("An action called " + actionInfo.name + " was already registered, no new action is registered."));
			}
			else
			{
				registeredActions.Add(actionInfo.name, actionInfo);
			}
		}
	}
	public class ActionInfo
	{
		public string name;

		public BindingSource keyboardlayout;

		public BindingSource controllerLayout;

		public ActionInfo(string name, BindingSource keyboardlayout = null, BindingSource controllerLayout = null)
		{
			this.name = name;
			this.keyboardlayout = keyboardlayout;
			this.controllerLayout = controllerLayout;
		}
	}
}
namespace PlayerActionsHelper.Patches
{
	[HarmonyPatch(typeof(PlayerActions))]
	internal class PlayerActions_Patch
	{
		[HarmonyPatch(typeof(PlayerActions))]
		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		[HarmonyPatch(new Type[] { })]
		[HarmonyPostfix]
		private static void CreateAction(PlayerActions __instance)
		{
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Expected O, but got Unknown
			foreach (string key in PlayerActionManager.RegisteredActions.Keys)
			{
				try
				{
					__instance.GetAdditionalData().actions[key] = (PlayerAction)typeof(PlayerActions).InvokeMember("CreatePlayerAction", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, __instance, new object[1] { key });
				}
				catch (Exception ex)
				{
					Debug.LogError((object)("Error thrown when attempting to create player action '" + key + "'."));
					Debug.LogException(ex);
					__instance.GetAdditionalData().actions[key] = ((PlayerActionSet)__instance).GetPlayerActionByName(key);
				}
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(PlayerActions), "CreateWithControllerBindings")]
		private static void SetControllerBinding(ref PlayerActions __result)
		{
			foreach (string key in PlayerActionManager.RegisteredActions.Keys)
			{
				try
				{
					BindingSource controllerLayout = PlayerActionManager.RegisteredActions[key].controllerLayout;
					if (controllerLayout != (BindingSource)null)
					{
						__result.GetAdditionalData().actions[key].AddDefaultBinding(controllerLayout);
					}
				}
				catch (Exception ex)
				{
					Debug.LogError((object)("Error thrown when attempting set default binding for player action '" + key + "'."));
					Debug.LogException(ex);
				}
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(PlayerActions), "CreateWithKeyboardBindings")]
		private static void SetKeyboardBinding(ref PlayerActions __result)
		{
			foreach (string key in PlayerActionManager.RegisteredActions.Keys)
			{
				try
				{
					BindingSource keyboardlayout = PlayerActionManager.RegisteredActions[key].keyboardlayout;
					if (keyboardlayout != (BindingSource)null)
					{
						__result.GetAdditionalData().actions[key].AddDefaultBinding(keyboardlayout);
					}
				}
				catch (Exception ex)
				{
					Debug.LogError((object)("Error thrown when attempting set default binding for player action '" + key + "'."));
					Debug.LogException(ex);
				}
			}
		}
	}
}
namespace PlayerActionsHelper.Extensions
{
	[Serializable]
	public class PlayerActionsAdditionalData
	{
		public Dictionary<string, PlayerAction> actions = new Dictionary<string, PlayerAction>();

		public PlayerActionsAdditionalData()
		{
			actions = new Dictionary<string, PlayerAction>();
		}
	}
	public static class PlayerActionsExtension
	{
		public static readonly ConditionalWeakTable<PlayerActions, PlayerActionsAdditionalData> data = new ConditionalWeakTable<PlayerActions, PlayerActionsAdditionalData>();

		internal static PlayerActionsAdditionalData GetAdditionalData(this PlayerActions playerActions)
		{
			return data.GetOrCreateValue(playerActions);
		}

		public static PlayerAction GetPlayerAction(this PlayerActions playerActions, string name)
		{
			if (playerActions.GetAdditionalData().actions.TryGetValue(name, out var value))
			{
				return value;
			}
			Debug.LogError((object)("Attempting to access a Player Action by the name of " + name + " that doesn't exist, returning null instead."));
			return null;
		}

		public static bool ActionWasPressed(this PlayerActions playerActions, string name)
		{
			Dictionary<string, PlayerAction> actions = playerActions.GetAdditionalData().actions;
			if (actions.ContainsKey(name))
			{
				return ((OneAxisInputControl)actions[name]).WasPressed;
			}
			Debug.LogError((object)("Attempting to access a Player Action by the name of " + name + " that doesn't exist, returning false instead."));
			return false;
		}

		public static bool ActionIsPressed(this PlayerActions playerActions, string name)
		{
			Dictionary<string, PlayerAction> actions = playerActions.GetAdditionalData().actions;
			if (actions.ContainsKey(name))
			{
				return ((OneAxisInputControl)actions[name]).IsPressed;
			}
			Debug.LogError((object)("Attempting to access a Player Action by the name of " + name + " that doesn't exist, returning false instead."));
			return false;
		}

		public static bool ActionWasReleased(this PlayerActions playerActions, string name)
		{
			Dictionary<string, PlayerAction> actions = playerActions.GetAdditionalData().actions;
			if (actions.ContainsKey(name))
			{
				return ((OneAxisInputControl)actions[name]).WasReleased;
			}
			Debug.LogError((object)("Attempting to access a Player Action by the name of " + name + " that doesn't exist, returning false instead."));
			return false;
		}
	}
}