Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/ShipInventoryUpdated/Dependencies/LethalLib/Dependency.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.CompilerServices;
using ShipInventoryUpdated.Helpers.API;

namespace ShipInventoryUpdated.Dependencies.LethalLib;

Expand Down Expand Up @@ -47,13 +48,24 @@ private static void LoadModdedItems()
}

/// <summary>
/// Fetches the modded item associated with the given ID
/// Fetches the modded item associated with the given hashed ID
/// </summary>
[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;
}
}
61 changes: 37 additions & 24 deletions src/ShipInventoryUpdated/Helpers/API/ItemIdentifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,18 @@ namespace ShipInventoryUpdated.Helpers.API;

internal static class ItemIdentifier
{
private const string INVALID_ITEM_ID = "InvalidItem";
private static readonly Dictionary<Item?, string> ItemToHash = new();
private static readonly Dictionary<Item, string> ItemToHash = new();
private static readonly Dictionary<string, Item> HashToItem = new();

/// <summary>
/// Fetches the generic ID of the given item
/// Fetches the hashed ID of the given item
/// </summary>
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();

Expand All @@ -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;
}

/// <summary>
/// Fetches the item associated with the given ID
/// Fetches the item associated with the given hashed ID
/// </summary>
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;
}

/// <summary>
/// Fetches the generic ID of the given item
/// </summary>
private static string GetGenericID(Item item)
{
string? id = null;

if (Dependency.Enabled)
id = Dependency.GetID(item);

return id ?? item.itemName;
}

/// <summary>
/// Fetches the item associated with the given hashed ID
/// </summary>
private static Item? GetItemFromHashedID(string id)
{
Item? item = null;

if (Dependency.Enabled)
item = Dependency.GetItem(id);

Expand All @@ -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;
}
}
12 changes: 5 additions & 7 deletions src/ShipInventoryUpdated/Objects/ItemData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,15 @@ public struct ItemData : INetworkSerializable, IEquatable<ItemData>
/// </summary>
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;
}

/// <summary>
Expand Down