From dac52cb35e0bb370f9828dd1a07bf95212745732 Mon Sep 17 00:00:00 2001 From: hungdk Date: Mon, 15 Sep 2025 16:12:01 +0700 Subject: [PATCH] Change file name --- .../{InventoryManager.cs => EC_Inventory.cs} | 204 ++++++++++++++++-- .../Scripts/Managers/EC_Inventory.cs.meta | 2 + .../Scripts/Managers/InventoryManager.cs.meta | 2 - 3 files changed, 191 insertions(+), 17 deletions(-) rename Assets/PerfectWorld/Scripts/Managers/{InventoryManager.cs => EC_Inventory.cs} (70%) create mode 100644 Assets/PerfectWorld/Scripts/Managers/EC_Inventory.cs.meta delete mode 100644 Assets/PerfectWorld/Scripts/Managers/InventoryManager.cs.meta diff --git a/Assets/PerfectWorld/Scripts/Managers/InventoryManager.cs b/Assets/PerfectWorld/Scripts/Managers/EC_Inventory.cs similarity index 70% rename from Assets/PerfectWorld/Scripts/Managers/InventoryManager.cs rename to Assets/PerfectWorld/Scripts/Managers/EC_Inventory.cs index 77d544018b..92eb539a53 100644 --- a/Assets/PerfectWorld/Scripts/Managers/InventoryManager.cs +++ b/Assets/PerfectWorld/Scripts/Managers/EC_Inventory.cs @@ -21,7 +21,7 @@ namespace PerfectWorld.Scripts.Managers public byte[] Content; // variable-length item-specific payload (can be null) } - public static class InventoryManager + public static class EC_Inventory { private static readonly Dictionary _packSizeByPackage = new Dictionary(); private static readonly Dictionary> _itemsByPackage = new Dictionary>(); @@ -85,6 +85,28 @@ namespace PerfectWorld.Scripts.Managers { if (data == null) return ""; var t = data.GetType(); + // Prefer decoding the raw fields first to control encoding (Unicode for Vietnamese), + // then fall back to any string properties if needed. + var fieldName = t.GetField("name", BindingFlags.Public | BindingFlags.Instance); + if (fieldName != null && fieldName.FieldType == typeof(ushort[])) + { + var arr = fieldName.GetValue(data) as ushort[]; + // Vietnamese names are stored as wide chars; decode as Unicode first. + var s = ByteToStringUtils.UshortArrayToUnicodeString(arr); + if (!string.IsNullOrEmpty(s)) return s; + // Fallback to legacy CP936 if Unicode was empty + s = ByteToStringUtils.UshortArrayToCP936String(arr); + if (!string.IsNullOrEmpty(s)) return s; + } + + var fieldReal = t.GetField("realname", BindingFlags.Public | BindingFlags.Instance); + if (fieldReal != null && fieldReal.FieldType == typeof(byte[])) + { + var arr = fieldReal.GetValue(data) as byte[]; + var s = ByteToStringUtils.ByteArrayToCP936String(arr); + if (!string.IsNullOrEmpty(s)) return s; + } + var prop = t.GetProperty("Name", BindingFlags.Public | BindingFlags.Instance); if (prop != null && prop.PropertyType == typeof(string)) { @@ -97,20 +119,6 @@ namespace PerfectWorld.Scripts.Managers var val = propReal.GetValue(data) as string; if (!string.IsNullOrEmpty(val)) return val; } - var fieldName = t.GetField("name", BindingFlags.Public | BindingFlags.Instance); - if (fieldName != null && fieldName.FieldType == typeof(ushort[])) - { - var arr = fieldName.GetValue(data) as ushort[]; - var s = ByteToStringUtils.UshortArrayToCP936String(arr); - if (!string.IsNullOrEmpty(s)) return s; - } - var fieldReal = t.GetField("realname", BindingFlags.Public | BindingFlags.Instance); - if (fieldReal != null && fieldReal.FieldType == typeof(byte[])) - { - var arr = fieldReal.GetValue(data) as byte[]; - var s = ByteToStringUtils.ByteArrayToCP936String(arr); - if (!string.IsNullOrEmpty(s)) return s; - } return ""; } @@ -334,6 +342,172 @@ namespace PerfectWorld.Scripts.Managers } } } + + // ====== Public query helpers for items/inventory properties ====== + public static bool TryGetItem(byte byPackage, int slot, out InventoryItemData item) + { + item = null; + if (_itemsByPackage.TryGetValue(byPackage, out var dict) && dict != null) + { + if (dict.TryGetValue(slot, out var it)) + { + item = it; + return true; + } + } + return false; + } + + public static InventoryItemData GetItemOrNull(byte byPackage, int slot) + { + return (_itemsByPackage.TryGetValue(byPackage, out var dict) && dict.TryGetValue(slot, out var it)) ? it : null; + } + + public static IEnumerable GetItemsInPack(byte byPackage) + { + if (_itemsByPackage.TryGetValue(byPackage, out var dict) && dict != null) + { + return dict.Values; + } + return Array.Empty(); + } + + public static int GetUsedSlots(byte byPackage) + { + return _itemsByPackage.TryGetValue(byPackage, out var dict) ? dict.Count : 0; + } + + public static int GetFreeSlots(byte byPackage) + { + int size = GetPackSize(byPackage); + int used = GetUsedSlots(byPackage); + int free = size - used; + return free > 0 ? free : 0; + } + + public static IEnumerable GetSlotsOfTemplate(byte byPackage, int templateId) + { + if (_itemsByPackage.TryGetValue(byPackage, out var dict) && dict != null) + { + foreach (var kv in dict) + { + if (kv.Value != null && kv.Value.TemplateId == templateId) + { + yield return kv.Key; + } + } + } + } + + public static InventoryItemData FindFirstItemByTemplate(byte byPackage, int templateId) + { + if (_itemsByPackage.TryGetValue(byPackage, out var dict) && dict != null) + { + foreach (var kv in dict) + { + if (kv.Value != null && kv.Value.TemplateId == templateId) + { + return kv.Value; + } + } + } + return null; + } + + public static int GetCountOfTemplateInPack(byte byPackage, int templateId) + { + int total = 0; + if (_itemsByPackage.TryGetValue(byPackage, out var dict) && dict != null) + { + foreach (var item in dict.Values) + { + if (item != null && item.TemplateId == templateId) + { + total += item.Count; + } + } + } + return total; + } + + public static int GetTotalUsedSlotsAcrossPacks() + { + int total = 0; + foreach (var dict in _itemsByPackage.Values) + { + total += dict?.Count ?? 0; + } + return total; + } + + public static int GetTotalPackSizeAcrossPacks() + { + int total = 0; + foreach (var kv in _packSizeByPackage) + { + total += kv.Value; + } + return total; + } + + public static int GetTotalFreeSlotsAcrossPacks() + { + int free = 0; + foreach (var kv in _packSizeByPackage) + { + byte pkg = kv.Key; + free += GetFreeSlots(pkg); + } + return free; + } + + public static IEnumerable GetAvailablePackages() + { + return _itemsByPackage.Keys; + } + + public static bool FindFirstTemplateAcrossPacks(int templateId, out byte byPackage, out int slot, out InventoryItemData item) + { + byPackage = 0; + slot = -1; + item = null; + foreach (var pkg in _itemsByPackage) + { + foreach (var kv in pkg.Value) + { + if (kv.Value != null && kv.Value.TemplateId == templateId) + { + byPackage = pkg.Key; + slot = kv.Key; + item = kv.Value; + return true; + } + } + } + return false; + } + + public static int GetTotalCountOfTemplateAcrossPacks(int templateId) + { + int total = 0; + foreach (var dict in _itemsByPackage.Values) + { + foreach (var it in dict.Values) + { + if (it != null && it.TemplateId == templateId) + { + total += it.Count; + } + } + } + return total; + } + + public static string GetTemplateName(int templateId) + { + return ResolveItemName(templateId) ?? string.Empty; + } } } + diff --git a/Assets/PerfectWorld/Scripts/Managers/EC_Inventory.cs.meta b/Assets/PerfectWorld/Scripts/Managers/EC_Inventory.cs.meta new file mode 100644 index 0000000000..2a2e71aaea --- /dev/null +++ b/Assets/PerfectWorld/Scripts/Managers/EC_Inventory.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: b060723330d7f49409ca241f4e460bed \ No newline at end of file diff --git a/Assets/PerfectWorld/Scripts/Managers/InventoryManager.cs.meta b/Assets/PerfectWorld/Scripts/Managers/InventoryManager.cs.meta deleted file mode 100644 index d2ca04007c..0000000000 --- a/Assets/PerfectWorld/Scripts/Managers/InventoryManager.cs.meta +++ /dev/null @@ -1,2 +0,0 @@ -fileFormatVersion: 2 -guid: 135967a674c33d64fb368288f9e719ef \ No newline at end of file