Files
bd2/plugins/LocalIdentity/ItemStackRefresh.cs
T

155 lines
5.7 KiB
C#

using System;
using System.Collections;
using System.Reflection;
using System.Threading;
using BD2.GameNames;
using BDNetwork;
using BepInEx.Logging;
using gamfs;
using Google.Protobuf;
using HarmonyLib;
using UnityEngine;
namespace Bd2LocalIdentity;
// The server reshapes item stacks as soon as the console switches an account's
// stack limit, and flags the next reply with Notify field 1000. The native
// client ignores that field; here it reloads the inventories the flag names, so
// merged or split stacks show up without a new login.
internal static class ItemStackRefresh
{
private const int NotifyField = 1000;
private const int PolicyField = 1001;
private const int RefreshItems = 1 << 0;
private const int RefreshLife = 1 << 1;
private static readonly FieldInfo KeepItems = typeof(CommonPacket).GetGameField("_itemKeepInfoDic", BindingFlags.Static | BindingFlags.NonPublic);
private static ManualLogSource Log;
private static int pending;
private static bool reloadingStorage;
internal static void Install(Harmony harmony, MonoBehaviour host, ManualLogSource logger)
{
Log = logger;
if (KeepItems == null)
{
throw new MissingFieldException(nameof(CommonPacket), "_itemKeepInfoDic");
}
InventoryStackPolicy.Install(harmony);
harmony.Patch(typeof(NetworkManager).GetGameMethod("ResponseCheck", BindingFlags.Instance | BindingFlags.NonPublic,
null, [typeof(string), typeof(PacketData), typeof(ResponseData), typeof(byte[])], null),
prefix: new HarmonyMethod(typeof(ItemStackRefresh), nameof(BeforeResponse)));
harmony.Patch(typeof(CommonPacket).GetGameMethod(nameof(CommonPacket.Reset), BindingFlags.Static | BindingFlags.Public),
prefix: new HarmonyMethod(typeof(ItemStackRefresh), nameof(Reset)));
harmony.Patch(typeof(CommonPacket).GetGameMethod(nameof(CommonPacket.SendLoginUserRequest), BindingFlags.Static | BindingFlags.Public),
prefix: new HarmonyMethod(typeof(ItemStackRefresh), nameof(Reset)));
harmony.Patch(Game.Method<NetworkManager>(network => network.Refresh()),
prefix: new HarmonyMethod(typeof(ItemStackRefresh), nameof(Reset)));
harmony.Patch(Game.Method(() => InventoryPacket.RecvItemStorageInfo(null, 0, 0)),
prefix: new HarmonyMethod(typeof(ItemStackRefresh), nameof(BeforeStorage)));
host.StartCoroutine(Reload());
}
private static void Reset()
{
InventoryStackPolicy.Reset();
Interlocked.Exchange(ref pending, 0);
reloadingStorage = false;
}
private static void BeforeResponse(ResponseData __2)
{
if (__2 == null || __2.errorType != 0)
{
return;
}
(int flags, int policy) = ReadNotify(__2.notify);
if (InventoryStackPolicy.Apply(policy))
{
Log.LogInfo("Inventory stack policy applied: unlimited=" + (policy == 2));
}
for (int seen = Volatile.Read(ref pending); flags != 0 && (seen & flags) != flags;)
{
int previous = Interlocked.CompareExchange(ref pending, seen | flags, seen);
if (previous == seen)
{
break;
}
seen = previous;
}
}
private static (int Flags, int Policy) ReadNotify(string notify)
{
if (string.IsNullOrEmpty(notify))
{
return (0, 0);
}
try
{
using var input = new CodedInputStream(Convert.FromBase64String(notify));
int flags = 0;
int policy = 0;
for (uint tag; (tag = input.ReadTag()) != 0;)
{
if (tag == WireFormat.MakeTag(NotifyField, WireFormat.WireType.Varint))
{
flags |= (int)input.ReadUInt64();
}
else if (tag == WireFormat.MakeTag(PolicyField, WireFormat.WireType.Varint))
{
ulong value = input.ReadUInt64();
policy = value == 1 || value == 2 ? (int)value : 1;
}
else
{
input.SkipLastField();
}
}
return (flags, policy);
}
catch (Exception ex) when (ex is FormatException or InvalidProtocolBufferException)
{
// The game itself reports a malformed notify.
}
return (0, 0);
}
// Item storage replies add to the counts the client already holds, so the
// reload starts storage over; the item info reply already does that itself.
private static void BeforeStorage(int __2)
{
if (reloadingStorage && __2 <= 0)
{
((IDictionary)KeepItems.GetValue(null)).Clear();
}
}
private static IEnumerator Reload()
{
while (true)
{
yield return null;
if (Volatile.Read(ref pending) == 0 || SG.Net == null ||
SG.Net.IsBatchModeState != NetworkManager.BatchModeState.NotWorking)
{
continue;
}
int flags = Interlocked.Exchange(ref pending, 0);
Log.LogInfo("Reloading inventory after an item stack change: flags=" + flags);
if ((flags & RefreshItems) != 0)
{
reloadingStorage = true;
using (new NetworkManager.BatchObject(() => reloadingStorage = false))
{
CommonPacket.SendItemInfoRequest();
InventoryPacket.SendItemStorageInfoRequest();
}
}
if ((flags & RefreshLife) != 0)
{
LifePacket.SendLifeInfoRequest(null);
}
}
}
}