Decompiled source of MultiGrab v0.1.0

MultiGrab.dll

Decompiled 12 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 Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("MultiGrab")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("MultiGrab")]
[assembly: AssemblyTitle("MultiGrab")]
[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 MultiGrab
{
	[BepInPlugin("lch.multigrab", "MultiGrab", "0.1.0")]
	public class MultiGrabPlugin : BaseUnityPlugin
	{
		private class FollowItem
		{
			public Rigidbody rb;

			public bool origKinematic;

			public float origMass;

			public Vector3 localPos;

			public Quaternion localRot;

			public List<Collider> colliders = new List<Collider>();
		}

		public const string GUID = "lch.multigrab";

		public const string MODNAME = "MultiGrab";

		public const string VERSION = "0.1.0";

		private ConfigEntry<KeyCode> _toggleKey;

		private ConfigEntry<int> _extraCount;

		private ConfigEntry<int> _radiusCm;

		private readonly List<(Collider a, Collider b)> _ignoredPairs = new List<(Collider, Collider)>();

		private readonly List<FollowItem> _follows = new List<FollowItem>();

		private Transform _anchor;

		private bool _active;

		private Rigidbody _mainRb;

		private void Awake()
		{
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Expected O, but got Unknown
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Expected O, but got Unknown
			_toggleKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Keys", "toggleKey", (KeyCode)118, "Press while holding the main item: grab extra items / release them.");
			_extraCount = ((BaseUnityPlugin)this).Config.Bind<int>("Settings", "extraCount", 2, new ConfigDescription("Extra valuables to carry beside the main item (main + this = total).", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 5), Array.Empty<object>()));
			_radiusCm = ((BaseUnityPlugin)this).Config.Bind<int>("Settings", "radius", 80, new ConfigDescription("Search radius in cm around the main valuable (80 = 0.8m).", (AcceptableValueBase)(object)new AcceptableValueRange<int>(20, 200), Array.Empty<object>()));
			((BaseUnityPlugin)this).Logger.LogInfo((object)"MultiGrab loaded.");
		}

		private void Update()
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			PhysGrabber instance = PhysGrabber.instance;
			if ((Object)(object)instance == (Object)null)
			{
				return;
			}
			if (_active && !instance.grabbed)
			{
				ReleaseAll();
			}
			else if (Input.GetKeyDown(_toggleKey.Value))
			{
				if (!_active && instance.grabbed && (Object)(object)instance.grabbedObjectTransform != (Object)null)
				{
					Attach(instance.grabbedObjectTransform);
				}
				else if (_active)
				{
					ReleaseAll();
				}
			}
		}

		private void LateUpdate()
		{
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			if (!_active || (Object)(object)_anchor == (Object)null)
			{
				return;
			}
			foreach (FollowItem follow in _follows)
			{
				if (!((Object)(object)follow.rb == (Object)null))
				{
					follow.rb.isKinematic = true;
					follow.rb.velocity = Vector3.zero;
					follow.rb.angularVelocity = Vector3.zero;
					follow.rb.position = _anchor.TransformPoint(follow.localPos);
					follow.rb.rotation = _anchor.rotation * follow.localRot;
				}
			}
		}

		private void Attach(Transform anchor)
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: 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_0230: Unknown result type (might be due to invalid IL or missing references)
			//IL_0235: Unknown result type (might be due to invalid IL or missing references)
			//IL_023b: 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_03db: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ec: Unknown result type (might be due to invalid IL or missing references)
			_anchor = anchor;
			_follows.Clear();
			Vector3 position = anchor.position;
			float num = (float)_radiusCm.Value / 100f;
			Collider[] array = Physics.OverlapSphere(position, num);
			_mainRb = ((Component)anchor).GetComponent<Rigidbody>();
			Vector3[] array2 = (Vector3[])(object)new Vector3[5]
			{
				new Vector3(-0.55f, 0.5f, 0.15f),
				new Vector3(0.55f, 0.5f, 0.15f),
				new Vector3(0f, 0.9f, 0.15f),
				new Vector3(-0.55f, 0.9f, 0.15f),
				new Vector3(0.55f, 0.9f, 0.15f)
			};
			List<Collider> list = new List<Collider>(((Component)anchor).GetComponentsInChildren<Collider>());
			_ignoredPairs.Clear();
			int num2 = 0;
			Collider[] array3 = array;
			foreach (Collider val in array3)
			{
				if (num2 >= _extraCount.Value)
				{
					break;
				}
				if ((Object)(object)((Component)val).GetComponentInParent<ValuableObject>() == (Object)null || (Object)(object)((Component)val).GetComponentInParent<ItemEquippable>() != (Object)null || (Object)(object)((Component)val).GetComponentInParent<NotValuableObject>() != (Object)null)
				{
					continue;
				}
				PhysGrabObject componentInParent = ((Component)val).GetComponentInParent<PhysGrabObject>();
				if ((Object)(object)componentInParent == (Object)null || componentInParent.dead || componentInParent.grabbed || (Object)(object)componentInParent.rb == (Object)null || ((Object)(object)_mainRb != (Object)null && (Object)(object)componentInParent.rb == (Object)(object)_mainRb))
				{
					continue;
				}
				bool flag = false;
				foreach (FollowItem follow in _follows)
				{
					if ((Object)(object)follow.rb == (Object)(object)componentInParent.rb)
					{
						flag = true;
						break;
					}
				}
				if (flag)
				{
					continue;
				}
				FollowItem followItem = new FollowItem
				{
					rb = componentInParent.rb,
					origKinematic = componentInParent.rb.isKinematic,
					origMass = componentInParent.rb.mass,
					localPos = array2[num2],
					localRot = Quaternion.identity
				};
				followItem.colliders.AddRange(((Component)componentInParent.rb).GetComponentsInChildren<Collider>());
				foreach (Collider item in list)
				{
					if ((Object)(object)item == (Object)null)
					{
						continue;
					}
					foreach (Collider collider in followItem.colliders)
					{
						if (!((Object)(object)collider == (Object)null) && !((Object)(object)collider == (Object)(object)item))
						{
							Physics.IgnoreCollision(item, collider, true);
							_ignoredPairs.Add((item, collider));
						}
					}
				}
				foreach (FollowItem follow2 in _follows)
				{
					foreach (Collider collider2 in follow2.colliders)
					{
						if ((Object)(object)collider2 == (Object)null)
						{
							continue;
						}
						foreach (Collider collider3 in followItem.colliders)
						{
							if (!((Object)(object)collider3 == (Object)null) && !((Object)(object)collider3 == (Object)(object)collider2))
							{
								Physics.IgnoreCollision(collider2, collider3, true);
								_ignoredPairs.Add((collider2, collider3));
							}
						}
					}
				}
				followItem.rb.velocity = Vector3.zero;
				followItem.rb.angularVelocity = Vector3.zero;
				_follows.Add(followItem);
				num2++;
			}
			if (_follows.Count > 0)
			{
				ApplyHeaviestOnly();
				_active = true;
				((BaseUnityPlugin)this).Logger.LogInfo((object)$"MultiGrab attached {_follows.Count} extra item(s).");
			}
		}

		private void ApplyHeaviestOnly()
		{
			Rigidbody val = null;
			float num = -1f;
			foreach (FollowItem follow in _follows)
			{
				if ((Object)(object)follow.rb != (Object)null && follow.rb.mass > num)
				{
					num = follow.rb.mass;
					val = follow.rb;
				}
			}
			foreach (FollowItem follow2 in _follows)
			{
				if ((Object)(object)follow2.rb != (Object)null && (Object)(object)follow2.rb != (Object)(object)val)
				{
					follow2.rb.mass = 0f;
				}
			}
		}

		private void ReleaseAll()
		{
			foreach (var ignoredPair in _ignoredPairs)
			{
				if ((Object)(object)ignoredPair.a != (Object)null && (Object)(object)ignoredPair.b != (Object)null)
				{
					Physics.IgnoreCollision(ignoredPair.a, ignoredPair.b, false);
				}
			}
			_ignoredPairs.Clear();
			foreach (FollowItem follow in _follows)
			{
				if (!((Object)(object)follow.rb == (Object)null))
				{
					follow.rb.isKinematic = follow.origKinematic;
					follow.rb.mass = follow.origMass;
					follow.rb.WakeUp();
				}
			}
			_follows.Clear();
			_anchor = null;
			_mainRb = null;
			_active = false;
		}
	}
}