Make runtime item catalog key-only

This commit is contained in:
Meta Vens 2026-06-15 01:31:16 +01:00
commit ffe9d6d7bf
7 changed files with 20 additions and 63 deletions

View file

@ -6,7 +6,6 @@ using GameCult.Aetheria.State.Unity;
public interface IRuntimeItemCatalogReader
{
AetheriaRuntimeCatalogItem GetRuntimeItem(string itemKey);
AetheriaRuntimeCatalogItem GetRuntimeItem(Guid guid);
}
public sealed class AetheriaRuntimeItemCatalog : IRuntimeItemCatalogReader
@ -15,19 +14,14 @@ public sealed class AetheriaRuntimeItemCatalog : IRuntimeItemCatalogReader
private const string LegacyItemDefinitionPrefix = "aetheria.item_definition:legacy:";
private readonly Dictionary<string, AetheriaRuntimeCatalogItem> _typedItemsByKey;
private readonly Dictionary<Guid, AetheriaRuntimeCatalogItem> _typedItemsByLegacyId;
public AetheriaRuntimeItemCatalog(AetheriaRuntimeCatalogSnapshot catalog)
{
if (catalog == null) throw new ArgumentNullException(nameof(catalog));
_typedItemsByKey = catalog.Items
.Where(item => !string.IsNullOrWhiteSpace(item.LegacyId))
.ToDictionary(item => ToItemKey(item.LegacyId), item => item, StringComparer.OrdinalIgnoreCase);
_typedItemsByLegacyId = catalog.Items
.Where(item => Guid.TryParse(item.LegacyId, out _))
.ToDictionary(item => Guid.Parse(item.LegacyId), item => item);
.Where(item => !string.IsNullOrWhiteSpace(item.ItemKey))
.ToDictionary(item => item.ItemKey, item => item, StringComparer.OrdinalIgnoreCase);
}
public AetheriaRuntimeCatalogItem GetRuntimeItem(string itemKey)
@ -39,21 +33,10 @@ public sealed class AetheriaRuntimeItemCatalog : IRuntimeItemCatalogReader
return item;
return Guid.TryParse(RemoveItemPrefix(itemKey), out var legacyId)
? GetRuntimeItem(legacyId)
? GetRuntimeItem(AetheriaRuntimeItemReference.FromLegacyId(legacyId))
: null;
}
public AetheriaRuntimeCatalogItem GetRuntimeItem(Guid guid)
{
_typedItemsByLegacyId.TryGetValue(guid, out var item);
return item;
}
private static string ToItemKey(string legacyId)
{
return string.IsNullOrWhiteSpace(legacyId) ? "" : $"{LegacyItemDefinitionPrefix}{legacyId}";
}
private static string RemoveItemPrefix(string itemKey)
{
if (itemKey.StartsWith(LegacyItemDefinitionPrefix, StringComparison.OrdinalIgnoreCase))

View file

@ -26,11 +26,6 @@ public class AetheriaRuntimeItemReference
ItemKey = itemKey;
}
public AetheriaRuntimeItemReference(Guid legacyItemId)
: this(FromLegacyId(legacyItemId))
{
}
public Guid LegacyItemId => TryParseLegacyId(ItemKey, out var itemId) ? itemId : Guid.Empty;
public static string FromLegacyId(Guid legacyItemId)

View file

@ -347,16 +347,7 @@ public abstract class Entity
private static string GetItemKey(AetheriaRuntimeCatalogItem item)
{
return GetLegacyGuid(item) is var itemId && itemId != Guid.Empty
? AetheriaRuntimeItemReference.FromLegacyId(itemId)
: "";
}
private static Guid GetLegacyGuid(AetheriaRuntimeCatalogItem item)
{
return item != null && Guid.TryParse(item.LegacyId, out var legacyId)
? legacyId
: Guid.Empty;
return item?.ItemKey ?? "";
}
private void MapEntity()

View file

@ -56,12 +56,7 @@ public class ItemManager
? null
: !string.IsNullOrWhiteSpace(item.ItemKey)
? _runtimeItems.GetRuntimeItem(item.ItemKey)
: GetRuntimeItem(item.ItemId);
}
public AetheriaRuntimeCatalogItem GetRuntimeItem(Guid itemId)
{
return itemId == Guid.Empty ? null : _runtimeItems.GetRuntimeItem(itemId);
: null;
}
public Behavior[] CreateRuntimeBehaviors(EquippedItem item)
@ -548,16 +543,9 @@ public class ItemManager
return new AetheriaRuntimeItemReference(ToItemKey(item));
}
public AetheriaRuntimeItemReference CreateReference(Guid itemId)
{
return new AetheriaRuntimeItemReference(itemId);
}
private static string ToItemKey(AetheriaRuntimeCatalogItem item)
{
return item != null && Guid.TryParse(item.LegacyId, out var itemId)
? AetheriaRuntimeItemReference.FromLegacyId(itemId)
: "";
return item?.ItemKey ?? "";
}
public void Log(string s)
@ -646,8 +634,7 @@ public class ItemManager
public SimpleCommodity CreateSimpleCommodityInstance(AetheriaRuntimeCatalogItem item, int count)
{
var itemId = GetLegacyGuid(item);
if (itemId != Guid.Empty)
if (!string.IsNullOrWhiteSpace(item?.ItemKey))
{
return new SimpleCommodity
{
@ -687,8 +674,7 @@ public class ItemManager
if (IsEquippable(item))
return CreateEquippableInstance(item, quality);
var itemId = GetLegacyGuid(item);
if (itemId == Guid.Empty)
if (string.IsNullOrWhiteSpace(item?.ItemKey))
{
throw new NullReferenceException("Attempted to create crafted item instance using missing typed item data!");
}
@ -716,8 +702,7 @@ public class ItemManager
public EquippableItem CreateEquippableInstance(AetheriaRuntimeCatalogItem item, float quality)
{
var itemId = GetLegacyGuid(item);
if (itemId == Guid.Empty)
if (string.IsNullOrWhiteSpace(item?.ItemKey))
{
throw new NullReferenceException("Attempted to create equippable item instance using missing typed item data!");
}
@ -748,13 +733,6 @@ public class ItemManager
return tier.Quality;
}
private static Guid GetLegacyGuid(AetheriaRuntimeCatalogItem item)
{
return item != null && Guid.TryParse(item.LegacyId, out var itemId)
? itemId
: Guid.Empty;
}
private static bool IsEquippable(AetheriaRuntimeCatalogItem item)
{
return !string.IsNullOrWhiteSpace(item?.HardpointType) ||