134 lines
4.1 KiB
C#
134 lines
4.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace PerfectWorld.Scripts.Managers
|
|
{
|
|
|
|
public static class EC_Inventory
|
|
{
|
|
private static readonly Dictionary<byte, int> _packSizeByPackage = new Dictionary<byte, int>();
|
|
private static readonly Dictionary<byte, Dictionary<int, InventoryItemData>> _itemsByPackage = new Dictionary<byte, Dictionary<int, InventoryItemData>>();
|
|
private const int MaxContentHexToLog = 64;
|
|
|
|
private static string GetPackageName(byte pkg)
|
|
{
|
|
switch (pkg)
|
|
{
|
|
case 0: return "PACK_INVENTORY";
|
|
case 1: return "PACK_EQUIPMENT";
|
|
case 2: return "PACK_TASKINVENTORY";
|
|
default: return "PACK_UNKNOWN";
|
|
}
|
|
}
|
|
|
|
private static string BytesToHex(byte[] bytes, int max)
|
|
{
|
|
if (bytes == null || bytes.Length == 0) return "";
|
|
int len = Math.Min(bytes.Length, max);
|
|
string hex = BitConverter.ToString(bytes, 0, len);
|
|
if (bytes.Length > len) hex += "-...";
|
|
return hex;
|
|
}
|
|
|
|
|
|
|
|
public static void UpdatePack(byte byPackage, int ivtrSize, IEnumerable<InventoryItemData> items)
|
|
{
|
|
_packSizeByPackage[byPackage] = ivtrSize;
|
|
if (!_itemsByPackage.TryGetValue(byPackage, out var slots))
|
|
{
|
|
slots = new Dictionary<int, InventoryItemData>();
|
|
_itemsByPackage[byPackage] = slots;
|
|
}
|
|
slots.Clear();
|
|
if (items != null)
|
|
{
|
|
foreach (var it in items)
|
|
{
|
|
if (it != null && it.Slot >= 0)
|
|
{
|
|
slots[it.Slot] = it;
|
|
}
|
|
}
|
|
}
|
|
// Log this pack's items
|
|
LogPackInternal(byPackage, ivtrSize, slots);
|
|
}
|
|
|
|
|
|
private static void LogPackInternal(byte byPackage, int ivtrSize, IReadOnlyDictionary<int, InventoryItemData> slots)
|
|
{
|
|
Debug.Log($"[Inventory] === Pack {GetPackageName(byPackage)}({byPackage}) size={ivtrSize}, items={(slots?.Count ?? 0)} ===");
|
|
if (slots == null || slots.Count == 0)
|
|
{
|
|
Debug.Log("[Inventory] (empty)");
|
|
return;
|
|
}
|
|
foreach (var kv in slots)
|
|
{
|
|
var it = kv.Value;
|
|
string itemName = EC_IvtrItem.ResolveItemName(it.TemplateId);
|
|
string extraHex = it.Content != null && it.Content.Length > 0 ? EC_IvtrItem.BytesToHex(it.Content, MaxContentHexToLog) : "";
|
|
int extraLen = it.Content?.Length ?? 0;
|
|
Debug.Log(
|
|
$"[Inventory] pkg={GetPackageName(it.Package)}({it.Package}) slot={it.Slot} tid={it.TemplateId}{(string.IsNullOrEmpty(itemName) ? "" : " \"" + itemName + "\"")} count={it.Count} state={it.State} expire={it.ExpireDate} crc={it.Crc} content_len={extraLen}{(extraLen > 0 ? ", content_hex=" + extraHex : "")}"
|
|
);
|
|
}
|
|
}
|
|
|
|
public static void LogInventoryPacket(string tag, byte[] buffer, int hostId)
|
|
{
|
|
if (buffer == null)
|
|
{
|
|
Debug.LogWarning($"[Inventory] {tag}: buffer is null (hostId={hostId})");
|
|
return;
|
|
}
|
|
|
|
int index = 0;
|
|
if (buffer.Length < 6)
|
|
{
|
|
Debug.LogWarning($"[Inventory] {tag}: buffer too small: {buffer.Length} bytes (hostId={hostId})");
|
|
LogInventoryRaw(tag, buffer);
|
|
return;
|
|
}
|
|
|
|
byte byPackage = buffer[index++];
|
|
byte ivtrSize = buffer[index++];
|
|
uint contentLength = BitConverter.ToUInt32(buffer, index); index += 4;
|
|
|
|
int remaining = buffer.Length - index;
|
|
int contentBytes = remaining;
|
|
if (contentLength < (uint)remaining)
|
|
{
|
|
contentBytes = (int)contentLength;
|
|
}
|
|
|
|
Debug.Log($"[Inventory] {tag}: hostId={hostId}, totalBytes={buffer.Length}, byPackage={byPackage}, ivtrSize={ivtrSize}, contentLength={contentLength}, actualContentBytes={contentBytes}");
|
|
|
|
if (contentBytes > 0)
|
|
{
|
|
byte[] content = new byte[contentBytes];
|
|
Buffer.BlockCopy(buffer, index, content, 0, contentBytes);
|
|
Debug.Log($"[Inventory] {tag}: content HEX=\n{BitConverter.ToString(content)}");
|
|
}
|
|
|
|
int trailing = buffer.Length - (index + contentBytes);
|
|
if (trailing > 0)
|
|
{
|
|
byte[] tail = new byte[trailing];
|
|
Buffer.BlockCopy(buffer, index + contentBytes, tail, 0, trailing);
|
|
Debug.Log($"[Inventory] {tag}: trailing {trailing} byte(s) HEX=\n{BitConverter.ToString(tail)}");
|
|
}
|
|
}
|
|
|
|
public static void LogInventoryRaw(string tag, byte[] buffer)
|
|
{
|
|
Debug.Log($"[Inventory] {tag}: RAW HEX (len={buffer?.Length ?? 0})=\n{(buffer == null ? "<null>" : BitConverter.ToString(buffer))}");
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|