Decompiled source of AOEMining v1.0.0

plugins/AOEMining.dll

Decompiled 16 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 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("AOEMining")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("AOEMining")]
[assembly: AssemblyTitle("AOEMining")]
[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 AOEMining
{
	[BepInPlugin("AOEMining", "AOEMining", "1.0.0")]
	public sealed class AOEMiningPlugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(MineRock5), "Damage")]
		private static class MineRock5DamagePatch
		{
			private static bool Prefix(MineRock5 __instance, HitData hit)
			{
				if (_redistributingMineRock5 || !IsShiftHeld())
				{
					return true;
				}
				if (!IsPickaxeHit(hit) || !IsLocalPlayerHit(hit))
				{
					return true;
				}
				if (!IsAllowedRock((MonoBehaviour)(object)__instance))
				{
					return true;
				}
				List<Collider> list = FindTargets(hit);
				if (list.Count <= 1)
				{
					return true;
				}
				float pickaxeDamage = hit.m_damage.m_pickaxe / (float)list.Count;
				_redistributingMineRock5 = true;
				try
				{
					foreach (Collider item in list)
					{
						MonoBehaviour rock = GetRock(item);
						if ((Object)(object)rock == (Object)null)
						{
							continue;
						}
						HitData val = CreateDistributedHit(hit, pickaxeDamage, item);
						MineRock5 val2 = (MineRock5)(object)((rock is MineRock5) ? rock : null);
						if ((Object)(object)val2 != (Object)null)
						{
							val2.Damage(val);
							continue;
						}
						MineRock val3 = (MineRock)(object)((rock is MineRock) ? rock : null);
						if ((Object)(object)val3 != (Object)null)
						{
							val3.Damage(val);
						}
					}
				}
				finally
				{
					_redistributingMineRock5 = false;
				}
				return false;
			}
		}

		[HarmonyPatch(typeof(MineRock), "Damage")]
		private static class MineRockDamagePatch
		{
			private static bool Prefix(MineRock __instance, HitData hit)
			{
				if (_redistributingMineRock || !IsShiftHeld())
				{
					return true;
				}
				if (!IsPickaxeHit(hit) || !IsLocalPlayerHit(hit))
				{
					return true;
				}
				if (!IsAllowedRock((MonoBehaviour)(object)__instance))
				{
					return true;
				}
				List<Collider> list = FindTargets(hit);
				if (list.Count <= 1)
				{
					return true;
				}
				float pickaxeDamage = hit.m_damage.m_pickaxe / (float)list.Count;
				_redistributingMineRock = true;
				try
				{
					foreach (Collider item in list)
					{
						MonoBehaviour rock = GetRock(item);
						if ((Object)(object)rock == (Object)null)
						{
							continue;
						}
						HitData val = CreateDistributedHit(hit, pickaxeDamage, item);
						MineRock5 val2 = (MineRock5)(object)((rock is MineRock5) ? rock : null);
						if ((Object)(object)val2 != (Object)null)
						{
							val2.Damage(val);
							continue;
						}
						MineRock val3 = (MineRock)(object)((rock is MineRock) ? rock : null);
						if ((Object)(object)val3 != (Object)null)
						{
							val3.Damage(val);
						}
					}
				}
				finally
				{
					_redistributingMineRock = false;
				}
				return false;
			}
		}

		public const string PluginGuid = "AOEMining";

		public const string PluginName = "AOEMining";

		public const string PluginVersion = "1.0.0";

		private static readonly HashSet<string> AllowedTokens = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
		{
			"stone", "rock", "copper", "tin", "silver", "flametal", "blackmarble", "black_marble", "black marble", "marble",
			"ancient sword", "ancientsword", "sword", "swordmistlands", "swordmist", "mistlands"
		};

		private static readonly string[] ExplicitExclusions = new string[6] { "blackmetal", "black_metal", "obsidian", "muddy", "muddy_scrap", "scrapiron" };

		private static bool _redistributingMineRock;

		private static bool _redistributingMineRock5;

		private void Awake()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Expected O, but got Unknown
			Harmony val = new Harmony("AOEMining");
			val.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"AOEMining 1.0.0 loaded. Hold Left Shift for AOE mining.");
		}

		private static bool IsShiftHeld()
		{
			return Input.GetKey((KeyCode)304);
		}

		private static float GetRadius()
		{
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null)
			{
				return 1f;
			}
			float num;
			try
			{
				num = ((Character)localPlayer).GetSkillFactor((SkillType)12);
			}
			catch
			{
				num = 0f;
			}
			return Mathf.Lerp(1f, 4f, Mathf.Clamp01(num));
		}

		private static bool IsPickaxeHit(HitData hit)
		{
			return hit != null && hit.m_damage.m_pickaxe > 0f;
		}

		private static bool IsLocalPlayerHit(HitData hit)
		{
			if ((Object)(object)Player.m_localPlayer == (Object)null)
			{
				return false;
			}
			try
			{
				return (Object)(object)hit.GetAttacker() == (Object)(object)Player.m_localPlayer;
			}
			catch
			{
				return false;
			}
		}

		private static string GetIdentity(MonoBehaviour rock)
		{
			if ((Object)(object)rock == (Object)null)
			{
				return string.Empty;
			}
			string text = ((Object)rock).name ?? string.Empty;
			string text2 = (((Object)(object)((Component)rock).gameObject != (Object)null) ? (((Object)((Component)rock).gameObject).name ?? string.Empty) : string.Empty);
			string text3 = string.Empty;
			MineRock val = (MineRock)(object)((rock is MineRock) ? rock : null);
			if ((Object)(object)val != (Object)null)
			{
				text3 = val.m_name ?? string.Empty;
			}
			MineRock5 val2 = (MineRock5)(object)((rock is MineRock5) ? rock : null);
			if ((Object)(object)val2 != (Object)null)
			{
				text3 = val2.m_name ?? string.Empty;
			}
			return (text + " | " + text2 + " | " + text3).ToLowerInvariant();
		}

		private static bool IsAllowedRock(MonoBehaviour rock)
		{
			string identity = GetIdentity(rock);
			string[] explicitExclusions = ExplicitExclusions;
			foreach (string value in explicitExclusions)
			{
				if (identity.Contains(value))
				{
					return false;
				}
			}
			if (identity.Contains("sword"))
			{
				return true;
			}
			foreach (string allowedToken in AllowedTokens)
			{
				if (identity.Contains(allowedToken))
				{
					return true;
				}
			}
			return false;
		}

		private static MonoBehaviour GetRock(Collider collider)
		{
			if ((Object)(object)collider == (Object)null)
			{
				return null;
			}
			MineRock5 componentInParent = ((Component)collider).GetComponentInParent<MineRock5>();
			if ((Object)(object)componentInParent != (Object)null)
			{
				return (MonoBehaviour)(object)componentInParent;
			}
			return (MonoBehaviour)(object)((Component)collider).GetComponentInParent<MineRock>();
		}

		private static Vector3 GetHitPoint(HitData hit)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			return hit.m_point;
		}

		private static List<Collider> FindTargets(HitData originalHit)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			List<Collider> list = new List<Collider>();
			HashSet<int> hashSet = new HashSet<int>();
			float radius = GetRadius();
			Vector3 hitPoint = GetHitPoint(originalHit);
			Collider[] array = Physics.OverlapSphere(hitPoint, radius);
			Collider[] array2 = array;
			foreach (Collider val in array2)
			{
				if ((Object)(object)val == (Object)null)
				{
					continue;
				}
				MonoBehaviour rock = GetRock(val);
				if ((Object)(object)rock == (Object)null || !IsAllowedRock(rock))
				{
					continue;
				}
				Vector3 val2 = val.ClosestPoint(hitPoint);
				Vector3 val3 = val2 - hitPoint;
				if (!(((Vector3)(ref val3)).sqrMagnitude > radius * radius))
				{
					int instanceID = ((Object)val).GetInstanceID();
					if (hashSet.Add(instanceID))
					{
						list.Add(val);
					}
				}
			}
			if ((Object)(object)originalHit.m_hitCollider != (Object)null && hashSet.Add(((Object)originalHit.m_hitCollider).GetInstanceID()))
			{
				list.Insert(0, originalHit.m_hitCollider);
			}
			return list;
		}

		private static HitData CreateDistributedHit(HitData original, float pickaxeDamage, Collider target)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Expected O, but got Unknown
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			HitData val = new HitData();
			val.m_damage.m_pickaxe = pickaxeDamage;
			val.m_point = (((Object)(object)target != (Object)null) ? target.ClosestPoint(original.m_point) : original.m_point);
			val.m_hitCollider = target;
			val.m_dir = original.m_dir;
			val.m_attacker = original.m_attacker;
			val.m_pushForce = original.m_pushForce;
			val.m_toolTier = original.m_toolTier;
			return val;
		}
	}
}