main #7
@ -1,12 +1,13 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Xml;
|
using System.Xml.Linq;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace Data
|
namespace Data
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
public struct PackAbout
|
public struct PackAbout
|
||||||
{
|
{
|
||||||
public string name;
|
public string name;
|
||||||
@ -24,59 +25,60 @@ namespace Data
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="doc">XML 文档。</param>
|
/// <param name="doc">XML 文档。</param>
|
||||||
/// <returns>初始化的 PackAbout 实例。</returns>
|
/// <returns>初始化的 PackAbout 实例。</returns>
|
||||||
public static PackAbout FromXmlDocument(XmlDocument doc)
|
public static PackAbout FromXDocument(XDocument doc)
|
||||||
{
|
{
|
||||||
var aboutNode = doc.DocumentElement;
|
var aboutElement = doc.Element("About"); // Assuming "about" is the root element name
|
||||||
if (aboutNode == null)
|
if (aboutElement == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentException("XML 文档无效,根节点为空。");
|
throw new ArgumentException("XML 文档无效,根节点为空或不是 'About'。");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化 PackAbout 实例
|
// Initialize PackAbout instance
|
||||||
PackAbout result = new();
|
PackAbout result = new();
|
||||||
|
|
||||||
// 读取子节点内容
|
// Read element content
|
||||||
result.name = aboutNode["name"]?.InnerText ?? "Unknown";
|
result.name = aboutElement.Element("name")?.Value ?? "Unknown";
|
||||||
result.description = aboutNode["description"]?.InnerText ?? "Unknown";
|
result.description = aboutElement.Element("description")?.Value ?? "Unknown";
|
||||||
result.version = aboutNode["version"]?.InnerText ?? "Unknown";
|
result.author = aboutElement.Element("author")?.Value ?? "Unknown"; // Assuming 'author' is also a direct child
|
||||||
result.packID = aboutNode["packID"]?.InnerText ?? "Unknown";
|
result.version = aboutElement.Element("version")?.Value ?? "Unknown";
|
||||||
|
result.packID = aboutElement.Element("packID")?.Value ?? "Unknown";
|
||||||
|
|
||||||
// 读取 "sort" 节点下的子节点
|
// Read child elements under the "sort" element
|
||||||
XmlNode sortNode = aboutNode["sort"];
|
XElement sortElement = aboutElement.Element("sort");
|
||||||
if (sortNode != null)
|
if (sortElement != null)
|
||||||
{
|
{
|
||||||
result.before = GetChildNodeValues(sortNode["before"]);
|
result.before = GetElementValues(sortElement.Element("before"));
|
||||||
result.after = GetChildNodeValues(sortNode["after"]);
|
result.after = GetElementValues(sortElement.Element("after"));
|
||||||
result.necessary = GetChildNodeValues(sortNode["necessary"]);
|
result.necessary = GetElementValues(sortElement.Element("necessary"));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
result.before = Array.Empty<string>();
|
result.before = System.Array.Empty<string>();
|
||||||
result.after = Array.Empty<string>();
|
result.after = System.Array.Empty<string>();
|
||||||
result.necessary = Array.Empty<string>();
|
result.necessary = System.Array.Empty<string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取子节点的所有文本值并返回为字符串数组。
|
/// 获取指定 XElement 下所有子元素的值并返回为字符串数组。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="node">父节点。</param>
|
/// <param name="element">父 XElement。</param>
|
||||||
/// <returns>字符串数组。</returns>
|
/// <returns>字符串数组。</returns>
|
||||||
private static string[] GetChildNodeValues(XmlNode node)
|
private static string[] GetElementValues(XElement element)
|
||||||
{
|
{
|
||||||
if (node == null || node.ChildNodes.Count == 0)
|
if (element == null || !element.HasElements)
|
||||||
{
|
{
|
||||||
return Array.Empty<string>();
|
return System.Array.Empty<string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
return node.ChildNodes.Cast<XmlNode>()
|
return element.Elements()
|
||||||
.Where(n => n.NodeType == XmlNodeType.Text)
|
.Select(e => e.Value.Trim())
|
||||||
.Select(n => n.Value.Trim())
|
|
||||||
.ToArray();
|
.ToArray();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class DefinePack
|
public class DefinePack
|
||||||
{
|
{
|
||||||
public string packID;
|
public string packID;
|
||||||
@ -93,42 +95,39 @@ namespace Data
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
var aboutXml=aboutXmls[0];
|
var aboutXml=aboutXmls[0];
|
||||||
packAbout=PackAbout.FromXmlDocument(aboutXml);
|
packAbout=PackAbout.FromXDocument(aboutXml);
|
||||||
|
packID=packAbout.packID;
|
||||||
if (aboutXmls.Count > 1)
|
if (aboutXmls.Count > 1)
|
||||||
{
|
{
|
||||||
Debug.LogWarning("包拥有多个配置文件,系统选择了加载序的第一个,请避免这种情况");
|
Debug.LogWarning($"{packAbout.name}包拥有多个配置文件,系统选择了加载序的第一个,请避免这种情况");
|
||||||
|
}
|
||||||
|
|
||||||
|
var defineXmls=FindDocumentsWithRootName(aboutXmls, "Define");
|
||||||
|
foreach (var defineXml in defineXmls)
|
||||||
|
{
|
||||||
|
LoadDefines(defineXml);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void LoadDefines(XDocument defineDoc)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 从 List<c>XmlDocument</c> 中查找指定根元素名称的文档。
|
/// 从 List<c>XDocument</c> 中查找指定根元素名称的文档。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="xmlDocuments">XML 文档列表。</param>
|
/// <param name="xmlDocuments">XML 文档列表。</param>
|
||||||
/// <param name="rootName">目标根元素名称。</param>
|
/// <param name="rootName">目标根元素名称。</param>
|
||||||
/// <returns>符合条件的 XML 文档列表。</returns>
|
/// <returns>符合条件的 XML 文档列表。</returns>
|
||||||
public static List<XmlDocument> FindDocumentsWithRootName(List<XmlDocument> xmlDocuments, string rootName)
|
public static List<XDocument> FindDocumentsWithRootName(List<XDocument> xmlDocuments, string rootName)
|
||||||
{
|
{
|
||||||
var result = new List<XmlDocument>();
|
// Using LINQ to Objects for a more concise solution
|
||||||
|
var result = xmlDocuments
|
||||||
foreach (var xmlDoc in xmlDocuments)
|
.Where(doc => doc.Root != null && doc.Root.Name.LocalName == rootName)
|
||||||
{
|
.ToList();
|
||||||
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Xml;
|
using System.Xml.Linq;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using Formatting = Newtonsoft.Json.Formatting;
|
using Formatting = Newtonsoft.Json.Formatting;
|
||||||
@ -196,37 +196,30 @@ namespace Utils
|
|||||||
|
|
||||||
return xmlFilePaths;
|
return xmlFilePaths;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 从指定路径加载所有 XML 文件并解析为 XmlDocument 对象。
|
/// 从指定路径加载所有 XML 文件并解析为 XDocument 对象。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="paths">文件夹路径数组。</param>
|
/// <param name="paths">文件夹路径数组。</param>
|
||||||
/// <returns>包含所有解析后的 XmlDocument 对象的列表。</returns>
|
/// <returns>包含所有解析后的 XDocument 对象的列表。</returns>
|
||||||
public static List<XmlDocument> LoadXmlFromPaths(string[] paths)
|
public static List<XDocument> LoadXmlFromPaths(string[] paths)
|
||||||
{
|
{
|
||||||
var xmlDocuments = new List<XmlDocument>();
|
var xDocuments = new List<XDocument>();
|
||||||
var xmlFilePaths = GetXmlFilePathsFromPaths(paths);
|
var xmlFilePaths = GetXmlFilePathsFromPaths(paths);
|
||||||
|
|
||||||
foreach (var filePath in xmlFilePaths)
|
foreach (var filePath in xmlFilePaths)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// 创建一个新的 XmlDocument 实例
|
var xDoc = XDocument.Load(filePath);
|
||||||
var xmlDoc = new XmlDocument();
|
xDocuments.Add(xDoc);
|
||||||
|
|
||||||
// 加载 XML 文件内容
|
|
||||||
xmlDoc.Load(filePath);
|
|
||||||
|
|
||||||
// 将解析后的 XmlDocument 添加到结果列表中
|
|
||||||
xmlDocuments.Add(xmlDoc);
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (System.Exception ex)
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine($"加载 XML 文件 {filePath} 时发生错误: {ex.Message}");
|
Debug.LogError($"加载 XML 文件 {filePath} 时发生错误: {ex.Message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return xmlDocuments;
|
return xDocuments;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取指定单个路径下的所有 XML 文件。
|
/// 获取指定单个路径下的所有 XML 文件。
|
||||||
@ -238,14 +231,42 @@ namespace Utils
|
|||||||
return GetXmlFilePathsFromPaths(new[] { path });
|
return GetXmlFilePathsFromPaths(new[] { path });
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 从指定单个路径加载所有 XML 文件并解析为 XmlDocument 对象。
|
/// 从指定单个路径加载所有 XML 文件并解析为 XDocument 对象。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="path">文件夹路径。</param>
|
/// <param name="path">文件夹路径。</param>
|
||||||
/// <returns>包含所有解析后的 XmlDocument 对象的列表。</returns>
|
/// <returns>包含所有解析后的 XDocument 对象的列表。</returns>
|
||||||
public static List<XmlDocument> LoadXmlFromPath(string path)
|
public static List<XDocument> LoadXmlFromPath(string path)
|
||||||
{
|
{
|
||||||
return LoadXmlFromPaths(new[] { path });
|
return LoadXmlFromPaths(new[] { path });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取文件夹列表中所有直接子文件夹的路径。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="folderPaths">文件夹路径列表。</param>
|
||||||
|
/// <returns>包含所有子文件夹路径的列表。</returns>
|
||||||
|
public static List<string> GetSubFolders(List<string> folderPaths)
|
||||||
|
{
|
||||||
|
List<string> result = new List<string>();
|
||||||
|
|
||||||
|
foreach (string folderPath in folderPaths)
|
||||||
|
{
|
||||||
|
if (Directory.Exists(folderPath))
|
||||||
|
{
|
||||||
|
// 获取当前文件夹的直接子文件夹
|
||||||
|
string[] subFolders = Directory.GetDirectories(folderPath);
|
||||||
|
|
||||||
|
// 将子文件夹路径添加到结果列表中
|
||||||
|
result.AddRange(subFolders);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.LogWarning($"警告: 文件夹不存在 - {folderPath}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Reference in New Issue
Block a user