2025-07-12 12:04:59 +08:00
|
|
|
|
using System;
|
2025-07-12 11:54:19 +08:00
|
|
|
|
using System.Collections.Generic;
|
2025-07-12 17:39:46 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Xml.Linq;
|
2025-07-12 21:35:58 +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-07-12 17:39:46 +08:00
|
|
|
|
public class PackAbout
|
2025-07-12 11:54:19 +08:00
|
|
|
|
{
|
2025-07-12 21:35:58 +08:00
|
|
|
|
public string name;
|
2025-07-13 08:56:33 +08:00
|
|
|
|
public string description;
|
|
|
|
|
public string author;
|
|
|
|
|
public string version;
|
|
|
|
|
public string packID;
|
2025-07-12 17:39:46 +08:00
|
|
|
|
|
|
|
|
|
public string[] necessary;
|
2025-07-13 08:56:33 +08:00
|
|
|
|
public string[] after;
|
|
|
|
|
public string[] before;
|
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>
|
|
|
|
|
public static PackAbout FromXDocument(XDocument doc)
|
|
|
|
|
{
|
|
|
|
|
var aboutElement = doc.Element("About");
|
2025-07-12 21:35:58 +08:00
|
|
|
|
if (aboutElement == null) throw new ArgumentException("XML 文档无效,根节点为空或不是 'About'。");
|
2025-07-12 17:39:46 +08:00
|
|
|
|
PackAbout result = new();
|
|
|
|
|
|
|
|
|
|
result.name = aboutElement.Element("name")?.Value ?? "Unknown";
|
|
|
|
|
result.description = aboutElement.Element("description")?.Value ?? "Unknown";
|
2025-07-12 21:35:58 +08:00
|
|
|
|
result.author = aboutElement.Element("author")?.Value ?? "Unknown";
|
2025-07-12 17:39:46 +08:00
|
|
|
|
result.version = aboutElement.Element("version")?.Value ?? "Unknown";
|
|
|
|
|
result.packID = aboutElement.Element("packID")?.Value ?? "Unknown";
|
2025-07-12 21:35:58 +08:00
|
|
|
|
|
|
|
|
|
var sortElement = aboutElement.Element("sort");
|
2025-07-12 17:39:46 +08:00
|
|
|
|
if (sortElement != null)
|
|
|
|
|
{
|
|
|
|
|
result.before = GetElementValues(sortElement.Element("before"));
|
|
|
|
|
result.after = GetElementValues(sortElement.Element("after"));
|
|
|
|
|
result.necessary = GetElementValues(sortElement.Element("necessary"));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-07-12 21:35:58 +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>
|
|
|
|
|
/// <returns>字符串数组。</returns>
|
|
|
|
|
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-07-12 21:35:58 +08:00
|
|
|
|
const int labelWidth = -12; // 负数为左对齐
|
|
|
|
|
const int valueWidth = -30; // 负数为左对齐
|
2025-07-12 17:39:46 +08:00
|
|
|
|
|
|
|
|
|
// 使用StringBuilder高效拼接
|
|
|
|
|
var sb = new StringBuilder();
|
2025-07-12 21:35:58 +08:00
|
|
|
|
|
2025-07-12 17:39:46 +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 21:35:58 +08:00
|
|
|
|
sb.AppendLine(
|
|
|
|
|
$"{"Necessary:",labelWidth}{string.Join(", ", necessary ?? Array.Empty<string>()),valueWidth}");
|
2025-07-12 17:39:46 +08:00
|
|
|
|
sb.AppendLine($"{"After:",labelWidth}{string.Join(", ", after ?? Array.Empty<string>()),valueWidth}");
|
|
|
|
|
sb.AppendLine($"{"Before:",labelWidth}{string.Join(", ", before ?? Array.Empty<string>()),valueWidth}");
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
2025-07-12 11:54:19 +08:00
|
|
|
|
}
|
2025-07-12 17:39:46 +08:00
|
|
|
|
|
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-07-12 17:39:46 +08:00
|
|
|
|
/// <summary>
|
2025-07-12 21:35:58 +08:00
|
|
|
|
/// define类别及其定义
|
2025-07-12 17:39:46 +08:00
|
|
|
|
/// </summary>
|
2025-07-12 21:35:58 +08:00
|
|
|
|
public Dictionary<string, List<Define>> defines;
|
|
|
|
|
|
|
|
|
|
public PackAbout packAbout;
|
|
|
|
|
public string packID;
|
|
|
|
|
|
2025-07-12 11:54:19 +08:00
|
|
|
|
|
2025-07-12 17:39:46 +08:00
|
|
|
|
public bool LoadPack(string packPath)
|
2025-07-12 11:54:19 +08:00
|
|
|
|
{
|
2025-07-12 21:41:54 +08:00
|
|
|
|
var packDatas = ConfigProcessor.LoadXmlFromPath(packPath);
|
2025-07-12 21:35:58 +08:00
|
|
|
|
var aboutXmls = FindDocumentsWithRootName(packDatas, "About");
|
2025-07-12 17:39:46 +08:00
|
|
|
|
if (aboutXmls == null || aboutXmls.Count < 1)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("包缺少配置文件,加载跳过");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-07-12 21:35:58 +08:00
|
|
|
|
|
|
|
|
|
var aboutXml = aboutXmls[0];
|
|
|
|
|
packAbout = PackAbout.FromXDocument(aboutXml);
|
|
|
|
|
packID = packAbout.packID;
|
|
|
|
|
if (aboutXmls.Count > 1) Debug.LogWarning($"{packAbout.name}包拥有多个配置文件,系统选择了加载序的第一个,请避免这种情况");
|
|
|
|
|
|
|
|
|
|
var defineXmls = FindDocumentsWithRootName(aboutXmls, "Define");
|
|
|
|
|
foreach (var defineXml in defineXmls) LoadDefines(defineXml);
|
|
|
|
|
|
2025-07-12 17:39:46 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoadDefines(XDocument defineDoc)
|
|
|
|
|
{
|
|
|
|
|
var rootElement = defineDoc.Root;
|
2025-07-12 21:35:58 +08:00
|
|
|
|
if (rootElement == null || rootElement.Name != "Define")
|
2025-07-12 17:39:46 +08:00
|
|
|
|
return;
|
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;
|
|
|
|
|
var def = LoadDefineClass(element);
|
|
|
|
|
if (def == null)
|
|
|
|
|
continue;
|
|
|
|
|
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-07-12 17:39:46 +08:00
|
|
|
|
|
|
|
|
|
private Define LoadDefineClass(XElement defineDoc)
|
2025-07-12 21:35:58 +08:00
|
|
|
|
{
|
2025-07-12 17:39:46 +08:00
|
|
|
|
var className = defineDoc.Name.ToString();
|
|
|
|
|
var assembly = Assembly.GetExecutingAssembly();
|
|
|
|
|
|
|
|
|
|
Type type;
|
|
|
|
|
if (!className.Contains('.'))
|
|
|
|
|
{
|
|
|
|
|
// 尝试拼接默认命名空间
|
|
|
|
|
var fullClassName = CoreNamespace + className;
|
|
|
|
|
type = assembly.GetType(fullClassName);
|
|
|
|
|
|
|
|
|
|
// 如果拼接命名空间后仍找不到,尝试直接查找(可能是全局命名空间下的类)
|
2025-07-12 21:35:58 +08:00
|
|
|
|
if (type == null) type = assembly.GetType(className);
|
2025-07-12 17:39:46 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// 直接查找
|
|
|
|
|
type = assembly.GetType(className);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError($"未定义的类型: {className}");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var constructor = type.GetConstructor(Type.EmptyTypes);
|
|
|
|
|
if (constructor == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError($"{className} 必须包含无参构造函数");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3. 创建实例
|
|
|
|
|
object instance;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
instance = Activator.CreateInstance(type);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError($"创建 {className} 实例失败: {ex.Message}");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4. 检查是否继承自 Define
|
|
|
|
|
if (instance is not Define define)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError($"{className} 必须继承自 Define");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-12 21:35:58 +08:00
|
|
|
|
if (define.Init(defineDoc)) return define;
|
2025-07-12 17:39:46 +08:00
|
|
|
|
// 获取类的所有字段(不包括私有字段)
|
|
|
|
|
var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
|
|
|
|
|
|
|
|
|
|
// 遍历字段并尝试从 XElement 中赋值
|
|
|
|
|
foreach (var field in fields)
|
|
|
|
|
{
|
|
|
|
|
// 查找对应的 XElement 子元素
|
|
|
|
|
var element = defineDoc.Element(field.Name);
|
|
|
|
|
if (element != null)
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// 将子元素的值转换为目标类型并赋值
|
2025-07-12 21:35:58 +08:00
|
|
|
|
var value = Convert.ChangeType(element.Value, field.FieldType);
|
2025-07-12 17:39:46 +08:00
|
|
|
|
field.SetValue(define, value);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning($"Error setting field {field.Name}: {ex.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-12 21:35:58 +08:00
|
|
|
|
|
2025-07-12 17:39:46 +08:00
|
|
|
|
return define;
|
|
|
|
|
}
|
2025-07-12 21:35:58 +08:00
|
|
|
|
|
2025-07-12 12:04:59 +08:00
|
|
|
|
/// <summary>
|
2025-07-12 21:35:58 +08:00
|
|
|
|
/// 从 List<c>XDocument</c> 中查找指定根元素名称的文档。
|
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)
|
|
|
|
|
{
|
|
|
|
|
// Using LINQ to Objects for a more concise solution
|
|
|
|
|
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 ===");
|
|
|
|
|
sb.AppendLine(packAbout?.ToString() ?? "N/A"); // 调用 PackAbout 的 ToString()
|
|
|
|
|
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
|
|
|
|
|
sb.AppendLine("No defines found.");
|
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-07-12 11:54:19 +08:00
|
|
|
|
}
|
|
|
|
|
}
|