(client) feat:实现热重载,实现多维度,实现武器,实现掉落物,实现状态UI,实现攻击AI (#44)
Co-authored-by: zzdxxz <2079238449@qq.com> Co-committed-by: zzdxxz <2079238449@qq.com>
This commit is contained in:
@ -1,40 +1,73 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Data;
|
||||
using UnityEngine;
|
||||
using Utils;
|
||||
|
||||
namespace Managers
|
||||
{
|
||||
public class DefineManager : Singleton<DefineManager>
|
||||
/// <summary>
|
||||
/// 定义管理器,负责加载、管理和查询所有数据定义(Define)对象。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 该管理器是一个单例,用于在应用程序中集中管理各种游戏或系统定义,
|
||||
/// 包括从不同数据包(Mods)加载定义,处理定义之间的引用,以及提供多种查询方法。
|
||||
/// </remarks>
|
||||
public class DefineManager : Singleton<DefineManager>,ILaunchManager
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据集文件路径数组,用于指定定义包的根目录。
|
||||
/// </summary>
|
||||
private static readonly string[] dataSetFilePath = { "Data", "Mods" };
|
||||
//类别,定义名,定义
|
||||
|
||||
/// <summary>
|
||||
/// 存储所有按类别和定义名索引的定义。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 外层字典的键是定义类别(例如,类的类型名),内层字典的键是定义的名称。
|
||||
/// </remarks>
|
||||
public Dictionary<string, Dictionary<string, Define>> defines = new();
|
||||
//包id,包
|
||||
|
||||
/// <summary>
|
||||
/// 存储所有按包ID索引的定义包。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 键是定义包的唯一ID,值是对应的 <see cref="DefinePack"/> 对象。
|
||||
/// </remarks>
|
||||
public Dictionary<string, DefinePack> packs = new();
|
||||
//类别,定义
|
||||
|
||||
/// <summary>
|
||||
/// 存储所有按类别索引的匿名定义列表。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 匿名定义是没有明确 <see cref="Define.defName"/> 的定义,它们通常作为其他定义的子元素出现。
|
||||
/// 外层字典的键是定义类别,值是该类别下的匿名定义列表。
|
||||
/// </remarks>
|
||||
public Dictionary<string, List<Define>> anonymousDefines = new();
|
||||
|
||||
public string StepDescription => "加载数据定义中";
|
||||
|
||||
/// <summary>
|
||||
/// 初始化定义管理器,加载所有定义包并构建定义字典。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 该方法执行以下操作:
|
||||
/// 1. 获取指定路径下的所有子文件夹,每个子文件夹代表一个定义包。
|
||||
/// 2. 遍历每个定义包,尝试加载其中的定义数据。
|
||||
/// 3. 将加载的定义数据按类型分类,并存储到定义字典中。
|
||||
/// <list type="number">
|
||||
/// <item>获取指定路径下的所有子文件夹,每个子文件夹代表一个定义包。</item>
|
||||
/// <item>遍历每个定义包,尝试加载其中的定义数据。</item>
|
||||
/// <item>将加载的定义数据按类型分类,并存储到定义字典中。</item>
|
||||
/// <item>处理定义内部的引用关系,将引用占位符替换为实际定义对象。</item>
|
||||
/// </list>
|
||||
/// </remarks>
|
||||
public void Init()
|
||||
{
|
||||
if (packs.Count > 0)
|
||||
return;
|
||||
// 单线程
|
||||
|
||||
// 获取所有定义包的文件夹路径
|
||||
var packFolder = Configs.ConfigProcessor.GetSubFolders(new(dataSetFilePath));
|
||||
foreach (var folder in packFolder)
|
||||
{
|
||||
@ -45,14 +78,16 @@ namespace Managers
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 字段信息缓存,用于优化反射性能。
|
||||
Dictionary<Type, FieldInfo[]> fieldCache = new();
|
||||
// 需要链接的定义、需要链接的字段、链接信息
|
||||
|
||||
// 存储需要进行链接的定义引用信息。
|
||||
// Tuple的元素依次代表:被引用的定义(Define),引用该定义的字段(FieldInfo),以及引用占位符(Define)。
|
||||
List<Tuple<Define, FieldInfo, Define>> defineCache = new();
|
||||
|
||||
string currentPackID = string.Empty;
|
||||
|
||||
string currentPackID;
|
||||
|
||||
// 递归处理定义对象及其内部的嵌套定义和引用。
|
||||
void ProcessDefine(Define def)
|
||||
{
|
||||
if (def == null || def.isReferene)
|
||||
@ -60,15 +95,14 @@ namespace Managers
|
||||
|
||||
def.packID = currentPackID;
|
||||
|
||||
// 如果字段信息已经缓存,则直接使用缓存
|
||||
// 检查是否已缓存字段信息,如果已缓存则直接使用。
|
||||
if (!fieldCache.TryGetValue(def.GetType(), out var defineFields))
|
||||
{
|
||||
// 获取所有字段类型为 Define 或其派生类型的字段
|
||||
// 获取所有公共实例字段。
|
||||
defineFields = def.GetType()
|
||||
.GetFields(BindingFlags.Public | BindingFlags.Instance)
|
||||
.ToArray(); // 不再过滤,先获取所有字段
|
||||
.GetFields(BindingFlags.Public | BindingFlags.Instance);
|
||||
|
||||
// 缓存字段信息
|
||||
// 缓存当前类型的字段信息。
|
||||
fieldCache[def.GetType()] = defineFields;
|
||||
}
|
||||
|
||||
@ -99,7 +133,7 @@ namespace Managers
|
||||
ProcessDefine(defRef);
|
||||
}
|
||||
}
|
||||
// 处理集合类型字段
|
||||
// 处理 List<Define> 类型的字段
|
||||
else if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(List<>))
|
||||
{
|
||||
var elementType = fieldType.GenericTypeArguments[0];
|
||||
@ -110,7 +144,7 @@ namespace Managers
|
||||
{
|
||||
foreach (var item in list)
|
||||
{
|
||||
if (item is Define defItem && !defItem.isReferene)
|
||||
if (item is Define { isReferene: false } defItem)
|
||||
{
|
||||
ProcessDefine(defItem);
|
||||
}
|
||||
@ -118,7 +152,7 @@ namespace Managers
|
||||
}
|
||||
}
|
||||
}
|
||||
// 处理数组类型字段
|
||||
// 处理 Define[] 类型的数组字段
|
||||
else if (fieldType.IsArray)
|
||||
{
|
||||
var elementType = fieldType.GetElementType();
|
||||
@ -129,7 +163,7 @@ namespace Managers
|
||||
{
|
||||
foreach (var item in array)
|
||||
{
|
||||
if (item is Define defItem && !defItem.isReferene)
|
||||
if (item is Define { isReferene: false } defItem)
|
||||
{
|
||||
ProcessDefine(defItem);
|
||||
}
|
||||
@ -139,6 +173,7 @@ namespace Managers
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var pack in packs)
|
||||
{
|
||||
currentPackID = pack.Value.packID;
|
||||
@ -154,6 +189,8 @@ namespace Managers
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 处理所有定义间的引用关系
|
||||
foreach (var defRef in defineCache)
|
||||
{
|
||||
if (defRef.Item1 == null)
|
||||
@ -186,19 +223,19 @@ namespace Managers
|
||||
}
|
||||
}
|
||||
|
||||
public void Reload()
|
||||
public void Clear()
|
||||
{
|
||||
defines.Clear();
|
||||
packs.Clear();
|
||||
anonymousDefines.Clear();
|
||||
Init();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查找指定定义类型的定义名对应的 Define 对象。
|
||||
/// 查找指定定义类型的定义名对应的 <see cref="Define"/> 对象。
|
||||
/// </summary>
|
||||
/// <param name="defineType">定义类型</param>
|
||||
/// <param name="defineName">定义名</param>
|
||||
/// <returns>如果找到,返回 Define 对象;否则返回 null。</returns>
|
||||
/// <param name="defineType">定义类型(通常是类的名称)。</param>
|
||||
/// <param name="defineName">定义名。</param>
|
||||
/// <returns>如果找到,返回 <see cref="Define"/> 对象;否则返回 null。</returns>
|
||||
public Define FindDefine(string defineType, string defineName)
|
||||
{
|
||||
if (defines.TryGetValue(defineType, out var typeDict))
|
||||
@ -210,23 +247,30 @@ namespace Managers
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用模板查找并返回指定类型的 Define 对象。
|
||||
/// 使用泛型模板查找并返回指定类型的 <see cref="Define"/> 对象。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">目标类型</typeparam>
|
||||
/// <param name="defineName">定义名</param>
|
||||
/// <returns>如果找到,返回转换为目标类型的 Define 对象;否则返回 null。</returns>
|
||||
/// <typeparam name="T">目标类型,必须继承自 <see cref="Define"/>。</typeparam>
|
||||
/// <param name="defineName">定义名。</param>
|
||||
/// <returns>如果找到,返回转换为目标类型的 <see cref="Define"/> 对象;否则返回 null。</returns>
|
||||
public T FindDefine<T>(string defineName) where T : Define
|
||||
{
|
||||
foreach (var typeDict in defines.Values)
|
||||
if (defines.TryGetValue(typeof(T).Name, out var typeDict))
|
||||
{
|
||||
if (typeDict.TryGetValue(defineName, out var define) && define is T result)
|
||||
if (typeDict.TryGetValue(defineName, out var define))
|
||||
{
|
||||
return result;
|
||||
return (T)define;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定 <see cref="Define"/> 对象所属的定义包。
|
||||
/// </summary>
|
||||
/// <param name="define">要查询的 <see cref="Define"/> 对象。</param>
|
||||
/// <returns>如果找到对应的定义包,则返回 <see cref="DefinePack"/> 对象;否则返回 null。</returns>
|
||||
public DefinePack GetDefinePackage(Define define)
|
||||
{
|
||||
if (define == null || define.packID == null)
|
||||
@ -235,10 +279,21 @@ namespace Managers
|
||||
return pack;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定 <see cref="Define"/> 对象所属定义包的名称。
|
||||
/// </summary>
|
||||
/// <param name="define">要查询的 <see cref="Define"/> 对象。</param>
|
||||
/// <returns>如果找到对应的定义包,则返回其名称;否则返回 null。</returns>
|
||||
public string GetDefinePackageName(Define define)
|
||||
{
|
||||
return GetDefinePackage(define)?.Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定包ID对应定义包的根路径。
|
||||
/// </summary>
|
||||
/// <param name="packID">定义包的唯一标识ID。</param>
|
||||
/// <returns>如果找到对应的定义包,则返回其根路径;否则返回 null。</returns>
|
||||
public string GetPackagePath(string packID)
|
||||
{
|
||||
if (packs.TryGetValue(packID, out var pack))
|
||||
@ -248,6 +303,10 @@ namespace Managers
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有已加载的 <see cref="Define"/> 对象。
|
||||
/// </summary>
|
||||
/// <returns>包含所有命名定义和匿名定义的数组。</returns>
|
||||
public Define[] GetAllDefine()
|
||||
{
|
||||
List<Define> defineList = new();
|
||||
@ -264,10 +323,10 @@ namespace Managers
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询指定类型下的所有 Define 对象。(包括匿名定义)
|
||||
/// 查询指定类型下的所有 <see cref="Define"/> 对象(包括命名定义和匿名定义)。
|
||||
/// </summary>
|
||||
/// <param name="defineType">定义类型(外层字典的键)。</param>
|
||||
/// <returns>该类型下的 Define 数组,如果未找到则返回 null。</returns>
|
||||
/// <param name="defineType">定义类型(外层字典的键,通常是类的名称)。</param>
|
||||
/// <returns>该类型下的 <see cref="Define"/> 数组。如果未找到任何定义,则返回 null。</returns>
|
||||
public Define[] QueryDefinesByType(string defineType)
|
||||
{
|
||||
if (string.IsNullOrEmpty(defineType))
|
||||
@ -276,21 +335,21 @@ namespace Managers
|
||||
return null;
|
||||
}
|
||||
|
||||
List<Define> result = new List<Define>();
|
||||
var result = new List<Define>();
|
||||
|
||||
// 从命名定义中查询
|
||||
// 从命名定义中查询。
|
||||
if (defines.TryGetValue(defineType, out var namedDefinitions))
|
||||
{
|
||||
result.AddRange(namedDefinitions.Values);
|
||||
}
|
||||
|
||||
// 从匿名定义中查询
|
||||
// 从匿名定义中查询。
|
||||
if (anonymousDefines.TryGetValue(defineType, out var anonymousDefinitionList))
|
||||
{
|
||||
result.AddRange(anonymousDefinitionList);
|
||||
}
|
||||
|
||||
// 如果结果为空,则返回 null
|
||||
// 如果结果为空,则返回 null。
|
||||
if (result.Count == 0)
|
||||
{
|
||||
Debug.LogWarning($"查询失败:未找到定义类型 '{defineType}'");
|
||||
@ -299,16 +358,17 @@ namespace Managers
|
||||
|
||||
return result.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询指定类型下的所有 Define 对象,并尝试转换为目标类型。(包括匿名定义)
|
||||
/// 查询指定类型下的所有 <see cref="Define"/> 对象,并尝试转换为目标类型(包括命名定义和匿名定义)。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">目标类型。</typeparam>
|
||||
/// <returns>转换后的目标类型数组,如果未找到或转换失败则返回 null。</returns>
|
||||
public T[] QueryDefinesByType<T>()
|
||||
/// <typeparam name="T">目标类型,必须继承自 <see cref="Define"/>。</typeparam>
|
||||
/// <returns>转换后的目标类型数组。如果未找到或转换失败,则返回 null。</returns>
|
||||
public T[] QueryDefinesByType<T>() where T : Define
|
||||
{
|
||||
var defineType = typeof(T).Name;
|
||||
|
||||
List<Define> allDefines = QueryDefinesByType(defineType)?.ToList();
|
||||
var allDefines = QueryDefinesByType(defineType)?.ToList();
|
||||
if (allDefines == null || allDefines.Count == 0)
|
||||
{
|
||||
return null;
|
||||
@ -316,7 +376,7 @@ namespace Managers
|
||||
|
||||
try
|
||||
{
|
||||
// 尝试将所有 Define 对象转换为目标类型 T
|
||||
// 尝试将所有 <see cref="Define"/> 对象转换为目标类型 T。
|
||||
var result = new List<T>();
|
||||
foreach (var item in allDefines)
|
||||
{
|
||||
@ -341,10 +401,10 @@ namespace Managers
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询指定类型下的所有 Define 对象(仅包含命名定义,不包括匿名定义)。
|
||||
/// 查询指定类型下的所有命名 <see cref="Define"/> 对象(不包括匿名定义)。
|
||||
/// </summary>
|
||||
/// <param name="defineType">定义类型(外层字典的键)。</param>
|
||||
/// <returns>该类型下的 Define 数组,如果未找到则返回 null。</returns>
|
||||
/// <param name="defineType">定义类型(外层字典的键,通常是类的名称)。</param>
|
||||
/// <returns>该类型下的命名 <see cref="Define"/> 数组。如果未找到任何命名定义,则返回 null。</returns>
|
||||
public Define[] QueryNamedDefinesByType(string defineType)
|
||||
{
|
||||
if (string.IsNullOrEmpty(defineType))
|
||||
@ -353,15 +413,15 @@ namespace Managers
|
||||
return null;
|
||||
}
|
||||
|
||||
List<Define> result = new List<Define>();
|
||||
var result = new List<Define>();
|
||||
|
||||
// 仅从命名定义中查询
|
||||
// 仅从命名定义中查询。
|
||||
if (defines.TryGetValue(defineType, out var namedDefinitions))
|
||||
{
|
||||
result.AddRange(namedDefinitions.Values);
|
||||
}
|
||||
|
||||
// 如果结果为空,则返回 null
|
||||
// 如果结果为空,则返回 null。
|
||||
if (result.Count == 0)
|
||||
{
|
||||
Debug.LogWarning($"查询失败:未找到定义类型 '{defineType}' 的命名定义");
|
||||
@ -372,15 +432,15 @@ namespace Managers
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询指定类型下的所有 Define 对象,并尝试转换为目标类型(仅包含命名定义,不包括匿名定义)。
|
||||
/// 查询指定类型下的所有命名 <see cref="Define"/> 对象,并尝试转换为目标类型(不包括匿名定义)。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">目标类型。</typeparam>
|
||||
/// <returns>转换后的目标类型数组,如果未找到或转换失败则返回 null。</returns>
|
||||
public T[] QueryNamedDefinesByType<T>()
|
||||
/// <typeparam name="T">目标类型,必须继承自 <see cref="Define"/>。</typeparam>
|
||||
/// <returns>转换后的目标类型数组。如果未找到或转换失败,则返回 null。</returns>
|
||||
public T[] QueryNamedDefinesByType<T>() where T : Define
|
||||
{
|
||||
var defineType = typeof(T).Name;
|
||||
|
||||
List<Define> allDefines = QueryNamedDefinesByType(defineType)?.ToList();
|
||||
var allDefines = QueryNamedDefinesByType(defineType)?.ToList();
|
||||
if (allDefines == null || allDefines.Count == 0)
|
||||
{
|
||||
return null;
|
||||
@ -388,7 +448,7 @@ namespace Managers
|
||||
|
||||
try
|
||||
{
|
||||
// 尝试将所有 Define 对象转换为目标类型 T
|
||||
// 尝试将所有 <see cref="Define"/> 对象转换为目标类型 T。
|
||||
var result = new List<T>();
|
||||
foreach (var item in allDefines)
|
||||
{
|
||||
@ -411,22 +471,28 @@ namespace Managers
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回所有加载的定义包的字符串表示。
|
||||
/// </summary>
|
||||
/// <returns>一个包含所有定义包信息的字符串,每个包信息占一行。</returns>
|
||||
public override string ToString()
|
||||
{
|
||||
if (packs == null || packs.Count == 0)
|
||||
{
|
||||
return "No packs available"; // 如果集合为空或为 null,返回默认信息
|
||||
// 如果集合为空或为 null,返回默认信息。
|
||||
return "No packs available";
|
||||
}
|
||||
|
||||
var result = new System.Text.StringBuilder();
|
||||
|
||||
foreach (var definePack in packs)
|
||||
{
|
||||
result.AppendLine(definePack.ToString()); // 每个元素占一行
|
||||
// 每个定义包对象占一行。
|
||||
result.AppendLine(definePack.ToString());
|
||||
}
|
||||
|
||||
return result.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user