using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace LordValdomodExtraSlots;
[BepInPlugin("LordValdomod.ExtraSlots", "Extra Slots", "1.1.0")]
public class Plugin : BaseUnityPlugin
{
internal static ManualLogSource Log;
internal static ConfigEntry<int> Extra;
internal static ConfigEntry<bool> FirstFreeSlot;
private void Awake()
{
//IL_002e: Unknown result type (might be due to invalid IL or missing references)
//IL_0038: Expected O, but got Unknown
//IL_0062: Unknown result type (might be due to invalid IL or missing references)
Log = ((BaseUnityPlugin)this).Logger;
Extra = ((BaseUnityPlugin)this).Config.Bind<int>("General", "ExtraSlots", 3, new ConfigDescription("Slots added on top of the game total (base, skills and furniture), ignoring the 10 slot cap", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 10), Array.Empty<object>()));
FirstFreeSlot = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AlwaysFirstFreeSlot", true, "Picked items always go to the lowest empty slot instead of the selected one");
new Harmony("LordValdomod.ExtraSlots").PatchAll(typeof(InventoryPatches));
Log.LogInfo((object)("Extra Slots loaded (+" + Extra.Value + ")"));
}
}
[HarmonyPatch]
internal static class InventoryPatches
{
[ThreadStatic]
private static bool pending;
private static readonly FieldInfo SlotsField = AccessTools.Field(typeof(PlayerInventory), "availableSlots");
private static readonly FieldInfo SlotsListField = AccessTools.Field(typeof(PlayerInventory), "itemSlots");
private static readonly MethodInfo HasInAnySlot = AccessTools.Method(typeof(PlayerInventory), "CheckIfHasInAnySlot", (Type[])null, (Type[])null);
[HarmonyPrefix]
[HarmonyPatch(typeof(PlayerInventory), "CheckSlots")]
private static void CheckSlotsPrefix()
{
pending = true;
}
[HarmonyFinalizer]
[HarmonyPatch(typeof(PlayerInventory), "CheckSlots")]
private static Exception CheckSlotsFinalizer(Exception __exception)
{
pending = false;
return __exception;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(PlayerInventory), "AddItem")]
private static bool AddItemPrefix(PlayerInventory __instance, Item item, float dmg, string customProperty)
{
try
{
if (!Plugin.FirstFreeSlot.Value || (Object)(object)item == (Object)null)
{
return true;
}
if (item.canStack && (bool)HasInAnySlot.Invoke(__instance, new object[1] { item }))
{
return true;
}
if (!(SlotsListField.GetValue(__instance) is List<ItemSlot> list))
{
return true;
}
foreach (ItemSlot item2 in list)
{
if ((Object)(object)item2 == (Object)null || (Object)(object)item2.assignedItem != (Object)null)
{
continue;
}
item2.AssignItem(item, dmg, customProperty);
return false;
}
}
catch (Exception ex)
{
Plugin.Log.LogError((object)("First free slot error: " + ex.Message));
}
return true;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(PlayerInventory), "SpawnItemSlots")]
private static void SpawnItemSlotsPrefix(PlayerInventory __instance)
{
if (!pending)
{
return;
}
pending = false;
try
{
SlotsField.SetValue(__instance, (int)SlotsField.GetValue(__instance) + Math.Max(0, Plugin.Extra.Value));
}
catch (Exception ex)
{
Plugin.Log.LogError((object)("Extra Slots error: " + ex.Message));
}
}
}