Decompiled source of MeleeFixes v1.3.1

MeleeFixes.dll

Decompiled 3 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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("MeleeFixes")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Fixes buggy collision with the shovel and kitchen knife")]
[assembly: AssemblyFileVersion("1.3.0.0")]
[assembly: AssemblyInformationalVersion("1.3.0+4985e55b7c6c675dec5c0458d4995927a7b254d3")]
[assembly: AssemblyProduct("MeleeFixes")]
[assembly: AssemblyTitle("MeleeFixes")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.3.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 MeleeFixes
{
	[BepInPlugin("butterystancakes.lethalcompany.meleefixes", "Melee Fixes", "1.3.0")]
	public class Plugin : BaseUnityPlugin
	{
		private const string PLUGIN_GUID = "butterystancakes.lethalcompany.meleefixes";

		private const string PLUGIN_NAME = "Melee Fixes";

		private const string PLUGIN_VERSION = "1.3.0";

		internal static ManualLogSource Logger;

		private void Awake()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			Logger = ((BaseUnityPlugin)this).Logger;
			new Harmony("butterystancakes.lethalcompany.meleefixes").PatchAll();
			Logger.LogInfo((object)"Melee Fixes v1.3.0 loaded");
		}
	}
	[HarmonyPatch]
	internal class MeleeFixesPatches
	{
		private static readonly MethodInfo GAME_OBJECT_LAYER = AccessTools.DeclaredPropertyGetter(typeof(GameObject), "layer");

		private static readonly MethodInfo RAYCAST_HIT_TRANSFORM = AccessTools.DeclaredPropertyGetter(typeof(RaycastHit), "transform");

		private static readonly MethodInfo RAYCAST_HIT_COLLIDER = AccessTools.DeclaredPropertyGetter(typeof(RaycastHit), "collider");

		private static readonly MethodInfo COLLIDER_IS_TRIGGER = AccessTools.DeclaredPropertyGetter(typeof(Collider), "isTrigger");

		private static readonly FieldInfo OBJECTS_HIT_BY_SHOVEL_LIST = AccessTools.Field(typeof(Shovel), "objectsHitByShovelList");

		private static readonly FieldInfo OBJECTS_HIT_BY_KNIFE_LIST = AccessTools.Field(typeof(KnifeItem), "objectsHitByKnifeList");

		private static readonly MethodInfo FIND_OBJECT_OF_TYPE_ROUND_MANAGER = AccessTools.Method(typeof(Object), "FindObjectOfType", (Type[])null, new Type[1] { typeof(RoundManager) });

		private static readonly MethodInfo ROUND_MANAGER_INSTANCE = AccessTools.DeclaredPropertyGetter(typeof(RoundManager), "Instance");

		private static readonly MethodInfo MELEE_HELPER_FILTER_TARGETS = AccessTools.Method(typeof(MeleeHelper), "FilterTargets", (Type[])null, (Type[])null);

		[HarmonyPatch(typeof(Shovel), "HitShovel")]
		[HarmonyPriority(800)]
		[HarmonyPatch(typeof(KnifeItem), "HitKnife")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> TransHit(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Expected O, but got Unknown
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: Expected O, but got Unknown
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Expected O, but got Unknown
			//IL_01a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ac: Expected O, but got Unknown
			//IL_01fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0203: Expected O, but got Unknown
			//IL_020f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0215: Expected O, but got Unknown
			//IL_022a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0230: Expected O, but got Unknown
			List<CodeInstruction> list = instructions.ToList();
			bool flag = true;
			bool flag2 = false;
			for (int i = 0; i < list.Count; i++)
			{
				if (!flag2 && list[i].opcode == OpCodes.Stfld)
				{
					FieldInfo fieldInfo = (FieldInfo)list[i].operand;
					if (fieldInfo == OBJECTS_HIT_BY_SHOVEL_LIST || fieldInfo == OBJECTS_HIT_BY_KNIFE_LIST)
					{
						list.InsertRange(i + 1, new <>z__ReadOnlyArray<CodeInstruction>((CodeInstruction[])(object)new CodeInstruction[3]
						{
							new CodeInstruction(OpCodes.Ldarg_0, (object)null),
							new CodeInstruction(OpCodes.Ldflda, list[i].operand),
							new CodeInstruction(OpCodes.Call, (object)MELEE_HELPER_FILTER_TARGETS)
						}));
						Plugin.Logger.LogDebug((object)"Transpiler: Filter targets by validity");
						flag2 = true;
						if (fieldInfo == OBJECTS_HIT_BY_KNIFE_LIST)
						{
							flag = false;
						}
					}
				}
				else if (!flag && list[i].opcode == OpCodes.Callvirt && (MethodInfo)list[i].operand == GAME_OBJECT_LAYER && list[i + 1].opcode == OpCodes.Ldc_I4_S && (sbyte)list[i + 1].operand == 11)
				{
					for (int num = i - 1; num >= 0; num--)
					{
						if (list[num].opcode == OpCodes.Ldarg_0)
						{
							List<CodeInstruction> list2 = new List<CodeInstruction>();
							for (int j = num; list[j].opcode != OpCodes.Call || (MethodInfo)list[j].operand != RAYCAST_HIT_TRANSFORM; j++)
							{
								list2.Add(new CodeInstruction(list[j].opcode, list[j].operand));
							}
							list2.AddRange(new <>z__ReadOnlyArray<CodeInstruction>((CodeInstruction[])(object)new CodeInstruction[3]
							{
								new CodeInstruction(OpCodes.Call, (object)RAYCAST_HIT_COLLIDER),
								new CodeInstruction(OpCodes.Callvirt, (object)COLLIDER_IS_TRIGGER),
								new CodeInstruction(OpCodes.Brtrue, list[i + 2].operand)
							}));
							for (int num2 = num - 1; num2 >= 0; num2--)
							{
								if (list[num2].opcode == OpCodes.Ldc_I4_8)
								{
									Label item = (Label)list[num2 + 1].operand;
									for (int k = i; k < list.Count; k++)
									{
										if (list[k].labels.Contains(item))
										{
											list[k].labels.Remove(item);
											list2[0].labels.Add(item);
											list.InsertRange(i + 3, list2);
											Plugin.Logger.LogDebug((object)"Transpiler (Knife): Add isTrigger check after layer checks");
											flag = true;
											break;
										}
									}
								}
								if (flag)
								{
									break;
								}
							}
						}
						if (flag)
						{
							break;
						}
					}
				}
				else if (list[i].opcode == OpCodes.Call && (MethodInfo)list[i].operand == FIND_OBJECT_OF_TYPE_ROUND_MANAGER)
				{
					list[i].operand = ROUND_MANAGER_INSTANCE;
					Plugin.Logger.LogDebug((object)"Transpiler: Replace FindObjectOfType<RoundManager>() with RoundManager.Instance");
				}
			}
			return list;
		}

		[HarmonyPatch(typeof(Shovel), "ItemActivate")]
		[HarmonyPrefix]
		private static void ShovelPreItemActivate(Shovel __instance, PlayerControllerB ___previousPlayerHeldBy, ref bool ___reelingUp)
		{
			if (___reelingUp && (Object)(object)___previousPlayerHeldBy != (Object)(object)((GrabbableObject)__instance).playerHeldBy)
			{
				___reelingUp = false;
				Plugin.Logger.LogInfo((object)"Reset broken shovel to allow swinging it again");
			}
		}
	}
	internal static class MeleeHelper
	{
		internal static void FilterTargets(ref List<RaycastHit> hits)
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			HashSet<IHittable> hashSet = new HashSet<IHittable>();
			IHittable val2 = default(IHittable);
			for (int i = 0; i < hits.Count; i++)
			{
				RaycastHit val = hits[i];
				if (!((Component)((RaycastHit)(ref val)).transform).TryGetComponent<IHittable>(ref val2))
				{
					continue;
				}
				if (!hashSet.Add(val2))
				{
					hits.RemoveAt(i--);
					continue;
				}
				EnemyAICollisionDetect val3 = (EnemyAICollisionDetect)(object)((val2 is EnemyAICollisionDetect) ? val2 : null);
				if ((Object)(object)val3 != (Object)null && val3.onlyCollideWhenGrounded)
				{
					hits.RemoveAt(i--);
				}
			}
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "MeleeFixes";

		public const string PLUGIN_NAME = "MeleeFixes";

		public const string PLUGIN_VERSION = "1.3.0";
	}
}
internal sealed class <>z__ReadOnlyArray<T> : IEnumerable, ICollection, IList, IEnumerable<T>, IReadOnlyCollection<T>, IReadOnlyList<T>, ICollection<T>, IList<T>
{
	int ICollection.Count => _items.Length;

	bool ICollection.IsSynchronized => false;

	object ICollection.SyncRoot => this;

	object IList.this[int index]
	{
		get
		{
			return _items[index];
		}
		set
		{
			throw new NotSupportedException();
		}
	}

	bool IList.IsFixedSize => true;

	bool IList.IsReadOnly => true;

	int IReadOnlyCollection<T>.Count => _items.Length;

	T IReadOnlyList<T>.this[int index] => _items[index];

	int ICollection<T>.Count => _items.Length;

	bool ICollection<T>.IsReadOnly => true;

	T IList<T>.this[int index]
	{
		get
		{
			return _items[index];
		}
		set
		{
			throw new NotSupportedException();
		}
	}

	public <>z__ReadOnlyArray(T[] items)
	{
		_items = items;
	}

	IEnumerator IEnumerable.GetEnumerator()
	{
		return ((IEnumerable)_items).GetEnumerator();
	}

	void ICollection.CopyTo(Array array, int index)
	{
		((ICollection)_items).CopyTo(array, index);
	}

	int IList.Add(object value)
	{
		throw new NotSupportedException();
	}

	void IList.Clear()
	{
		throw new NotSupportedException();
	}

	bool IList.Contains(object value)
	{
		return ((IList)_items).Contains(value);
	}

	int IList.IndexOf(object value)
	{
		return ((IList)_items).IndexOf(value);
	}

	void IList.Insert(int index, object value)
	{
		throw new NotSupportedException();
	}

	void IList.Remove(object value)
	{
		throw new NotSupportedException();
	}

	void IList.RemoveAt(int index)
	{
		throw new NotSupportedException();
	}

	IEnumerator<T> IEnumerable<T>.GetEnumerator()
	{
		return ((IEnumerable<T>)_items).GetEnumerator();
	}

	void ICollection<T>.Add(T item)
	{
		throw new NotSupportedException();
	}

	void ICollection<T>.Clear()
	{
		throw new NotSupportedException();
	}

	bool ICollection<T>.Contains(T item)
	{
		return ((ICollection<T>)_items).Contains(item);
	}

	void ICollection<T>.CopyTo(T[] array, int arrayIndex)
	{
		((ICollection<T>)_items).CopyTo(array, arrayIndex);
	}

	bool ICollection<T>.Remove(T item)
	{
		throw new NotSupportedException();
	}

	int IList<T>.IndexOf(T item)
	{
		return ((IList<T>)_items).IndexOf(item);
	}

	void IList<T>.Insert(int index, T item)
	{
		throw new NotSupportedException();
	}

	void IList<T>.RemoveAt(int index)
	{
		throw new NotSupportedException();
	}
}