135 lines
4.7 KiB
C#
135 lines
4.7 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
/// 使用静态方法从 XML 文档创建 PackAbout 实例。
|
|
/// </summary>
|
|
/// <param name="doc">XML 文档。</param>
|
|
/// <returns>初始化的 PackAbout 实例。</returns>
|
|
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<string>();
|
|
result.after = System.Array.Empty<string>();
|
|
result.necessary = System.Array.Empty<string>();
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取指定 XElement 下所有子元素的值并返回为字符串数组。
|
|
/// </summary>
|
|
/// <param name="element">父 XElement。</param>
|
|
/// <returns>字符串数组。</returns>
|
|
private static string[] GetElementValues(XElement element)
|
|
{
|
|
if (element == null || !element.HasElements)
|
|
{
|
|
return System.Array.Empty<string>();
|
|
}
|
|
|
|
return element.Elements()
|
|
.Select(e => e.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.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)
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从 List<c>XDocument</c> 中查找指定根元素名称的文档。
|
|
/// </summary>
|
|
/// <param name="xmlDocuments">XML 文档列表。</param>
|
|
/// <param name="rootName">目标根元素名称。</param>
|
|
/// <returns>符合条件的 XML 文档列表。</returns>
|
|
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;
|
|
}
|
|
}
|
|
} |