(client) feat:实现包的About加载
This commit is contained in:
@ -1,14 +1,81 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Xml;
|
using System.Xml;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
namespace Data
|
namespace Data
|
||||||
{
|
{
|
||||||
|
|
||||||
public struct PackAbout
|
public struct PackAbout
|
||||||
{
|
{
|
||||||
public string name;
|
public string name;
|
||||||
public string description;
|
public string description;
|
||||||
public string author;
|
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 class DefinePack
|
||||||
{
|
{
|
||||||
@ -16,10 +83,23 @@ namespace Data
|
|||||||
public PackAbout packAbout;
|
public PackAbout packAbout;
|
||||||
public List<Define> defines;
|
public List<Define> defines;
|
||||||
|
|
||||||
public void LoadPack(string packPath)
|
public bool LoadPack(string packPath)
|
||||||
{
|
{
|
||||||
var packDatas=Utils.FileHandler.LoadXmlFromPath(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>
|
/// <summary>
|
||||||
/// 从 List<c>XmlDocument</c> 中查找指定根元素名称的文档。
|
/// 从 List<c>XmlDocument</c> 中查找指定根元素名称的文档。
|
||||||
|
Reference in New Issue
Block a user