diff --git a/src/ShipInventoryUpdated/Dependencies/LethalLib/Dependency.cs b/src/ShipInventoryUpdated/Dependencies/LethalLib/Dependency.cs index 40e6a12..15d5ddc 100644 --- a/src/ShipInventoryUpdated/Dependencies/LethalLib/Dependency.cs +++ b/src/ShipInventoryUpdated/Dependencies/LethalLib/Dependency.cs @@ -1,4 +1,5 @@ using System.Runtime.CompilerServices; +using ShipInventoryUpdated.Helpers.API; namespace ShipInventoryUpdated.Dependencies.LethalLib; @@ -47,13 +48,24 @@ private static void LoadModdedItems() } /// - /// Fetches the modded item associated with the given ID + /// Fetches the modded item associated with the given hashed ID /// [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)] public static Item? GetItem(string id) { LoadModdedItems(); - return _cachedModdedItems?.GetValueOrDefault(id); + if (_cachedModdedItems == null) + return null; + + foreach ((_, var moddedItem) in _cachedModdedItems) + { + var hashedId = ItemIdentifier.GetID(moddedItem); + + if (hashedId == id) + return moddedItem; + } + + return null; } } \ No newline at end of file diff --git a/src/ShipInventoryUpdated/Helpers/API/ItemIdentifier.cs b/src/ShipInventoryUpdated/Helpers/API/ItemIdentifier.cs index 12b83e1..3473062 100644 --- a/src/ShipInventoryUpdated/Helpers/API/ItemIdentifier.cs +++ b/src/ShipInventoryUpdated/Helpers/API/ItemIdentifier.cs @@ -7,27 +7,18 @@ namespace ShipInventoryUpdated.Helpers.API; internal static class ItemIdentifier { - private const string INVALID_ITEM_ID = "InvalidItem"; - private static readonly Dictionary ItemToHash = new(); + private static readonly Dictionary ItemToHash = new(); private static readonly Dictionary HashToItem = new(); /// - /// Fetches the generic ID of the given item + /// Fetches the hashed ID of the given item /// - public static string GetID(Item? item) + public static string GetID(Item item) { - if (item == null) - return INVALID_ITEM_ID; - if (ItemToHash.TryGetValue(item, out var hashedId)) return hashedId; - string? id = null; - - if (Dependency.Enabled) - id = Dependency.GetID(item); - - id ??= item.itemName; + var id = GetGenericID(item); using var sha256Hash = SHA256.Create(); @@ -36,23 +27,51 @@ public static string GetID(Item? item) hashedId = Encoding.Default.GetString(hashedData); hashedId = new FixedString32Bytes(hashedId).ToString(); - ItemToHash.Add(item, hashedId); + ItemToHash.TryAdd(item, hashedId); HashToItem.TryAdd(hashedId, item); return hashedId; } /// - /// Fetches the item associated with the given ID + /// Fetches the item associated with the given hashed ID /// public static Item? GetItem(string id) { - if (id == INVALID_ITEM_ID) - return null; - if (HashToItem.TryGetValue(id, out var item)) return item; + item = GetItemFromHashedID(id); + + if (item == null) + return null; + + HashToItem.TryAdd(id, item); + ItemToHash.TryAdd(item, id); + + return item; + } + + /// + /// Fetches the generic ID of the given item + /// + private static string GetGenericID(Item item) + { + string? id = null; + + if (Dependency.Enabled) + id = Dependency.GetID(item); + + return id ?? item.itemName; + } + + /// + /// Fetches the item associated with the given hashed ID + /// + private static Item? GetItemFromHashedID(string id) + { + Item? item = null; + if (Dependency.Enabled) item = Dependency.GetItem(id); @@ -62,12 +81,6 @@ public static string GetID(Item? item) item = itemList.FirstOrDefault(i => GetID(i) == id); } - if (item == null) - return null; - - HashToItem.Add(id, item); - ItemToHash.TryAdd(item, id); - return item; } } \ No newline at end of file diff --git a/src/ShipInventoryUpdated/Objects/ItemData.cs b/src/ShipInventoryUpdated/Objects/ItemData.cs index 4135de5..89dee9f 100644 --- a/src/ShipInventoryUpdated/Objects/ItemData.cs +++ b/src/ShipInventoryUpdated/Objects/ItemData.cs @@ -34,17 +34,15 @@ public struct ItemData : INetworkSerializable, IEquatable /// public bool PERSISTED_THROUGH_ROUNDS; - public ItemData() : this(null) { } - - public ItemData(GrabbableObject? item) + public ItemData(GrabbableObject item) { - ID = ItemIdentifier.GetID(item?.itemProperties); - SCRAP_VALUE = item?.scrapValue ?? 0; + ID = ItemIdentifier.GetID(item.itemProperties); + SCRAP_VALUE = item.scrapValue; - if (item?.itemProperties != null && item.itemProperties.saveItemVariable) + if (item.itemProperties.saveItemVariable) SAVE_DATA = item.GetItemDataToSave(); - PERSISTED_THROUGH_ROUNDS = item?.scrapPersistedThroughRounds ?? false; + PERSISTED_THROUGH_ROUNDS = item.scrapPersistedThroughRounds; } ///