Decompiled source of Shrinker Drone v1.0.3

ShrinkDrone.dll

Decompiled 5 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using ExitGames.Client.Photon;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using Photon.Realtime;
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("Omniscye")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ShrinkDrone")]
[assembly: AssemblyTitle("ShrinkDrone")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 Empress.DroneShrink
{
	[BepInPlugin("Empress.DroneShrink", "DroneShrink", "1.1.0")]
	public class DroneShrinkPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "Empress.DroneShrink";

		public const string PluginName = "DroneShrink";

		public const string PluginVersion = "1.1.0";

		internal static DroneShrinkPlugin Instance;

		internal static ManualLogSource Logger => Instance._logger;

		private ManualLogSource _logger => ((BaseUnityPlugin)this).Logger;

		internal Harmony? Harmony { get; set; }

		private void Awake()
		{
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Expected O, but got Unknown
			//IL_0041: Expected O, but got Unknown
			Instance = this;
			((Component)this).gameObject.transform.parent = null;
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
			if (Harmony == null)
			{
				Harmony val = new Harmony("Empress.DroneShrink");
				Harmony val2 = val;
				Harmony = val;
			}
			Harmony.PatchAll();
			Logger.LogInfo((object)$"{((BaseUnityPlugin)this).Info.Metadata.GUID} v{((BaseUnityPlugin)this).Info.Metadata.Version} loaded.");
		}

		private void OnDestroy()
		{
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	[DisallowMultipleComponent]
	[RequireComponent(typeof(ItemDrone))]
	public class DroneShrinkValuables : MonoBehaviour, IOnEventCallback
	{
		public float minScale = 0.35f;

		public float shrinkSpeed = 6f;

		public float restoreSpeed = 6f;

		public float drainWhileActive = 0.75f;

		public float drainWhileShrinking = 2f;

		private const byte ScaleEvent = 45;

		private ItemDrone itemDrone;

		private ItemEquippable itemEquippable;

		private ItemBattery itemBattery;

		private PhysGrabObject myPhysGrabObject;

		private Transform currentTarget;

		private Vector3 originalScale;

		private int currentTargetViewId = -1;

		private float syncTimer;

		private float syncInterval = 0.1f;

		private bool restoreRequested;

		private void Awake()
		{
			itemDrone = ((Component)this).GetComponent<ItemDrone>();
			itemEquippable = ((Component)this).GetComponent<ItemEquippable>();
			itemBattery = ((Component)this).GetComponent<ItemBattery>();
			myPhysGrabObject = ((Component)this).GetComponent<PhysGrabObject>();
		}

		private void OnEnable()
		{
			PhotonNetwork.AddCallbackTarget((object)this);
		}

		private void OnDisable()
		{
			PhotonNetwork.RemoveCallbackTarget((object)this);
		}

		private bool ConnectedToValuable()
		{
			if ((Object)(object)itemDrone == (Object)null || !itemDrone.itemActivated || !itemDrone.magnetActive)
			{
				return false;
			}
			if (itemDrone.targetIsLocalPlayer)
			{
				return false;
			}
			if ((Object)(object)itemDrone.magnetTargetPhysGrabObject == (Object)null && (Object)(object)itemDrone.magnetTargetRigidbody == (Object)null)
			{
				return false;
			}
			Transform val = (Object.op_Implicit((Object)(object)itemDrone.magnetTargetRigidbody) ? ((Component)itemDrone.magnetTargetRigidbody).transform : ((Component)itemDrone.magnetTargetPhysGrabObject).transform);
			if (!Object.op_Implicit((Object)(object)val) || !((Component)val).gameObject.activeInHierarchy)
			{
				return false;
			}
			PhysGrabObjectImpactDetector component = ((Component)val).GetComponent<PhysGrabObjectImpactDetector>();
			if ((Object)(object)component == (Object)null || !component.isValuable)
			{
				return false;
			}
			return true;
		}

		private Transform TargetRoot()
		{
			return Object.op_Implicit((Object)(object)itemDrone.magnetTargetRigidbody) ? ((Component)itemDrone.magnetTargetRigidbody).transform : ((Component)itemDrone.magnetTargetPhysGrabObject).transform;
		}

		private void FixedUpdate()
		{
			if (!((Object)(object)itemBattery == (Object)null) && itemDrone.itemActivated)
			{
				float num = drainWhileActive;
				if ((Object)(object)currentTarget != (Object)null)
				{
					num += drainWhileShrinking;
				}
				itemBattery.batteryLife = Mathf.Max(0f, itemBattery.batteryLife - num * Time.fixedDeltaTime);
			}
		}

		private void Update()
		{
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)myPhysGrabObject != (Object)null && itemDrone.itemActivated && (!itemDrone.magnetActive || (Object)(object)itemDrone.magnetTargetPhysGrabObject == (Object)null))
			{
				myPhysGrabObject.OverrideZeroGravity(0.1f);
				myPhysGrabObject.OverrideDrag(1f, 0.1f);
				myPhysGrabObject.OverrideAngularDrag(10f, 0.1f);
			}
			if (!SemiFunc.IsMasterClientOrSingleplayer())
			{
				return;
			}
			if (ConnectedToValuable())
			{
				Transform val = TargetRoot();
				if ((Object)(object)currentTarget != (Object)(object)val)
				{
					BeginTarget(val);
				}
				Vector3 target = originalScale * Mathf.Clamp(minScale, 0.05f, 1f);
				LerpScale(currentTarget, target, shrinkSpeed);
				if ((Object)(object)itemDrone.magnetTargetPhysGrabObject != (Object)null)
				{
					itemDrone.magnetTargetPhysGrabObject.OverrideIndestructible(0.15f);
					itemDrone.magnetTargetPhysGrabObject.OverrideDrag(0.2f, 0.15f);
					itemDrone.magnetTargetPhysGrabObject.OverrideAngularDrag(0.2f, 0.15f);
				}
				SyncScaleTick(currentTarget);
				restoreRequested = false;
			}
			else
			{
				if (restoreRequested && (Object)(object)currentTarget == (Object)null)
				{
					restoreRequested = false;
				}
				Restore(lerp: true);
			}
		}

		private void BeginTarget(Transform t)
		{
			//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)
			Restore(lerp: false);
			currentTarget = t;
			originalScale = currentTarget.localScale;
			currentTargetViewId = FindPhotonViewId(currentTarget);
			syncTimer = 0f;
		}

		private void LerpScale(Transform t, Vector3 target, float speed)
		{
			//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_0029: Unknown result type (might be due to invalid IL or missing references)
			if (Object.op_Implicit((Object)(object)t))
			{
				t.localScale = Vector3.Lerp(t.localScale, target, Time.deltaTime * Mathf.Max(0.01f, speed));
			}
		}

		private void SyncScaleTick(Transform t)
		{
			//IL_0063: 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_007f: 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_009b: 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_00ad: 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_00b5: Expected O, but got Unknown
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			syncTimer -= Time.deltaTime;
			if (!(syncTimer > 0f))
			{
				syncTimer = syncInterval;
				if (currentTargetViewId == -1)
				{
					currentTargetViewId = FindPhotonViewId(t);
				}
				if (currentTargetViewId != -1)
				{
					Vector3 localScale = t.localScale;
					object[] array = new object[4] { currentTargetViewId, localScale.x, localScale.y, localScale.z };
					RaiseEventOptions val = new RaiseEventOptions
					{
						Receivers = (ReceiverGroup)1
					};
					PhotonNetwork.RaiseEvent((byte)45, (object)array, val, SendOptions.SendUnreliable);
				}
			}
		}

		private int FindPhotonViewId(Transform t)
		{
			if ((Object)(object)t == (Object)null)
			{
				return -1;
			}
			PhotonView componentInParent = ((Component)t).GetComponentInParent<PhotonView>();
			return ((Object)(object)componentInParent != (Object)null) ? componentInParent.ViewID : (-1);
		}

		internal void ForceRestoreAnimated()
		{
			restoreRequested = true;
		}

		private void Restore(bool lerp)
		{
			//IL_0019: 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_00aa: 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_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_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_0065: Unknown result type (might be due to invalid IL or missing references)
			if (!Object.op_Implicit((Object)(object)currentTarget))
			{
				return;
			}
			Vector3 val = originalScale;
			if (lerp)
			{
				LerpScale(currentTarget, val, restoreSpeed);
				Vector3 val2 = currentTarget.localScale - val;
				if (((Vector3)(ref val2)).sqrMagnitude < 0.0001f)
				{
					currentTarget.localScale = val;
					SyncScaleTick(currentTarget);
					currentTarget = null;
					currentTargetViewId = -1;
					restoreRequested = false;
				}
				else
				{
					SyncScaleTick(currentTarget);
				}
			}
			else
			{
				currentTarget.localScale = val;
				SyncScaleTick(currentTarget);
				currentTarget = null;
				currentTargetViewId = -1;
				restoreRequested = false;
			}
		}

		public void OnEvent(EventData photonEvent)
		{
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			if (photonEvent.Code == 45 && photonEvent.CustomData is object[] array && array.Length == 4)
			{
				int num = (int)array[0];
				float num2 = (float)array[1];
				float num3 = (float)array[2];
				float num4 = (float)array[3];
				PhotonView val = PhotonView.Find(num);
				if (!((Object)(object)val == (Object)null))
				{
					((Component)val).transform.localScale = new Vector3(num2, num3, num4);
				}
			}
		}
	}
	[HarmonyPatch(typeof(ItemDrone), "Start")]
	public static class Patch_ItemDrone_Start
	{
		public static void Postfix(ItemDrone __instance)
		{
			if (!Object.op_Implicit((Object)(object)((Component)__instance).GetComponent<DroneShrinkValuables>()))
			{
				((Component)__instance).gameObject.AddComponent<DroneShrinkValuables>();
			}
		}
	}
	[HarmonyPatch(typeof(ItemDrone), "MagnetActiveToggleLogic")]
	public static class Patch_ItemDrone_MagnetActiveToggleLogic
	{
		public static void Postfix(ItemDrone __instance, bool activated)
		{
			if (!activated)
			{
				DroneShrinkValuables component = ((Component)__instance).GetComponent<DroneShrinkValuables>();
				if ((Object)(object)component != (Object)null)
				{
					component.ForceRestoreAnimated();
				}
			}
		}
	}
	[HarmonyPatch(typeof(ItemEquippable), "PerformUnequip")]
	public static class Patch_ItemEquippable_PerformUnequip
	{
		public static void Postfix(ItemEquippable __instance)
		{
			ItemDrone component = ((Component)__instance).GetComponent<ItemDrone>();
			if (!((Object)(object)component == (Object)null))
			{
				DroneShrinkValuables component2 = ((Component)__instance).GetComponent<DroneShrinkValuables>();
				if ((Object)(object)component2 != (Object)null)
				{
					component2.ForceRestoreAnimated();
				}
			}
		}
	}
}