Decompiled source of AedenthornsEquipMultipleUtilityItems v0.6.0

EquipMultipleUtilityItems.dll

Decompiled a year ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("EquipMultipleUtilityItems")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("EquipMultipleUtilityItems")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("83ca0d39-50f6-450b-a596-5d3535675fd7")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace EquipMultipleUtilityItems;

[BepInPlugin("aedenthorn.EquipMultipleUtilityItems", "Equip Multiple Utility Items", "0.6.0")]
public class BepInExPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Player), "UpdateMovementModifier")]
	private static class UpdateMovementModifier_Patch
	{
		private static void Postfix(Player __instance, ref float ___m_equipmentMovementModifier, ItemData ___m_utilityItem)
		{
			if (!modEnabled.Value)
			{
				return;
			}
			try
			{
				List<ItemData> list = ((Humanoid)__instance).GetInventory().GetAllItems().FindAll((ItemData i) => i.m_equipped && (int)i.m_shared.m_itemType == 18 && i != ___m_utilityItem);
				foreach (ItemData item in list)
				{
					___m_equipmentMovementModifier += item.m_shared.m_movementModifier;
				}
			}
			catch
			{
			}
		}
	}

	private static class ApplyArmorDamageMods_Patch
	{
		private static void Postfix(Player __instance, ref DamageModifiers mods, ItemData ___m_utilityItem)
		{
			if (!modEnabled.Value)
			{
				return;
			}
			try
			{
				List<ItemData> list = ((Humanoid)__instance).GetInventory().GetAllItems().FindAll((ItemData i) => i.m_equipped && (int)i.m_shared.m_itemType == 18 && i != ___m_utilityItem);
				foreach (ItemData item in list)
				{
					((DamageModifiers)(ref mods)).Apply(item.m_shared.m_damageModifiers);
				}
			}
			catch
			{
			}
		}
	}

	private static class GetBodyArmor_Patch
	{
		private static void Postfix(Player __instance, ref float __result)
		{
			if (!modEnabled.Value)
			{
				return;
			}
			try
			{
				List<ItemData> list = ((Humanoid)__instance).GetInventory().GetAllItems().FindAll((ItemData i) => i.m_equipped && (int)i.m_shared.m_itemType == 18);
				foreach (ItemData item in list)
				{
					__result += item.GetArmor();
				}
			}
			catch
			{
			}
		}
	}

	[HarmonyPatch(typeof(Humanoid), "IsItemEquiped")]
	private static class Humanoid_IsItemEquiped_Patch
	{
		private static void Postfix(Humanoid __instance, ItemData item, ItemData ___m_utilityItem, ref bool __result)
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Invalid comparison between Unknown and I4
			if (!modEnabled.Value | __result)
			{
				return;
			}
			try
			{
				__result = item.m_equipped && (int)item.m_shared.m_itemType == 18 && item != ___m_utilityItem;
			}
			catch
			{
			}
		}
	}

	[HarmonyPatch(typeof(Player), "GetEquipmentEitrRegenModifier")]
	private static class GetEquipmentEitrRegenModifier_Patch
	{
		private static void Postfix(Player __instance, ItemData ___m_utilityItem, ref float __result)
		{
			if (!modEnabled.Value)
			{
				return;
			}
			try
			{
				List<ItemData> list = ((Humanoid)__instance).GetInventory().GetAllItems().FindAll((ItemData i) => i.m_equipped && (int)i.m_shared.m_itemType == 18 && i != ___m_utilityItem);
				foreach (ItemData item in list)
				{
					__result += item.m_shared.m_eitrRegenModifier;
				}
			}
			catch
			{
			}
		}
	}

	[HarmonyPatch(typeof(Player), "QueueEquipAction")]
	private static class QueueEquipItem_Patch
	{
		private static bool Prefix(Player __instance, ItemData item)
		{
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Invalid comparison between Unknown and I4
			if (!modEnabled.Value || item == null || __instance.IsEquipActionQueued(item) || (int)item.m_shared.m_itemType != 18)
			{
				return true;
			}
			try
			{
				List<ItemData> list = ((Humanoid)__instance).GetInventory().GetAllItems().FindAll((ItemData i) => i.m_equipped && (int)i.m_shared.m_itemType == 18);
				if (list.Exists((ItemData i) => i.m_shared.m_name == item.m_shared.m_name))
				{
					return false;
				}
				if (list.Count >= maxEquippedItems.Value)
				{
					return false;
				}
			}
			catch
			{
				Dbgl("Error: " + Environment.StackTrace);
			}
			return true;
		}
	}

	[HarmonyPatch(typeof(Humanoid), "EquipItem")]
	private static class EquipItem_Patch
	{
		private static bool Prefix(Humanoid __instance, ItemData item, bool triggerEquipEffects, Inventory ___m_inventory, ref bool __result, ref ItemData ___m_utilityItem)
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Invalid comparison between Unknown and I4
			try
			{
				if (!modEnabled.Value || item == null || (int)item.m_shared.m_itemType != 18 || !((Character)__instance).IsPlayer() || !___m_inventory.ContainsItem(item) || ((Character)__instance).InAttack() || ((Character)__instance).InDodge() || (((Character)__instance).IsPlayer() && !((Character)__instance).IsDead() && ((Character)__instance).IsSwimming() && !((Character)__instance).IsOnGround()) || (item.m_shared.m_useDurability && item.m_durability <= 0f) || (item.m_shared.m_dlc.Length > 0 && !DLCMan.instance.IsDLCInstalled(item.m_shared.m_dlc)))
				{
					return true;
				}
				int count = __instance.GetInventory().GetAllItems().FindAll((ItemData i) => i.m_equipped && (int)i.m_shared.m_itemType == 18)
					.Count;
				if (count >= maxEquippedItems.Value)
				{
					__result = false;
					return false;
				}
				if (___m_utilityItem == null)
				{
					___m_utilityItem = item;
				}
				item.m_equipped = true;
				typeof(Humanoid).GetMethod("SetupEquipment", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(__instance, new object[0]);
				if (triggerEquipEffects)
				{
					typeof(Humanoid).GetMethod("TriggerEquipEffect", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(__instance, new object[1] { item });
				}
				__result = true;
				return false;
			}
			catch
			{
				Dbgl("Error: " + Environment.StackTrace);
			}
			return true;
		}
	}

	[HarmonyPatch(typeof(Humanoid), "UpdateEquipmentStatusEffects")]
	private static class UpdateEquipmentStatusEffects_Patch
	{
		private static void Prefix(Humanoid __instance, ItemData ___m_utilityItem, SEMan ___m_seman)
		{
			try
			{
				if (!modEnabled.Value || !((Character)__instance).IsPlayer())
				{
					return;
				}
				List<ItemData> list = __instance.GetInventory().GetAllItems().FindAll((ItemData i) => !i.m_equipped && (int)i.m_shared.m_itemType == 18 && i != ___m_utilityItem && Object.op_Implicit((Object)(object)i.m_shared.m_equipStatusEffect));
				List<ItemData> list2 = __instance.GetInventory().GetAllItems().FindAll((ItemData i) => i.m_equipped && (int)i.m_shared.m_itemType == 18 && i != ___m_utilityItem && Object.op_Implicit((Object)(object)i.m_shared.m_equipStatusEffect));
				foreach (ItemData item in list)
				{
					foreach (StatusEffect statusEffect in AccessTools.FieldRefAccess<SEMan, List<StatusEffect>>(___m_seman, "m_statusEffects"))
					{
						if (((Object)statusEffect).name == ((Object)item.m_shared.m_equipStatusEffect).name && (___m_utilityItem == null || ((Object)___m_utilityItem.m_shared.m_equipStatusEffect).name != ((Object)statusEffect).name) && !list2.Exists((ItemData i) => ((Object)i.m_shared.m_equipStatusEffect).name == ((Object)statusEffect).name))
						{
							___m_seman.RemoveStatusEffect(statusEffect.NameHash(), false);
						}
					}
				}
			}
			catch
			{
			}
		}

		private static void Postfix(Humanoid __instance, ItemData ___m_utilityItem, SEMan ___m_seman)
		{
			try
			{
				if (!modEnabled.Value || !((Character)__instance).IsPlayer())
				{
					return;
				}
				List<ItemData> list = __instance.GetInventory().GetAllItems().FindAll((ItemData i) => i.m_equipped && (int)i.m_shared.m_itemType == 18 && i != ___m_utilityItem && Object.op_Implicit((Object)(object)i.m_shared.m_equipStatusEffect));
				foreach (ItemData item in list)
				{
					___m_seman.AddStatusEffect(item.m_shared.m_equipStatusEffect, false, 0, 0f);
				}
			}
			catch
			{
			}
		}
	}

	[HarmonyPatch(typeof(Humanoid), "UnequipAllItems")]
	private static class UnequipAllItems_Patch
	{
		private static void Postfix(Humanoid __instance, ItemData ___m_utilityItem)
		{
			try
			{
				if (!modEnabled.Value || !((Character)__instance).IsPlayer())
				{
					return;
				}
				List<ItemData> list = __instance.GetInventory().GetAllItems().FindAll((ItemData i) => i.m_equipped && (int)i.m_shared.m_itemType == 18 && i != ___m_utilityItem);
				foreach (ItemData item in list)
				{
					__instance.UnequipItem(item, false);
				}
			}
			catch
			{
				Dbgl("Error: " + Environment.StackTrace);
			}
		}
	}

	[HarmonyPatch(typeof(Player), "UnequipDeathDropItems")]
	private static class UnequipDeathDropItems_PatchUnequipItem
	{
		private static void Postfix(Player __instance, ItemData ___m_utilityItem)
		{
			try
			{
				if (!modEnabled.Value)
				{
					return;
				}
				List<ItemData> list = ((Humanoid)__instance).GetInventory().GetAllItems().FindAll((ItemData i) => i.m_equipped && (int)i.m_shared.m_itemType == 18 && i != ___m_utilityItem);
				foreach (ItemData item in list)
				{
					((Humanoid)__instance).UnequipItem(item, false);
				}
			}
			catch
			{
				Dbgl("Error: " + Environment.StackTrace);
			}
		}
	}

	[HarmonyPatch(typeof(ItemData), "GetTooltip", new Type[]
	{
		typeof(ItemData),
		typeof(int),
		typeof(bool),
		typeof(float)
	})]
	private static class GetTooltip_Patch
	{
		private static void Postfix(ref ItemData item, int qualityLevel, float worldLevel, ref string __result)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Invalid comparison between Unknown and I4
			try
			{
				if (modEnabled.Value && (int)item.m_shared.m_itemType == 18)
				{
					__result += $"\n\n$item_armor: <color=orange>{item.GetArmor(qualityLevel, worldLevel)}</color>";
					if (item.m_shared.m_damageModifiers.Count > 0)
					{
						__result += SE_Stats.GetDamageModifiersTooltipString(item.m_shared.m_damageModifiers);
					}
				}
			}
			catch
			{
			}
		}
	}

	[HarmonyPatch(typeof(Terminal), "InputText")]
	private static class InputText_Patch
	{
		private static bool Prefix(Terminal __instance)
		{
			if (!modEnabled.Value)
			{
				return true;
			}
			try
			{
				string text = ((TMP_InputField)__instance.m_input).text;
				if (text.ToLower().Equals(typeof(BepInExPlugin).Namespace.ToLower() + " reset"))
				{
					((BaseUnityPlugin)context).Config.Reload();
					((BaseUnityPlugin)context).Config.Save();
					__instance.AddString(text);
					__instance.AddString(((BaseUnityPlugin)context).Info.Metadata.Name + " config reloaded");
					return false;
				}
			}
			catch
			{
				Dbgl("Error: " + Environment.StackTrace);
			}
			return true;
		}
	}

	private static BepInExPlugin context;

	private Harmony harmony;

	public static ConfigEntry<bool> modEnabled;

	public static ConfigEntry<bool> isDebug;

	public static ConfigEntry<int> nexusID;

	public static ConfigEntry<int> maxEquippedItems;

	public static void Dbgl(string str = "", bool pref = true)
	{
		if (isDebug.Value)
		{
			Debug.Log((object)((pref ? (typeof(BepInExPlugin).Namespace + " ") : "") + str));
		}
	}

	private void Awake()
	{
		//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c9: Expected O, but got Unknown
		context = this;
		modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable this mod");
		isDebug = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "IsDebug", true, "Enable debug logs");
		nexusID = ((BaseUnityPlugin)this).Config.Bind<int>("General", "NexusID", 1348, "Nexus mod ID for updates");
		nexusID.Value = 1348;
		maxEquippedItems = ((BaseUnityPlugin)this).Config.Bind<int>("Variables", "MaxEquippedItems", 5, "Maximum number of utility items equipped at once.");
		if (modEnabled.Value)
		{
			harmony = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
			harmony.PatchAll();
		}
	}
}