2025-07-12 12:04:59 +08:00
|
|
|
|
using System;
|
2025-08-19 20:22:10 +08:00
|
|
|
|
using System.Collections;
|
2025-07-12 11:54:19 +08:00
|
|
|
|
using System.Collections.Generic;
|
2025-10-10 14:08:23 +08:00
|
|
|
|
using System.IO;
|
2025-07-12 17:39:46 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Xml.Linq;
|
2025-10-10 14:08:23 +08:00
|
|
|
|
using Configs;
|
2025-07-12 17:39:46 +08:00
|
|
|
|
using UnityEngine;
|
2025-07-12 11:54:19 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Data
|
|
|
|
|
|
{
|
2025-08-27 19:56:49 +08:00
|
|
|
|
/// <summary>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// 表示模组包的基本信息。
|
2025-08-27 19:56:49 +08:00
|
|
|
|
/// </summary>
|
2025-07-12 17:39:46 +08:00
|
|
|
|
public class PackAbout
|
2025-07-12 11:54:19 +08:00
|
|
|
|
{
|
2025-08-27 19:56:49 +08:00
|
|
|
|
private const string RootElementName = "About";
|
|
|
|
|
|
private const string ElementName_Name = "name";
|
|
|
|
|
|
private const string ElementName_Description = "description";
|
|
|
|
|
|
private const string ElementName_Author = "author";
|
|
|
|
|
|
private const string ElementName_Version = "version";
|
|
|
|
|
|
private const string ElementName_PackID = "packID";
|
|
|
|
|
|
private const string ElementName_Sort = "sort";
|
|
|
|
|
|
private const string ElementName_Before = "before";
|
|
|
|
|
|
private const string ElementName_After = "after";
|
|
|
|
|
|
private const string ElementName_Necessary = "necessary";
|
2025-10-10 14:08:23 +08:00
|
|
|
|
public string Name { get; private set; }
|
|
|
|
|
|
public string Description { get; private set; }
|
|
|
|
|
|
public string Author { get; private set; }
|
|
|
|
|
|
public string Version { get; private set; }
|
|
|
|
|
|
public string PackID { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
public string[] Necessary { get; private set; }
|
|
|
|
|
|
public string[] After { get; private set; }
|
|
|
|
|
|
public string[] Before { get; private set; }
|
2025-07-12 17:39:46 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-07-12 21:35:58 +08:00
|
|
|
|
/// 使用静态方法从 XML 文档创建 PackAbout 实例。
|
2025-07-12 17:39:46 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="doc">XML 文档。</param>
|
|
|
|
|
|
/// <returns>初始化的 PackAbout 实例。</returns>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// <exception cref="ArgumentException">当 XML 文档无效,根节点为空或不是 'About' 时抛出。</exception>
|
2025-07-12 17:39:46 +08:00
|
|
|
|
public static PackAbout FromXDocument(XDocument doc)
|
|
|
|
|
|
{
|
2025-08-27 19:56:49 +08:00
|
|
|
|
var aboutElement = doc.Element(RootElementName);
|
2025-10-10 14:08:23 +08:00
|
|
|
|
if (aboutElement == null)
|
|
|
|
|
|
throw new ArgumentException($"<color=red>XML 文档无效,根节点为空或不是 '{RootElementName}'。</color>");
|
2025-07-12 17:39:46 +08:00
|
|
|
|
PackAbout result = new();
|
|
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
result.Name = aboutElement.Element(ElementName_Name)?.Value ?? "Unknown";
|
|
|
|
|
|
result.Description = aboutElement.Element(ElementName_Description)?.Value ?? "Unknown";
|
|
|
|
|
|
result.Author = aboutElement.Element(ElementName_Author)?.Value ?? "Unknown";
|
|
|
|
|
|
result.Version = aboutElement.Element(ElementName_Version)?.Value ?? "Unknown";
|
|
|
|
|
|
result.PackID = aboutElement.Element(ElementName_PackID)?.Value ?? "Unknown";
|
2025-07-12 21:35:58 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
var sortElement = aboutElement.Element(ElementName_Sort);
|
2025-07-12 17:39:46 +08:00
|
|
|
|
if (sortElement != null)
|
|
|
|
|
|
{
|
2025-08-27 19:56:49 +08:00
|
|
|
|
result.Before = GetElementValues(sortElement.Element(ElementName_Before));
|
|
|
|
|
|
result.After = GetElementValues(sortElement.Element(ElementName_After));
|
|
|
|
|
|
result.Necessary = GetElementValues(sortElement.Element(ElementName_Necessary));
|
2025-07-12 17:39:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-08-27 19:56:49 +08:00
|
|
|
|
result.Before = Array.Empty<string>();
|
|
|
|
|
|
result.After = Array.Empty<string>();
|
|
|
|
|
|
result.Necessary = Array.Empty<string>();
|
2025-07-12 17:39:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-07-12 21:35:58 +08:00
|
|
|
|
/// 获取指定 XElement 下所有子元素的值并返回为字符串数组。
|
2025-07-12 17:39:46 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="element">父 XElement。</param>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// <returns>包含子元素值的字符串数组。</returns>
|
2025-07-12 17:39:46 +08:00
|
|
|
|
private static string[] GetElementValues(XElement element)
|
|
|
|
|
|
{
|
2025-07-12 21:35:58 +08:00
|
|
|
|
if (element == null || !element.HasElements) return Array.Empty<string>();
|
2025-07-12 17:39:46 +08:00
|
|
|
|
|
|
|
|
|
|
return element.Elements()
|
2025-07-12 21:35:58 +08:00
|
|
|
|
.Select(e => e.Value.Trim())
|
|
|
|
|
|
.ToArray();
|
2025-07-12 17:39:46 +08:00
|
|
|
|
}
|
2025-07-12 21:35:58 +08:00
|
|
|
|
|
2025-07-12 17:39:46 +08:00
|
|
|
|
public override string ToString()
|
|
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
// 定义字段标签和值的对齐格式
|
2025-07-12 21:35:58 +08:00
|
|
|
|
const int labelWidth = -12; // 负数为左对齐
|
|
|
|
|
|
const int valueWidth = -30; // 负数为左对齐
|
2025-07-12 17:39:46 +08:00
|
|
|
|
|
|
|
|
|
|
var sb = new StringBuilder();
|
2025-07-12 21:35:58 +08:00
|
|
|
|
|
2025-10-10 14:08:23 +08:00
|
|
|
|
// 基础字段
|
2025-08-27 19:56:49 +08:00
|
|
|
|
sb.AppendLine($"{"Name:",labelWidth}{Name,valueWidth}");
|
|
|
|
|
|
sb.AppendLine($"{"Description:",labelWidth}{Description,valueWidth}");
|
|
|
|
|
|
sb.AppendLine($"{"Author:",labelWidth}{Author,valueWidth}");
|
|
|
|
|
|
sb.AppendLine($"{"Version:",labelWidth}{Version,valueWidth}");
|
|
|
|
|
|
sb.AppendLine($"{"PackID:",labelWidth}{PackID,valueWidth}");
|
2025-07-12 17:39:46 +08:00
|
|
|
|
|
2025-10-10 14:08:23 +08:00
|
|
|
|
// 数组字段
|
2025-07-12 21:35:58 +08:00
|
|
|
|
sb.AppendLine(
|
2025-08-27 19:56:49 +08:00
|
|
|
|
$"{"Necessary:",labelWidth}{string.Join(", ", Necessary ?? Array.Empty<string>()),valueWidth}");
|
|
|
|
|
|
sb.AppendLine($"{"After:",labelWidth}{string.Join(", ", After ?? Array.Empty<string>()),valueWidth}");
|
|
|
|
|
|
sb.AppendLine($"{"Before:",labelWidth}{string.Join(", ", Before ?? Array.Empty<string>()),valueWidth}");
|
2025-07-12 17:39:46 +08:00
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
|
}
|
2025-07-12 11:54:19 +08:00
|
|
|
|
}
|
2025-07-12 17:39:46 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// 表示一个模组包的定义集合。
|
2025-08-27 19:56:49 +08:00
|
|
|
|
/// </summary>
|
2025-07-12 11:54:19 +08:00
|
|
|
|
public class DefinePack
|
|
|
|
|
|
{
|
2025-07-12 21:35:58 +08:00
|
|
|
|
private const string CoreNamespace = "Data.";
|
2025-08-27 19:56:49 +08:00
|
|
|
|
private const string RootElementName_About = "About";
|
|
|
|
|
|
private const string RootElementName_Define = "Define";
|
|
|
|
|
|
|
2025-10-10 14:08:23 +08:00
|
|
|
|
// 反射缓存
|
2025-08-27 19:56:49 +08:00
|
|
|
|
private static readonly Dictionary<string, Type> _typeCache = new();
|
|
|
|
|
|
private static readonly Dictionary<Type, ConstructorInfo> _constructorCache = new();
|
|
|
|
|
|
private static readonly Dictionary<Type, FieldInfo[]> _fieldCache = new();
|
|
|
|
|
|
private static readonly List<Assembly> _assembliesToSearch = new(); // 缓存要搜索的程序集
|
|
|
|
|
|
|
2025-07-12 17:39:46 +08:00
|
|
|
|
/// <summary>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// 定义类别及其定义。
|
2025-07-12 17:39:46 +08:00
|
|
|
|
/// </summary>
|
2025-08-27 19:56:49 +08:00
|
|
|
|
public Dictionary<string, List<Define>> defines = new();
|
2025-07-12 21:35:58 +08:00
|
|
|
|
|
|
|
|
|
|
public PackAbout packAbout;
|
|
|
|
|
|
public string packID;
|
2025-07-17 15:42:24 +08:00
|
|
|
|
public string packRootPath;
|
2025-07-12 21:35:58 +08:00
|
|
|
|
|
2025-09-03 19:59:22 +08:00
|
|
|
|
public int priority = -1;
|
|
|
|
|
|
|
2025-10-10 14:08:23 +08:00
|
|
|
|
static DefinePack()
|
2025-07-17 15:42:24 +08:00
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
// 初始化要搜索的程序集。
|
|
|
|
|
|
_assembliesToSearch.AddRange(AppDomain.CurrentDomain.GetAssemblies());
|
2025-07-17 15:42:24 +08:00
|
|
|
|
}
|
2025-07-12 11:54:19 +08:00
|
|
|
|
|
2025-10-10 14:08:23 +08:00
|
|
|
|
public string Name =>
|
|
|
|
|
|
// 使用 PackAbout.Name 属性,若为空则返回默认值。
|
|
|
|
|
|
packAbout?.Name ?? "Unnamed Pack";
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 加载模组包。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="packPath">模组包的路径。</param>
|
|
|
|
|
|
/// <returns>如果模组包加载成功,则为 true;否则为 false。</returns>
|
2025-07-12 17:39:46 +08:00
|
|
|
|
public bool LoadPack(string packPath)
|
2025-07-12 11:54:19 +08:00
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
packRootPath = Path.GetFullPath(packPath);
|
2025-07-12 21:41:54 +08:00
|
|
|
|
var packDatas = ConfigProcessor.LoadXmlFromPath(packPath);
|
2025-08-27 19:56:49 +08:00
|
|
|
|
var aboutXmls = FindDocumentsWithRootName(packDatas, RootElementName_About);
|
2025-07-12 17:39:46 +08:00
|
|
|
|
if (aboutXmls == null || aboutXmls.Count < 1)
|
|
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
Debug.LogError("<color=red>包缺少配置文件,加载跳过。</color>");
|
2025-07-12 17:39:46 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2025-07-12 21:35:58 +08:00
|
|
|
|
|
|
|
|
|
|
var aboutXml = aboutXmls[0];
|
|
|
|
|
|
packAbout = PackAbout.FromXDocument(aboutXml);
|
2025-08-27 19:56:49 +08:00
|
|
|
|
packID = packAbout.PackID;
|
2025-10-10 14:08:23 +08:00
|
|
|
|
if (aboutXmls.Count > 1)
|
|
|
|
|
|
Debug.LogWarning($"<color=yellow>{packAbout.Name} 包拥有多个配置文件,系统选择了加载序的第一个,请避免这种情况。</color>");
|
2025-07-12 21:35:58 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
var defineXmls = FindDocumentsWithRootName(packDatas, RootElementName_Define);
|
2025-07-12 21:35:58 +08:00
|
|
|
|
foreach (var defineXml in defineXmls) LoadDefines(defineXml);
|
2025-07-12 17:39:46 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 加载定义。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="defineDoc">包含定义的 XML 文档。</param>
|
2025-07-12 17:39:46 +08:00
|
|
|
|
private void LoadDefines(XDocument defineDoc)
|
|
|
|
|
|
{
|
|
|
|
|
|
var rootElement = defineDoc.Root;
|
2025-10-10 14:08:23 +08:00
|
|
|
|
if (rootElement == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogWarning("<color=yellow>加载定义时发现一个XML文档的根元素为空,跳过此文档。</color>");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (rootElement.Name != RootElementName_Define)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogWarning(
|
|
|
|
|
|
$"<color=yellow>加载定义时发现XML文档根元素名称 '{rootElement.Name}' 不符合预期 '{RootElementName_Define}',跳过此文档。</color>");
|
2025-07-12 17:39:46 +08:00
|
|
|
|
return;
|
2025-10-10 14:08:23 +08:00
|
|
|
|
}
|
2025-07-12 21:35:58 +08:00
|
|
|
|
|
2025-07-12 17:39:46 +08:00
|
|
|
|
foreach (var element in rootElement.Elements())
|
|
|
|
|
|
{
|
|
|
|
|
|
var className = element.Name.ToString();
|
|
|
|
|
|
if (string.IsNullOrEmpty(className))
|
|
|
|
|
|
continue;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
var def = LoadDefineClass(element, className);
|
2025-07-12 17:39:46 +08:00
|
|
|
|
if (def == null)
|
|
|
|
|
|
continue;
|
2025-07-15 15:26:58 +08:00
|
|
|
|
def.packID = packID;
|
2025-07-12 17:39:46 +08:00
|
|
|
|
if (!defines.ContainsKey(className))
|
2025-07-12 21:35:58 +08:00
|
|
|
|
defines.Add(className, new List<Define>());
|
2025-07-12 17:39:46 +08:00
|
|
|
|
defines[className].Add(def);
|
|
|
|
|
|
}
|
2025-07-12 11:54:19 +08:00
|
|
|
|
}
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
2025-07-15 15:26:58 +08:00
|
|
|
|
/// <summary>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// 根据指定的 XML 元素 (<paramref name="defineDoc" />) 和类名 (<paramref name="className" />),
|
|
|
|
|
|
/// 动态加载并初始化一个继承自 <see cref="Define" /> 的类实例。
|
2025-07-15 15:26:58 +08:00
|
|
|
|
/// </summary>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// <param name="defineDoc">包含类定义的 XML 元素 (<see cref="XElement" />)。</param>
|
2025-07-15 15:26:58 +08:00
|
|
|
|
/// <param name="className">目标类的全限定名或简短名称。</param>
|
|
|
|
|
|
/// <returns>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// 如果成功加载并初始化,则返回对应的 <see cref="Define" /> 类实例;
|
|
|
|
|
|
/// 否则返回 null。
|
2025-07-15 15:26:58 +08:00
|
|
|
|
/// </returns>
|
2025-08-27 19:56:49 +08:00
|
|
|
|
public static Define LoadDefineClass(XElement defineDoc, string className)
|
2025-07-12 21:35:58 +08:00
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
// 反射缓存和改进的类型查找
|
2025-08-27 19:56:49 +08:00
|
|
|
|
if (!_typeCache.TryGetValue(className, out var type))
|
2025-07-12 17:39:46 +08:00
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
// 尝试 CoreNamespace + className
|
2025-07-12 17:39:46 +08:00
|
|
|
|
var fullClassName = CoreNamespace + className;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
type = _assembliesToSearch.Select(a => a.GetType(fullClassName)).FirstOrDefault(t => t != null);
|
2025-07-12 17:39:46 +08:00
|
|
|
|
|
2025-10-10 14:08:23 +08:00
|
|
|
|
// 如果未找到,尝试不使用 CoreNamespace
|
2025-08-27 19:56:49 +08:00
|
|
|
|
if (type == null)
|
|
|
|
|
|
type = _assembliesToSearch.Select(a => a.GetType(className)).FirstOrDefault(t => t != null);
|
2025-07-12 17:39:46 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
if (type == null)
|
|
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
Debug.LogError($"<color=red>未定义的类型: {className}。</color>");
|
2025-08-27 19:56:49 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2025-10-10 14:08:23 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
_typeCache[className] = type;
|
2025-07-12 17:39:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-10 14:08:23 +08:00
|
|
|
|
// 构造函数缓存
|
2025-08-27 19:56:49 +08:00
|
|
|
|
if (!_constructorCache.TryGetValue(type, out var constructor))
|
2025-07-12 17:39:46 +08:00
|
|
|
|
{
|
2025-08-27 19:56:49 +08:00
|
|
|
|
constructor = type.GetConstructor(Type.EmptyTypes);
|
|
|
|
|
|
if (constructor == null)
|
|
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
Debug.LogError($"<color=red>类 {className} 必须包含无参构造函数。</color>");
|
2025-08-27 19:56:49 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2025-10-10 14:08:23 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
_constructorCache[type] = constructor;
|
2025-07-12 17:39:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-10 14:08:23 +08:00
|
|
|
|
// 创建实例
|
2025-07-12 17:39:46 +08:00
|
|
|
|
object instance;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
instance = constructor.Invoke(null);
|
2025-07-12 17:39:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
Debug.LogError($"<color=red>创建 {className} 实例失败: {ex.Message}。</color>");
|
2025-07-12 17:39:46 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-10 14:08:23 +08:00
|
|
|
|
// 检查是否继承自 Define
|
2025-07-12 17:39:46 +08:00
|
|
|
|
if (instance is not Define define)
|
|
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
Debug.LogError($"<color=red>类 {className} 必须继承自 Define。</color>");
|
2025-07-12 17:39:46 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-12 21:35:58 +08:00
|
|
|
|
if (define.Init(defineDoc)) return define;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
DefaultInitDefine(define, defineDoc, type);
|
2025-07-14 11:39:31 +08:00
|
|
|
|
|
|
|
|
|
|
return define;
|
|
|
|
|
|
}
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
2025-07-15 15:26:58 +08:00
|
|
|
|
/// <summary>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// 初始化指定的 <paramref name="define" /> 对象,根据 <paramref name="defineDoc" /> 中的 XML 元素内容,
|
|
|
|
|
|
/// 将对应的字段值赋给 <paramref name="define" /> 对象。
|
2025-07-15 15:26:58 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="define">需要初始化的对象实例。</param>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// <param name="defineDoc">包含字段定义的 XML 元素 (<see cref="XElement" />)。</param>
|
|
|
|
|
|
/// <param name="defineType">目标对象的类型 (<see cref="Type" />)。</param>
|
2025-08-27 19:56:49 +08:00
|
|
|
|
public static void DefaultInitDefine(Define define, XElement defineDoc, Type defineType)
|
2025-07-14 11:39:31 +08:00
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
// FieldInfo 缓存
|
2025-08-27 19:56:49 +08:00
|
|
|
|
if (!_fieldCache.TryGetValue(defineType, out var fields))
|
|
|
|
|
|
{
|
|
|
|
|
|
fields = defineType.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
|
|
|
|
|
|
_fieldCache[defineType] = fields;
|
|
|
|
|
|
}
|
2025-07-12 17:39:46 +08:00
|
|
|
|
|
|
|
|
|
|
// 遍历字段并尝试从 XElement 中赋值
|
|
|
|
|
|
foreach (var field in fields)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 查找对应的 XElement 子元素
|
|
|
|
|
|
var element = defineDoc.Element(field.Name);
|
|
|
|
|
|
if (element != null)
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-08-27 19:56:49 +08:00
|
|
|
|
var value = ConvertXElementValueToType(element, field.FieldType);
|
2025-07-12 17:39:46 +08:00
|
|
|
|
field.SetValue(define, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
Debug.LogWarning(
|
|
|
|
|
|
$"<color=yellow>设置字段时出错,字段名: {field.Name}; 值: {element.Value}; 错误: {ex.Message}。</color>");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var properties = defineType
|
|
|
|
|
|
.GetProperties(BindingFlags.Public | BindingFlags.Instance |
|
|
|
|
|
|
BindingFlags.NonPublic) // 也获取私有属性,但要求私有属性有 setter
|
|
|
|
|
|
.Where(p => p.CanWrite); // 只要可写就可以设置
|
|
|
|
|
|
foreach (var property in properties)
|
|
|
|
|
|
{
|
|
|
|
|
|
var element = defineDoc.Element(property.Name);
|
|
|
|
|
|
if (element != null)
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var value = ConvertXElementValueToType(element, property.PropertyType);
|
|
|
|
|
|
property.SetValue(define, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogWarning(
|
|
|
|
|
|
$"<color=yellow>设置属性 '{property.Name}' 时出错,值: '{element.Value}',错误: {ex.Message}。</color>");
|
2025-07-12 17:39:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 将 XElement 值转换为特定类型。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="element">XElement 元素。</param>
|
|
|
|
|
|
/// <param name="targetType">目标类型。</param>
|
|
|
|
|
|
/// <returns>转换后的值。</returns>
|
2025-08-27 19:56:49 +08:00
|
|
|
|
private static object ConvertXElementValueToType(XElement element, Type targetType)
|
2025-07-21 13:58:58 +08:00
|
|
|
|
{
|
2025-08-27 19:56:49 +08:00
|
|
|
|
if (IsTypeInheritedFrom(targetType, typeof(Define)))
|
2025-07-21 13:58:58 +08:00
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
if (element.HasElements) return LoadDefineClass(element, targetType.Name);
|
|
|
|
|
|
|
|
|
|
|
|
// 引用另一个 Define
|
|
|
|
|
|
var reference = (Define)Activator.CreateInstance(targetType);
|
|
|
|
|
|
reference.isReferene = true;
|
|
|
|
|
|
reference.description = targetType.Name;
|
|
|
|
|
|
reference.label = element.Name.LocalName; // 使用元素的名称作为标签
|
|
|
|
|
|
reference.defName = element.Value;
|
|
|
|
|
|
return reference;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
2025-10-10 14:08:23 +08:00
|
|
|
|
|
|
|
|
|
|
if (targetType.IsArray || typeof(IList).IsAssignableFrom(targetType))
|
2025-08-27 19:56:49 +08:00
|
|
|
|
return ProcessArrayField(targetType, element);
|
2025-10-10 14:08:23 +08:00
|
|
|
|
|
|
|
|
|
|
if (targetType.IsEnum) return Enum.Parse(targetType, element.Value);
|
|
|
|
|
|
|
|
|
|
|
|
return Convert.ChangeType(element.Value, targetType);
|
2025-08-27 19:56:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 处理数组字段。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="fieldType">字段类型。</param>
|
|
|
|
|
|
/// <param name="element">XElement 元素。</param>
|
|
|
|
|
|
/// <returns>处理后的数组对象。</returns>
|
2025-08-27 19:56:49 +08:00
|
|
|
|
private static object ProcessArrayField(Type fieldType, XElement element)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 获取集合的元素类型
|
|
|
|
|
|
var elementType = fieldType.IsArray
|
|
|
|
|
|
? fieldType.GetElementType()
|
2025-10-10 14:08:23 +08:00
|
|
|
|
: fieldType.GetGenericArguments().FirstOrDefault();
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
|
|
|
|
|
if (elementType == null)
|
|
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
Debug.LogWarning($"<color=yellow>无法确定类型为 {fieldType.Name} 的集合字段的元素类型。</color>");
|
2025-08-27 19:56:49 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2025-07-21 13:58:58 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
var arrayElements = new List<object>();
|
|
|
|
|
|
|
|
|
|
|
|
// 遍历 XML 子元素
|
|
|
|
|
|
foreach (var liElement in element.Elements())
|
|
|
|
|
|
arrayElements.Add(ConvertXElementValueToType(liElement, elementType));
|
|
|
|
|
|
|
|
|
|
|
|
// 根据目标字段的类型构造结果
|
|
|
|
|
|
if (fieldType.IsArray)
|
|
|
|
|
|
{
|
|
|
|
|
|
var resultArray = Array.CreateInstance(elementType, arrayElements.Count);
|
2025-10-10 14:08:23 +08:00
|
|
|
|
for (var i = 0; i < arrayElements.Count; i++) resultArray.SetValue(arrayElements[i], i);
|
2025-08-27 19:56:49 +08:00
|
|
|
|
return resultArray;
|
|
|
|
|
|
}
|
2025-10-10 14:08:23 +08:00
|
|
|
|
|
|
|
|
|
|
if (typeof(IList).IsAssignableFrom(fieldType))
|
2025-08-27 19:56:49 +08:00
|
|
|
|
{
|
|
|
|
|
|
var listType = typeof(List<>).MakeGenericType(elementType);
|
|
|
|
|
|
var resultList = (IList)Activator.CreateInstance(listType);
|
2025-10-10 14:08:23 +08:00
|
|
|
|
foreach (var item in arrayElements) resultList.Add(item);
|
2025-08-27 19:56:49 +08:00
|
|
|
|
return resultList;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-07-12 12:04:59 +08:00
|
|
|
|
/// <summary>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// 从 <see cref="List{XDocument}" /> 中查找指定根元素名称的文档。
|
2025-07-12 12:04:59 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="xmlDocuments">XML 文档列表。</param>
|
|
|
|
|
|
/// <param name="rootName">目标根元素名称。</param>
|
|
|
|
|
|
/// <returns>符合条件的 XML 文档列表。</returns>
|
2025-07-12 17:39:46 +08:00
|
|
|
|
public static List<XDocument> FindDocumentsWithRootName(List<XDocument> xmlDocuments, string rootName)
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = xmlDocuments
|
|
|
|
|
|
.Where(doc => doc.Root != null && doc.Root.Name.LocalName == rootName)
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
2025-07-12 12:04:59 +08:00
|
|
|
|
{
|
2025-07-12 17:39:46 +08:00
|
|
|
|
// 对齐格式(左对齐,固定宽度)
|
|
|
|
|
|
const int labelWidth = -15;
|
|
|
|
|
|
const int valueWidth = -30;
|
|
|
|
|
|
|
|
|
|
|
|
var sb = new StringBuilder();
|
2025-07-12 21:35:58 +08:00
|
|
|
|
|
2025-07-12 17:39:46 +08:00
|
|
|
|
// 基础字段
|
|
|
|
|
|
sb.AppendLine($"{"PackID:",labelWidth}{packID,valueWidth}");
|
|
|
|
|
|
sb.AppendLine();
|
|
|
|
|
|
|
|
|
|
|
|
// PackAbout 对象
|
|
|
|
|
|
sb.AppendLine("=== PackAbout ===");
|
2025-08-27 19:56:49 +08:00
|
|
|
|
sb.AppendLine(packAbout?.ToString() ?? "N/A");
|
2025-07-12 17:39:46 +08:00
|
|
|
|
sb.AppendLine();
|
2025-07-12 12:04:59 +08:00
|
|
|
|
|
2025-07-12 17:39:46 +08:00
|
|
|
|
// 字典字段(defines)
|
|
|
|
|
|
sb.AppendLine("=== Defines ===");
|
|
|
|
|
|
if (defines != null && defines.Count > 0)
|
|
|
|
|
|
foreach (var kvp in defines)
|
2025-07-12 12:04:59 +08:00
|
|
|
|
{
|
2025-07-12 17:39:46 +08:00
|
|
|
|
sb.AppendLine($"【{kvp.Key}】"); // 输出字典的键(类别名)
|
|
|
|
|
|
foreach (var define in kvp.Value) // 遍历该类别下的所有 Define 对象
|
|
|
|
|
|
sb.AppendLine(define.ToString()); // 调用 Define 的 ToString()
|
|
|
|
|
|
sb.AppendLine(); // 每个类别后空一行
|
2025-07-12 12:04:59 +08:00
|
|
|
|
}
|
2025-07-12 17:39:46 +08:00
|
|
|
|
else
|
2025-08-27 19:56:49 +08:00
|
|
|
|
sb.AppendLine("未找到定义。");
|
2025-07-12 12:04:59 +08:00
|
|
|
|
|
2025-07-12 17:39:46 +08:00
|
|
|
|
return sb.ToString();
|
2025-07-12 12:04:59 +08:00
|
|
|
|
}
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
2025-07-14 11:39:31 +08:00
|
|
|
|
/// <summary>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// 检查字段的类型是否继承自指定的类(严格派生,不包括基类本身)。
|
2025-07-14 11:39:31 +08:00
|
|
|
|
/// </summary>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// <param name="field">字段信息。</param>
|
|
|
|
|
|
/// <param name="baseType">要检查的基类类型。</param>
|
|
|
|
|
|
/// <returns>如果字段的类型是基类的严格派生类,则返回 true;否则返回 false。</returns>
|
2025-07-14 11:39:31 +08:00
|
|
|
|
public static bool IsFieldTypeInheritedFrom(FieldInfo field, Type baseType)
|
|
|
|
|
|
{
|
|
|
|
|
|
var fieldType = field.FieldType;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
// 严格派生:不包括基类本身
|
|
|
|
|
|
return fieldType != null && fieldType != baseType && baseType.IsAssignableFrom(fieldType);
|
2025-07-14 11:39:31 +08:00
|
|
|
|
}
|
2025-07-15 15:26:58 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
/// <summary>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// 检查一个类型是否继承自指定的基类(严格派生,不包括基类本身)。
|
2025-08-27 19:56:49 +08:00
|
|
|
|
/// </summary>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// <param name="type">要检查的类型。</param>
|
|
|
|
|
|
/// <param name="baseType">要检查的基类类型。</param>
|
|
|
|
|
|
/// <returns>如果类型是基类的严格派生类,则返回 true;否则返回 false。</returns>
|
2025-08-27 19:56:49 +08:00
|
|
|
|
public static bool IsTypeInheritedFrom(Type type, Type baseType)
|
|
|
|
|
|
{
|
|
|
|
|
|
return type != null && type != baseType && baseType.IsAssignableFrom(type);
|
|
|
|
|
|
}
|
2025-07-12 11:54:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|