Decompiled source of ShipLootGuard v1.0.0

BepInEx/plugins/ShipLootGuard/ShipLootGuard.dll

Decompiled 17 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 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("ShipLootGuard")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ShipLootGuard")]
[assembly: AssemblyTitle("ShipLootGuard")]
[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 ShipLootGuard
{
	[BepInPlugin("local.shiplootguard", "Ship Loot Guard", "1.0.0")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string Guid = "local.shiplootguard";

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<float> Clearance;

		private Harmony harmony;

		private float nextCheck;

		private readonly List<GrabbableObject> items = new List<GrabbableObject>();

		private void Awake()
		{
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Expected O, but got Unknown
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Expected O, but got Unknown
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Protect loose items inside the ship.");
			Clearance = ((BaseUnityPlugin)this).Config.Bind<float>("General", "FloorClearance", 0.03f, new ConfigDescription("Extra height above the ship floor, in metres.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.01f, 0.2f), Array.Empty<object>()));
			harmony = new Harmony("local.shiplootguard");
			harmony.PatchAll(typeof(Plugin).Assembly);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Ship Loot Guard 1.0.0 loaded. Install on every player's profile.");
		}

		private void LateUpdate()
		{
			if (!Enabled.Value || Time.unscaledTime < nextCheck)
			{
				return;
			}
			nextCheck = Time.unscaledTime + 0.25f;
			StartOfRound instance = StartOfRound.Instance;
			if ((Object)(object)instance == (Object)null || (Object)(object)instance.elevatorTransform == (Object)null)
			{
				return;
			}
			items.Clear();
			((Component)instance.elevatorTransform).GetComponentsInChildren<GrabbableObject>(false, items);
			foreach (GrabbableObject item in items)
			{
				Guard(item);
			}
		}

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

		internal static void Guard(GrabbableObject item)
		{
			//IL_00a8: 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_00cd: 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_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00db: 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_0102: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0124: 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_014c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0155: Unknown result type (might be due to invalid IL or missing references)
			//IL_0164: Unknown result type (might be due to invalid IL or missing references)
			//IL_017c: Unknown result type (might be due to invalid IL or missing references)
			//IL_017d: 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_01a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01db: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0204: Unknown result type (might be due to invalid IL or missing references)
			//IL_0206: Unknown result type (might be due to invalid IL or missing references)
			//IL_020f: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_021b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0224: Unknown result type (might be due to invalid IL or missing references)
			//IL_025f: 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_0239: 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_027f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0284: Unknown result type (might be due to invalid IL or missing references)
			//IL_028b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0290: Unknown result type (might be due to invalid IL or missing references)
			//IL_0245: 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)
			StartOfRound instance = StartOfRound.Instance;
			if (Enabled == null || !Enabled.Value || (Object)(object)item == (Object)null || (Object)(object)item.itemProperties == (Object)null || (Object)(object)instance == (Object)null || (Object)(object)instance.elevatorTransform == (Object)null || (Object)(object)instance.shipInnerRoomBounds == (Object)null || item.isHeld || item.isHeldByEnemy || item.isPocketed || (Object)(object)item.parentObject != (Object)null)
			{
				return;
			}
			Transform elevatorTransform = instance.elevatorTransform;
			Transform transform = ((Component)item).transform;
			if ((Object)(object)transform.parent == (Object)null || !transform.IsChildOf(elevatorTransform))
			{
				return;
			}
			Bounds bounds = instance.shipInnerRoomBounds.bounds;
			if (!item.isInShipRoom && !((Bounds)(ref bounds)).Contains(transform.position))
			{
				return;
			}
			Vector3 val = transform.parent.TransformPoint(item.targetFloorPosition);
			Vector3 val2 = val;
			float num = Mathf.Min(0.15f, ((Bounds)(ref bounds)).extents.x * 0.25f);
			float num2 = Mathf.Min(0.15f, ((Bounds)(ref bounds)).extents.z * 0.25f);
			val2.x = Mathf.Clamp(val2.x, ((Bounds)(ref bounds)).min.x + num, ((Bounds)(ref bounds)).max.x - num);
			val2.z = Mathf.Clamp(val2.z, ((Bounds)(ref bounds)).min.z + num2, ((Bounds)(ref bounds)).max.z - num2);
			if (TryFloor(elevatorTransform, bounds, val2, out var floor))
			{
				float num3 = floor + item.itemProperties.verticalOffset + Clearance.Value;
				val2.y = Mathf.Clamp(val2.y, num3, Mathf.Max(num3, ((Bounds)(ref bounds)).max.y - 0.1f));
				Vector3 val3 = val - val2;
				if (((Vector3)(ref val3)).sqrMagnitude > 1E-06f)
				{
					item.targetFloorPosition = transform.parent.InverseTransformPoint(val2);
				}
				Vector3 position = transform.position;
				bool flag = position.x < ((Bounds)(ref bounds)).min.x || position.x > ((Bounds)(ref bounds)).max.x || position.z < ((Bounds)(ref bounds)).min.z || position.z > ((Bounds)(ref bounds)).max.z;
				if (position.y < num3 - 0.015f || flag)
				{
					transform.position = val2;
					item.startFallingPosition = transform.localPosition;
					item.targetFloorPosition = transform.localPosition;
					item.fallTime = 1f;
					item.reachedFloorTarget = false;
				}
			}
		}

		private static bool TryFloor(Transform root, Bounds bounds, Vector3 point, out float floor)
		{
			//IL_0007: 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_001f: 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_002a: 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_0056: 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_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			floor = float.PositiveInfinity;
			RaycastHit[] array = Physics.RaycastAll(new Vector3(point.x, ((Bounds)(ref bounds)).max.y - 0.1f, point.z), Vector3.down, ((Bounds)(ref bounds)).size.y + 2f, 268437760, (QueryTriggerInteraction)1);
			for (int i = 0; i < array.Length; i++)
			{
				RaycastHit val = array[i];
				if (!((Object)(object)((RaycastHit)(ref val)).collider == (Object)null) && !(((RaycastHit)(ref val)).normal.y < 0.5f) && ((Component)((RaycastHit)(ref val)).collider).transform.IsChildOf(root) && !((Object)(object)((Component)((RaycastHit)(ref val)).collider).GetComponentInParent<GrabbableObject>() != (Object)null) && !(((RaycastHit)(ref val)).point.y < ((Bounds)(ref bounds)).min.y - 0.25f))
				{
					floor = Mathf.Min(floor, ((RaycastHit)(ref val)).point.y);
				}
			}
			return !float.IsPositiveInfinity(floor);
		}
	}
	[HarmonyPatch(typeof(GrabbableObject), "FallToGround")]
	internal static class FallPatch
	{
		[HarmonyPostfix]
		private static void Postfix(GrabbableObject __instance, bool justSpawned)
		{
			if (!justSpawned)
			{
				Plugin.Guard(__instance);
			}
		}
	}
	[HarmonyPatch(typeof(PlayerControllerB), "SetObjectAsNoLongerHeld")]
	internal static class DropPatch
	{
		[HarmonyPostfix]
		private static void Postfix(GrabbableObject dropObject)
		{
			Plugin.Guard(dropObject);
		}
	}
	[HarmonyPatch(typeof(PlayerControllerB), "PlaceGrabbableObject")]
	internal static class PlacePatch
	{
		[HarmonyPostfix]
		private static void Postfix(GrabbableObject placeObject)
		{
			Plugin.Guard(placeObject);
		}
	}
}