Elisa/Elisa.Common/Data/Table.cs

134 lines
5.0 KiB
C#
Raw Permalink Normal View History

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
{
2024-05-25 08:20:13 +00:00
#region CatchData
[LoadData("gun_exp_info.json", LoadDataType.CatchData)]
2024-05-25 08:20:13 +00:00
public static List<GunExpInfo> gun_exp_info { get; private set; } = null!;
#endregion
#region STC
[LoadData("daily.json", LoadDataType.STC)]
public static List<Daily> daily { get; private set; } = null!;
2024-05-25 08:20:13 +00:00
[LoadData("equip.json", LoadDataType.STC)]
public static List<Equip> equip { get; private set; } = null!;
[LoadData("fairy.json", LoadDataType.STC)]
public static List<Fairy> fairy { get; private set; } = null!;
[LoadData("furniture.json", LoadDataType.STC)]
public static List<Furniture> furniture { get; private set; } = null!;
[LoadData("gift_item.json", LoadDataType.STC)]
public static List<GiftItem> gift_item { get; private set; } = null!;
2024-05-25 08:20:13 +00:00
[LoadData("gun.json", LoadDataType.STC)]
public static List<Gun> gun { get; private set; } = null!;
2024-05-25 08:20:13 +00:00
[LoadData("item.json", LoadDataType.STC)]
public static List<Item> item { get; private set; } = null!;
[LoadData("mission.json", LoadDataType.STC)]
public static List<Mission> mission { get; private set; } = null!;
[LoadData("present.json", LoadDataType.STC)]
public static List<Present> present { get; private set; } = null!;
2024-05-25 08:20:13 +00:00
[LoadData("prize.json", LoadDataType.STC)]
public static List<Prize> prize { get; private set; } = null!;
[LoadData("sangvis.json", LoadDataType.STC)]
public static List<Sangvis> sangvis { get; private set; } = null!;
[LoadData("skin.json", LoadDataType.STC)]
public static List<Skin> skin { get; private set; } = null!;
[LoadData("spot.json", LoadDataType.STC)]
2024-05-25 08:20:13 +00:00
public static List<Spot> spot { get; private set; } = null!;
[LoadData("squad.json", LoadDataType.STC)]
public static List<Squad> squad { get; private set; } = null!;
2024-05-25 08:20:13 +00:00
[LoadData("vehicle.json", LoadDataType.STC)]
public static List<Vehicle> vehicle { get; private set; } = null!;
[LoadData("vehicle_component.json", LoadDataType.STC)]
public static List<VehicleComponent> vehicle_component { get; private set; } = null!;
[LoadData("weekly.json", LoadDataType.STC)]
public static List<Weekly> weekly { get; private set; } = null!;
2024-05-25 08:20:13 +00:00
#endregion
public static void Load()
{
2024-05-27 00:11:10 +00:00
int catchDataCount = 0;
int stcCount = 0;
int textCount = 0;
foreach (PropertyInfo? prop in typeof(Table).GetProperties().Where(x => x.GetCustomAttribute<LoadDataAttribute>() is not null))
{
LoadDataAttribute attr = prop.GetCustomAttribute<LoadDataAttribute>()!;
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");
2024-05-27 00:11:10 +00:00
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");
2024-05-27 00:11:10 +00:00
stcCount++;
break;
case LoadDataType.Text:
// TODO: textdata is just a flat list of key-value pairs that are looked up from stc
2024-05-27 00:11:10 +00:00
textCount++;
break;
}
}
2024-05-27 00:11:10 +00:00
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
{
}