Decompiled source of CruiserMines v0.1.1

BepInEx/plugins/CruiserMines/CruiserMines.dll

Decompiled 2 days 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 Unity.Netcode;
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("CruiserMines")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.1.0")]
[assembly: AssemblyInformationalVersion("0.1.1")]
[assembly: AssemblyProduct("CruiserMines")]
[assembly: AssemblyTitle("CruiserMines")]
[assembly: AssemblyVersion("0.1.1.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 CruiserMines
{
	[BepInPlugin("local.cruisermines", "Cruiser Mines", "0.1.1")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string Id = "local.cruisermines";

		internal static ConfigEntry<float> ContactMargin;

		private Harmony harmony;

		private void Awake()
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Expected O, but got Unknown
			ContactMargin = ((BaseUnityPlugin)this).Config.Bind<float>("Detection", "ContactMargin", 0.25f, new ConfigDescription("Extra contact distance in metres around the vehicle and below its wheels.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			harmony = new Harmony("local.cruisermines");
			harmony.PatchAll(typeof(Plugin).Assembly);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Cruiser Mines 0.1.1 loaded. Mine contact instantly destroys the cruiser.");
		}

		private void OnDestroy()
		{
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
		}
	}
	[HarmonyPatch(typeof(Landmine), "Start")]
	internal static class MineStartPatch
	{
		private static void Postfix(Landmine __instance)
		{
			if ((Object)(object)((Component)__instance).GetComponent<MineMarker>() == (Object)null)
			{
				((Component)__instance).gameObject.AddComponent<MineMarker>().Mine = __instance;
			}
		}
	}
	public sealed class MineMarker : MonoBehaviour
	{
		internal static readonly List<MineMarker> All = new List<MineMarker>();

		internal Landmine Mine;

		private void OnEnable()
		{
			All.Add(this);
		}

		private void OnDisable()
		{
			All.Remove(this);
		}
	}
	[HarmonyPatch(typeof(VehicleController), "Start")]
	internal static class VehicleStartPatch
	{
		private static void Postfix(VehicleController __instance)
		{
			if ((Object)(object)((Component)__instance).GetComponent<CruiserDetector>() == (Object)null)
			{
				((Component)__instance).gameObject.AddComponent<CruiserDetector>().Vehicle = __instance;
			}
		}
	}
	public sealed class CruiserDetector : MonoBehaviour
	{
		internal VehicleController Vehicle;

		private static readonly FieldRef<Landmine, bool> Activated = AccessTools.FieldRefAccess<Landmine, bool>("mineActivated");

		private Matrix4x4 previousWorldToLocal;

		private Vector3 previousPosition;

		private Quaternion previousRotation;

		private bool hasPrevious;

		private bool destructionRequested;

		private void OnDisable()
		{
			hasPrevious = false;
		}

		private void FixedUpdate()
		{
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: 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_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0140: Unknown result type (might be due to invalid IL or missing references)
			//IL_014a: Unknown result type (might be due to invalid IL or missing references)
			//IL_015d: 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_0168: 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_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)
			//IL_01a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_017e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0184: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0300: Unknown result type (might be due to invalid IL or missing references)
			//IL_0214: Unknown result type (might be due to invalid IL or missing references)
			//IL_0219: Unknown result type (might be due to invalid IL or missing references)
			//IL_021d: Unknown result type (might be due to invalid IL or missing references)
			//IL_021f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0226: Unknown result type (might be due to invalid IL or missing references)
			//IL_022b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0230: Unknown result type (might be due to invalid IL or missing references)
			//IL_0240: Unknown result type (might be due to invalid IL or missing references)
			//IL_0242: Unknown result type (might be due to invalid IL or missing references)
			//IL_0249: Unknown result type (might be due to invalid IL or missing references)
			//IL_024e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0236: Unknown result type (might be due to invalid IL or missing references)
			//IL_0253: Unknown result type (might be due to invalid IL or missing references)
			//IL_0255: Unknown result type (might be due to invalid IL or missing references)
			//IL_025c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0263: Unknown result type (might be due to invalid IL or missing references)
			//IL_026a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0271: Unknown result type (might be due to invalid IL or missing references)
			//IL_0278: Unknown result type (might be due to invalid IL or missing references)
			//IL_027f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0286: Unknown result type (might be due to invalid IL or missing references)
			//IL_028d: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)NetworkManager.Singleton == (Object)null || !NetworkManager.Singleton.IsHost || (Object)(object)Vehicle == (Object)null || !((NetworkBehaviour)Vehicle).IsSpawned || destructionRequested || Vehicle.carDestroyed || Vehicle.magnetedToShip || (Object)(object)Vehicle.boundsCollider == (Object)null)
			{
				hasPrevious = false;
				return;
			}
			BoxCollider boundsCollider = Vehicle.boundsCollider;
			Transform transform = ((Component)boundsCollider).transform;
			Bounds bounds = default(Bounds);
			((Bounds)(ref bounds))..ctor(boundsCollider.center, boundsCollider.size);
			IncludeWheelBottom(ref bounds, transform, Vehicle.FrontLeftWheel);
			IncludeWheelBottom(ref bounds, transform, Vehicle.FrontRightWheel);
			IncludeWheelBottom(ref bounds, transform, Vehicle.BackLeftWheel);
			IncludeWheelBottom(ref bounds, transform, Vehicle.BackRightWheel);
			Vector3 lossyScale = transform.lossyScale;
			float value = Plugin.ContactMargin.Value;
			((Bounds)(ref bounds)).Expand(new Vector3(value / Mathf.Max(Mathf.Abs(lossyScale.x), 0.001f), value / Mathf.Max(Mathf.Abs(lossyScale.y), 0.001f), value / Mathf.Max(Mathf.Abs(lossyScale.z), 0.001f)) * 2f);
			int num;
			if (hasPrevious)
			{
				Vector3 val = transform.position - previousPosition;
				if (((Vector3)(ref val)).sqrMagnitude < 64f)
				{
					num = ((Quaternion.Angle(previousRotation, transform.rotation) < 30f) ? 1 : 0);
					goto IL_0198;
				}
			}
			num = 0;
			goto IL_0198;
			IL_0198:
			bool flag = (byte)num != 0;
			Matrix4x4 worldToLocalMatrix = transform.worldToLocalMatrix;
			Vector3 extents = ((Bounds)(ref bounds)).extents;
			foreach (MineMarker item in MineMarker.All)
			{
				Landmine mine = item.Mine;
				if (!((Object)(object)mine == (Object)null) && ((NetworkBehaviour)mine).IsSpawned && ((Behaviour)mine).isActiveAndEnabled && !mine.hasExploded && Activated.Invoke(mine))
				{
					Vector3 position = ((Component)mine).transform.position;
					Vector3 val2 = ((Matrix4x4)(ref worldToLocalMatrix)).MultiplyPoint3x4(position) - ((Bounds)(ref bounds)).center;
					Vector3 val3 = (flag ? (((Matrix4x4)(ref previousWorldToLocal)).MultiplyPoint3x4(position) - ((Bounds)(ref bounds)).center) : val2);
					if (Sweep.Intersects(val3.x, val3.y, val3.z, val2.x, val2.y, val2.z, extents.x, extents.y, extents.z))
					{
						mine.hasExploded = true;
						mine.ExplodeMineServerRpc();
						destructionRequested = true;
						Vehicle.DestroyCarServerRpc((int)NetworkManager.Singleton.LocalClientId);
						break;
					}
				}
			}
			previousWorldToLocal = worldToLocalMatrix;
			previousPosition = transform.position;
			previousRotation = transform.rotation;
			hasPrevious = true;
		}

		private static void IncludeWheelBottom(ref Bounds bounds, Transform frame, WheelCollider wheel)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: 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_0040: 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_004a: 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_004e: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)wheel == (Object)null))
			{
				Vector3 val = default(Vector3);
				Quaternion val2 = default(Quaternion);
				wheel.GetWorldPose(ref val, ref val2);
				Vector3 val3 = val - ((Component)wheel).transform.up * wheel.radius * Mathf.Abs(((Component)wheel).transform.lossyScale.y);
				((Bounds)(ref bounds)).Encapsulate(frame.InverseTransformPoint(val3));
			}
		}
	}
	internal static class Sweep
	{
		internal static bool Intersects(float ax, float ay, float az, float bx, float by, float bz, float ex, float ey, float ez)
		{
			float entry = 0f;
			float exit = 1f;
			if (Axis(ax, bx, ex, ref entry, ref exit) && Axis(ay, by, ey, ref entry, ref exit))
			{
				return Axis(az, bz, ez, ref entry, ref exit);
			}
			return false;
		}

		private static bool Axis(float a, float b, float extent, ref float entry, ref float exit)
		{
			float num = b - a;
			if (Math.Abs(num) < 1E-06f)
			{
				if (a >= 0f - extent)
				{
					return a <= extent;
				}
				return false;
			}
			float num2 = (0f - extent - a) / num;
			float num3 = (extent - a) / num;
			if (num2 > num3)
			{
				float num4 = num2;
				num2 = num3;
				num3 = num4;
			}
			entry = Math.Max(entry, num2);
			exit = Math.Min(exit, num3);
			return entry <= exit;
		}
	}
}