115 lines
4.2 KiB
C#
115 lines
4.2 KiB
C#
using System;
|
|
using System.Reflection;
|
|
using System.Threading;
|
|
using BD2.GameNames;
|
|
using BDNetwork;
|
|
using HarmonyLib;
|
|
using Proto.Design.common;
|
|
using Proto.Net;
|
|
|
|
namespace Bd2LocalIdentity;
|
|
|
|
internal static class InventoryStackPolicy
|
|
{
|
|
private static int policy;
|
|
|
|
internal static void Reset() => Volatile.Write(ref policy, 0);
|
|
|
|
internal static bool Apply(int value)
|
|
{
|
|
if (value == 0)
|
|
{
|
|
return false;
|
|
}
|
|
int next = value == 2 ? 2 : 1;
|
|
return Interlocked.Exchange(ref policy, next) != next;
|
|
}
|
|
|
|
internal static int EffectiveLimit(int original) => Volatile.Read(ref policy) == 2 ? int.MaxValue : original;
|
|
|
|
internal static void Install(Harmony harmony)
|
|
{
|
|
harmony.Patch(Game.Getter<LifeItemTable, int>(item => item.StackCount),
|
|
postfix: new HarmonyMethod(typeof(InventoryStackPolicy), nameof(LifeStackLimit)));
|
|
harmony.Patch(Game.Method(() => CommonPacket.GetItemCountOfAddition(default, 0, default)),
|
|
prefix: new HarmonyMethod(typeof(InventoryStackPolicy), nameof(RemainingStackRoom)));
|
|
harmony.Patch(typeof(InventoryPacket).GetGameMethod("IsEnoughStorageSlot", BindingFlags.Static | BindingFlags.NonPublic),
|
|
prefix: new HarmonyMethod(typeof(InventoryStackPolicy), nameof(StorageCapacity)));
|
|
harmony.Patch(typeof(InventoryPacket).GetGameMethod("IsEnoughInventorySlot", BindingFlags.Static | BindingFlags.NonPublic),
|
|
prefix: new HarmonyMethod(typeof(InventoryStackPolicy), nameof(InventoryCapacity)));
|
|
}
|
|
|
|
private static void LifeStackLimit(ref int __result)
|
|
{
|
|
if (__result > 0)
|
|
{
|
|
__result = EffectiveLimit(__result);
|
|
}
|
|
}
|
|
|
|
private static bool Stackable(EElementType type) => (int)type is 5 or 8 or 9 or 14;
|
|
|
|
private static long StackRoom(EElementType type, int id, EItemInvenType inventory, int limit)
|
|
{
|
|
long room = 0;
|
|
foreach (ItemDBInfo item in (inventory == EItemInvenType.Inven
|
|
? CommonPacket.GetItemDictionary(type) : CommonPacket.GetKeepItemDictionary(type)).Values)
|
|
{
|
|
if (item.Id == id)
|
|
{
|
|
room += Math.Max(0L, (long)limit - item.Count);
|
|
if (room >= int.MaxValue)
|
|
{
|
|
return int.MaxValue;
|
|
}
|
|
}
|
|
}
|
|
return room;
|
|
}
|
|
|
|
private static bool RemainingStackRoom(EElementType __0, int __1, EItemInvenType __2, ref int __result)
|
|
{
|
|
if (!Stackable(__0))
|
|
{
|
|
return true;
|
|
}
|
|
ItemBaseDTO item = DataManager.GetItemDTO(__0, __1);
|
|
if (item == null || item.StackCount <= 0)
|
|
{
|
|
return true;
|
|
}
|
|
__result = (int)StackRoom(__0, __1, __2, EffectiveLimit(item.StackCount));
|
|
return false;
|
|
}
|
|
|
|
private static bool StorageCapacity(EElementType __0, int __1, int __2, ref bool __result)
|
|
=> Capacity(__0, __1, __2, EItemInvenType.Storeage, ref __result);
|
|
|
|
private static bool InventoryCapacity(EElementType __0, int __1, int __2, ref bool __result)
|
|
=> Capacity(__0, __1, __2, EItemInvenType.Inven, ref __result);
|
|
|
|
private static bool Capacity(EElementType type, int id, int count, EItemInvenType destination, ref bool result)
|
|
{
|
|
if (!Stackable(type) || CommonPacket.UserInfo == null)
|
|
{
|
|
return true;
|
|
}
|
|
ItemBaseDTO item = DataManager.GetItemDTO(type, id);
|
|
if (item == null || item.StackCount <= 0)
|
|
{
|
|
return true;
|
|
}
|
|
int limit = EffectiveLimit(item.StackCount);
|
|
long remaining = Math.Max(0L, (long)count - StackRoom(type, id, destination, limit));
|
|
long needed = remaining / limit + (remaining % limit == 0 ? 0 : 1);
|
|
int used = destination == EItemInvenType.Inven ? CommonPacket.InventoryItemList.Count : CommonPacket.AllItemKeepList.Count;
|
|
int slots = destination == EItemInvenType.Inven ? CommonPacket.UserInfo.InvenSlot : CommonPacket.UserInfo.StorageSlot;
|
|
result = count > 0 && (needed == 0 || used + needed <= slots);
|
|
if (!result)
|
|
{
|
|
UIManager.OpenBarNotice(LocalTextInfo.GetText(destination == EItemInvenType.Inven ? 601 : 600), false, null);
|
|
}
|
|
return false;
|
|
}
|
|
}
|