using System; using System.Collections; using System.Collections.Generic; using Dalamud; using Dalamud.Game; using Lumina.Excel; namespace KamiLib.Caching; public class LuminaCache : IEnumerable where T : ExcelRow { private readonly Func searchAction; private static LuminaCache? _instance; public static LuminaCache Instance => _instance ??= new LuminaCache(); private LuminaCache(Func? action = null) { searchAction = action ?? (row => Service.DataManager.GetExcelSheet()!.GetRow(row)); } private readonly Dictionary cache = new(); private readonly Dictionary, T> subRowCache = new (); public ExcelSheet OfLanguage(ClientLanguage language) { return Service.DataManager.GetExcelSheet(language)!; } public T? GetRow(uint id) { if (cache.TryGetValue(id, out var value)) { return value; } else { if (searchAction(id) is not { } result) return null; return cache[id] = result; } } public T? GetRow(uint row, uint subRow) { var targetRow = new Tuple(row, subRow); if (subRowCache.TryGetValue(targetRow, out var value)) { return value; } else { if (Service.DataManager.GetExcelSheet()!.GetRow(row, subRow) is not { } result) return null; return subRowCache[targetRow] = result; } } public IEnumerator GetEnumerator() => Service.DataManager.GetExcelSheet()!.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); }