更新最新框架
This commit is contained in:
85
Entity/Modules/ConfigTable/ConfigContext.cs
Normal file
85
Entity/Modules/ConfigTable/ConfigContext.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
namespace NBF.ConfigTable;
|
||||
|
||||
public interface IConfigContext
|
||||
{
|
||||
// 定义非泛型接口
|
||||
}
|
||||
|
||||
public class ConfigContext<T> : IConfigContext where T : IConfigTable
|
||||
{
|
||||
private static List<T> _cacheList = new List<T>();
|
||||
|
||||
#region Cache
|
||||
|
||||
public void Association(List<T> list)
|
||||
{
|
||||
if (list != null)
|
||||
{
|
||||
_cacheList = list;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public int Count()
|
||||
{
|
||||
return _cacheList.Count;
|
||||
}
|
||||
|
||||
public int Count(Func<T, bool> predicate)
|
||||
{
|
||||
return _cacheList.Count(predicate);
|
||||
}
|
||||
|
||||
public T Get(uint key)
|
||||
{
|
||||
return First(key);
|
||||
}
|
||||
|
||||
public T Fist()
|
||||
{
|
||||
return _cacheList.First();
|
||||
}
|
||||
|
||||
public T Last()
|
||||
{
|
||||
return _cacheList.Last();
|
||||
}
|
||||
|
||||
public T Fist(Predicate<T> match)
|
||||
{
|
||||
return Get(match);
|
||||
}
|
||||
|
||||
public T Last(Predicate<T> match)
|
||||
{
|
||||
return _cacheList.FindLast(match);
|
||||
}
|
||||
|
||||
public T Get(Predicate<T> match)
|
||||
{
|
||||
return _cacheList.Find(match);
|
||||
}
|
||||
|
||||
public T GetRandom()
|
||||
{
|
||||
Random random = new Random();
|
||||
// 随机从列表中取一个对象
|
||||
return _cacheList[random.Next(_cacheList.Count)];
|
||||
}
|
||||
|
||||
public List<T> GetList()
|
||||
{
|
||||
return _cacheList;
|
||||
}
|
||||
|
||||
public List<T> GetList(Predicate<T> match)
|
||||
{
|
||||
return _cacheList.FindAll(match);
|
||||
}
|
||||
|
||||
private T First(uint key)
|
||||
{
|
||||
return _cacheList.Find(t => t.Key == key);
|
||||
}
|
||||
}
|
||||
122
Entity/Modules/ConfigTable/ConfigTableHelper.cs
Normal file
122
Entity/Modules/ConfigTable/ConfigTableHelper.cs
Normal file
@@ -0,0 +1,122 @@
|
||||
using System.Reflection;
|
||||
using Fantasy;
|
||||
using Fantasy.Helper;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
|
||||
// ReSharper disable SuspiciousTypeConversion.Global
|
||||
|
||||
namespace NBF.ConfigTable
|
||||
{
|
||||
/// <summary>
|
||||
/// 配置表帮助类
|
||||
/// </summary>
|
||||
public static class ConfigTableHelper
|
||||
{
|
||||
private static readonly Dictionary<Type, IConfigContext> _dictionary = new Dictionary<Type, IConfigContext>();
|
||||
|
||||
public static void Initialize()
|
||||
{
|
||||
//解析配置文件
|
||||
var gameConfigText = File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "configs.Json"));
|
||||
|
||||
Initialize(gameConfigText, typeof(Fantasy.AssemblyHelper).Assembly);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化ConfigTableHelper
|
||||
/// </summary>
|
||||
public static void Initialize(string json, params System.Reflection.Assembly[] assemblies)
|
||||
{
|
||||
_dictionary.Clear();
|
||||
var jsonObj = JObject.Parse(json);
|
||||
Dictionary<string, JArray> tokens = new();
|
||||
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}");
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var type in GetAllConfigTableTypes(assemblies))
|
||||
{
|
||||
var name = type.Name;
|
||||
if (tokens.TryGetValue(name, out var jArray))
|
||||
{
|
||||
// 通过反射调用 ParseJson 方法
|
||||
var parseMethod = type.GetMethod("ParseJson", BindingFlags.Public | BindingFlags.Static);
|
||||
parseMethod?.Invoke(null, [jArray]);
|
||||
}
|
||||
}
|
||||
|
||||
// var d = _dictionary;
|
||||
}
|
||||
|
||||
public static ConfigContext<T>? Table<T>() where T : IConfigTable
|
||||
{
|
||||
var type = typeof(T);
|
||||
if (_dictionary.TryGetValue(type, out var context))
|
||||
{
|
||||
return context as ConfigContext<T>;
|
||||
}
|
||||
|
||||
var jsonContext = new ConfigContext<T>();
|
||||
_dictionary[type] = jsonContext;
|
||||
return jsonContext;
|
||||
}
|
||||
|
||||
public static List<T> ParseLine<T>(JArray arr) where T : IConfigTable, new()
|
||||
{
|
||||
List<T> list = new List<T>();
|
||||
foreach (var jToken in arr)
|
||||
{
|
||||
T? instance = jToken.ToObject<T>();
|
||||
|
||||
if (instance != null)
|
||||
{
|
||||
list.Add(instance);
|
||||
}
|
||||
}
|
||||
|
||||
var context = Table<T>();
|
||||
context?.Association(list);
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有实现了 IConfigTable 接口的非抽象类
|
||||
/// </summary>
|
||||
/// <returns>所有非抽象的配置对象类</returns>
|
||||
private static List<Type> GetAllConfigTableTypes(params System.Reflection.Assembly[] assemblies)
|
||||
{
|
||||
var types = new List<Type>();
|
||||
var interfaceType = typeof(IConfigTable);
|
||||
|
||||
// 遍历当前程序集中的所有类型
|
||||
foreach (var assembly in assemblies)
|
||||
{
|
||||
foreach (var type in assembly.GetTypes())
|
||||
{
|
||||
// 检查是否实现了 IConfigTable 接口,并且不是抽象类
|
||||
if (interfaceType.IsAssignableFrom(type) && !type.IsAbstract && !type.IsInterface)
|
||||
{
|
||||
types.Add(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return types;
|
||||
}
|
||||
}
|
||||
}
|
||||
10
Entity/Modules/ConfigTable/Interface/IConfigTable.cs
Normal file
10
Entity/Modules/ConfigTable/Interface/IConfigTable.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace NBF.ConfigTable
|
||||
{
|
||||
/// <summary>
|
||||
/// 表示是一个配置文件
|
||||
/// </summary>
|
||||
public interface IConfigTable
|
||||
{
|
||||
public uint Key { get; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user