using BLHX.Server.Common.Utils; using System.Reflection; using System.Text.RegularExpressions; namespace BLHX.Server.Common.Data; public static class Data { static readonly Logger c = new(nameof(Data), ConsoleColor.Yellow); [LoadData("oilfield_template.json", LoadDataType.ShareCfg)] public static Dictionary OilFieldTemplate { get; private set; } = null!; [LoadData("tradingport_template.json", LoadDataType.ShareCfg)] public static Dictionary GoldFieldTemplate { get; private set; } = null!; [LoadData("chapter_template.json", LoadDataType.ShareCfgData)] public static Dictionary ChapterTemplate { get; private set; } = null!; [LoadData("ship_data_statistics.json", LoadDataType.ShareCfgData)] public static Dictionary ShipDataStatistics { get; private set; } = null!; [LoadData("ship_data_template.json", LoadDataType.ShareCfgData)] public static Dictionary ShipDataTemplate { get; private set; } = null!; [LoadData("task_data_template.json", LoadDataType.ShareCfgData)] public static Dictionary TaskDataTemplate { get; private set; } = null!; public static void Load() { foreach (var prop in typeof(Data).GetProperties().Where(x => x.GetCustomAttribute() is not null)) { var attr = prop.GetCustomAttribute()!; prop.SetValue(null, typeof(JSON).GetMethod("Load")!.MakeGenericMethod(prop.PropertyType).Invoke(null, [Path.Combine(attr.DataType switch { LoadDataType.ShareCfg => JSON.ShareCfgPath, LoadDataType.ShareCfgData => JSON.ShareCfgDataPath, _ => "" }, attr.FileName), false])); c.Warn($"Loaded {prop.Name}"); } c.Log("All data tables loaded"); } static void LoadData(ref Dictionary data, string fileName, string dataName) { try { data = JSON.Load>(Path.Combine(JSON.ShareCfgDataPath, fileName)); c.Warn($"Loaded {data.Count} {dataName}"); } catch (Exception e) { c.Error(e.Message); } } } public enum LoadDataType { ShareCfg, ShareCfgData } [AttributeUsage(AttributeTargets.Property)] public class LoadDataAttribute(string fileName, LoadDataType dataType) : Attribute { public LoadDataType DataType = dataType; public string FileName = fileName; }