Move runtime item references to typed keys

This commit is contained in:
Meta Vens 2026-06-14 22:58:02 +01:00
commit 8337d5f5c7
9 changed files with 153 additions and 60 deletions

View file

@ -5,25 +5,62 @@ using GameCult.Aetheria.State.Unity;
public interface IRuntimeItemCatalogReader
{
AetheriaRuntimeCatalogItem GetRuntimeItem(string itemKey);
AetheriaRuntimeCatalogItem GetRuntimeItem(Guid guid);
}
public sealed class AetheriaRuntimeItemCatalog : IRuntimeItemCatalogReader
{
private readonly Dictionary<Guid, AetheriaRuntimeCatalogItem> _typedItems;
private const string ItemDefinitionPrefix = "aetheria.item_definition:";
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));
_typedItems = catalog.Items
_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);
}
public AetheriaRuntimeCatalogItem GetRuntimeItem(string itemKey)
{
if (string.IsNullOrWhiteSpace(itemKey))
return null;
if (_typedItemsByKey.TryGetValue(itemKey, out var item))
return item;
return Guid.TryParse(RemoveItemPrefix(itemKey), out var legacyId)
? GetRuntimeItem(legacyId)
: null;
}
public AetheriaRuntimeCatalogItem GetRuntimeItem(Guid guid)
{
_typedItems.TryGetValue(guid, out var item);
_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))
return itemKey.Substring(LegacyItemDefinitionPrefix.Length);
return itemKey.StartsWith(ItemDefinitionPrefix, StringComparison.OrdinalIgnoreCase)
? itemKey.Substring(ItemDefinitionPrefix.Length)
: itemKey;
}
}

View file

@ -0,0 +1,60 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
using System;
using Unity.Mathematics;
public interface INamedEntry
{
string EntryName { get; set; }
}
public class AetheriaRuntimeItemReference
{
private const string ItemDefinitionPrefix = "aetheria.item_definition:";
private const string LegacyItemDefinitionPrefix = "aetheria.item_definition:legacy:";
public string ItemKey;
public AetheriaRuntimeItemReference()
{
}
public AetheriaRuntimeItemReference(string itemKey)
{
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)
{
return legacyItemId == Guid.Empty ? "" : $"{LegacyItemDefinitionPrefix}{legacyItemId:D}";
}
private static bool TryParseLegacyId(string itemKey, out Guid itemId)
{
itemId = Guid.Empty;
if (string.IsNullOrWhiteSpace(itemKey))
return false;
var legacyId = itemKey.StartsWith(LegacyItemDefinitionPrefix, StringComparison.OrdinalIgnoreCase)
? itemKey.Substring(LegacyItemDefinitionPrefix.Length)
: itemKey.StartsWith(ItemDefinitionPrefix, StringComparison.OrdinalIgnoreCase)
? itemKey.Substring(ItemDefinitionPrefix.Length)
: itemKey;
return Guid.TryParse(legacyId, out itemId);
}
}
public interface ITintInspector
{
float3 TintColor { get; }
}

View file

@ -12,10 +12,11 @@ using static Unity.Mathematics.math;
public abstract class ItemInstance
{
public RuntimeItemDefinitionReference Reference;
public AetheriaRuntimeItemReference Reference;
public ItemRotation Rotation;
public Guid ItemId => Reference?.ItemId ?? Guid.Empty;
public string ItemKey => Reference?.ItemKey ?? "";
public Guid ItemId => Reference?.LegacyItemId ?? Guid.Empty;
}
public abstract class CraftedItemInstance : ItemInstance

View file

@ -52,7 +52,11 @@ public class ItemManager
public AetheriaRuntimeCatalogItem GetRuntimeItem(ItemInstance item)
{
return GetRuntimeItem(item?.ItemId ?? Guid.Empty);
return item == null
? null
: !string.IsNullOrWhiteSpace(item.ItemKey)
? _runtimeItems.GetRuntimeItem(item.ItemKey)
: GetRuntimeItem(item.ItemId);
}
public AetheriaRuntimeCatalogItem GetRuntimeItem(Guid itemId)
@ -532,9 +536,21 @@ public class ItemManager
return shape;
}
public RuntimeItemDefinitionReference CreateReference(Guid itemId)
public AetheriaRuntimeItemReference CreateReference(AetheriaRuntimeCatalogItem item)
{
return new RuntimeItemDefinitionReference(itemId);
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)
: "";
}
public void Log(string s)
@ -628,7 +644,7 @@ public class ItemManager
{
return new SimpleCommodity
{
Reference = CreateReference(itemId),
Reference = CreateReference(item),
Quantity = count
};
}
@ -674,14 +690,14 @@ public class ItemManager
{
return new ConsumableItem
{
Reference = CreateReference(itemId),
Reference = CreateReference(item),
Quality = quality
};
}
return new CompoundCommodity
{
Reference = CreateReference(itemId),
Reference = CreateReference(item),
Quality = quality
};
}
@ -701,7 +717,7 @@ public class ItemManager
return new EquippableItem
{
Reference = CreateReference(itemId),
Reference = CreateReference(item),
Quality = quality,
Durability = item.Durability > 0 ? (float)item.Durability : 1f
};

View file

@ -1,30 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
using System;
using Unity.Mathematics;
public interface INamedEntry
{
string EntryName { get; set; }
}
public class RuntimeItemDefinitionReference
{
public Guid ItemId;
public RuntimeItemDefinitionReference()
{
}
public RuntimeItemDefinitionReference(Guid itemId)
{
ItemId = itemId;
}
}
public interface ITintInspector
{
float3 TintColor { get; }
}