Decompiled source of DroneRecycler v1.0.1

DroneRecycler.dll

Decompiled 2 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("Chinchi")]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyTitle("DroneRecycler")]
[assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/schinchi/DroneRecycler")]
[assembly: AssemblyProduct("DroneRecycler")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1+225a97aac4dfbc6f9133c322020795a6b2fc6825")]
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.1.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace DroneRecycler
{
	[BepInPlugin("Chinchi.DroneRecycler", "DroneRecycler", "1.0.1")]
	public class DroneRecycler : BaseUnityPlugin
	{
		public const string PluginGUID = "Chinchi.DroneRecycler";

		public const string PluginAuthor = "Chinchi";

		public const string PluginName = "DroneRecycler";

		public const string PluginVersion = "1.0.1";

		internal static ConfigEntry<KeyCode> commandKey;

		internal static ConfigEntry<bool> isCommandManual;

		internal static MasterIndex equipmentDroneIndex;

		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()
		{
			//IL_0005: 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)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			GameObject masterPrefab = MasterCatalog.GetMasterPrefab(MasterCatalog.FindMasterIndex("EquipmentDroneMaster"));
			CharacterMaster val = default(CharacterMaster);
			if (Object.op_Implicit((Object)(object)masterPrefab) && masterPrefab.TryGetComponent<CharacterMaster>(ref val))
			{
				equipmentDroneIndex = val.masterIndex;
				PatchAI();
				PatchPlayer();
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"EquipmentDrone not found");
			}
		}

		private void PatchAI()
		{
			//IL_0000: 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_004e: 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_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: 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)
			//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: 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_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_0119: Unknown result type (might be due to invalid IL or missing references)
			GameObject masterPrefab = MasterCatalog.GetMasterPrefab(equipmentDroneIndex);
			if (!Object.op_Implicit((Object)(object)masterPrefab))
			{
				return;
			}
			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 void Awake()
		{
			if (!NetworkServer.active)
			{
				((Behaviour)this).enabled = false;
			}
		}

		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;
			}
		}

		internal 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)
				{
					SetTargetInternal(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)
			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 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");
		}

		static EquipmentDroneRecyclerController()
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Expected O, but got Unknown
			kCmdCmdSetTarget = 1918539950;
			NetworkBehaviour.RegisterCommandDelegate(typeof(EquipmentDroneRecyclerController), kCmdCmdSetTarget, new CmdDelegate(InvokeCmdCmdSetTarget));
			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_0014: 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)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: 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)
			MinionGroup val = MinionGroup.FindGroup(((NetworkBehaviour)masterObject.GetComponent<CharacterMaster>()).netId);
			if (val != null)
			{
				MasterIndex equipmentDroneIndex = DroneRecycler.equipmentDroneIndex;
				MinionOwnership[] members = val.members;
				CharacterMaster val3 = default(CharacterMaster);
				foreach (MinionOwnership val2 in members)
				{
					if (Object.op_Implicit((Object)(object)((Component)val2).gameObject) && ((Component)val2).gameObject.TryGetComponent<CharacterMaster>(ref val3) && val3.masterIndex == equipmentDroneIndex && val3.inventory.currentEquipmentIndex == Equipment.Recycle.equipmentIndex)
					{
						return val3;
					}
				}
			}
			return null;
		}
	}
}