Decompiled source of UniversalGrasp v0.1.1

plugins\UniversalGrasp.dll

Decompiled a month ago
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using EquipmentAndQuickSlots;
using HarmonyLib;
using Jotunn;
using Jotunn.Entities;
using Jotunn.Managers;
using Jotunn.Utils;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("JotunnModStub")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("JotunnModStub")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e3243d22-4307-4008-ba36-9f326008cde5")]
[assembly: AssemblyFileVersion("0.0.1.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.1.0")]
namespace UniversalGrasp
{
	[BepInPlugin("org.bepinex.plugins.bid.universalgrasp", "UniversalGrasp", "0.1.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	internal class UniversalGrasp : BaseUnityPlugin
	{
		public const string PluginGUID = "org.bepinex.plugins.bid.universalgrasp";

		public const string PluginName = "UniversalGrasp";

		public const string PluginVersion = "0.1.1";

		public static ConfigEntry<bool> EnabledConfig;

		private readonly Harmony harmony = new Harmony("org.bepinex.plugins.bid.universalgrasp");

		public static CustomLocalization Localization = LocalizationManager.Instance.GetLocalization();

		private void Awake()
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			Logger.LogInfo((object)"UniversalGrasp has landed");
			CreateConfigValues();
			AddInputs();
			new Harmony("org.bepinex.plugins.bid.universalgrasp").PatchAll();
			CreatureManager.OnVanillaCreaturesAvailable += ModifyAndCloneVanillaCreatures;
		}

		private void OnDestroy()
		{
			harmony.UnpatchSelf();
		}

		private void AddInputs()
		{
		}

		private void CreateConfigValues()
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Expected O, but got Unknown
			((BaseUnityPlugin)this).Config.SaveOnConfigSet = true;
			EnabledConfig = ((BaseUnityPlugin)this).Config.Bind<bool>("Client config", "enabled", true, new ConfigDescription("enable or disable the mod", (AcceptableValueBase)null, Array.Empty<object>()));
		}

		private void Update()
		{
			if (ZInput.instance != null && ZInput.GetButtonDown("Hide") && (Object)(object)Player.m_localPlayer != (Object)null)
			{
				bool flag = false;
				if (((Humanoid)Player.m_localPlayer).m_rightItem == null && ((Humanoid)Player.m_localPlayer).m_visEquipment.m_rightItem != null)
				{
					((Humanoid)Player.m_localPlayer).m_visEquipment.SetRightItem((string)null);
					flag = true;
				}
				if (((Humanoid)Player.m_localPlayer).m_leftItem == null && ((Humanoid)Player.m_localPlayer).m_visEquipment.m_leftItem != null)
				{
					((Humanoid)Player.m_localPlayer).m_visEquipment.SetLeftItem((string)null, -1);
					flag = true;
				}
				if (flag)
				{
					((Character)Player.m_localPlayer).m_zanim.SetTrigger("equip_hip");
				}
			}
		}

		private void ModifyAndCloneVanillaCreatures()
		{
			CreatureManager.OnVanillaCreaturesAvailable -= ModifyAndCloneVanillaCreatures;
		}

		private static GameObject GetTransform(GameObject item, string name)
		{
			Transform val = item.transform.Find(name);
			if (!Object.op_Implicit((Object)(object)val))
			{
				return null;
			}
			return ((Component)val).gameObject;
		}

		private static bool TwoHandedEquipped(Player player)
		{
			return false;
		}

		public static void SetRightItem(Player player, ItemData item)
		{
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Invalid comparison between Unknown and I4
			if (!Utility.IsNullOrWhiteSpace(((Humanoid)player).m_visEquipment.m_rightItem) || TwoHandedEquipped(Player.m_localPlayer))
			{
				((Humanoid)Player.m_localPlayer).UseItem((Inventory)(object)InventoryExtensions.Extended(((Humanoid)Player.m_localPlayer).m_inventory), item, true);
				if ((int)item.m_shared.m_itemType == 2 && ((Humanoid)player).m_visEquipment.m_rightItem == ((Object)item.m_dropPrefab).name)
				{
					((Humanoid)player).m_visEquipment.SetRightItem((string)null);
				}
			}
			else
			{
				((Humanoid)player).UnequipItem(((Humanoid)player).m_rightItem, false);
				((Humanoid)player).m_visEquipment.SetRightItem(((Object)item.m_dropPrefab).name);
				((Character)player).m_zanim.SetTrigger("equip_hip");
			}
		}

		public static void SetLeftItem(Player player, ItemData item)
		{
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Invalid comparison between Unknown and I4
			if (!Utility.IsNullOrWhiteSpace(((Humanoid)player).m_visEquipment.m_leftItem) || TwoHandedEquipped(Player.m_localPlayer))
			{
				((Humanoid)Player.m_localPlayer).UseItem((Inventory)(object)InventoryExtensions.Extended(((Humanoid)Player.m_localPlayer).m_inventory), item, true);
				if ((int)item.m_shared.m_itemType == 2 && ((Humanoid)player).m_visEquipment.m_leftItem == ((Object)item.m_dropPrefab).name)
				{
					((Humanoid)player).m_visEquipment.SetLeftItem((string)null, -1);
				}
			}
			else
			{
				((Humanoid)player).UnequipItem(((Humanoid)player).m_leftItem, false);
				((Humanoid)player).m_visEquipment.SetLeftItem(((Object)item.m_dropPrefab).name, -1);
				((Character)player).m_zanim.SetTrigger("equip_hip");
			}
		}

		public static GameObject GetAttach(GameObject item)
		{
			GameObject transform = GetTransform(item, "attach");
			if ((Object)(object)transform != (Object)null && transform.GetComponentsInChildren<Renderer>().Count() > 0)
			{
				return transform;
			}
			transform = GetChildModel(item);
			if ((Object)(object)transform != (Object)null)
			{
				return transform;
			}
			return item;
		}

		public static GameObject GetAttachObjectLegacy(GameObject item)
		{
			Collider componentInChildren = ((Component)item.transform).GetComponentInChildren<Collider>();
			if (!Object.op_Implicit((Object)(object)componentInChildren))
			{
				return null;
			}
			return ((Component)((Component)componentInChildren).transform).gameObject;
		}

		public static GameObject GetChildModel(GameObject item)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Expected O, but got Unknown
			GameObject val = null;
			foreach (Transform item2 in item.transform)
			{
				Transform val2 = item2;
				if (((Component)val2).gameObject.layer == item.layer)
				{
					if (Object.op_Implicit((Object)(object)val))
					{
						return null;
					}
					val = ((Component)val2).gameObject;
				}
			}
			if (Object.op_Implicit((Object)(object)val))
			{
				return val;
			}
			foreach (Transform item3 in item.transform)
			{
				Transform val3 = item3;
				if (Object.op_Implicit((Object)(object)val))
				{
					return null;
				}
				val = ((Component)val3).gameObject;
			}
			return val;
		}

		public static void CleanUpGameObject(GameObject item, bool removeall = true)
		{
			if (removeall)
			{
				Rigidbody[] componentsInChildren = item.GetComponentsInChildren<Rigidbody>();
				for (int i = 0; i < componentsInChildren.Length; i++)
				{
					Object.Destroy((Object)(object)componentsInChildren[i]);
				}
				MonoBehaviour[] componentsInChildren2 = item.GetComponentsInChildren<MonoBehaviour>();
				for (int i = 0; i < componentsInChildren2.Length; i++)
				{
					Object.Destroy((Object)(object)componentsInChildren2[i]);
				}
			}
			VisEquipment.CleanupInstance(item);
		}
	}
}
namespace UniversalGrasp.patches
{
	[HarmonyPatch(typeof(Player), "Update")]
	public class Humanoid_HideHandItems_Patch
	{
	}
	[HarmonyPatch(typeof(InventoryGui_Awake_Patch), "OnRightClicked", new Type[] { typeof(InventoryGui) })]
	public class InventoryGui_OnRightClick_Patch
	{
		private static bool Prefix(InventoryGui inventoryGui, ref Action<InventoryGrid, ItemData, Vector2i> __result)
		{
			if (!UniversalGrasp.EnabledConfig.Value)
			{
				return true;
			}
			__result = delegate(InventoryGrid inventoryGrid, ItemData item, Vector2i pos)
			{
				//IL_0014: Unknown result type (might be due to invalid IL or missing references)
				bool flag = false;
				if (((Object)inventoryGrid).name == "QuickSlotGrid")
				{
					switch (pos.x)
					{
					case 0:
						UniversalGrasp.SetRightItem(Player.m_localPlayer, item);
						flag = true;
						break;
					case 1:
						UniversalGrasp.SetLeftItem(Player.m_localPlayer, item);
						flag = true;
						break;
					}
				}
				if (!flag && item != null && !((Object)(object)Player.m_localPlayer == (Object)null))
				{
					((Humanoid)Player.m_localPlayer).UseItem((Inventory)(object)InventoryExtensions.Extended(((Humanoid)Player.m_localPlayer).m_inventory), item, true);
				}
			};
			return false;
		}
	}
	[HarmonyPatch(typeof(HotkeyBar_UpdateIcons_Patch), "UpdateIconBinding", new Type[]
	{
		typeof(HotkeyBar),
		typeof(int),
		typeof(ElementData)
	})]
	public class CustomHotkeyBar_UpdateIconBinding_patch
	{
		private static void Postfix(HotkeyBar instance, int index, ElementData elementData)
		{
			if (UniversalGrasp.EnabledConfig.Value && !(((Object)instance).name != "QuickSlotsHotkeyBar"))
			{
				TMP_Text component = ((Component)elementData.m_go.transform.Find("binding")).GetComponent<TMP_Text>();
				component.alignment = (TextAlignmentOptions)4097;
				component.fontSize = 12f;
			}
		}
	}
	[HarmonyPatch(typeof(InventoryGrid), "UpdateGui", new Type[]
	{
		typeof(Player),
		typeof(ItemData)
	})]
	public class InventoryGrid_UpdateGui_Patch
	{
		private static void Postfix(InventoryGrid __instance)
		{
			if (UniversalGrasp.EnabledConfig.Value && !(((Object)__instance).name != "QuickSlotGrid"))
			{
				for (int i = 0; i < 2; i++)
				{
					TMP_Text component = ((Component)__instance.m_elements[i].m_go.transform.Find("binding")).GetComponent<TMP_Text>();
					component.alignment = (TextAlignmentOptions)4097;
					component.fontSize = 12f;
				}
			}
		}
	}
	[HarmonyPatch(typeof(EquipmentAndQuickSlots), "GetBindingLabel", new Type[] { typeof(int) })]
	public class EquipmentAndQuickSlots_GetBindingLabel_patch
	{
		private static void Postfix(int index, ref string __result)
		{
			if (UniversalGrasp.EnabledConfig.Value)
			{
				index = Mathf.Clamp(index, 0, 2);
				switch (index)
				{
				case 0:
					__result += " (RH)";
					break;
				case 1:
					__result += " (LH)";
					break;
				}
			}
		}
	}
	[HarmonyPatch(typeof(Humanoid), "DropItem", new Type[]
	{
		typeof(Inventory),
		typeof(ItemData),
		typeof(int)
	})]
	public class Humanoid_DropItem_patch
	{
		private static void Postfix(Inventory inventory, ItemData item, int amount, Humanoid __instance)
		{
			if (UniversalGrasp.EnabledConfig.Value)
			{
				if (((Object)item.m_dropPrefab).name == __instance.m_visEquipment.m_leftItem)
				{
					__instance.m_visEquipment.SetLeftItem((string)null, -1);
				}
				else if (((Object)item.m_dropPrefab).name == __instance.m_visEquipment.m_rightItem)
				{
					__instance.m_visEquipment.SetRightItem((string)null);
				}
			}
		}
	}
	[HarmonyPatch(typeof(InventoryGrid), "DropItem", new Type[]
	{
		typeof(Inventory),
		typeof(ItemData),
		typeof(int),
		typeof(Vector2i)
	})]
	public class InventoryGrid_DropItem_patch
	{
	}
	[HarmonyPatch(typeof(VisEquipment), "AttachItem", new Type[]
	{
		typeof(int),
		typeof(int),
		typeof(Transform),
		typeof(bool),
		typeof(bool)
	})]
	public class VisEquipment_AttachItem_patch
	{
		private static bool Prefix(VisEquipment __instance, ref GameObject __result, int itemHash, int variant, Transform joint, bool enableEquipEffects = true, bool backAttach = false)
		{
			//IL_02f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0309: Unknown result type (might be due to invalid IL or missing references)
			//IL_0284: Unknown result type (might be due to invalid IL or missing references)
			//IL_0295: Unknown result type (might be due to invalid IL or missing references)
			if (!UniversalGrasp.EnabledConfig.Value)
			{
				return true;
			}
			GameObject itemPrefab = ObjectDB.instance.GetItemPrefab(itemHash);
			if ((Object)(object)itemPrefab == (Object)null)
			{
				ZLog.Log((object)("Missing attach item: " + itemHash + "  ob:" + ((Object)((Component)__instance).gameObject).name + "  joint:" + (Object.op_Implicit((Object)(object)joint) ? ((Object)joint).name : "none")));
				__result = null;
				return false;
			}
			if (((Object)itemPrefab).name.Equals("Lantern"))
			{
				return true;
			}
			if (((Object)itemPrefab).name.Equals("Lantern") && ((Object)joint).name == "RightHand_Attach")
			{
				return true;
			}
			bool flag = false;
			ItemDrop component = itemPrefab.GetComponent<ItemDrop>();
			if ((Object)(object)component != (Object)null)
			{
				ItemData itemData = component.m_itemData;
				if (itemData != null)
				{
					flag = itemData.IsWeapon();
				}
			}
			GameObject val = null;
			if ((Object)(object)joint == (Object)null)
			{
				return true;
			}
			MonoBehaviour.print((object)("joint: " + ((Object)joint).name));
			if ((!(((Object)joint).name == "LeftHand_Attach") && !(((Object)joint).name == "RightHand_Attach")) || flag)
			{
				int childCount = itemPrefab.transform.childCount;
				for (int i = 0; i < childCount; i++)
				{
					Transform child = itemPrefab.transform.GetChild(i);
					if (backAttach && ((Object)((Component)child).gameObject).name == "attach_back")
					{
						val = ((Component)child).gameObject;
						break;
					}
					if ((((Object)((Component)child).gameObject).name == "attach" || (!backAttach && ((Object)((Component)child).gameObject).name == "attach_skin")) && ((Component)child).GetComponentsInChildren<Renderer>().Count() > 0)
					{
						val = ((Component)child).gameObject;
						break;
					}
				}
			}
			if ((Object)(object)val == (Object)null)
			{
				val = UniversalGrasp.GetAttach(itemPrefab);
				if ((Object)(object)val == (Object)null)
				{
					__result = null;
					return false;
				}
			}
			ZNetView.m_forceDisableInit = true;
			GameObject val2 = Object.Instantiate<GameObject>(val);
			val2.SetActive(true);
			UniversalGrasp.CleanUpGameObject(val2);
			if (enableEquipEffects)
			{
				VisEquipment.EnableEquippedEffects(val2);
			}
			if (((Object)val).name == "attach_skin" && ((!(((Object)joint).name == "LeftHand_Attach") && !(((Object)joint).name == "RightHand_Attach")) || flag))
			{
				val2.transform.SetParent(((Component)__instance.m_bodyModel).transform.parent);
				val2.transform.localPosition = Vector3.zero;
				val2.transform.localRotation = Quaternion.identity;
				SkinnedMeshRenderer[] componentsInChildren = val2.GetComponentsInChildren<SkinnedMeshRenderer>();
				foreach (SkinnedMeshRenderer obj in componentsInChildren)
				{
					obj.rootBone = __instance.m_bodyModel.rootBone;
					obj.bones = __instance.m_bodyModel.bones;
				}
			}
			else
			{
				val2.transform.SetParent(joint);
				val2.transform.localPosition = Vector3.zero;
				val2.transform.localRotation = Quaternion.identity;
			}
			IEquipmentVisual componentInChildren = val2.GetComponentInChildren<IEquipmentVisual>();
			if (componentInChildren != null)
			{
				componentInChildren.Setup(variant);
			}
			__result = val2;
			ZNetView.m_forceDisableInit = false;
			return false;
		}
	}
	[HarmonyPatch(typeof(EquipmentAndQuickSlots), "CheckQuickUseInput", new Type[]
	{
		typeof(Player),
		typeof(int)
	})]
	internal class EquipmentAndQuickSlots_CheckQuickUseInput_patch
	{
		private static bool Prefix(Player player, int index, EquipmentAndQuickSlots __instance)
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			if (!UniversalGrasp.EnabledConfig.Value)
			{
				return true;
			}
			if (ZInput.GetKeyDown(EquipmentAndQuickSlots.GetBindingKeycode(index), true))
			{
				ItemData quickSlotItem = PlayerExtensions.GetQuickSlotItem(player, index);
				if (quickSlotItem != null)
				{
					switch (index)
					{
					case 0:
						UniversalGrasp.SetRightItem(player, quickSlotItem);
						break;
					case 1:
						UniversalGrasp.SetLeftItem(player, quickSlotItem);
						break;
					default:
						((Humanoid)player).UseItem((Inventory)null, quickSlotItem, false);
						break;
					}
					return false;
				}
			}
			return true;
		}
	}
}