(client) feat:完成实体生成函数,修复行为树加载错误,改进Define打印缩进
This commit is contained in:
@ -44,16 +44,14 @@ namespace Managers
|
||||
|
||||
|
||||
Dictionary<Type, FieldInfo[]> fieldCache = new();
|
||||
// 需要链接的定义、需要链接的字段、链接信息
|
||||
List<Tuple<Define, FieldInfo, Define>> defineCache = new();
|
||||
HashSet<Define> processedDefines = new(); // 用于跟踪已处理的 Define 对象
|
||||
|
||||
void ProcessDefine(Define def, Define parentDef, FieldInfo parentField)
|
||||
void ProcessDefine(Define def)
|
||||
{
|
||||
if (def == null || def.isReferene || processedDefines.Contains(def))
|
||||
if (def == null || def.isReferene)
|
||||
return;
|
||||
|
||||
processedDefines.Add(def);
|
||||
|
||||
|
||||
// 如果字段信息已经缓存,则直接使用缓存
|
||||
if (!fieldCache.TryGetValue(def.GetType(), out var defineFields))
|
||||
{
|
||||
@ -73,7 +71,7 @@ namespace Managers
|
||||
continue;
|
||||
if (defRef.isReferene)
|
||||
{
|
||||
defineCache.Add(new Tuple<Define, FieldInfo, Define>(parentDef, parentField, defRef));
|
||||
defineCache.Add(new Tuple<Define, FieldInfo, Define>(def, defineField, defRef));
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -84,33 +82,54 @@ namespace Managers
|
||||
anonymousDefines.Add(typeName, new List<Define>());
|
||||
anonymousDefines[typeName].Add(defRef);
|
||||
}
|
||||
ProcessDefine(defRef, def, defineField);
|
||||
ProcessDefine(defRef);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var pack in packs)
|
||||
{
|
||||
foreach (var define in pack.Value.defines)
|
||||
foreach (var (typeName, defList) in pack.Value.defines)
|
||||
{
|
||||
var typeName = define.Key;
|
||||
var defList = define.Value;
|
||||
|
||||
if (!defines.ContainsKey(typeName))
|
||||
defines[typeName] = new Dictionary<string, Define>();
|
||||
|
||||
foreach (var def in defList)
|
||||
{
|
||||
defines[typeName][def.defName] = def;
|
||||
|
||||
// 处理顶层 Define
|
||||
ProcessDefine(def, null, null);
|
||||
ProcessDefine(def);
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach (var defRef in defineCache)
|
||||
{
|
||||
defRef.Item2.SetValue(defRef.Item1, FindDefine(defRef.Item3.description, defRef.Item3.defName));
|
||||
if (defRef.Item1 == null)
|
||||
{
|
||||
Debug.LogError("defRef.Item1 为 null!");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (defRef.Item2 == null)
|
||||
{
|
||||
Debug.LogError("defRef.Item2 为 null!");
|
||||
continue;
|
||||
}
|
||||
|
||||
var value = FindDefine(defRef.Item3.description, defRef.Item3.defName);
|
||||
if (value == null)
|
||||
{
|
||||
Debug.LogError($"FindDefine 返回 null: description={defRef.Item3.description}, defName={defRef.Item3.defName}");
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
defRef.Item2.SetValue(defRef.Item1, value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogError($"SetValue 出错: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -138,7 +157,23 @@ namespace Managers
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用模板查找并返回指定类型的 Define 对象。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">目标类型</typeparam>
|
||||
/// <param name="defineName">定义名</param>
|
||||
/// <returns>如果找到,返回转换为目标类型的 Define 对象;否则返回 null。</returns>
|
||||
public T FindDefine<T>(string defineName) where T : Define
|
||||
{
|
||||
foreach (var typeDict in defines.Values)
|
||||
{
|
||||
if (typeDict.TryGetValue(defineName, out var define) && define is T result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public DefinePack GetDefinePackage(Define define)
|
||||
{
|
||||
if (define == null || define.packID == null)
|
||||
|
Reference in New Issue
Block a user