136 lines
4.5 KiB
C#
136 lines
4.5 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
/// 使用静态方法从 XML 文档创建 PackAbout 实例。
|
|
/// </summary>
|
|
/// <param name="doc">XML 文档。</param>
|
|
/// <returns>初始化的 PackAbout 实例。</returns>
|
|
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<string>();
|
|
result.after = Array.Empty<string>();
|
|
result.necessary = Array.Empty<string>();
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取子节点的所有文本值并返回为字符串数组。
|
|
/// </summary>
|
|
/// <param name="node">父节点。</param>
|
|
/// <returns>字符串数组。</returns>
|
|
private static string[] GetChildNodeValues(XmlNode node)
|
|
{
|
|
if (node == null || node.ChildNodes.Count == 0)
|
|
{
|
|
return Array.Empty<string>();
|
|
}
|
|
|
|
return node.ChildNodes.Cast<XmlNode>()
|
|
.Where(n => n.NodeType == XmlNodeType.Text)
|
|
.Select(n => n.Value.Trim())
|
|
.ToArray();
|
|
}
|
|
}
|
|
public class DefinePack
|
|
{
|
|
public string packID;
|
|
public PackAbout packAbout;
|
|
public List<Define> 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.FromXmlDocument(aboutXml);
|
|
if (aboutXmls.Count > 1)
|
|
{
|
|
Debug.LogWarning("包拥有多个配置文件,系统选择了加载序的第一个,请避免这种情况");
|
|
}
|
|
|
|
return true;
|
|
}
|
|
/// <summary>
|
|
/// 从 List<c>XmlDocument</c> 中查找指定根元素名称的文档。
|
|
/// </summary>
|
|
/// <param name="xmlDocuments">XML 文档列表。</param>
|
|
/// <param name="rootName">目标根元素名称。</param>
|
|
/// <returns>符合条件的 XML 文档列表。</returns>
|
|
public static List<XmlDocument> FindDocumentsWithRootName(List<XmlDocument> xmlDocuments, string rootName)
|
|
{
|
|
var result = new List<XmlDocument>();
|
|
|
|
foreach (var xmlDoc in xmlDocuments)
|
|
{
|
|
try
|
|
{
|
|
// 获取根节点
|
|
var root = xmlDoc.DocumentElement;
|
|
|
|
if (root != null && root.Name == rootName)
|
|
{
|
|
// 如果根节点名称匹配,则添加到结果列表
|
|
result.Add(xmlDoc);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.Error.WriteLine($"处理 XML 文档时发生错误: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
} |