结构大修改,改成朴实无华的结构,不过度架构。能跑就行
This commit is contained in:
90
Assets/Scripts/NBC/Config/ConfigContext.cs
Normal file
90
Assets/Scripts/NBC/Config/ConfigContext.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace NBC
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/NBC/Config/ConfigContext.cs.meta
Normal file
3
Assets/Scripts/NBC/Config/ConfigContext.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f916dd8f41b047068d570fc87f9163c2
|
||||
timeCreated: 1760064105
|
||||
114
Assets/Scripts/NBC/Config/ConfigTableHelper.cs
Normal file
114
Assets/Scripts/NBC/Config/ConfigTableHelper.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace NBC
|
||||
{
|
||||
/// <summary>
|
||||
/// 配置表帮助类
|
||||
/// </summary>
|
||||
public static class ConfigTableHelper
|
||||
{
|
||||
private static readonly Dictionary<Type, IConfigContext> _dictionary = new Dictionary<Type, IConfigContext>();
|
||||
|
||||
/// <summary>
|
||||
/// 初始化ConfigTableHelper
|
||||
/// </summary>
|
||||
public static void Initialize(string json)
|
||||
{
|
||||
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}");
|
||||
}
|
||||
}
|
||||
|
||||
//反射获取所有IConfigTable的非抽象类
|
||||
var tableTypes = GetAllConfigTableTypes();
|
||||
foreach (var type in tableTypes)
|
||||
{
|
||||
var name = type.Name;
|
||||
if (tokens.TryGetValue(name, out var jArray))
|
||||
{
|
||||
// 通过反射调用 ParseJson 方法
|
||||
var parseMethod = type.GetMethod("ParseJson", BindingFlags.Public | BindingFlags.Static);
|
||||
parseMethod?.Invoke(null, new object[] { jArray });
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
var types = new List<Type>();
|
||||
var interfaceType = typeof(IConfigTable);
|
||||
|
||||
// 遍历当前程序集中的所有类型
|
||||
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
|
||||
{
|
||||
foreach (var type in assembly.GetTypes())
|
||||
{
|
||||
// 检查是否实现了 IConfigTable 接口,并且不是抽象类
|
||||
if (interfaceType.IsAssignableFrom(type) && !type.IsAbstract && !type.IsInterface)
|
||||
{
|
||||
types.Add(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return types;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/NBC/Config/ConfigTableHelper.cs.meta
Normal file
3
Assets/Scripts/NBC/Config/ConfigTableHelper.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: de53a788ec4e446eb6c07c50c65afcc2
|
||||
timeCreated: 1759160093
|
||||
19
Assets/Scripts/NBC/Config/IConfigTable.cs
Normal file
19
Assets/Scripts/NBC/Config/IConfigTable.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
namespace NBC
|
||||
{
|
||||
/// <summary>
|
||||
/// 表示是一个配置文件
|
||||
/// </summary>
|
||||
public interface IConfigTable
|
||||
{
|
||||
public uint Key { get; }
|
||||
}
|
||||
|
||||
public interface IConfigItemTable
|
||||
{
|
||||
public string Model { get; }
|
||||
|
||||
public string Quality { get; }
|
||||
|
||||
public string Icon { get; }
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/NBC/Config/IConfigTable.cs.meta
Normal file
3
Assets/Scripts/NBC/Config/IConfigTable.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: df4eb038440a4a498e27ccaf23a1cd9b
|
||||
timeCreated: 1759160060
|
||||
Reference in New Issue
Block a user