A Meaningless PR #10
@ -1,12 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml;
|
||||
using System.Xml.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Data
|
||||
{
|
||||
|
||||
|
||||
public struct PackAbout
|
||||
{
|
||||
public string name;
|
||||
@ -24,59 +25,60 @@ namespace Data
|
||||
/// </summary>
|
||||
/// <param name="doc">XML 文档。</param>
|
||||
/// <returns>初始化的 PackAbout 实例。</returns>
|
||||
public static PackAbout FromXmlDocument(XmlDocument doc)
|
||||
public static PackAbout FromXDocument(XDocument doc)
|
||||
{
|
||||
var aboutNode = doc.DocumentElement;
|
||||
if (aboutNode == null)
|
||||
var aboutElement = doc.Element("About"); // Assuming "about" is the root element name
|
||||
if (aboutElement == null)
|
||||
{
|
||||
throw new ArgumentException("XML 文档无效,根节点为空。");
|
||||
throw new ArgumentException("XML 文档无效,根节点为空或不是 'About'。");
|
||||
}
|
||||
|
||||
// 初始化 PackAbout 实例
|
||||
// Initialize PackAbout instance
|
||||
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";
|
||||
// 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";
|
||||
|
||||
// 读取 "sort" 节点下的子节点
|
||||
XmlNode sortNode = aboutNode["sort"];
|
||||
if (sortNode != null)
|
||||
// Read child elements under the "sort" element
|
||||
XElement sortElement = aboutElement.Element("sort");
|
||||
if (sortElement != null)
|
||||
{
|
||||
result.before = GetChildNodeValues(sortNode["before"]);
|
||||
result.after = GetChildNodeValues(sortNode["after"]);
|
||||
result.necessary = GetChildNodeValues(sortNode["necessary"]);
|
||||
result.before = GetElementValues(sortElement.Element("before"));
|
||||
result.after = GetElementValues(sortElement.Element("after"));
|
||||
result.necessary = GetElementValues(sortElement.Element("necessary"));
|
||||
}
|
||||
else
|
||||
{
|
||||
result.before = Array.Empty<string>();
|
||||
result.after = Array.Empty<string>();
|
||||
result.necessary = Array.Empty<string>();
|
||||
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="node">父节点。</param>
|
||||
/// <param name="element">父 XElement。</param>
|
||||
/// <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>()
|
||||
.Where(n => n.NodeType == XmlNodeType.Text)
|
||||
.Select(n => n.Value.Trim())
|
||||
.ToArray();
|
||||
return element.Elements()
|
||||
.Select(e => e.Value.Trim())
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public class DefinePack
|
||||
{
|
||||
public string packID;
|
||||
@ -93,42 +95,39 @@ namespace Data
|
||||
return false;
|
||||
}
|
||||
var aboutXml=aboutXmls[0];
|
||||
packAbout=PackAbout.FromXmlDocument(aboutXml);
|
||||
packAbout=PackAbout.FromXDocument(aboutXml);
|
||||
packID=packAbout.packID;
|
||||
if (aboutXmls.Count > 1)
|
||||
{
|
||||
Debug.LogWarning("包拥有多个配置文件,系统选择了加载序的第一个,请避免这种情况");
|
||||
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>XmlDocument</c> 中查找指定根元素名称的文档。
|
||||
/// 从 List<c>XDocument</c> 中查找指定根元素名称的文档。
|
||||
/// </summary>
|
||||
/// <param name="xmlDocuments">XML 文档列表。</param>
|
||||
/// <param name="rootName">目标根元素名称。</param>
|
||||
/// <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>();
|
||||
|
||||
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}");
|
||||
}
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Xml;
|
||||
using System.Xml.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using UnityEngine;
|
||||
using Formatting = Newtonsoft.Json.Formatting;
|
||||
@ -196,37 +196,30 @@ namespace Utils
|
||||
|
||||
return xmlFilePaths;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从指定路径加载所有 XML 文件并解析为 XmlDocument 对象。
|
||||
/// 从指定路径加载所有 XML 文件并解析为 XDocument 对象。
|
||||
/// </summary>
|
||||
/// <param name="paths">文件夹路径数组。</param>
|
||||
/// <returns>包含所有解析后的 XmlDocument 对象的列表。</returns>
|
||||
public static List<XmlDocument> LoadXmlFromPaths(string[] paths)
|
||||
/// <returns>包含所有解析后的 XDocument 对象的列表。</returns>
|
||||
public static List<XDocument> LoadXmlFromPaths(string[] paths)
|
||||
{
|
||||
var xmlDocuments = new List<XmlDocument>();
|
||||
var xDocuments = new List<XDocument>();
|
||||
var xmlFilePaths = GetXmlFilePathsFromPaths(paths);
|
||||
|
||||
foreach (var filePath in xmlFilePaths)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 创建一个新的 XmlDocument 实例
|
||||
var xmlDoc = new XmlDocument();
|
||||
|
||||
// 加载 XML 文件内容
|
||||
xmlDoc.Load(filePath);
|
||||
|
||||
// 将解析后的 XmlDocument 添加到结果列表中
|
||||
xmlDocuments.Add(xmlDoc);
|
||||
var xDoc = XDocument.Load(filePath);
|
||||
xDocuments.Add(xDoc);
|
||||
}
|
||||
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>
|
||||
/// 获取指定单个路径下的所有 XML 文件。
|
||||
@ -238,14 +231,42 @@ namespace Utils
|
||||
return GetXmlFilePathsFromPaths(new[] { path });
|
||||
}
|
||||
/// <summary>
|
||||
/// 从指定单个路径加载所有 XML 文件并解析为 XmlDocument 对象。
|
||||
/// 从指定单个路径加载所有 XML 文件并解析为 XDocument 对象。
|
||||
/// </summary>
|
||||
/// <param name="path">文件夹路径。</param>
|
||||
/// <returns>包含所有解析后的 XmlDocument 对象的列表。</returns>
|
||||
public static List<XmlDocument> LoadXmlFromPath(string path)
|
||||
/// <returns>包含所有解析后的 XDocument 对象的列表。</returns>
|
||||
public static List<XDocument> LoadXmlFromPath(string 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