Decompiled source of OreHitboxVisualizer v1.0.0

OreHitboxVisualizer.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Rendering;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("assembly_guiutils")]
[assembly: IgnoresAccessChecksTo("assembly_valheim")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("OreHitboxVisualizer")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+418888f5c566799456f00d0c246c1f0789b1a93b")]
[assembly: AssemblyProduct("OreHitboxVisualizer")]
[assembly: AssemblyTitle("OreHitboxVisualizer")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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 OreHitboxVisualizer
{
	internal static class HitboxManager
	{
		private const float RefreshInterval = 0.1f;

		private static readonly Dictionary<MineRock5, RockVisual> s_rocks = new Dictionary<MineRock5, RockVisual>();

		private static readonly List<MineRock5> s_toRemove = new List<MineRock5>();

		private static readonly Collider[] s_overlap = (Collider[])(object)new Collider[128];

		private static int s_mask;

		private static int s_terrainLayer = -1;

		private static float s_nextRefresh;

		private static GameObject s_root;

		private static Mesh s_cubeMesh;

		private static Material s_lineColliding;

		private static Material s_lineFree;

		private static Material s_depthMask;

		private const int CollidingLineQueue = 3989;

		private const int DepthMaskQueue = 3990;

		private const int FreeLineQueue = 3991;

		private static MineRock5 s_countRock;

		private static float s_countValidUntil;

		private static int s_countTotal;

		private static int s_countColliding;

		private static bool[] s_countAlive;

		private static Bounds[] s_countBounds;

		private static bool[] s_countRed;

		internal static Mesh CubeMesh
		{
			get
			{
				if ((Object)(object)s_cubeMesh == (Object)null)
				{
					s_cubeMesh = BuildUnitCube();
				}
				return s_cubeMesh;
			}
		}

		public static void OnRockHit(MineRock5 rock, HitData hit)
		{
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Invalid comparison between Unknown and I4
			if ((Object)(object)rock == (Object)null || rock.m_hitAreas == null || hit == null)
			{
				return;
			}
			Player localPlayer = Player.m_localPlayer;
			if (!((Object)(object)localPlayer == (Object)null) && !((Object)(object)hit.GetAttacker() != (Object)(object)localPlayer) && (!OreHitboxVisualizerPlugin.PickaxeOnly.Value || !(hit.m_damage.m_pickaxe <= 0f) || (int)hit.m_skill == 12))
			{
				if (!s_rocks.TryGetValue(rock, out var value))
				{
					ClearAll();
					value = new RockVisual(rock, GetRoot().transform);
					s_rocks[rock] = value;
					s_nextRefresh = 0f;
				}
				value.LastHitTime = Time.time;
			}
		}

		public static void Tick()
		{
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			if (s_rocks.Count == 0)
			{
				return;
			}
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null)
			{
				ClearAll();
				return;
			}
			float time = Time.time;
			float value = OreHitboxVisualizerPlugin.HideDistance.Value;
			float value2 = OreHitboxVisualizerPlugin.HideAfterSeconds.Value;
			Vector3 position = ((Component)localPlayer).transform.position;
			s_toRemove.Clear();
			foreach (KeyValuePair<MineRock5, RockVisual> s_rock in s_rocks)
			{
				MineRock5 key = s_rock.Key;
				if ((Object)(object)key == (Object)null || key.m_allDestroyed || Vector3.Distance(position, ((Component)key).transform.position) > value || (value2 > 0f && time - s_rock.Value.LastHitTime > value2))
				{
					s_toRemove.Add(key);
				}
			}
			foreach (MineRock5 item in s_toRemove)
			{
				s_rocks[item].Destroy();
				s_rocks.Remove(item);
			}
			bool value3 = OreHitboxVisualizerPlugin.Enabled.Value;
			GameObject root = GetRoot();
			if (root.activeSelf != value3)
			{
				root.SetActive(value3);
				s_nextRefresh = 0f;
			}
			if (!value3 || time < s_nextRefresh)
			{
				return;
			}
			s_nextRefresh = time + 0.1f;
			foreach (RockVisual value4 in s_rocks.Values)
			{
				value4.Refresh();
			}
		}

		public static void ClearAll()
		{
			foreach (RockVisual value in s_rocks.Values)
			{
				value.Destroy();
			}
			s_rocks.Clear();
		}

		public static void Shutdown()
		{
			ClearAll();
			OnAppearanceChanged();
			if ((Object)(object)s_cubeMesh != (Object)null)
			{
				Object.Destroy((Object)(object)s_cubeMesh);
				s_cubeMesh = null;
			}
			if ((Object)(object)s_root != (Object)null)
			{
				Object.Destroy((Object)(object)s_root);
				s_root = null;
			}
		}

		public static void OnAppearanceChanged()
		{
			DestroyMaterial(ref s_lineColliding);
			DestroyMaterial(ref s_lineFree);
			DestroyMaterial(ref s_depthMask);
			foreach (RockVisual value in s_rocks.Values)
			{
				value.DestroyAreas();
			}
			s_nextRefresh = 0f;
		}

		public static void GetNodeCounts(MineRock5 rock, out int total, out int colliding)
		{
			float time = Time.time;
			if ((Object)(object)rock == (Object)(object)s_countRock && time < s_countValidUntil)
			{
				total = s_countTotal;
				colliding = s_countColliding;
				return;
			}
			int count = rock.m_hitAreas.Count;
			if (s_countAlive == null || s_countAlive.Length != count)
			{
				s_countAlive = new bool[count];
				s_countBounds = (Bounds[])(object)new Bounds[count];
				s_countRed = new bool[count];
			}
			CollectAreas(rock, s_countAlive, s_countBounds);
			ComputeStates(rock, s_countAlive, s_countBounds, s_countRed);
			total = 0;
			colliding = 0;
			for (int i = 0; i < count; i++)
			{
				if (s_countAlive[i])
				{
					total++;
					if (s_countRed[i])
					{
						colliding++;
					}
				}
			}
			s_countRock = rock;
			s_countValidUntil = time + 0.1f;
			s_countTotal = total;
			s_countColliding = colliding;
		}

		internal static void CollectAreas(MineRock5 rock, bool[] alive, Bounds[] bounds)
		{
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			List<HitArea> hitAreas = rock.m_hitAreas;
			for (int i = 0; i < alive.Length; i++)
			{
				HitArea val = hitAreas[i];
				alive[i] = val.m_health > 0f && (Object)(object)val.m_collider != (Object)null && ((Component)val.m_collider).gameObject.activeInHierarchy;
				if (alive[i])
				{
					bounds[i] = val.m_collider.bounds;
				}
			}
		}

		internal static void ComputeStates(MineRock5 rock, bool[] alive, Bounds[] bounds, bool[] red)
		{
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_0156: Unknown result type (might be due to invalid IL or missing references)
			//IL_0163: Unknown result type (might be due to invalid IL or missing references)
			//IL_016d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			if (s_mask == 0)
			{
				s_mask = LayerMask.GetMask(new string[5] { "piece", "Default", "static_solid", "Default_small", "terrain" });
				s_terrainLayer = LayerMask.NameToLayer("terrain");
			}
			int num = alive.Length;
			for (int i = 0; i < num; i++)
			{
				red[i] = false;
			}
			if (OreHitboxVisualizerPlugin.Mode.Value == HitboxMode.Collision)
			{
				for (int j = 0; j < num; j++)
				{
					if (!alive[j])
					{
						continue;
					}
					Collider collider = rock.m_hitAreas[j].m_collider;
					int num2 = Physics.OverlapBoxNonAlloc(((Bounds)(ref bounds[j])).center, ((Bounds)(ref bounds[j])).extents, s_overlap, Quaternion.identity, s_mask);
					for (int k = 0; k < num2; k++)
					{
						if (IsValidContact(s_overlap[k], collider) && rock.GetAreaIndex(s_overlap[k]) < 0)
						{
							red[j] = true;
							break;
						}
					}
					if (!red[j] && IsBelowGround(bounds[j]))
					{
						red[j] = true;
					}
				}
				return;
			}
			for (int l = 0; l < 3; l++)
			{
				for (int m = 0; m < num; m++)
				{
					if (!alive[m] || red[m])
					{
						continue;
					}
					Collider collider2 = rock.m_hitAreas[m].m_collider;
					int num3 = Physics.OverlapBoxNonAlloc(((Bounds)(ref bounds[m])).center, ((Bounds)(ref bounds[m])).extents, s_overlap, Quaternion.identity, s_mask);
					for (int n = 0; n < num3; n++)
					{
						Collider other = s_overlap[n];
						if (IsValidContact(other, collider2) && GivesSupport(rock, other, red))
						{
							red[m] = true;
							break;
						}
					}
				}
			}
		}

		private static bool IsBelowGround(Bounds bounds)
		{
			//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)
			//IL_000f: 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)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: 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_0024: 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_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: 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)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: 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_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			Vector3 min = ((Bounds)(ref bounds)).min;
			Vector3 max = ((Bounds)(ref bounds)).max;
			Vector3 center = ((Bounds)(ref bounds)).center;
			if (!BelowGroundAt(center.x, center.z, min.y) && !BelowGroundAt(min.x, min.z, min.y) && !BelowGroundAt(min.x, max.z, min.y) && !BelowGroundAt(max.x, min.z, min.y))
			{
				return BelowGroundAt(max.x, max.z, min.y);
			}
			return true;
		}

		private static bool BelowGroundAt(float x, float z, float y)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			float num = default(float);
			if (Heightmap.GetHeight(new Vector3(x, y, z), ref num))
			{
				return y < num;
			}
			return false;
		}

		private static bool IsValidContact(Collider other, Collider own)
		{
			if ((Object)(object)other != (Object)null && (Object)(object)other != (Object)(object)own && (Object)(object)other.attachedRigidbody == (Object)null)
			{
				return !other.isTrigger;
			}
			return false;
		}

		private static bool GivesSupport(MineRock5 rock, Collider other, bool[] supported)
		{
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			if (((Component)other).gameObject.layer == s_terrainLayer)
			{
				return true;
			}
			IDestructible componentInParent = ((Component)other).gameObject.GetComponentInParent<IDestructible>();
			if (componentInParent == null)
			{
				return true;
			}
			if ((object)componentInParent == rock)
			{
				int areaIndex = rock.GetAreaIndex(other);
				if (areaIndex >= 0)
				{
					return supported[areaIndex];
				}
			}
			return ((Component)other).transform.position.y < ((Component)rock).transform.position.y;
		}

		private static GameObject GetRoot()
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			if ((Object)(object)s_root == (Object)null)
			{
				s_root = new GameObject("OreHitboxVisualizer");
				Object.DontDestroyOnLoad((Object)(object)s_root);
			}
			return s_root;
		}

		internal static Material GetLineMaterial(bool colliding)
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: 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_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			ref Material reference = ref colliding ? ref s_lineColliding : ref s_lineFree;
			if ((Object)(object)reference == (Object)null)
			{
				Color color = (colliding ? OreHitboxVisualizerPlugin.CollidingColor.Value : OreHitboxVisualizerPlugin.FreeColor.Value);
				CompareFunction zTest = (CompareFunction)(OreHitboxVisualizerPlugin.HideHiddenEdges.Value ? 4 : 8);
				int renderQueue = (colliding ? 3989 : 3991);
				reference = CreateMaterial(color, renderQueue, zTest, zWrite: false, (BlendMode)5, (BlendMode)10, (CullMode)0);
			}
			return reference;
		}

		internal static Material GetDepthMaskMaterial()
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)s_depthMask == (Object)null)
			{
				s_depthMask = CreateMaterial(Color.white, 3990, (CompareFunction)4, zWrite: true, (BlendMode)0, (BlendMode)1, (CullMode)2);
			}
			return s_depthMask;
		}

		private static Material CreateMaterial(Color color, int renderQueue, CompareFunction zTest, bool zWrite, BlendMode src, BlendMode dst, CullMode cull)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: 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_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Expected I4, but got Unknown
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Expected I4, but got Unknown
			//IL_003f: 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)
			//IL_004c: Expected I4, but got Unknown
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Expected I4, but got Unknown
			//IL_0068: Expected O, but got Unknown
			Material val = new Material(Shader.Find("Hidden/Internal-Colored"))
			{
				hideFlags = (HideFlags)61,
				color = color,
				renderQueue = renderQueue
			};
			val.SetInt("_SrcBlend", (int)src);
			val.SetInt("_DstBlend", (int)dst);
			val.SetInt("_Cull", (int)cull);
			val.SetInt("_ZWrite", zWrite ? 1 : 0);
			val.SetInt("_ZTest", (int)zTest);
			return val;
		}

		private static void DestroyMaterial(ref Material mat)
		{
			if ((Object)(object)mat != (Object)null)
			{
				Object.Destroy((Object)(object)mat);
			}
			mat = null;
		}

		private static Mesh BuildUnitCube()
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0125: Unknown result type (might be due to invalid IL or missing references)
			//IL_012d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0134: Unknown result type (might be due to invalid IL or missing references)
			//IL_013b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0142: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: Expected O, but got Unknown
			Vector3[] array = (Vector3[])(object)new Vector3[8]
			{
				new Vector3(-0.5f, -0.5f, -0.5f),
				new Vector3(0.5f, -0.5f, -0.5f),
				new Vector3(0.5f, 0.5f, -0.5f),
				new Vector3(-0.5f, 0.5f, -0.5f),
				new Vector3(-0.5f, -0.5f, 0.5f),
				new Vector3(0.5f, -0.5f, 0.5f),
				new Vector3(0.5f, 0.5f, 0.5f),
				new Vector3(-0.5f, 0.5f, 0.5f)
			};
			int[] triangles = new int[36]
			{
				0, 2, 1, 0, 3, 2, 4, 5, 6, 4,
				6, 7, 0, 1, 5, 0, 5, 4, 3, 6,
				2, 3, 7, 6, 0, 4, 7, 0, 7, 3,
				1, 2, 6, 1, 6, 5
			};
			Color[] array2 = (Color[])(object)new Color[array.Length];
			for (int i = 0; i < array2.Length; i++)
			{
				array2[i] = Color.white;
			}
			Mesh val = new Mesh
			{
				name = "OreHitbox_Cube",
				hideFlags = (HideFlags)61,
				vertices = array,
				triangles = triangles,
				colors = array2
			};
			val.RecalculateBounds();
			return val;
		}

		internal static Mesh BuildEdgeMesh(Bounds bounds, float thickness)
		{
			//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)
			//IL_000f: 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)
			//IL_0017: 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_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0112: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0120: Unknown result type (might be due to invalid IL or missing references)
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0133: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e4: Expected O, but got Unknown
			//IL_0181: Unknown result type (might be due to invalid IL or missing references)
			//IL_0187: Unknown result type (might be due to invalid IL or missing references)
			//IL_018e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0196: Unknown result type (might be due to invalid IL or missing references)
			//IL_019b: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a0: Unknown result type (might be due to invalid IL or missing references)
			Vector3 min = ((Bounds)(ref bounds)).min;
			Vector3 max = ((Bounds)(ref bounds)).max;
			Vector3 center = ((Bounds)(ref bounds)).center;
			Vector3 size = ((Bounds)(ref bounds)).size;
			CombineInstance[] array = (CombineInstance[])(object)new CombineInstance[12];
			int num = 0;
			float[] array2 = new float[2] { min.x, max.x };
			float[] array3 = new float[2] { min.y, max.y };
			float[] array4 = new float[2] { min.z, max.z };
			float[] array5 = array3;
			foreach (float num2 in array5)
			{
				float[] array6 = array4;
				foreach (float num3 in array6)
				{
					array[num++] = Edge(new Vector3(center.x, num2, num3), new Vector3(size.x + thickness, thickness, thickness));
				}
			}
			array5 = array2;
			foreach (float num4 in array5)
			{
				float[] array6 = array4;
				foreach (float num5 in array6)
				{
					array[num++] = Edge(new Vector3(num4, center.y, num5), new Vector3(thickness, size.y + thickness, thickness));
				}
			}
			array5 = array2;
			foreach (float num6 in array5)
			{
				float[] array6 = array3;
				foreach (float num7 in array6)
				{
					array[num++] = Edge(new Vector3(num6, num7, center.z), new Vector3(thickness, thickness, size.z + thickness));
				}
			}
			Mesh val = new Mesh
			{
				name = "OreHitbox_Edges",
				hideFlags = (HideFlags)61
			};
			val.CombineMeshes(array, true, true);
			return val;
		}

		private static CombineInstance Edge(Vector3 center, Vector3 size)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			CombineInstance result = default(CombineInstance);
			((CombineInstance)(ref result)).mesh = CubeMesh;
			((CombineInstance)(ref result)).transform = Matrix4x4.TRS(center, Quaternion.identity, size);
			return result;
		}
	}
	internal class RockVisual
	{
		public float LastHitTime;

		private readonly MineRock5 m_rock;

		private readonly GameObject m_root;

		private AreaVisual[] m_areas;

		private bool[] m_alive;

		private Bounds[] m_bounds;

		private bool[] m_red;

		public RockVisual(MineRock5 rock, Transform parent)
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Expected O, but got Unknown
			m_rock = rock;
			m_root = new GameObject("OreHitbox_" + ((Object)rock).name);
			m_root.transform.SetParent(parent, false);
		}

		public void Refresh()
		{
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: Unknown result type (might be due to invalid IL or missing references)
			int count = m_rock.m_hitAreas.Count;
			if (m_areas == null || m_areas.Length != count)
			{
				DestroyAreas();
				m_areas = new AreaVisual[count];
				m_alive = new bool[count];
				m_bounds = (Bounds[])(object)new Bounds[count];
				m_red = new bool[count];
			}
			HitboxManager.CollectAreas(m_rock, m_alive, m_bounds);
			HitboxManager.ComputeStates(m_rock, m_alive, m_bounds, m_red);
			for (int i = 0; i < count; i++)
			{
				if (!m_alive[i])
				{
					if (m_areas[i] != null)
					{
						m_areas[i].Destroy();
						m_areas[i] = null;
					}
					continue;
				}
				if (m_areas[i] == null || m_areas[i].Bounds != m_bounds[i])
				{
					m_areas[i]?.Destroy();
					m_areas[i] = new AreaVisual(m_root.transform, m_bounds[i]);
				}
				m_areas[i].SetColliding(m_red[i]);
			}
		}

		public void DestroyAreas()
		{
			if (m_areas != null)
			{
				for (int i = 0; i < m_areas.Length; i++)
				{
					m_areas[i]?.Destroy();
					m_areas[i] = null;
				}
			}
		}

		public void Destroy()
		{
			DestroyAreas();
			if ((Object)(object)m_root != (Object)null)
			{
				Object.Destroy((Object)(object)m_root);
			}
		}
	}
	internal class AreaVisual
	{
		public readonly Bounds Bounds;

		private readonly GameObject m_edges;

		private readonly Mesh m_edgeMesh;

		private readonly MeshRenderer m_edgeRenderer;

		private readonly GameObject m_mask;

		private bool? m_colliding;

		public AreaVisual(Transform parent, Bounds bounds)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Expected O, but got Unknown
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Expected O, but got Unknown
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
			Bounds = bounds;
			float num = Mathf.Max(0.001f, OreHitboxVisualizerPlugin.LineThickness.Value);
			m_edgeMesh = HitboxManager.BuildEdgeMesh(bounds, num);
			m_edges = new GameObject("Edges");
			m_edges.transform.SetParent(parent, false);
			m_edges.AddComponent<MeshFilter>().sharedMesh = m_edgeMesh;
			m_edgeRenderer = SetupRenderer(m_edges.AddComponent<MeshRenderer>());
			if (OreHitboxVisualizerPlugin.HideHiddenEdges.Value)
			{
				m_mask = new GameObject("DepthMask");
				m_mask.transform.SetParent(parent, false);
				m_mask.transform.position = ((Bounds)(ref bounds)).center;
				m_mask.transform.localScale = Vector3.Max(((Bounds)(ref bounds)).size - Vector3.one * num, Vector3.one * 0.001f);
				m_mask.AddComponent<MeshFilter>().sharedMesh = HitboxManager.CubeMesh;
				((Renderer)SetupRenderer(m_mask.AddComponent<MeshRenderer>())).sharedMaterial = HitboxManager.GetDepthMaskMaterial();
			}
		}

		private static MeshRenderer SetupRenderer(MeshRenderer renderer)
		{
			((Renderer)renderer).shadowCastingMode = (ShadowCastingMode)0;
			((Renderer)renderer).receiveShadows = false;
			((Renderer)renderer).lightProbeUsage = (LightProbeUsage)0;
			((Renderer)renderer).reflectionProbeUsage = (ReflectionProbeUsage)0;
			return renderer;
		}

		public void SetColliding(bool colliding)
		{
			if (m_colliding != colliding)
			{
				m_colliding = colliding;
				((Renderer)m_edgeRenderer).sharedMaterial = HitboxManager.GetLineMaterial(colliding);
			}
		}

		public void Destroy()
		{
			Object.Destroy((Object)(object)m_edges);
			Object.Destroy((Object)(object)m_edgeMesh);
			if ((Object)(object)m_mask != (Object)null)
			{
				Object.Destroy((Object)(object)m_mask);
			}
		}
	}
	[HarmonyPatch(typeof(MineRock5), "Damage")]
	internal static class MineRock5_Damage_Patch
	{
		private static void Prefix(MineRock5 __instance, HitData hit)
		{
			try
			{
				HitboxManager.OnRockHit(__instance, hit);
			}
			catch (Exception ex)
			{
				OreHitboxVisualizerPlugin.Log.LogError((object)("Error on MineRock5 hit: " + ex));
			}
		}
	}
	[HarmonyPatch(typeof(MineRock5), "GetHoverText")]
	internal static class MineRock5_GetHoverText_Patch
	{
		private static void Postfix(MineRock5 __instance, ref string __result)
		{
			if (!OreHitboxVisualizerPlugin.Enabled.Value || !OreHitboxVisualizerPlugin.ShowNodeCount.Value || __instance.m_hitAreas == null)
			{
				return;
			}
			try
			{
				HitboxManager.GetNodeCounts(__instance, out var total, out var colliding);
				__result = __result + "\n" + NodeCountText(total, colliding, total - colliding);
			}
			catch (Exception ex)
			{
				OreHitboxVisualizerPlugin.Log.LogError((object)("Error on MineRock5 hover text: " + ex));
			}
		}

		private static string NodeCountText(int total, int colliding, int free)
		{
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			bool flag = OreHitboxVisualizerPlugin.IsSpanish();
			bool num = OreHitboxVisualizerPlugin.Mode.Value == HitboxMode.Support;
			string text = (flag ? "Nodos" : "Nodes");
			string text2 = ((!num) ? (flag ? "colisionando" : "colliding") : (flag ? "sostenidos" : "supported"));
			string text3 = ((!num) ? (flag ? "libres" : "free") : (flag ? "sueltos" : "loose"));
			string text4 = ColorUtility.ToHtmlStringRGB(OreHitboxVisualizerPlugin.CollidingColor.Value);
			string text5 = ColorUtility.ToHtmlStringRGB(OreHitboxVisualizerPlugin.FreeColor.Value);
			return $"<size=80%>{text}: {total}  ·  <color=#{text4}>{colliding} {text2}</color>  ·  <color=#{text5}>{free} {text3}</color></size>";
		}
	}
	public enum HitboxMode
	{
		Collision,
		Support
	}
	[BepInPlugin("adrirf7.OreHitboxVisualizer", "OreHitboxVisualizer", "1.0.0")]
	public class OreHitboxVisualizerPlugin : BaseUnityPlugin
	{
		public const string ModGuid = "adrirf7.OreHitboxVisualizer";

		public const string ModName = "OreHitboxVisualizer";

		public const string ModVersion = "1.0.0";

		internal const string FilePrefix = "OreHitboxVisualizer.adrirf7";

		internal static ManualLogSource Log;

		internal static ConfigFile MainConfig;

		private Harmony m_harmony;

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<KeyboardShortcut> ToggleKey;

		internal static ConfigEntry<bool> PickaxeOnly;

		internal static ConfigEntry<HitboxMode> Mode;

		internal static ConfigEntry<float> HideDistance;

		internal static ConfigEntry<float> HideAfterSeconds;

		internal static ConfigEntry<bool> ShowNodeCount;

		internal static ConfigEntry<Color> CollidingColor;

		internal static ConfigEntry<Color> FreeColor;

		internal static ConfigEntry<float> LineThickness;

		internal static ConfigEntry<bool> HideHiddenEdges;

		private void Awake()
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Expected O, but got Unknown
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0176: Unknown result type (might be due to invalid IL or missing references)
			//IL_0276: Unknown result type (might be due to invalid IL or missing references)
			//IL_0280: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			MainConfig = new ConfigFile(Path.Combine(Paths.ConfigPath, "OreHitboxVisualizer.adrirf7.cfg"), true, ((BaseUnityPlugin)this).Info.Metadata);
			Enabled = MainConfig.Bind<bool>("1 - General", "Enabled", true, "true = hitboxes are shown after hitting an ore. Toggled in game with ToggleKey (the state is saved).");
			ToggleKey = MainConfig.Bind<KeyboardShortcut>("1 - General", "ToggleKey", new KeyboardShortcut((KeyCode)108, Array.Empty<KeyCode>()), "Key to show / hide the hitboxes.");
			PickaxeOnly = MainConfig.Bind<bool>("1 - General", "PickaxeOnly", true, "true = hitboxes only appear when hitting the rock with a pickaxe; false = any weapon or tool.");
			Mode = MainConfig.Bind<HitboxMode>("1 - General", "Mode", HitboxMode.Collision, "Collision = a piece is red when its hitbox touches anything outside its own deposit (ground, other rocks, buildings), or is buried below the terrain surface, and green otherwise. Other pieces of the same deposit are ignored.\nSupport = a piece is red when the game considers it supported and green when it will break off on the next mined piece (exact vanilla support logic).");
			HideDistance = MainConfig.Bind<float>("1 - General", "HideDistance", 40f, "The hitboxes of a rock are removed when you move farther than this (metres). Hit it again to show them again.");
			HideAfterSeconds = MainConfig.Bind<float>("1 - General", "HideAfterSeconds", 0f, "Seconds since the last hit after which the hitboxes are removed. 0 = never (only by distance).");
			ShowNodeCount = MainConfig.Bind<bool>("1 - General", "ShowNodeCount", true, "true = when you aim at a deposit, a line under its name shows how many nodes (pieces) it has, how many are colliding and how many are free.");
			CollidingColor = MainConfig.Bind<Color>("2 - Appearance", "CollidingColor", new Color(1f, 0.15f, 0.15f, 1f), "Colour of a piece whose hitbox is colliding with something.");
			FreeColor = MainConfig.Bind<Color>("2 - Appearance", "FreeColor", new Color(0.15f, 1f, 0.2f, 1f), "Colour of a piece whose hitbox is not colliding with anything.");
			LineThickness = MainConfig.Bind<float>("2 - Appearance", "LineThickness", 0.03f, "Thickness of the hitbox edges (metres).");
			HideHiddenEdges = MainConfig.Bind<bool>("2 - Appearance", "HideHiddenEdges", true, "true = only the edges you can actually see are drawn: edges behind the rock or the terrain are hidden, and free (green) edges are also hidden behind other hitboxes. Colliding (red) edges are never hidden by other hitboxes.\nfalse = every edge is drawn through everything.");
			CollidingColor.SettingChanged += delegate
			{
				HitboxManager.OnAppearanceChanged();
			};
			FreeColor.SettingChanged += delegate
			{
				HitboxManager.OnAppearanceChanged();
			};
			LineThickness.SettingChanged += delegate
			{
				HitboxManager.OnAppearanceChanged();
			};
			HideHiddenEdges.SettingChanged += delegate
			{
				HitboxManager.OnAppearanceChanged();
			};
			m_harmony = new Harmony("adrirf7.OreHitboxVisualizer");
			m_harmony.PatchAll();
			Log.LogInfo((object)"OreHitboxVisualizer 1.0.0 loaded.");
		}

		private void Update()
		{
			//IL_0005: 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)
			KeyboardShortcut value = ToggleKey.Value;
			if (((KeyboardShortcut)(ref value)).IsDown() && CanUseHotkeys())
			{
				Enabled.Value = !Enabled.Value;
				string text = ((!IsSpanish()) ? (Enabled.Value ? "Ore hitboxes: shown" : "Ore hitboxes: hidden") : (Enabled.Value ? "Hitbox de minerales: visible" : "Hitbox de minerales: oculta"));
				Player localPlayer = Player.m_localPlayer;
				if (localPlayer != null)
				{
					((Character)localPlayer).Message((MessageType)1, text, 0, (Sprite)null, false);
				}
			}
			HitboxManager.Tick();
		}

		private void OnDestroy()
		{
			Harmony harmony = m_harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			HitboxManager.Shutdown();
		}

		private static bool CanUseHotkeys()
		{
			if ((Object)(object)Player.m_localPlayer == (Object)null)
			{
				return false;
			}
			if ((Object)(object)Chat.instance != (Object)null && Chat.instance.HasFocus())
			{
				return false;
			}
			if (!Console.IsVisible() && !TextInput.IsVisible() && !Minimap.InTextInput())
			{
				return !Menu.IsVisible();
			}
			return false;
		}

		internal static bool IsSpanish()
		{
			Localization instance = Localization.instance;
			string text = ((instance != null) ? instance.GetSelectedLanguage() : null);
			if (!(text == "Spanish"))
			{
				return text == "Spanish_LatAm";
			}
			return true;
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}