Decompiled source of MeleeRange v1.0.1

HowToFish.MeleeRange.dll

Decompiled 15 hours 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 HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("HowToFish.MeleeRange")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("HowToFish.MeleeRange")]
[assembly: AssemblyTitle("HowToFish.MeleeRange")]
[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]
	[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 HowToFish.MeleeRange
{
	[BepInPlugin("community.howtofish.meleerange", "Melee Range", "0.2.0")]
	[BepInProcess("How to Fish.exe")]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(PlayerPunching), "StartPunching")]
		private static class PatchPunchStretch
		{
			private static void Postfix(PlayerPunching __instance, int side)
			{
				//IL_0033: Unknown result type (might be due to invalid IL or missing references)
				//IL_0039: Expected O, but got Unknown
				//IL_0096: Unknown result type (might be due to invalid IL or missing references)
				//IL_009c: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
				//IL_00da: Unknown result type (might be due to invalid IL or missing references)
				//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
				//IL_0100: Unknown result type (might be due to invalid IL or missing references)
				//IL_0107: Expected O, but got Unknown
				//IL_0126: Unknown result type (might be due to invalid IL or missing references)
				//IL_012d: Expected O, but got Unknown
				if (!EnableStretch.Value)
				{
					return;
				}
				Player val = (Player)AccessTools.Field(typeof(PlayerPunching), "_player").GetValue(__instance);
				if ((Object)(object)val == (Object)null || ((Object)(object)val.Holding != (Object)null && (Object)(object)val.Holding.HeldItem != (Object)null))
				{
					return;
				}
				float value = Traverse.Create((object)__instance).Field("_range").GetValue<float>();
				Transform camObject = val.CamObject;
				Vector3 position = camObject.position + camObject.forward * Mathf.Max(value, 2f);
				Transform[] array = GetVirtual(val);
				if (side == 0)
				{
					array[0].position = position;
				}
				else
				{
					array[1].position = position;
				}
				Transform val2 = (Transform)AccessTools.Field(typeof(PlayerHands), "_handBoneRight").GetValue(val.Hands);
				Transform val3 = (Transform)AccessTools.Field(typeof(PlayerHands), "_handBoneLeft").GetValue(val.Hands);
				if (!((Object)(object)val2 == (Object)null) && !((Object)(object)val3 == (Object)null))
				{
					if (side == 0)
					{
						val.Arms.SetIKTarget(array[0], val3);
					}
					else
					{
						val.Arms.SetIKTarget(val2, array[1]);
					}
					_helper.RequestRestore(val, 0.4f);
				}
			}
		}

		public class StretchHelper : MonoBehaviour
		{
			private readonly List<(Player p, float t)> _pending = new List<(Player, float)>();

			public void RequestRestore(Player p, float delay)
			{
				_pending.Add((p, Time.time + delay));
			}

			private void Update()
			{
				//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
				//IL_00a8: Expected O, but got Unknown
				//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ce: Expected O, but got Unknown
				for (int num = _pending.Count - 1; num >= 0; num--)
				{
					if (Time.time >= _pending[num].t)
					{
						Player item = _pending[num].p;
						_pending.RemoveAt(num);
						if ((Object)(object)item != (Object)null && (Object)(object)item.Hands != (Object)null && (Object)(object)item.Arms != (Object)null)
						{
							Transform val = (Transform)AccessTools.Field(typeof(PlayerHands), "_handBoneRight").GetValue(item.Hands);
							Transform val2 = (Transform)AccessTools.Field(typeof(PlayerHands), "_handBoneLeft").GetValue(item.Hands);
							if ((Object)(object)val != (Object)null && (Object)(object)val2 != (Object)null)
							{
								item.Arms.SetIKTarget(val, val2);
							}
						}
					}
				}
			}
		}

		[HarmonyPatch(typeof(PlayerPunching), "CheckForItems")]
		private static class PatchPunchItems
		{
			private static void Prefix(PlayerPunching __instance)
			{
				Boost(__instance);
			}

			private static void Postfix(PlayerPunching __instance)
			{
				Restore(__instance);
			}
		}

		[HarmonyPatch(typeof(PlayerPunching), "CheckForPlayersAndLevel")]
		private static class PatchPunchPlayers
		{
			private static void Prefix(PlayerPunching __instance)
			{
				Boost(__instance);
			}

			private static void Postfix(PlayerPunching __instance)
			{
				Restore(__instance);
			}
		}

		[HarmonyPatch(typeof(Melee), "CheckForItems")]
		private static class PatchMeleeItems
		{
			private static void Prefix(Melee __instance)
			{
				Boost(__instance);
			}

			private static void Postfix(Melee __instance)
			{
				Restore(__instance);
			}
		}

		[HarmonyPatch(typeof(Melee), "CheckForPlayersAndLevel")]
		private static class PatchMeleePlayers
		{
			private static void Prefix(Melee __instance)
			{
				Boost(__instance);
			}

			private static void Postfix(Melee __instance)
			{
				Restore(__instance);
			}
		}

		private static ConfigEntry<float> RangeMultiplier;

		private static ConfigEntry<bool> EnableStretch;

		private static readonly Dictionary<object, float> _baseRange = new Dictionary<object, float>();

		private static readonly Dictionary<Player, Transform[]> _virtual = new Dictionary<Player, Transform[]>();

		private static StretchHelper _helper;

		private void Awake()
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Expected O, but got Unknown
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Expected O, but got Unknown
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Expected O, but got Unknown
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Expected O, but got Unknown
			RangeMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("General", "RangeMultiplier", 3f, new ConfigDescription("近战攻击距离倍数 (1 = 原版)。影响拳击与近战武器。", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 10f), Array.Empty<object>()));
			EnableStretch = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableStretch", true, new ConfigDescription("空手出拳时把手臂伸到攻击距离处(视觉拉长)。", (AcceptableValueBase)null, Array.Empty<object>()));
			GameObject val = new GameObject("MeleeRangeStretch");
			_helper = val.AddComponent<StretchHelper>();
			Object.DontDestroyOnLoad((Object)(object)val);
			Harmony val2 = new Harmony("community.howtofish.meleerange");
			val2.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)$"Melee Range loaded. Multiplier={RangeMultiplier.Value} Stretch={EnableStretch.Value}");
		}

		private static void Boost(object inst)
		{
			if (!_baseRange.ContainsKey(inst))
			{
				_baseRange[inst] = Traverse.Create(inst).Field("_range").GetValue<float>();
			}
			Traverse.Create(inst).Field("_range").SetValue((object)(_baseRange[inst] * RangeMultiplier.Value));
		}

		private static void Restore(object inst)
		{
			if (_baseRange.ContainsKey(inst))
			{
				Traverse.Create(inst).Field("_range").SetValue((object)_baseRange[inst]);
			}
		}

		private static Transform[] GetVirtual(Player p)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			if (!_virtual.TryGetValue(p, out var value))
			{
				value = (Transform[])(object)new Transform[2]
				{
					new GameObject("VirtR").transform,
					default(Transform)
				};
				value[0].SetParent(((Component)p).transform);
				value[1] = new GameObject("VirtL").transform;
				value[1].SetParent(((Component)p).transform);
				_virtual[p] = value;
			}
			return value;
		}
	}
}