using Elisa.Common.Data.Tables; using Serilog; using System.Collections; using System.Reflection; using System.Text.Json; namespace Elisa.Common.Data; public enum LoadDataType { CatchData, STC, Text } [AttributeUsage(AttributeTargets.Property)] public class LoadDataAttribute(string fileName, LoadDataType dataType) : Attribute { public LoadDataType DataType = dataType; public string FileName = fileName; } public static class Table { #region CatchData [LoadData("gun_exp_info.json", LoadDataType.CatchData)] public static List gun_exp_info { get; private set; } = null!; #endregion #region STC [LoadData("daily.json", LoadDataType.STC)] public static List daily { get; private set; } = null!; [LoadData("equip.json", LoadDataType.STC)] public static List equip { get; private set; } = null!; [LoadData("fairy.json", LoadDataType.STC)] public static List fairy { get; private set; } = null!; [LoadData("furniture.json", LoadDataType.STC)] public static List furniture { get; private set; } = null!; [LoadData("gift_item.json", LoadDataType.STC)] public static List gift_item { get; private set; } = null!; [LoadData("gun.json", LoadDataType.STC)] public static List gun { get; private set; } = null!; [LoadData("item.json", LoadDataType.STC)] public static List item { get; private set; } = null!; [LoadData("mission.json", LoadDataType.STC)] public static List mission { get; private set; } = null!; [LoadData("present.json", LoadDataType.STC)] public static List present { get; private set; } = null!; [LoadData("prize.json", LoadDataType.STC)] public static List prize { get; private set; } = null!; [LoadData("sangvis.json", LoadDataType.STC)] public static List sangvis { get; private set; } = null!; [LoadData("skin.json", LoadDataType.STC)] public static List skin { get; private set; } = null!; [LoadData("spot.json", LoadDataType.STC)] public static List spot { get; private set; } = null!; [LoadData("squad.json", LoadDataType.STC)] public static List squad { get; private set; } = null!; [LoadData("vehicle.json", LoadDataType.STC)] public static List vehicle { get; private set; } = null!; [LoadData("vehicle_component.json", LoadDataType.STC)] public static List vehicle_component { get; private set; } = null!; [LoadData("weekly.json", LoadDataType.STC)] public static List weekly { get; private set; } = null!; #endregion public static void Load() { int catchDataCount = 0; int stcCount = 0; int textCount = 0; foreach (PropertyInfo? prop in typeof(Table).GetProperties().Where(x => x.GetCustomAttribute() is not null)) { LoadDataAttribute attr = prop.GetCustomAttribute()!; switch (attr.DataType) { case LoadDataType.CatchData: object? obj = typeof(JSON).GetMethod(nameof(JSON.Load))!.MakeGenericMethod(typeof(object)).Invoke(null, [Path.Combine(GetPath(attr.DataType), attr.FileName), false]); JsonProperty entry = ((JsonElement)obj).EnumerateObject().First(); object? value = JsonSerializer.Deserialize(entry.Value.GetRawText(), prop.PropertyType, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); prop.SetValue(null, value); Log.Information($"Loaded {(prop.GetValue(null) as IList)?.Count} entries from {prop.Name} catchdata table"); catchDataCount++; break; case LoadDataType.STC: prop.SetValue(null, typeof(JSON).GetMethod(nameof(JSON.Load))!.MakeGenericMethod(prop.PropertyType).Invoke(null, [Path.Combine(GetPath(attr.DataType), attr.FileName), false])); Log.Information($"Loaded {(prop.GetValue(null) as IList)?.Count} entries from {prop.Name} stc table"); stcCount++; break; case LoadDataType.Text: // TODO: textdata is just a flat list of key-value pairs that are looked up from stc textCount++; break; } } Log.Information($"Finished loading data tables ({catchDataCount} catchdata, {stcCount} stc, {textCount} text)"); } public static string GetPath(LoadDataType type) => type switch { LoadDataType.CatchData => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources/catchdata/"), LoadDataType.STC => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources/stc/"), LoadDataType.Text => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources/text/table") }; } public abstract class Model { }