74 lines
2.4 KiB
C#
74 lines
2.4 KiB
C#
using System.Collections.Generic;
|
||
using System.Linq;
|
||
|
||
namespace cfg
|
||
{
|
||
/// <summary>
|
||
/// TbFishingEventCycleItem 的扩展方法
|
||
/// 提供通过物品ID反向查询活动配置的能力
|
||
/// </summary>
|
||
public static class TbFishingEventCycleItemExtensions
|
||
{
|
||
// 按物品ID索引的缓存,用于反向查询
|
||
private static Dictionary<int, FishingEventCycleItem> _itemIdIndex;
|
||
private static readonly object _lock = new object();
|
||
|
||
/// <summary>
|
||
/// 初始化物品ID索引(在 Tables 初始化后调用)
|
||
/// </summary>
|
||
public static void BuildItemIdIndex(this TbFishingEventCycleItem table)
|
||
{
|
||
lock (_lock)
|
||
{
|
||
if (_itemIdIndex != null) return; // 已经初始化过
|
||
|
||
_itemIdIndex = new Dictionary<int, FishingEventCycleItem>();
|
||
|
||
foreach (var item in table.DataList)
|
||
{
|
||
if (item.ItemId > 0)
|
||
{
|
||
// 如果有重复的 ItemId,使用第一个找到的
|
||
if (!_itemIdIndex.ContainsKey(item.ItemId))
|
||
{
|
||
_itemIdIndex[item.ItemId] = item;
|
||
}
|
||
}
|
||
}
|
||
|
||
#if UNITY_EDITOR
|
||
UnityEngine.Debug.Log($"[TbFishingEventCycleItemExtensions] Built item ID index with {_itemIdIndex.Count} entries");
|
||
#endif
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据物品ID查找对应的活动周期配置
|
||
/// </summary>
|
||
/// <param name="table">配置表实例</param>
|
||
/// <param name="itemId">物品ID</param>
|
||
/// <returns>活动周期配置,如果未找到返回 null</returns>
|
||
public static FishingEventCycleItem FindByItemId(this TbFishingEventCycleItem table, int itemId)
|
||
{
|
||
// 确保索引已初始化
|
||
if (_itemIdIndex == null)
|
||
{
|
||
table.BuildItemIdIndex();
|
||
}
|
||
|
||
return _itemIdIndex.TryGetValue(itemId, out var config) ? config : null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 清空索引缓存(用于测试或重新加载配置时)
|
||
/// </summary>
|
||
public static void ClearItemIdIndex()
|
||
{
|
||
lock (_lock)
|
||
{
|
||
_itemIdIndex = null;
|
||
}
|
||
}
|
||
}
|
||
}
|