From 288f4dce6fc7ab36389f9ed2a561884629c048f6 Mon Sep 17 00:00:00 2001 From: m0_75251201 Date: Sat, 12 Jul 2025 12:25:32 +0800 Subject: [PATCH] =?UTF-8?q?(client)=20feat:=E5=AE=9E=E7=8E=B0=E5=8C=85?= =?UTF-8?q?=E7=9A=84About=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Client/Assets/Scripts/Data/DefinePack.cs | 84 +++++++++++++++++++++++- 1 file changed, 82 insertions(+), 2 deletions(-) diff --git a/Client/Assets/Scripts/Data/DefinePack.cs b/Client/Assets/Scripts/Data/DefinePack.cs index e14972d..4f51f73 100644 --- a/Client/Assets/Scripts/Data/DefinePack.cs +++ b/Client/Assets/Scripts/Data/DefinePack.cs @@ -1,14 +1,81 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Xml; +using UnityEngine; namespace Data { + public struct PackAbout { public string name; public string description; public string author; + public string version; + public string packID; + + public string[] necessary; + public string[] after; + public string[] before; + + /// + /// 使用静态方法从 XML 文档创建 PackAbout 实例。 + /// + /// XML 文档。 + /// 初始化的 PackAbout 实例。 + public static PackAbout FromXmlDocument(XmlDocument doc) + { + var aboutNode = doc.DocumentElement; + if (aboutNode == null) + { + throw new ArgumentException("XML 文档无效,根节点为空。"); + } + + // 初始化 PackAbout 实例 + PackAbout result = new(); + + // 读取子节点内容 + result.name = aboutNode["name"]?.InnerText ?? "Unknown"; + result.description = aboutNode["description"]?.InnerText ?? "Unknown"; + result.version = aboutNode["version"]?.InnerText ?? "Unknown"; + result.packID = aboutNode["packID"]?.InnerText ?? "Unknown"; + + // 读取 "sort" 节点下的子节点 + XmlNode sortNode = aboutNode["sort"]; + if (sortNode != null) + { + result.before = GetChildNodeValues(sortNode["before"]); + result.after = GetChildNodeValues(sortNode["after"]); + result.necessary = GetChildNodeValues(sortNode["necessary"]); + } + else + { + result.before = Array.Empty(); + result.after = Array.Empty(); + result.necessary = Array.Empty(); + } + + return result; + } + + /// + /// 获取子节点的所有文本值并返回为字符串数组。 + /// + /// 父节点。 + /// 字符串数组。 + private static string[] GetChildNodeValues(XmlNode node) + { + if (node == null || node.ChildNodes.Count == 0) + { + return Array.Empty(); + } + + return node.ChildNodes.Cast() + .Where(n => n.NodeType == XmlNodeType.Text) + .Select(n => n.Value.Trim()) + .ToArray(); + } } public class DefinePack { @@ -16,10 +83,23 @@ namespace Data public PackAbout packAbout; public List defines; - public void LoadPack(string packPath) + public bool LoadPack(string packPath) { var packDatas=Utils.FileHandler.LoadXmlFromPath(packPath); - + var aboutXmls=FindDocumentsWithRootName(packDatas,"About"); + if (aboutXmls == null || aboutXmls.Count < 1) + { + Debug.LogError("包缺少配置文件,加载跳过"); + return false; + } + var aboutXml=aboutXmls[0]; + packAbout=PackAbout.FromXmlDocument(aboutXml); + if (aboutXmls.Count > 1) + { + Debug.LogWarning("包拥有多个配置文件,系统选择了加载序的第一个,请避免这种情况"); + } + + return true; } /// /// 从 ListXmlDocument 中查找指定根元素名称的文档。