using System.Collections.Immutable; using System.Reflection; using System.Text.Json; using NahidaImpact.Common.Data.Excel.Attributes; using NahidaImpact.Common.Data.Provider; using Microsoft.Extensions.Logging; namespace NahidaImpact.Common.Data.Excel; public class ExcelTableCollection { private readonly ImmutableDictionary _tables; public ExcelTableCollection(IAssetProvider assetProvider, ILogger logger) { _tables = LoadTables(assetProvider); logger.LogInformation("Loaded {count} excel tables", _tables.Count); } public TExcel? GetExcel(ExcelType type, uint id) where TExcel : ExcelItem => _tables[type].GetItemById(id); public ExcelTable GetTable(ExcelType type) => _tables[type]; private static ImmutableDictionary LoadTables(IAssetProvider assetProvider) { ImmutableDictionary.Builder tables = ImmutableDictionary.CreateBuilder(); IEnumerable types = Assembly.GetExecutingAssembly().GetTypes() .Where(type => type.GetCustomAttribute() != null); foreach (Type type in types) { ExcelAttribute attribute = type.GetCustomAttribute()!; JsonDocument tableJson = assetProvider.GetExcelTableJson(attribute.AssetName); tables.Add(attribute.Type, new ExcelTable(tableJson, type)); } return tables.ToImmutable(); } }