using System; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; 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 FromXDocument(XDocument doc) { var aboutElement = doc.Element("About"); // Assuming "about" is the root element name if (aboutElement == null) { throw new ArgumentException("XML 文档无效,根节点为空或不是 'About'。"); } // Initialize PackAbout instance PackAbout result = new(); // Read element content result.name = aboutElement.Element("name")?.Value ?? "Unknown"; result.description = aboutElement.Element("description")?.Value ?? "Unknown"; result.author = aboutElement.Element("author")?.Value ?? "Unknown"; // Assuming 'author' is also a direct child result.version = aboutElement.Element("version")?.Value ?? "Unknown"; result.packID = aboutElement.Element("packID")?.Value ?? "Unknown"; // Read child elements under the "sort" element XElement sortElement = aboutElement.Element("sort"); if (sortElement != null) { result.before = GetElementValues(sortElement.Element("before")); result.after = GetElementValues(sortElement.Element("after")); result.necessary = GetElementValues(sortElement.Element("necessary")); } else { result.before = System.Array.Empty(); result.after = System.Array.Empty(); result.necessary = System.Array.Empty(); } return result; } /// /// 获取指定 XElement 下所有子元素的值并返回为字符串数组。 /// /// 父 XElement。 /// 字符串数组。 private static string[] GetElementValues(XElement element) { if (element == null || !element.HasElements) { return System.Array.Empty(); } return element.Elements() .Select(e => e.Value.Trim()) .ToArray(); } } public class DefinePack { public string packID; public PackAbout packAbout; public List defines; 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.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); } return true; } private void LoadDefines(XDocument defineDoc) { } /// /// 从 ListXDocument 中查找指定根元素名称的文档。 /// /// XML 文档列表。 /// 目标根元素名称。 /// 符合条件的 XML 文档列表。 public static List FindDocumentsWithRootName(List 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; } } }