Decompiled source of ClickPunch v1.1.1

BepInEx/plugins/ClickPunch/ClickPunch.dll

Decompiled an hour ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using FishNet.Object;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.InputSystem;

[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("ClickPunch")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ClickPunch")]
[assembly: AssemblyTitle("ClickPunch")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[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 ClickPunch
{
	[BepInPlugin("local.howtofish.clickpunch", "ClickPunch", "1.1.1")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "local.howtofish.clickpunch";

		public const string Name = "ClickPunch";

		public const string Version = "1.1.1";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<bool> BrassKnuckles;

		internal static ConfigEntry<bool> AllMeleeWeapons;

		internal static ConfigEntry<bool> CookEnabled;

		internal static ConfigEntry<int> CookClicks;

		internal static ConfigEntry<float> CookWindowSeconds;

		private void Awake()
		{
			//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Punch once per click with no cooldown. Each click instantly lands any punch still mid-swing, then throws the next one.");
			BrassKnuckles = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "BrassKnuckles", true, "Brass knuckles also attack once per click with no cooldown.");
			AllMeleeWeapons = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AllMeleeWeapons", false, "Apply the no-cooldown clicking to every melee weapon, not just brass knuckles.");
			CookEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Cooking", "CookWithClicks", true, "Click fast enough while hitting a fish and it gets cooked.");
			CookClicks = ((BaseUnityPlugin)this).Config.Bind<int>("Cooking", "Clicks", 15, "How many clicks are needed inside the time window to cook the fish you are hitting.");
			CookWindowSeconds = ((BaseUnityPlugin)this).Config.Bind<float>("Cooking", "WindowSeconds", 3f, "The time window, in seconds, the clicks have to fit inside.");
			if (!Access.Resolve())
			{
				Log.LogError((object)"PlayerPunching or Melee does not look the way this mod expects (game update?). ClickPunch is disabled.");
				return;
			}
			new Harmony("local.howtofish.clickpunch").PatchAll(typeof(Patches));
			Log.LogInfo((object)string.Format("{0} {1} loaded: attack speed follows click speed; {2} clicks in {3}s cooks a fish.", "ClickPunch", "1.1.1", CookClicks.Value, CookWindowSeconds.Value));
		}
	}
	internal static class Access
	{
		internal static FieldRef<PlayerPunching, Player> PunchPlayer;

		internal static FieldRef<PlayerPunching, bool[]> PunchAttacking;

		internal static FieldRef<PlayerPunching, float[]> PunchPercent;

		internal static FieldRef<PlayerPunching, Transform[]> PunchTarget;

		internal static FieldRef<PlayerPunching, int> PunchLastSide;

		internal static FieldRef<PlayerPunching, bool> PunchHolding;

		internal static FieldRef<PlayerPunching, bool> PunchQueued;

		internal static Action<PlayerPunching, bool, int> PunchFind;

		internal static Action<PlayerPunching, int, bool> PunchReturn;

		internal static FieldRef<Item, Player> ItemHolder;

		internal static FieldRef<Melee, bool[]> MeleeAttacking;

		internal static FieldRef<Melee, float[]> MeleePercent;

		internal static FieldRef<Melee, Transform[]> MeleeTarget;

		internal static FieldRef<Melee, int> MeleeLastSide;

		internal static FieldRef<Melee, bool> MeleeHolding;

		internal static FieldRef<Melee, bool> MeleeQueued;

		internal static Action<Melee, bool, int> MeleeFind;

		internal static Action<Melee, int, bool> MeleeReturn;

		internal static bool Resolve()
		{
			try
			{
				PunchPlayer = AccessTools.FieldRefAccess<PlayerPunching, Player>("_player");
				PunchAttacking = AccessTools.FieldRefAccess<PlayerPunching, bool[]>("_isAttacking");
				PunchPercent = AccessTools.FieldRefAccess<PlayerPunching, float[]>("_curAttackPercent");
				PunchTarget = AccessTools.FieldRefAccess<PlayerPunching, Transform[]>("_curTarget");
				PunchLastSide = AccessTools.FieldRefAccess<PlayerPunching, int>("_lastPunchSide");
				PunchHolding = AccessTools.FieldRefAccess<PlayerPunching, bool>("_holdingPunchInput");
				PunchQueued = AccessTools.FieldRefAccess<PlayerPunching, bool>("_queuedPunch");
				PunchFind = AccessTools.MethodDelegate<Action<PlayerPunching, bool, int>>(AccessTools.Method(typeof(PlayerPunching), "FindPunchTarget", new Type[2]
				{
					typeof(bool),
					typeof(int)
				}, (Type[])null), (object)null, true);
				PunchReturn = AccessTools.MethodDelegate<Action<PlayerPunching, int, bool>>(AccessTools.Method(typeof(PlayerPunching), "StartReturning", new Type[2]
				{
					typeof(int),
					typeof(bool)
				}, (Type[])null), (object)null, true);
				ItemHolder = AccessTools.FieldRefAccess<Item, Player>("_holder");
				MeleeAttacking = AccessTools.FieldRefAccess<Melee, bool[]>("_isAttacking");
				MeleePercent = AccessTools.FieldRefAccess<Melee, float[]>("_curAttackPercent");
				MeleeTarget = AccessTools.FieldRefAccess<Melee, Transform[]>("_curTarget");
				MeleeLastSide = AccessTools.FieldRefAccess<Melee, int>("_lastAttackSide");
				MeleeHolding = AccessTools.FieldRefAccess<Melee, bool>("_holdingPrimaryInput");
				MeleeQueued = AccessTools.FieldRefAccess<Melee, bool>("_queuedAttack");
				MeleeFind = AccessTools.MethodDelegate<Action<Melee, bool, int>>(AccessTools.Method(typeof(Melee), "FindAttackTarget", new Type[2]
				{
					typeof(bool),
					typeof(int)
				}, (Type[])null), (object)null, true);
				MeleeReturn = AccessTools.MethodDelegate<Action<Melee, int, bool>>(AccessTools.Method(typeof(Melee), "StartReturning", new Type[2]
				{
					typeof(int),
					typeof(bool)
				}, (Type[])null), (object)null, true);
				return PunchFind != null && PunchReturn != null && MeleeFind != null && MeleeReturn != null;
			}
			catch (Exception ex)
			{
				Plugin.Log.LogError((object)ex);
				return false;
			}
		}
	}
	internal static class ClickCook
	{
		private static readonly Queue<float> _clicks = new Queue<float>();

		private static Item _lastFish;

		private static float _lastFishTime = -999f;

		internal static void OnClick(Transform target)
		{
			if (!Plugin.CookEnabled.Value)
			{
				return;
			}
			float time = Time.time;
			float num = Mathf.Max(0.1f, Plugin.CookWindowSeconds.Value);
			int num2 = Mathf.Max(1, Plugin.CookClicks.Value);
			_clicks.Enqueue(time);
			while (_clicks.Count > 0 && time - _clicks.Peek() > num)
			{
				_clicks.Dequeue();
			}
			Item val = (Object.op_Implicit((Object)(object)target) ? ItemManager.Get(target) : null);
			if (Object.op_Implicit((Object)(object)val) && IsFish(val))
			{
				_lastFish = val;
				_lastFishTime = time;
			}
			if (_clicks.Count >= num2)
			{
				_clicks.Clear();
				if (Object.op_Implicit((Object)(object)_lastFish) && !(time - _lastFishTime > num))
				{
					Cook(_lastFish);
				}
			}
		}

		private static bool IsFish(Item item)
		{
			if (!Object.op_Implicit((Object)(object)item.Fish))
			{
				return Object.op_Implicit((Object)(object)item.Creature);
			}
			return true;
		}

		private static void Cook(Item item)
		{
			if (!((NetworkBehaviour)item).IsDeinitializing)
			{
				if (item.Cookness >= 1f)
				{
					Plugin.Log.LogInfo((object)("'" + item.GetName() + "' is already cooked."));
					return;
				}
				Server.Instance.GrillItemInLava(item);
				Plugin.Log.LogInfo((object)$"Cooked '{item.GetName()}' with {Plugin.CookClicks.Value} clicks.");
			}
		}
	}
	internal static class Patches
	{
		[HarmonyPatch(typeof(PlayerPunching), "PunchInput")]
		[HarmonyPrefix]
		private static bool PunchInputPrefix(PlayerPunching __instance, CallbackContext context)
		{
			if (!Plugin.Enabled.Value)
			{
				return true;
			}
			try
			{
				Player val = Access.PunchPlayer.Invoke(__instance);
				if ((Object)(object)val == (Object)null || val.BlockInputs)
				{
					return false;
				}
				if ((Object)(object)val.Holding != (Object)null && Object.op_Implicit((Object)(object)val.Holding.HeldItem))
				{
					return false;
				}
				if (Boat.IsDrivingLocally || Boat.WantToDrive)
				{
					return false;
				}
				Access.PunchHolding.Invoke(__instance) = true;
				bool[] array = Access.PunchAttacking.Invoke(__instance);
				float[] array2 = Access.PunchPercent.Invoke(__instance);
				for (int i = 0; i < array.Length; i++)
				{
					if (array[i])
					{
						array2[i] = 1f;
						Access.PunchReturn(__instance, i, arg3: false);
					}
				}
				Access.PunchQueued.Invoke(__instance) = false;
				Access.PunchFind(__instance, arg2: false, 0);
				Transform[] array3 = Access.PunchTarget.Invoke(__instance);
				int num = Access.PunchLastSide.Invoke(__instance);
				ClickCook.OnClick((num >= 0 && num < array3.Length) ? array3[num] : null);
				return false;
			}
			catch (Exception arg)
			{
				Plugin.Log.LogError((object)$"Click punch failed, falling back to vanilla: {arg}");
				return true;
			}
		}

		[HarmonyPatch(typeof(Melee), "PrimaryInput")]
		[HarmonyPrefix]
		private static bool MeleeInputPrefix(Melee __instance, CallbackContext context)
		{
			if (!Plugin.Enabled.Value || !Applies(__instance))
			{
				return true;
			}
			try
			{
				Player val = Access.ItemHolder.Invoke((Item)(object)__instance);
				if ((Object)(object)val == (Object)null || (Object)(object)val != (Object)(object)Player.LocalPlayer)
				{
					return true;
				}
				if (val.BlockInputs)
				{
					return false;
				}
				Access.MeleeHolding.Invoke(__instance) = true;
				bool[] array = Access.MeleeAttacking.Invoke(__instance);
				float[] array2 = Access.MeleePercent.Invoke(__instance);
				for (int i = 0; i < array.Length; i++)
				{
					if (array[i])
					{
						array2[i] = 1f;
						Access.MeleeReturn(__instance, i, arg3: false);
					}
				}
				Access.MeleeQueued.Invoke(__instance) = false;
				Access.MeleeFind(__instance, arg2: false, 0);
				Transform[] array3 = Access.MeleeTarget.Invoke(__instance);
				int num = Access.MeleeLastSide.Invoke(__instance);
				ClickCook.OnClick((num >= 0 && num < array3.Length) ? array3[num] : null);
				return false;
			}
			catch (Exception arg)
			{
				Plugin.Log.LogError((object)$"Click melee failed, falling back to vanilla: {arg}");
				return true;
			}
		}

		private static bool Applies(Melee melee)
		{
			if (Plugin.AllMeleeWeapons.Value)
			{
				return true;
			}
			if (!Plugin.BrassKnuckles.Value)
			{
				return false;
			}
			if ((((Item)melee).GetName() ?? ((Object)melee).name ?? "").IndexOf("knuckle", StringComparison.OrdinalIgnoreCase) < 0)
			{
				return ((Object)melee).name.IndexOf("knuckle", StringComparison.OrdinalIgnoreCase) >= 0;
			}
			return true;
		}
	}
}