Change file name

This commit is contained in:
hungdk
2025-09-15 16:12:01 +07:00
parent c827dc6ed8
commit dac52cb35e
3 changed files with 191 additions and 17 deletions
@@ -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<byte, int> _packSizeByPackage = new Dictionary<byte, int>();
private static readonly Dictionary<byte, Dictionary<int, InventoryItemData>> _itemsByPackage = new Dictionary<byte, Dictionary<int, InventoryItemData>>();
@@ -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<InventoryItemData> GetItemsInPack(byte byPackage)
{
if (_itemsByPackage.TryGetValue(byPackage, out var dict) && dict != null)
{
return dict.Values;
}
return Array.Empty<InventoryItemData>();
}
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<int> 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<byte> 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;
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: b060723330d7f49409ca241f4e460bed
@@ -1,2 +0,0 @@
fileFormatVersion: 2
guid: 135967a674c33d64fb368288f9e719ef