(client) fix:修复匿名定义中的图片无法加载的问题
This commit is contained in:
@ -11,10 +11,11 @@ namespace Managers
|
||||
{
|
||||
public class DefineManager : Singleton<DefineManager>
|
||||
{
|
||||
private static readonly string[] dataSetFilePath = { "Data", "Mod" };
|
||||
private static readonly string[] dataSetFilePath = { "Data", "Mods" };
|
||||
|
||||
public Dictionary<string, Dictionary<string, Define>> defines = new();
|
||||
public Dictionary<string, DefinePack> packs = new();
|
||||
public Dictionary<string, List<Define>> anonymousDefines = new();
|
||||
/// <summary>
|
||||
/// 初始化定义管理器,加载所有定义包并构建定义字典。
|
||||
/// </summary>
|
||||
@ -32,11 +33,52 @@ namespace Managers
|
||||
var pack = new DefinePack();
|
||||
if (pack.LoadPack(folder)) packs.Add(pack.packID, pack);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (def == null || def.isReferene || processedDefines.Contains(def))
|
||||
return;
|
||||
|
||||
processedDefines.Add(def);
|
||||
|
||||
// 如果字段信息已经缓存,则直接使用缓存
|
||||
if (!fieldCache.TryGetValue(def.GetType(), out var defineFields))
|
||||
{
|
||||
// 获取所有字段类型为 Define 或其派生类型的字段
|
||||
defineFields = def.GetType()
|
||||
.GetFields(BindingFlags.Public | BindingFlags.Instance)
|
||||
.Where(field => typeof(Define).IsAssignableFrom(field.FieldType))
|
||||
.ToArray();
|
||||
// 缓存字段信息
|
||||
fieldCache[def.GetType()] = defineFields;
|
||||
}
|
||||
|
||||
foreach (var defineField in defineFields)
|
||||
{
|
||||
var defRef = (Define)defineField.GetValue(def);
|
||||
if (defRef == null)
|
||||
continue;
|
||||
if (defRef.isReferene)
|
||||
{
|
||||
defineCache.Add(new Tuple<Define, FieldInfo, Define>(parentDef, parentField, defRef));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (string.IsNullOrEmpty(defRef.defName))
|
||||
{
|
||||
var typeName = defRef.GetType().Name;
|
||||
if (!anonymousDefines.ContainsKey(typeName))
|
||||
anonymousDefines.Add(typeName, new List<Define>());
|
||||
anonymousDefines[typeName].Add(defRef);
|
||||
}
|
||||
ProcessDefine(defRef, def, defineField);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var pack in packs)
|
||||
{
|
||||
foreach (var define in pack.Value.defines)
|
||||
@ -51,34 +93,16 @@ namespace Managers
|
||||
{
|
||||
defines[typeName][def.defName] = def;
|
||||
|
||||
// 如果字段信息已经缓存,则直接使用缓存
|
||||
if (!fieldCache.TryGetValue(def.GetType(), out var defineFields))
|
||||
{
|
||||
// 获取所有字段类型为 Define 或其派生类型的字段
|
||||
defineFields = def.GetType()
|
||||
.GetFields(BindingFlags.Public | BindingFlags.Instance)
|
||||
.Where(field => typeof(Define).IsAssignableFrom(field.FieldType))
|
||||
.ToArray();
|
||||
// 缓存字段信息
|
||||
fieldCache[def.GetType()] = defineFields;
|
||||
}
|
||||
|
||||
foreach (var defineField in defineFields)
|
||||
{
|
||||
var defRef=(Define)defineField.GetValue(def);
|
||||
if (defRef==null || !defRef.isReferene)
|
||||
continue;
|
||||
defineCache.Add(new(def, defineField, defRef));
|
||||
}
|
||||
// 处理顶层 Define
|
||||
ProcessDefine(def, null, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var defRef in defineCache)
|
||||
{
|
||||
defRef.Item2.SetValue(defRef.Item1, FindDefine(defRef.Item3.description, defRef.Item3.defName));
|
||||
}
|
||||
|
||||
Debug.Log(anonymousDefines.Count);
|
||||
}
|
||||
/// <summary>
|
||||
/// 查找指定定义类型的定义名对应的 Define 对象。
|
||||
@ -126,6 +150,11 @@ namespace Managers
|
||||
{
|
||||
defineList.AddRange(define.Values);
|
||||
}
|
||||
|
||||
foreach (var anonymousDefine in anonymousDefines)
|
||||
{
|
||||
defineList.AddRange(anonymousDefine.Value);
|
||||
}
|
||||
return defineList.ToArray();
|
||||
}
|
||||
|
||||
@ -160,7 +189,7 @@ namespace Managers
|
||||
return targetDefine;
|
||||
}
|
||||
/// <summary>
|
||||
/// 查询指定类型下的所有 Define 对象。
|
||||
/// 查询指定类型下的所有 Define 对象。(包括匿名定义)
|
||||
/// </summary>
|
||||
/// <param name="defineType">定义类型(外层字典的键)。</param>
|
||||
/// <returns>该类型下的 Define 数组,如果未找到则返回 null。</returns>
|
||||
@ -172,39 +201,49 @@ namespace Managers
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!defines.TryGetValue(defineType, out var typeDefinitions))
|
||||
List<Define> 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
|
||||
if (result.Count == 0)
|
||||
{
|
||||
Debug.LogWarning($"查询失败:未找到定义类型 '{defineType}'");
|
||||
return null;
|
||||
}
|
||||
|
||||
return typeDefinitions.Values.ToArray();
|
||||
return result.ToArray();
|
||||
}
|
||||
/// <summary>
|
||||
/// 查询指定类型下的所有 Define 对象,并尝试转换为目标类型。
|
||||
/// 查询指定类型下的所有 Define 对象,并尝试转换为目标类型。(包括匿名定义)
|
||||
/// </summary>
|
||||
/// <typeparam name="T">目标类型。</typeparam>
|
||||
/// <returns>转换后的目标类型数组,如果未找到或转换失败则返回 null。</returns>
|
||||
public T[] QueryDefinesByType<T>()
|
||||
{
|
||||
var defineType = typeof(T).Name;
|
||||
if (string.IsNullOrEmpty(defineType))
|
||||
{
|
||||
Debug.LogError("查询失败:定义类型参数不能为空!");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!defines.TryGetValue(defineType, out var typeDefinitions))
|
||||
List<Define> allDefines = QueryDefinesByType(defineType)?.ToList();
|
||||
if (allDefines == null || allDefines.Count == 0)
|
||||
{
|
||||
Debug.LogWarning($"查询失败:未找到定义类型 '{defineType}'");
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 获取所有值并尝试转换为目标类型
|
||||
// 尝试将所有 Define 对象转换为目标类型 T
|
||||
var result = new List<T>();
|
||||
foreach (var item in typeDefinitions.Values)
|
||||
foreach (var item in allDefines)
|
||||
{
|
||||
if (item is T converted)
|
||||
{
|
||||
@ -216,6 +255,7 @@ namespace Managers
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return result.ToArray();
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
Reference in New Issue
Block a user