using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using TMPro;
using Unity.Netcode;
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("OneHandNeeded")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("OneHandNeeded")]
[assembly: AssemblyCopyright("Copyright © 2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("8fd9cbdb-051b-435f-b586-1a9ed221ccda")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace HoppinHauler.OneHandNeeded;
[BepInPlugin("HoppinHauler.OneHandNeeded", "One Hand Needed (Updated)", "2.0.1")]
public sealed class Plugin : BaseUnityPlugin
{
public const string PluginGuid = "HoppinHauler.OneHandNeeded";
public const string PluginName = "One Hand Needed (Updated)";
public const string PluginVersion = "2.0.1";
internal static ManualLogSource Log;
internal static ConfigEntry<int> MaxTwoHandeds;
private Harmony _harmony;
private void Awake()
{
//IL_002f: Unknown result type (might be due to invalid IL or missing references)
//IL_0039: Expected O, but got Unknown
//IL_0044: Unknown result type (might be due to invalid IL or missing references)
//IL_004e: Expected O, but got Unknown
Log = ((BaseUnityPlugin)this).Logger;
MaxTwoHandeds = ((BaseUnityPlugin)this).Config.Bind<int>("General", "MaxTwoHandedItems", 0, new ConfigDescription("How many two-handed items you can carry without 'Hands Full'. 0 = unlimited (never block).", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 10), Array.Empty<object>()));
_harmony = new Harmony("HoppinHauler.OneHandNeeded");
_harmony.PatchAll();
Log.LogInfo((object)"One Hand Needed (Updated) v2.0.1 loaded.");
}
private void OnDestroy()
{
try
{
Harmony harmony = _harmony;
if (harmony != null)
{
harmony.UnpatchSelf();
}
}
catch (Exception arg)
{
ManualLogSource log = Log;
if (log != null)
{
log.LogWarning((object)$"UnpatchSelf error: {arg}");
}
}
}
}
[HarmonyPatch]
internal static class PlayerPatches
{
[HarmonyPatch(typeof(PlayerControllerB), "BeginGrabObject")]
[HarmonyPostfix]
private static void BeginGrabObject_Postfix(PlayerControllerB __instance)
{
CancelTwoHanded(__instance);
}
[HarmonyPatch(typeof(PlayerControllerB), "SwitchToItemSlot")]
[HarmonyPostfix]
private static void SwitchToItemSlot_Postfix(PlayerControllerB __instance)
{
CancelTwoHanded(__instance);
}
[HarmonyPatch(typeof(PlayerControllerB), "GrabObjectClientRpc")]
[HarmonyPostfix]
private static void GrabObjectClientRpc_Postfix(PlayerControllerB __instance)
{
CancelTwoHanded(__instance);
}
private static void CancelTwoHanded(PlayerControllerB player)
{
try
{
if ((Object)(object)player == (Object)null || !player.twoHanded)
{
return;
}
int num = ((Plugin.MaxTwoHandeds != null) ? Plugin.MaxTwoHandeds.Value : 0);
if (IsHoldingLiveEnemy(player))
{
return;
}
if (num == 0)
{
ForceNotTwoHanded(player);
return;
}
int num2 = CountTwoHandedItemsInInventory(player);
if (num2 <= num)
{
ForceNotTwoHanded(player);
}
}
catch (Exception arg)
{
ManualLogSource log = Plugin.Log;
if (log != null)
{
log.LogWarning((object)$"CancelTwoHanded exception: {arg}");
}
}
}
private static void ForceNotTwoHanded(PlayerControllerB player)
{
player.twoHanded = false;
if (!((NetworkBehaviour)player).IsOwner)
{
return;
}
HUDManager instance = HUDManager.Instance;
if (!((Object)(object)instance == (Object)null))
{
TextMeshProUGUI holdingTwoHandedItem = instance.holdingTwoHandedItem;
if (!((Object)(object)holdingTwoHandedItem == (Object)null))
{
((Behaviour)holdingTwoHandedItem).enabled = false;
}
}
}
private static bool IsHoldingLiveEnemy(PlayerControllerB player)
{
GrabbableObject currentlyHeldObjectServer = player.currentlyHeldObjectServer;
if ((Object)(object)currentlyHeldObjectServer == (Object)null)
{
return false;
}
Component val = (Component)(object)currentlyHeldObjectServer;
if ((Object)(object)val == (Object)null)
{
return false;
}
EnemyAI component = val.GetComponent<EnemyAI>();
if ((Object)(object)component == (Object)null)
{
return false;
}
return !component.isEnemyDead;
}
private static int CountTwoHandedItemsInInventory(PlayerControllerB player)
{
int num = 0;
GrabbableObject[] itemSlots = player.ItemSlots;
if (itemSlots == null)
{
return 0;
}
foreach (GrabbableObject val in itemSlots)
{
if (!((Object)(object)val == (Object)null))
{
Item itemProperties = val.itemProperties;
if (!((Object)(object)itemProperties == (Object)null) && itemProperties.twoHanded)
{
num++;
}
}
}
return num;
}
}