this took soo long, but here you are wells

This commit is contained in:
rfi
2024-02-22 16:11:23 +07:00
parent 5d39e1c6be
commit db93c8f49d
18 changed files with 1210 additions and 17 deletions

View File

@@ -1,4 +1,6 @@
using BLHX.Server.Common.Utils;
using System.Reflection;
using System.Text.RegularExpressions;
namespace BLHX.Server.Common.Data;
@@ -6,17 +8,37 @@ public static class Data
{
static readonly Logger c = new(nameof(Data), ConsoleColor.Yellow);
public static Dictionary<int, ChapterTemplate> ChapterTemplate = [];
public static Dictionary<int, ShipDataStatistics> ShipDataStatistics = [];
public static Dictionary<int, ShipDataTemplate> ShipDataTemplate = [];
public static Dictionary<int, TaskDateTemplate> TaskDataTemplate = [];
[LoadData("oilfield_template.json", LoadDataType.ShareCfg)]
public static Dictionary<int, ResourceFieldTemplate> OilFieldTemplate { get; private set; } = null!;
[LoadData("tradingport_template.json", LoadDataType.ShareCfg)]
public static Dictionary<int, ResourceFieldTemplate> GoldFieldTemplate { get; private set; } = null!;
[LoadData("chapter_template.json", LoadDataType.ShareCfgData)]
public static Dictionary<int, ChapterTemplate> ChapterTemplate { get; private set; } = null!;
[LoadData("ship_data_statistics.json", LoadDataType.ShareCfgData)]
public static Dictionary<int, ShipDataStatistics> ShipDataStatistics { get; private set; } = null!;
[LoadData("ship_data_template.json", LoadDataType.ShareCfgData)]
public static Dictionary<int, ShipDataTemplate> ShipDataTemplate { get; private set; } = null!;
[LoadData("task_data_template.json", LoadDataType.ShareCfgData)]
public static Dictionary<int, TaskDateTemplate> TaskDataTemplate { get; private set; } = null!;
public static void Load()
{
LoadData(ref ChapterTemplate, "chapter_template.json", nameof(ChapterTemplate));
LoadData(ref ShipDataStatistics, "ship_data_statistics.json", nameof(ShipDataStatistics));
LoadData(ref ShipDataTemplate, "ship_data_template.json", nameof(ShipDataTemplate));
LoadData(ref TaskDataTemplate, "task_data_template.json", nameof(TaskDataTemplate));
foreach (var prop in typeof(Data).GetProperties().Where(x => x.GetCustomAttribute<LoadDataAttribute>() is not null))
{
var attr = prop.GetCustomAttribute<LoadDataAttribute>()!;
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");
}
@@ -25,7 +47,7 @@ public static class Data
{
try
{
data = JSON.Load<Dictionary<int, T>>(Path.Combine(JSON.ShareConfigPath, fileName));
data = JSON.Load<Dictionary<int, T>>(Path.Combine(JSON.ShareCfgDataPath, fileName));
c.Warn($"Loaded {data.Count} {dataName}");
}
catch (Exception e)
@@ -34,3 +56,16 @@ public static class Data
}
}
}
public enum LoadDataType
{
ShareCfg,
ShareCfgData
}
[AttributeUsage(AttributeTargets.Property)]
public class LoadDataAttribute(string fileName, LoadDataType dataType) : Attribute
{
public LoadDataType DataType = dataType;
public string FileName = fileName;
}

View File

@@ -1,12 +1,14 @@
using System.Text.Json;
using System.Text.RegularExpressions;
namespace BLHX.Server.Common.Data;
public static class JSON
public static partial class JSON
{
public static JsonSerializerOptions serializerOptions = new() { IncludeFields = true, WriteIndented = true };
public static string ConfigPath => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.json");
public static string ShareConfigPath => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources\\sharecfgdata\\");
public static string ShareCfgDataPath => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources/sharecfgdata/");
public static string ShareCfgPath => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources/ShareCfg/");
public static T Load<T>(string path, bool create = true) where T : new()
{
@@ -16,7 +18,13 @@ public static class JSON
Save(path, obj);
}
return JsonSerializer.Deserialize<T>(File.ReadAllText(path), serializerOptions) ?? new T();
string text = File.ReadAllText(path);
if (typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(Dictionary<,>) && typeof(T).GetGenericArguments()[0] == typeof(int))
{
text = DictKeyAll().Replace(text, "");
}
return JsonSerializer.Deserialize<T>(text, serializerOptions) ?? new T();
}
public static void Save<T>(string path, T obj)
@@ -28,4 +36,7 @@ public static class JSON
{
return JsonSerializer.Serialize(obj, serializerOptions);
}
[GeneratedRegex(@",[\n\s\t]+?""all"":[\s\S]+?]", RegexOptions.Multiline)]
public static partial Regex DictKeyAll();
}

View File

@@ -0,0 +1,28 @@
using System.Text.Json.Serialization;
namespace BLHX.Server.Common.Data
{
public class ResourceFieldTemplate : Model
{
[JsonPropertyName("hour_time")]
public uint HourTime { get; set; }
[JsonPropertyName("level")]
public uint Level { get; set; }
[JsonPropertyName("production")]
public uint Production { get; set; }
[JsonPropertyName("store")]
public uint Store { get; set; }
[JsonPropertyName("time")]
public uint Time { get; set; }
[JsonPropertyName("use")]
public List<uint> Use { get; set; } = [];
[JsonPropertyName("user_level")]
public uint UserLevel { get; set; }
}
}