Decompiled source of DroneRecycler v1.1.0

DroneRecycler.dll

Decompiled a month ago
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using Mono.Cecil;
using MonoMod.Cil;
using On.RoR2;
using On.RoR2.CharacterAI;
using RiskOfOptions;
using RiskOfOptions.Options;
using RoR2;
using RoR2.CharacterAI;
using RoR2.UI;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Networking;

[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Chinchi")]
[assembly: AssemblyConfiguration("Release")]
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyDescription("The Equipment Drone uses the Recycler on command.")]
[assembly: AssemblyProduct("DroneRecycler")]
[assembly: AssemblyTitle("DroneRecycler")]
[assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/schinchi/DroneRecycler")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0+81774e8a0e13c35e2c6da54a06d7481e933e9a2c")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.0.0")]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace DroneRecycler
{
	internal class Configs
	{
		internal static ConfigEntry<KeyboardShortcut> CommandKey { get; private set; }

		internal static ConfigEntry<bool> IsCommandManual { get; private set; }

		internal static void Init(ConfigFile config, string assemblyLocation)
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			CommandKey = config.Bind<KeyboardShortcut>("Settings", "Command Key", new KeyboardShortcut((KeyCode)99, Array.Empty<KeyCode>()), "Which key to use to manually issue or revoke the recycling command.");
			IsCommandManual = config.Bind<bool>("Settings", "Is Command Manual", false, "Whether assigning an Equipment Drone to recycle an item is done by pressing a key (see 'Command Key') after pinging a pickup or it is done automatically. If this is enabled, the 'Command Key' can further be used to revoke the command.");
			if (Chainloader.PluginInfos.ContainsKey("com.rune580.riskofoptions"))
			{
				RiskOfOptionsInit(assemblyLocation);
			}
		}

		[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
		private static void RiskOfOptionsInit(string assemblyLocation)
		{
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Expected O, but got Unknown
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Expected O, but got Unknown
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Expected O, but got Unknown
			//IL_0070: 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)
			FileInfo fileInfo = null;
			FileInfo[] files = new DirectoryInfo(Path.GetDirectoryName(assemblyLocation)).GetFiles("icon.png", SearchOption.TopDirectoryOnly);
			if (files != null && files.Length != 0)
			{
				fileInfo = files[0];
			}
			if (fileInfo != null)
			{
				string name = "DroneRecyclerIcon";
				Texture2D val = new Texture2D(256, 256);
				((Object)val).name = name;
				if (ImageConversion.LoadImage(val, File.ReadAllBytes(fileInfo.FullName)))
				{
					Sprite obj = Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f));
					((Object)obj).name = name;
					ModSettingsManager.SetModIcon(obj, "Chinchi.DroneRecycler", "DroneRecycler");
				}
			}
			ModSettingsManager.AddOption((BaseOption)new CheckBoxOption(IsCommandManual));
			ModSettingsManager.AddOption((BaseOption)new KeyBindOption(CommandKey));
		}
	}
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("Chinchi.DroneRecycler", "DroneRecycler", "1.1.0")]
	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.1.0";

		internal const string RiskOfOptionsGUID = "com.rune580.riskofoptions";

		internal static MasterIndex equipmentDroneIndex = MasterIndex.none;

		public void Awake()
		{
			Log.Init(((BaseUnityPlugin)this).Logger);
			Configs.Init(((BaseUnityPlugin)this).Config, ((BaseUnityPlugin)this).Info.Location);
			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)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: 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)
			if (!Object.op_Implicit((Object)(object)Run.instance) || !Configs.IsCommandManual.Value)
			{
				return;
			}
			KeyboardShortcut value = Configs.CommandKey.Value;
			if (!Input.GetKeyDown(((KeyboardShortcut)(ref value)).MainKey))
			{
				return;
			}
			value = Configs.CommandKey.Value;
			if (!((KeyboardShortcut)(ref value)).Modifiers.All((Func<KeyCode, bool>)Input.GetKey) || 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.op_Implicit((Object)(object)val))
			{
				CharacterMaster val2 = Utils.FindEquipmentDroneWithRecycler(((Component)val).gameObject);
				if (Object.op_Implicit((Object)(object)val2))
				{
					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
			{
				Log.Error("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)
			{
				Component obj5 = masterPrefab.AddComponent(((object)val).GetType());
				FieldInfo[] fields = ((object)val).GetType().GetFields();
				foreach (FieldInfo fieldInfo in fields)
				{
					fieldInfo.SetValue(obj5, fieldInfo.GetValue(val));
				}
				Object.DestroyImmediate((Object)(object)val);
			}
		}

		private void PatchPlayer()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			GameObject masterPrefab = MasterCatalog.GetMasterPrefab(MasterCatalog.FindMasterIndex("PlayerMaster"));
			if ((Object)(object)masterPrefab == (Object)null)
			{
				Log.Error("PlayerMaster not found");
			}
			else
			{
				((Behaviour)masterPrefab.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.op_Implicit((Object)(object)drone))
			{
				((Behaviour)this).enabled = false;
			}
			else if (!Object.op_Implicit((Object)(object)pickup) || pickup.NetworkRecycled)
			{
				BaseAI[] aiComponents = drone.AiComponents;
				for (int i = 0; i < aiComponents.Length; i++)
				{
					aiComponents[i].customTarget.gameObject = null;
				}
				((Behaviour)this).enabled = false;
			}
		}

		internal void SetTarget(CharacterMaster droneMaster, GenericPickupController pickup)
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: 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_0034: 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_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: 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_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			if (Util.HasEffectiveAuthority(((NetworkBehaviour)this).netIdentity))
			{
				NetworkInstanceId droneNetId = (Object.op_Implicit((Object)(object)droneMaster) ? ((NetworkBehaviour)droneMaster).netId : NetworkInstanceId.Invalid);
				NetworkInstanceId pickupNetId = (Object.op_Implicit((Object)(object)pickup) ? ((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);
			BaseAI[] aiComponents = drone.aiComponents;
			for (int i = 0; i < aiComponents.Length; i++)
			{
				Target customTarget = aiComponents[i].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>__IsTargetPickup;

			public static hook_RebuildPing <1>__CommandEquipmentDroneWithPing;
		}

		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>__IsTargetPickup;
			if (obj == null)
			{
				hook_GetBullseyePosition val = IsTargetPickup;
				<>O.<0>__IsTargetPickup = val;
				obj = (object)val;
			}
			Target.GetBullseyePosition += (hook_GetBullseyePosition)obj;
			object obj2 = <>O.<1>__CommandEquipmentDroneWithPing;
			if (obj2 == null)
			{
				hook_RebuildPing val2 = CommandEquipmentDroneWithPing;
				<>O.<1>__CommandEquipmentDroneWithPing = val2;
				obj2 = (object)val2;
			}
			PingerController.RebuildPing += (hook_RebuildPing)obj2;
		}

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

		private static void CommandEquipmentDroneWithPing(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.op_Implicit((Object)(object)val) && !val.Recycled && !Configs.IsCommandManual.Value)
			{
				CharacterMaster val2 = Utils.FindEquipmentDroneWithRecycler(((Component)self).gameObject);
				if (Object.op_Implicit((Object)(object)val2))
				{
					((Component)self).GetComponent<EquipmentDroneRecyclerController>().SetTarget(val2, val);
				}
			}
		}
	}
	internal static class Log
	{
		private static ManualLogSource _logger;

		internal static void Init(ManualLogSource logger)
		{
			_logger = logger;
		}

		internal static void Info(object data)
		{
			_logger.LogInfo(data);
		}

		internal static void Debug(object data)
		{
			_logger.LogDebug(data);
		}

		internal static void Message(object data)
		{
			_logger.LogMessage(data);
		}

		internal static void Warning(object data)
		{
			_logger.LogWarning(data);
		}

		internal static void Error(object data)
		{
			_logger.LogError(data);
		}

		internal static void Fatal(object data)
		{
			_logger.LogFatal(data);
		}

		internal static void PatchFail(string message)
		{
			_logger.LogError((object)("Failed to patch " + message));
		}

		internal static void PatchFail(ILContext il)
		{
			PatchFail(((MemberReference)il.Method).Name);
		}
	}
	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_0053: 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_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)
			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)val2) && 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;
		}
	}
}