Decompiled source of DroneRecycler v1.0.0

DroneRecycler.dll

Decompiled 6 months ago
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using On.RoR2;
using On.RoR2.CharacterAI;
using RoR2;
using RoR2.CharacterAI;
using RoR2.UI;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.EventSystems;
using UnityEngine.Networking;

[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("The Equipment Drone uses the Recycler on command.")]
[assembly: AssemblyCompany("GChinchi")]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: AssemblyTitle("DroneRecycler")]
[assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/schinchi/DroneRecycler")]
[assembly: AssemblyProduct("DroneRecycler")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: CompilationRelaxations(8)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[Microsoft.CodeAnalysis.Embedded]
	[CompilerGenerated]
	[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 DroneRecycler
{
	[BepInPlugin("GChinchi.DroneRecycler", "DroneRecycler", "1.0.0")]
	public class DroneRecycler : BaseUnityPlugin
	{
		public const string PluginGUID = "GChinchi.DroneRecycler";

		public const string PluginAuthor = "GChinchi";

		public const string PluginName = "DroneRecycler";

		public const string PluginVersion = "1.0.0";

		internal static ConfigEntry<KeyCode> commandKey;

		internal static ConfigEntry<bool> isCommandManual;

		public void Awake()
		{
			commandKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Key Binds", "commandKey", (KeyCode)99, "Which key to use to manually issue or revoke the recycling command");
			isCommandManual = ((BaseUnityPlugin)this).Config.Bind<bool>("Settings", "isCommandManual", false, "Whether assigning an Equipment Drone to recycle an item is done by pressing a key (see 'commandKey') after pinging a pickup or it is done automatically. If this is enabled, the 'commandKey' can further be used to revoke the command.");
			Hooks.Init();
			RoR2Application.onLoad = (Action)Delegate.Combine(RoR2Application.onLoad, new Action(Patch));
		}

		private void Update()
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			if (!Object.op_Implicit((Object)(object)Run.instance) || !isCommandManual.Value || !Input.GetKeyDown(commandKey.Value) || MPEventSystemManager.eventSystems.Values.Any((MPEventSystem x) => Object.op_Implicit((Object)(object)x) & Object.op_Implicit((Object)(object)((EventSystem)x).currentSelectedGameObject)) || Object.op_Implicit((Object)(object)EventSystem.current.currentSelectedGameObject) || PauseScreenController.instancesList.Count > 0)
			{
				return;
			}
			NetworkUser? obj = NetworkUser.readOnlyLocalPlayersList.FirstOrDefault();
			CharacterMaster val = ((obj != null) ? obj.master : null);
			if ((Object)(object)val != (Object)null)
			{
				CharacterMaster val2 = Utils.FindEquipmentDroneWithRecycler(((Component)val).gameObject);
				if ((Object)(object)val2 != (Object)null)
				{
					GameObject gameObject = ((Component)val2).GetComponent<BaseAI>().customTarget.gameObject;
					((Component)val).GetComponent<EquipmentDroneRecyclerController>().SetTarget(val2, ((Object)(object)gameObject == (Object)(object)((Component)Hooks.pingedTarget).gameObject) ? null : Hooks.pingedTarget);
				}
			}
		}

		private void Patch()
		{
			PatchAI();
			PatchPlayer();
		}

		private void PatchAI()
		{
			//IL_0005: 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_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: 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)
			//IL_007d: 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_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: 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_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: 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)
			//IL_0108: Unknown result type (might be due to invalid IL or missing references)
			//IL_010f: 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)
			GameObject masterPrefab = MasterCatalog.GetMasterPrefab(MasterCatalog.FindMasterIndex("EquipmentDroneMaster"));
			AISkillDriver[] components = masterPrefab.GetComponents<AISkillDriver>();
			AISkillDriver obj = masterPrefab.AddComponent<AISkillDriver>();
			obj.customName = "RecyclePickup";
			obj.skillSlot = (SkillSlot)(-1);
			obj.requireEquipmentReady = true;
			obj.maxDistance = 30f;
			obj.activationRequiresAimConfirmation = true;
			obj.moveTargetType = (TargetType)3;
			obj.aimType = (AimType)1;
			obj.movementType = (MovementType)1;
			obj.shouldFireEquipment = true;
			obj.resetCurrentEnemyOnNextDriverSelection = true;
			AISkillDriver obj2 = masterPrefab.AddComponent<AISkillDriver>();
			obj2.customName = "ApproachPickup";
			obj2.skillSlot = (SkillSlot)(-1);
			obj2.minDistance = 30f;
			obj2.moveTargetType = (TargetType)3;
			obj2.aimType = (AimType)1;
			obj2.movementType = (MovementType)1;
			AISkillDriver obj3 = masterPrefab.AddComponent<AISkillDriver>();
			obj3.customName = "StrafePickup";
			obj3.skillSlot = (SkillSlot)(-1);
			obj3.minDistance = 20f;
			obj3.maxDistance = 30f;
			obj3.moveTargetType = (TargetType)3;
			obj3.aimType = (AimType)1;
			obj3.movementType = (MovementType)2;
			AISkillDriver obj4 = masterPrefab.AddComponent<AISkillDriver>();
			obj4.customName = "BackUpFromPickup";
			obj4.skillSlot = (SkillSlot)(-1);
			obj4.maxDistance = 20f;
			obj4.moveTargetType = (TargetType)3;
			obj4.aimType = (AimType)1;
			obj4.movementType = (MovementType)3;
			AISkillDriver[] array = components;
			foreach (AISkillDriver val in array)
			{
				AISkillDriver obj5 = masterPrefab.AddComponent<AISkillDriver>();
				FieldInfo[] fields = ((object)val).GetType().GetFields();
				foreach (FieldInfo fieldInfo in fields)
				{
					fieldInfo.SetValue(obj5, fieldInfo.GetValue(val));
				}
				Object.Destroy((Object)(object)val);
			}
		}

		private void PatchPlayer()
		{
			//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)
			GameObject val = Addressables.LoadAssetAsync<GameObject>((object)"RoR2/Base/Core/PlayerMaster.prefab").WaitForCompletion();
			if ((Object)(object)val == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"PlayerMaster not found");
			}
			else
			{
				((Behaviour)val.AddComponent<EquipmentDroneRecyclerController>()).enabled = false;
			}
		}
	}
	internal class EquipmentDroneRecyclerController : NetworkBehaviour
	{
		private CharacterMaster drone;

		private GenericPickupController pickup;

		private static int kCmdCmdSetTarget;

		private static int kRpcRpcSetTargetClient;

		public void SetTarget(CharacterMaster droneMaster, GenericPickupController pickup)
		{
			//IL_001f: 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_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: 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)
			//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)
			if (Util.HasEffectiveAuthority(((NetworkBehaviour)this).netIdentity))
			{
				NetworkInstanceId droneNetId = (((Object)(object)droneMaster != (Object)null) ? ((NetworkBehaviour)droneMaster).netId : NetworkInstanceId.Invalid);
				NetworkInstanceId pickupNetId = (((Object)(object)pickup != (Object)null) ? ((NetworkBehaviour)pickup).netId : NetworkInstanceId.Invalid);
				if (NetworkServer.active)
				{
					CallRpcSetTargetClient(droneNetId, pickupNetId);
				}
				else
				{
					CallCmdSetTarget(droneNetId, pickupNetId);
				}
			}
		}

		[Command]
		private void CmdSetTarget(NetworkInstanceId droneNetId, NetworkInstanceId pickupNetId)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			CallRpcSetTargetClient(droneNetId, pickupNetId);
		}

		[ClientRpc]
		private void RpcSetTargetClient(NetworkInstanceId droneNetId, NetworkInstanceId pickupNetId)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			SetTargetInternal(droneNetId, pickupNetId);
		}

		private void SetTargetInternal(NetworkInstanceId droneNetId, NetworkInstanceId pickupNetId)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			GameObject obj = Util.FindNetworkObject(pickupNetId);
			pickup = ((obj != null) ? obj.GetComponent<GenericPickupController>() : null);
			GameObject obj2 = Util.FindNetworkObject(droneNetId);
			drone = ((obj2 != null) ? obj2.GetComponent<CharacterMaster>() : null);
			Target customTarget = ((Component)drone).GetComponent<BaseAI>().customTarget;
			GenericPickupController obj3 = pickup;
			customTarget.gameObject = ((obj3 != null) ? ((Component)obj3).gameObject : null);
			((Behaviour)this).enabled = Object.op_Implicit((Object)(object)drone) && Object.op_Implicit((Object)(object)pickup);
		}

		private void FixedUpdate()
		{
			if ((Object)(object)drone == (Object)null)
			{
				((Behaviour)this).enabled = false;
			}
			else if ((Object)(object)pickup == (Object)null || pickup.NetworkRecycled)
			{
				((Component)drone).GetComponent<BaseAI>().customTarget.gameObject = null;
				((Behaviour)this).enabled = false;
			}
		}

		private void UNetVersion()
		{
		}

		protected static void InvokeCmdCmdSetTarget(NetworkBehaviour obj, NetworkReader reader)
		{
			//IL_001d: 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)
			if (!NetworkServer.active)
			{
				Debug.LogError((object)"Command CmdSetTarget called on client.");
			}
			else
			{
				((EquipmentDroneRecyclerController)(object)obj).CmdSetTarget(reader.ReadNetworkId(), reader.ReadNetworkId());
			}
		}

		public void CallCmdSetTarget(NetworkInstanceId droneNetId, NetworkInstanceId pickupNetId)
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Expected O, but got Unknown
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: 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_0022: 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)
			if (!NetworkClient.active)
			{
				Debug.LogError((object)"Command function CmdSetTarget called on server.");
				return;
			}
			if (((NetworkBehaviour)this).isServer)
			{
				CmdSetTarget(droneNetId, pickupNetId);
				return;
			}
			NetworkWriter val = new NetworkWriter();
			val.Write((short)0);
			val.Write((short)5);
			val.WritePackedUInt32((uint)kCmdCmdSetTarget);
			val.Write(((Component)this).GetComponent<NetworkIdentity>().netId);
			val.Write(droneNetId);
			val.Write(pickupNetId);
			((NetworkBehaviour)this).SendCommandInternal(val, 0, "CmdSetTarget");
		}

		protected static void InvokeRpcRpcSetTargetClient(NetworkBehaviour obj, NetworkReader reader)
		{
			//IL_001d: 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)
			if (!NetworkClient.active)
			{
				Debug.LogError((object)"RPC RpcSetTargetClient called on server.");
			}
			else
			{
				((EquipmentDroneRecyclerController)(object)obj).RpcSetTargetClient(reader.ReadNetworkId(), reader.ReadNetworkId());
			}
		}

		public void CallRpcSetTargetClient(NetworkInstanceId droneNetId, NetworkInstanceId pickupNetId)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: 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)
			if (!NetworkServer.active)
			{
				Debug.LogError((object)"RPC Function RpcSetTargetClient called on client.");
				return;
			}
			NetworkWriter val = new NetworkWriter();
			val.Write((short)0);
			val.Write((short)2);
			val.WritePackedUInt32((uint)kRpcRpcSetTargetClient);
			val.Write(((Component)this).GetComponent<NetworkIdentity>().netId);
			val.Write(droneNetId);
			val.Write(pickupNetId);
			((NetworkBehaviour)this).SendRPCInternal(val, 0, "RpcSetTargetClient");
		}

		static EquipmentDroneRecyclerController()
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Expected O, but got Unknown
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Expected O, but got Unknown
			kCmdCmdSetTarget = 1918539950;
			NetworkBehaviour.RegisterCommandDelegate(typeof(EquipmentDroneRecyclerController), kCmdCmdSetTarget, new CmdDelegate(InvokeCmdCmdSetTarget));
			kRpcRpcSetTargetClient = -1678527613;
			NetworkBehaviour.RegisterRpcDelegate(typeof(EquipmentDroneRecyclerController), kRpcRpcSetTargetClient, new CmdDelegate(InvokeRpcRpcSetTargetClient));
			NetworkCRC.RegisterBehaviour("EquipmentDroneRecyclerController", 0);
		}

		public override bool OnSerialize(NetworkWriter writer, bool forceAll)
		{
			bool result = default(bool);
			return result;
		}

		public override void OnDeserialize(NetworkReader reader, bool initialState)
		{
		}
	}
	internal class Hooks
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static hook_GetBullseyePosition <0>__Target_GetBullseyePosition;

			public static hook_RebuildPing <1>__PingerController_RebuildPing;
		}

		internal static GenericPickupController pingedTarget;

		public static void Init()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			//IL_0030: 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_003b: Expected O, but got Unknown
			object obj = <>O.<0>__Target_GetBullseyePosition;
			if (obj == null)
			{
				hook_GetBullseyePosition val = Target_GetBullseyePosition;
				<>O.<0>__Target_GetBullseyePosition = val;
				obj = (object)val;
			}
			Target.GetBullseyePosition += (hook_GetBullseyePosition)obj;
			object obj2 = <>O.<1>__PingerController_RebuildPing;
			if (obj2 == null)
			{
				hook_RebuildPing val2 = PingerController_RebuildPing;
				<>O.<1>__PingerController_RebuildPing = val2;
				obj2 = (object)val2;
			}
			PingerController.RebuildPing += (hook_RebuildPing)obj2;
		}

		private static bool Target_GetBullseyePosition(orig_GetBullseyePosition orig, Target self, out Vector3 position)
		{
			return orig.Invoke(self, ref position) | ((Object)(object)self.gameObject != (Object)null && (Object)(object)self.gameObject.GetComponent<GenericPickupController>() != (Object)null);
		}

		private static void PingerController_RebuildPing(orig_RebuildPing orig, PingerController self, PingInfo pingInfo)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			orig.Invoke(self, pingInfo);
			PingIndicator pingIndicator = self.pingIndicator;
			object obj;
			if (pingIndicator == null)
			{
				obj = null;
			}
			else
			{
				GameObject pingTarget = pingIndicator.pingTarget;
				obj = ((pingTarget != null) ? pingTarget.GetComponent<GenericPickupController>() : null);
			}
			GenericPickupController val = (pingedTarget = (GenericPickupController)obj);
			if ((Object)(object)val != (Object)null && !val.Recycled && !DroneRecycler.isCommandManual.Value)
			{
				CharacterMaster val2 = Utils.FindEquipmentDroneWithRecycler(((Component)self).gameObject);
				if ((Object)(object)val2 != (Object)null)
				{
					((Component)self).GetComponent<EquipmentDroneRecyclerController>().SetTarget(val2, val);
				}
			}
		}
	}
	internal class Utils
	{
		public static CharacterMaster FindEquipmentDroneWithRecycler(GameObject masterObject)
		{
			//IL_0006: 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_0053: Unknown result type (might be due to invalid IL or missing references)
			MinionGroup val = MinionGroup.FindGroup(((NetworkBehaviour)masterObject.GetComponent<CharacterMaster>()).netId);
			if (val != null)
			{
				MinionOwnership[] members = val.members;
				foreach (MinionOwnership val2 in members)
				{
					if (((Object)((Component)val2).gameObject).name.Contains("EquipmentDrone"))
					{
						CharacterMaster component = ((Component)val2).GetComponent<CharacterMaster>();
						if (component.inventory.currentEquipmentIndex == Equipment.Recycle.equipmentIndex)
						{
							return component;
						}
					}
				}
			}
			return null;
		}
	}
}