导表工具修改
This commit is contained in:
3
Assets/Scripts/NBC/Runtime/Core/Config.meta
Normal file
3
Assets/Scripts/NBC/Runtime/Core/Config.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5ccc2499679c40909ac59a0a325c78c4
|
||||
timeCreated: 1759160041
|
||||
90
Assets/Scripts/NBC/Runtime/Core/Config/ConfigTableHelper.cs
Normal file
90
Assets/Scripts/NBC/Runtime/Core/Config/ConfigTableHelper.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using NBC.Serialize;
|
||||
|
||||
namespace NBC
|
||||
{
|
||||
/// <summary>
|
||||
/// 配置表帮助类
|
||||
/// </summary>
|
||||
public static class ConfigTableHelper
|
||||
{
|
||||
private static string _configTableBinaryDirectory;
|
||||
private static readonly object Lock = new object();
|
||||
// 配置表数据缓存字典
|
||||
private static readonly Dictionary<string, ASerialize> ConfigDic = new ();
|
||||
/// <summary>
|
||||
/// 初始化ConfigTableHelper
|
||||
/// </summary>
|
||||
/// <param name="configTableBinaryDirectory"></param>
|
||||
public static void Initialize(string configTableBinaryDirectory)
|
||||
{
|
||||
_configTableBinaryDirectory = configTableBinaryDirectory;
|
||||
}
|
||||
/// <summary>
|
||||
/// 加载配置表数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T">配置表类型</typeparam>
|
||||
/// <returns>配置表数据</returns>
|
||||
public static T Load<T>() where T : ASerialize
|
||||
{
|
||||
lock (Lock)
|
||||
{
|
||||
try
|
||||
{
|
||||
var dataConfig = typeof(T).Name;
|
||||
|
||||
if (ConfigDic.TryGetValue(dataConfig, out var aProto))
|
||||
{
|
||||
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;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception($"ConfigTableManage:{typeof(T).Name} 数据表加载之后反序列化时出错:{ex}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: de53a788ec4e446eb6c07c50c65afcc2
|
||||
timeCreated: 1759160093
|
||||
7
Assets/Scripts/NBC/Runtime/Core/Config/IConfigTable.cs
Normal file
7
Assets/Scripts/NBC/Runtime/Core/Config/IConfigTable.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace NBC
|
||||
{
|
||||
/// <summary>
|
||||
/// 表示是一个配置文件
|
||||
/// </summary>
|
||||
public interface IConfigTable { }
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: df4eb038440a4a498e27ccaf23a1cd9b
|
||||
timeCreated: 1759160060
|
||||
Reference in New Issue
Block a user