修改调整
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using NBC.Serialize;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace NBC
|
||||
{
|
||||
@@ -10,24 +11,47 @@ namespace NBC
|
||||
/// </summary>
|
||||
public static class ConfigTableHelper
|
||||
{
|
||||
private static string _configTableBinaryDirectory;
|
||||
// private static string _configTableBinaryDirectory;
|
||||
// private static string _jsonData;
|
||||
|
||||
private static readonly object Lock = new object();
|
||||
|
||||
// 配置表数据缓存字典
|
||||
private static readonly Dictionary<string, ASerialize> ConfigDic = new ();
|
||||
private static readonly Dictionary<string, ASerialize> ConfigDic = new();
|
||||
|
||||
private static readonly Dictionary<string, JArray> Tokens = new();
|
||||
|
||||
/// <summary>
|
||||
/// 初始化ConfigTableHelper
|
||||
/// </summary>
|
||||
/// <param name="configTableBinaryDirectory"></param>
|
||||
public static void Initialize(string configTableBinaryDirectory)
|
||||
public static void Initialize(string json)
|
||||
{
|
||||
_configTableBinaryDirectory = configTableBinaryDirectory;
|
||||
// _jsonData = json;
|
||||
var jsonObj = JObject.Parse(json);
|
||||
foreach (var item in jsonObj)
|
||||
{
|
||||
try
|
||||
{
|
||||
var name = item.Key;
|
||||
var value = item.Value;
|
||||
if (value is JArray jArray)
|
||||
{
|
||||
Tokens[name] = jArray;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error($"读表异常,请检查,name={item.Key} ex={e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载配置表数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T">配置表类型</typeparam>
|
||||
/// <returns>配置表数据</returns>
|
||||
public static T Load<T>() where T : ASerialize
|
||||
public static T Load<T>() where T : ASerialize, new()
|
||||
{
|
||||
lock (Lock)
|
||||
{
|
||||
@@ -39,13 +63,19 @@ namespace NBC
|
||||
{
|
||||
return (T)aProto;
|
||||
}
|
||||
|
||||
var configFile = GetConfigPath(dataConfig);
|
||||
var bytes = File.ReadAllBytes(configFile);
|
||||
// Log.Debug($"dataConfig:{dataConfig} {bytes.Length}");
|
||||
var data = SerializerManager.GetSerializer(FantasySerializerType.ProtoBuf).Deserialize<T>(bytes);
|
||||
ConfigDic[dataConfig] = data;
|
||||
return data;
|
||||
|
||||
var configName = typeof(T).Name;
|
||||
if (!Tokens.TryGetValue(configName, out var jArray))
|
||||
{
|
||||
// jArray.ToObject<List<T>>()
|
||||
// return new T();
|
||||
}
|
||||
|
||||
// var configFile = GetConfigPath(dataConfig);
|
||||
// var bytes = File.ReadAllBytes(configFile);
|
||||
// var data = SerializerManager.GetSerializer(FantasySerializerType.ProtoBuf).Deserialize<T>(bytes);
|
||||
// ConfigDic[dataConfig] = data;
|
||||
return new T();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -53,38 +83,5 @@ namespace NBC
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取配置表文件路径
|
||||
/// </summary>
|
||||
/// <param name="name">配置表名称</param>
|
||||
/// <returns>配置表文件路径</returns>
|
||||
private static string GetConfigPath(string name)
|
||||
{
|
||||
var configFile = Path.Combine(_configTableBinaryDirectory, $"{name}.bytes");
|
||||
|
||||
if (File.Exists(configFile))
|
||||
{
|
||||
return configFile;
|
||||
}
|
||||
|
||||
throw new FileNotFoundException($"{name}.byte not found: {configFile}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重新加载配置表数据
|
||||
/// </summary>
|
||||
public static void ReLoadConfigTable()
|
||||
{
|
||||
lock (Lock)
|
||||
{
|
||||
foreach (var (_, aProto) in ConfigDic)
|
||||
{
|
||||
((IDisposable) aProto).Dispose();
|
||||
}
|
||||
|
||||
ConfigDic.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user