NahidaImpact/NahidaImpact.Common/Data/Excel/ExcelTableCollection.cs

42 lines
1.5 KiB
C#
Raw Normal View History

2024-01-04 13:48:39 +00:00
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<ExcelType, ExcelTable> _tables;
public ExcelTableCollection(IAssetProvider assetProvider, ILogger<ExcelTableCollection> logger)
{
_tables = LoadTables(assetProvider);
logger.LogInformation("Loaded {count} excel tables", _tables.Count);
}
public TExcel? GetExcel<TExcel>(ExcelType type, uint id) where TExcel : ExcelItem
=> _tables[type].GetItemById<TExcel>(id);
public ExcelTable GetTable(ExcelType type) => _tables[type];
private static ImmutableDictionary<ExcelType, ExcelTable> LoadTables(IAssetProvider assetProvider)
{
ImmutableDictionary<ExcelType, ExcelTable>.Builder tables = ImmutableDictionary.CreateBuilder<ExcelType, ExcelTable>();
IEnumerable<Type> types = Assembly.GetExecutingAssembly().GetTypes()
.Where(type => type.GetCustomAttribute<ExcelAttribute>() != null);
foreach (Type type in types)
{
ExcelAttribute attribute = type.GetCustomAttribute<ExcelAttribute>()!;
JsonDocument tableJson = assetProvider.GetExcelTableJson(attribute.AssetName);
tables.Add(attribute.Type, new ExcelTable(tableJson, type));
}
return tables.ToImmutable();
}
}