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 HarmonyLib;
using Jotunn;
using Jotunn.Utils;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("UniversalGraspCore")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("UniversalGraspCore")]
[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.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.1.0")]
namespace UniversalGraspCore
{
[BepInPlugin("org.bepinex.plugins.bid.universalgraspcore", "UniversalGraspCore", "0.0.1")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
internal class UniversalGraspCore : BaseUnityPlugin
{
public const string PluginGUID = "org.bepinex.plugins.bid.universalgraspcore";
public const string PluginName = "UniversalGraspCore";
public const string PluginVersion = "0.0.1";
private readonly Harmony harmony = new Harmony("org.bepinex.plugins.bid.universalgraspcore");
private void Awake()
{
Logger.LogInfo((object)"UniversalGraspCore has landed");
harmony.PatchAll();
}
private void OnDestroy()
{
harmony.UnpatchSelf();
}
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;
}
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;
}
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 UniversalGraspCore.Patches
{
[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 (((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(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_027a: Unknown result type (might be due to invalid IL or missing references)
//IL_028b: Unknown result type (might be due to invalid IL or missing references)
//IL_0206: Unknown result type (might be due to invalid IL or missing references)
//IL_0217: Unknown result type (might be due to invalid IL or missing references)
GameObject itemPrefab = ObjectDB.instance.GetItemPrefab(itemHash);
if ((Object)(object)itemPrefab == (Object)null)
{
__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;
}
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 = UniversalGraspCore.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);
UniversalGraspCore.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;
}
}
}