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 HarmonyLib;
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("ItemStackerFix")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ItemStackerFix")]
[assembly: AssemblyCopyright("Copyright © 2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("68fef771-f28c-4880-bfed-027323d829a8")]
[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")]
[BepInPlugin("com.travv.aleandtale.itemstack99safe", "ItemStack99 Safe (Shop UI + Buy Fixed)", "1.0.4")]
public class ItemStack99Safe : BaseUnityPlugin
{
internal const ushort NewMax = 99;
internal static readonly Dictionary<ushort, ushort> OriginalMaxById = new Dictionary<ushort, ushort>();
[ThreadStatic]
internal static bool InShopBuy;
private void Awake()
{
//IL_0099: Unknown result type (might be due to invalid IL or missing references)
ItemData[] array = Resources.FindObjectsOfTypeAll<ItemData>();
int num = 0;
ItemData[] array2 = array;
foreach (ItemData val in array2)
{
if (!((Object)(object)val == (Object)null))
{
if (!OriginalMaxById.ContainsKey(val.id))
{
OriginalMaxById[val.id] = val.maxStack;
}
if (val.maxStack > 1 && val.maxStack < 99)
{
val.maxStack = 99;
num++;
}
}
}
new Harmony("com.travv.aleandtale.itemstack99safe").PatchAll();
((BaseUnityPlugin)this).Logger.LogInfo((object)$"ItemStack99Safe loaded. Updated {num}/{array.Length} ItemData maxStack values to {(ushort)99}.");
}
internal static ushort GetOriginalMax(ushort itemDataId)
{
ushort value;
return (ushort)((!OriginalMaxById.TryGetValue(itemDataId, out value) || value <= 0) ? 1 : value);
}
internal static ushort GetDefaultShopAmount(ItemData data)
{
if ((Object)(object)data == (Object)null)
{
return 1;
}
return (ushort)(data.buyByOne ? 1 : GetOriginalMax(data.id));
}
}
[HarmonyPatch(typeof(ItemManager), "BuyServerRpc")]
public static class Patch_ItemManager_BuyServerRpc_Flag
{
private static void Prefix()
{
ItemStack99Safe.InShopBuy = true;
}
private static void Postfix()
{
ItemStack99Safe.InShopBuy = false;
}
}
[HarmonyPatch(typeof(ContainerNet), "AddNewItem", new Type[]
{
typeof(Item),
typeof(Vector3),
typeof(bool)
})]
public static class Patch_ContainerNet_AddNewItem_ShopAmount
{
private static void Prefix(ref Item item)
{
ItemData val = default(ItemData);
if (ItemStack99Safe.InShopBuy && (Object)(object)ItemManager.Instance != (Object)null && ItemManager.Instance.GetItemData((uint)item.dataId, ref val) && (Object)(object)val != (Object)null)
{
item.amount = ItemStack99Safe.GetDefaultShopAmount(val);
}
}
}
[HarmonyPatch(typeof(ShopItemUI), "SetItemData")]
public static class Patch_ShopItemUI_SetItemData_ShowDefault
{
[ThreadStatic]
private static ItemData _swapped;
[ThreadStatic]
private static ushort _saved;
private static void Prefix(ItemData itemData)
{
_swapped = null;
if (!((Object)(object)itemData == (Object)null))
{
_swapped = itemData;
_saved = itemData.maxStack;
itemData.maxStack = ItemStack99Safe.GetDefaultShopAmount(itemData);
}
}
private static void Postfix()
{
if ((Object)(object)_swapped != (Object)null)
{
_swapped.maxStack = _saved;
_swapped = null;
}
}
}