(client) feat: Complete data processing logic
This commit is contained in:
@ -1,13 +1,14 @@
|
|||||||
|
|
||||||
|
using System.Text;
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
|
|
||||||
namespace Data
|
namespace Data
|
||||||
{
|
{
|
||||||
public class Define
|
public abstract class Define
|
||||||
{
|
{
|
||||||
public string defName;
|
public string defName;
|
||||||
public string label;
|
public string label;
|
||||||
public string discription;
|
public string description;
|
||||||
public string packID;
|
public string packID;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 初始化方法,根据传入的 XML 元素 (<paramref name="xmlDef"/>) 进行处理。
|
/// 初始化方法,根据传入的 XML 元素 (<paramref name="xmlDef"/>) 进行处理。
|
||||||
@ -34,9 +35,24 @@ namespace Data
|
|||||||
{
|
{
|
||||||
defName = xmlDef.Element("defName")?.Value;
|
defName = xmlDef.Element("defName")?.Value;
|
||||||
label = xmlDef.Element("label")?.Value;
|
label = xmlDef.Element("label")?.Value;
|
||||||
discription = xmlDef.Element("discription")?.Value;
|
description = xmlDef.Element("description")?.Value;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
// 定义对齐格式(左对齐,固定宽度)
|
||||||
|
const int labelWidth = -15; // 标签左对齐,占15字符
|
||||||
|
const int valueWidth = -30; // 值左对齐,占30字符
|
||||||
|
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
|
||||||
|
sb.AppendLine($"{"DefName:",labelWidth}{defName,valueWidth}");
|
||||||
|
sb.AppendLine($"{"Label:",labelWidth}{label,valueWidth}");
|
||||||
|
sb.AppendLine($"{"Description:",labelWidth}{description,valueWidth}");
|
||||||
|
sb.AppendLine($"{"PackID:",labelWidth}{packID,valueWidth}");
|
||||||
|
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,9 +1,11 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UnityEngine.Rendering;
|
||||||
using Utils;
|
using Utils;
|
||||||
|
|
||||||
namespace Data
|
namespace Data
|
||||||
@ -15,149 +17,19 @@ namespace Data
|
|||||||
const string coreNamespace = "Data";
|
const string coreNamespace = "Data";
|
||||||
|
|
||||||
public Dictionary<string, Dictionary<string, Define>> defines = new();
|
public Dictionary<string, Dictionary<string, Define>> defines = new();
|
||||||
|
public Dictionary<string, DefinePack> packs = new();
|
||||||
|
|
||||||
public void Init()
|
public void Init()
|
||||||
{
|
{
|
||||||
// 加载所有 XML 文件路径
|
var packFolder = Directory.GetDirectories(dataSetFilePath[0]);
|
||||||
var xmlFilePaths = FileHandler.GetXmlFilePathsFromPaths(dataSetFilePath);
|
foreach (var folder in packFolder)
|
||||||
|
|
||||||
// 遍历并加载每个 XML 文件
|
|
||||||
foreach (var xmlFilePath in xmlFilePaths)
|
|
||||||
{
|
{
|
||||||
LoadXmlData(xmlFilePath);
|
var pack=new DefinePack();
|
||||||
}
|
if (pack.LoadPack(folder))
|
||||||
|
|
||||||
}
|
|
||||||
public void LoadXmlData(string xmlFilePath)
|
|
||||||
{
|
|
||||||
// 检查文件是否存在
|
|
||||||
if (!File.Exists(xmlFilePath))
|
|
||||||
{
|
|
||||||
Debug.LogWarning($"XML文件不存在: {xmlFilePath}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var xdoc = XDocument.Load(xmlFilePath);
|
|
||||||
// 解析Define节点
|
|
||||||
var rootElement = xdoc.Root;
|
|
||||||
|
|
||||||
if (rootElement != null)
|
|
||||||
{
|
|
||||||
if (rootElement.Name == "Define")
|
|
||||||
{
|
{
|
||||||
foreach (var element in rootElement.Elements())
|
packs.Add(pack.packID, pack);
|
||||||
{
|
|
||||||
var def = LoadDefine(element);
|
|
||||||
var className = element.Name.ToString();
|
|
||||||
var name = element.Element("defName")?.Value;
|
|
||||||
if (def != null && !string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(className))
|
|
||||||
{
|
|
||||||
defines.TryAdd(className, new Dictionary<string, Define>());
|
|
||||||
defines[className][name] = (Define)def;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
public object LoadDefine(XElement xElement)
|
|
||||||
{
|
|
||||||
var className = xElement.Name.ToString();
|
|
||||||
var assembly = Assembly.GetExecutingAssembly();
|
|
||||||
|
|
||||||
Type type;
|
|
||||||
if (!className.Contains('.'))
|
|
||||||
{
|
|
||||||
// 尝试拼接默认命名空间
|
|
||||||
var fullClassName = coreNamespace + className;
|
|
||||||
type = assembly.GetType(fullClassName);
|
|
||||||
|
|
||||||
// 如果拼接命名空间后仍找不到,尝试直接查找(可能是全局命名空间下的类)
|
|
||||||
|
|
||||||
if (type == null)
|
|
||||||
{
|
|
||||||
type = assembly.GetType(className);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// 直接查找
|
|
||||||
type = assembly.GetType(className);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type == null)
|
|
||||||
{
|
|
||||||
Debug.LogError($"未定义的类型: {className}");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
var constructor = type.GetConstructor(Type.EmptyTypes);
|
|
||||||
if (constructor == null)
|
|
||||||
{
|
|
||||||
Debug.LogError($"{className} 必须包含无参构造函数");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. 创建实例
|
|
||||||
object instance;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
instance = Activator.CreateInstance(type);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Debug.LogError($"创建 {className} 实例失败: {ex.Message}");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 4. 检查是否继承自 Define
|
|
||||||
if (instance is not Define define)
|
|
||||||
{
|
|
||||||
Debug.LogError($"{className} 必须继承自 Define");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (define.Init(xElement))
|
|
||||||
{
|
|
||||||
return define;
|
|
||||||
}
|
|
||||||
// 获取类的所有字段(不包括私有字段)
|
|
||||||
var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
|
|
||||||
|
|
||||||
// 遍历字段并尝试从 XElement 中赋值
|
|
||||||
foreach (var field in fields)
|
|
||||||
{
|
|
||||||
// 查找对应的 XElement 子元素
|
|
||||||
var element = xElement.Element(field.Name);
|
|
||||||
if (element != null)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// 将子元素的值转换为目标类型并赋值
|
|
||||||
object value = Convert.ChangeType(element.Value, field.FieldType);
|
|
||||||
field.SetValue(define, value);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Debug.LogWarning($"Error setting field {field.Name}: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return define;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void SetField(object obj, string fieldName, object value)
|
|
||||||
{
|
|
||||||
var type = obj.GetType();
|
|
||||||
var field = type.GetField(fieldName);
|
|
||||||
|
|
||||||
if (field != null)
|
|
||||||
{
|
|
||||||
field.SetValue(obj, Convert.ChangeType(value, field.FieldType));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Debug.LogWarning($"Field '{fieldName}' not found.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,56 +1,302 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Xml;
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Text;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
namespace Data
|
namespace Data
|
||||||
{
|
{
|
||||||
public struct PackAbout
|
|
||||||
|
|
||||||
|
public class PackAbout
|
||||||
{
|
{
|
||||||
public string name;
|
public string name;
|
||||||
public string description;
|
public string description;
|
||||||
public string author;
|
public string author;
|
||||||
}
|
public string version;
|
||||||
public class DefinePack
|
|
||||||
{
|
|
||||||
public string packID;
|
public string packID;
|
||||||
public PackAbout packAbout;
|
|
||||||
public List<Define> defines;
|
|
||||||
|
|
||||||
public void LoadPack(string packPath)
|
public string[] necessary;
|
||||||
{
|
public string[] after;
|
||||||
var packDatas=Utils.FileHandler.LoadXmlFromPath(packPath);
|
public string[] before;
|
||||||
|
|
||||||
}
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 从 List<c>XmlDocument</c> 中查找指定根元素名称的文档。
|
/// 使用静态方法从 XML 文档创建 PackAbout 实例。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="xmlDocuments">XML 文档列表。</param>
|
/// <param name="doc">XML 文档。</param>
|
||||||
/// <param name="rootName">目标根元素名称。</param>
|
/// <returns>初始化的 PackAbout 实例。</returns>
|
||||||
/// <returns>符合条件的 XML 文档列表。</returns>
|
public static PackAbout FromXDocument(XDocument doc)
|
||||||
public static List<XmlDocument> FindDocumentsWithRootName(List<XmlDocument> xmlDocuments, string rootName)
|
|
||||||
{
|
{
|
||||||
var result = new List<XmlDocument>();
|
var aboutElement = doc.Element("About");
|
||||||
|
if (aboutElement == null)
|
||||||
foreach (var xmlDoc in xmlDocuments)
|
|
||||||
{
|
{
|
||||||
try
|
throw new ArgumentException("XML 文档无效,根节点为空或不是 'About'。");
|
||||||
{
|
}
|
||||||
// 获取根节点
|
PackAbout result = new();
|
||||||
var root = xmlDoc.DocumentElement;
|
|
||||||
|
|
||||||
if (root != null && root.Name == rootName)
|
result.name = aboutElement.Element("name")?.Value ?? "Unknown";
|
||||||
{
|
result.description = aboutElement.Element("description")?.Value ?? "Unknown";
|
||||||
// 如果根节点名称匹配,则添加到结果列表
|
result.author = aboutElement.Element("author")?.Value ?? "Unknown";
|
||||||
result.Add(xmlDoc);
|
result.version = aboutElement.Element("version")?.Value ?? "Unknown";
|
||||||
}
|
result.packID = aboutElement.Element("packID")?.Value ?? "Unknown";
|
||||||
}
|
|
||||||
catch (Exception ex)
|
XElement sortElement = aboutElement.Element("sort");
|
||||||
{
|
if (sortElement != null)
|
||||||
Console.Error.WriteLine($"处理 XML 文档时发生错误: {ex.Message}");
|
{
|
||||||
}
|
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;
|
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 override string ToString()
|
||||||
|
{
|
||||||
|
// 定义字段标签和值的对齐格式(如左对齐,固定宽度)
|
||||||
|
const int labelWidth = -12; // 负数为左对齐
|
||||||
|
const int valueWidth = -30; // 负数为左对齐
|
||||||
|
|
||||||
|
// 使用StringBuilder高效拼接
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
|
||||||
|
// 基础字段(单行)
|
||||||
|
sb.AppendLine($"{"Name:",labelWidth}{name,valueWidth}");
|
||||||
|
sb.AppendLine($"{"Description:",labelWidth}{description,valueWidth}");
|
||||||
|
sb.AppendLine($"{"Author:",labelWidth}{author,valueWidth}");
|
||||||
|
sb.AppendLine($"{"Version:",labelWidth}{version,valueWidth}");
|
||||||
|
sb.AppendLine($"{"PackID:",labelWidth}{packID,valueWidth}");
|
||||||
|
|
||||||
|
// 数组字段(多行,每项缩进)
|
||||||
|
sb.AppendLine($"{"Necessary:",labelWidth}{string.Join(", ", necessary ?? Array.Empty<string>()),valueWidth}");
|
||||||
|
sb.AppendLine($"{"After:",labelWidth}{string.Join(", ", after ?? Array.Empty<string>()),valueWidth}");
|
||||||
|
sb.AppendLine($"{"Before:",labelWidth}{string.Join(", ", before ?? Array.Empty<string>()),valueWidth}");
|
||||||
|
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class DefinePack
|
||||||
|
{
|
||||||
|
public string packID;
|
||||||
|
public PackAbout packAbout;
|
||||||
|
/// <summary>
|
||||||
|
/// define类别及其定义
|
||||||
|
/// </summary>
|
||||||
|
public Dictionary<string,List<Define>> defines;
|
||||||
|
|
||||||
|
private const string CoreNamespace = "Data.";
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
var rootElement = defineDoc.Root;
|
||||||
|
if(rootElement==null||rootElement.Name != "Define")
|
||||||
|
return;
|
||||||
|
|
||||||
|
foreach (var element in rootElement.Elements())
|
||||||
|
{
|
||||||
|
var className = element.Name.ToString();
|
||||||
|
if (string.IsNullOrEmpty(className))
|
||||||
|
continue;
|
||||||
|
var def = LoadDefineClass(element);
|
||||||
|
if (def == null)
|
||||||
|
continue;
|
||||||
|
if (!defines.ContainsKey(className))
|
||||||
|
defines.Add(className,new List<Define>());
|
||||||
|
defines[className].Add(def);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Define LoadDefineClass(XElement defineDoc)
|
||||||
|
{
|
||||||
|
var className = defineDoc.Name.ToString();
|
||||||
|
var assembly = Assembly.GetExecutingAssembly();
|
||||||
|
|
||||||
|
Type type;
|
||||||
|
if (!className.Contains('.'))
|
||||||
|
{
|
||||||
|
// 尝试拼接默认命名空间
|
||||||
|
var fullClassName = CoreNamespace + className;
|
||||||
|
type = assembly.GetType(fullClassName);
|
||||||
|
|
||||||
|
// 如果拼接命名空间后仍找不到,尝试直接查找(可能是全局命名空间下的类)
|
||||||
|
if (type == null)
|
||||||
|
{
|
||||||
|
type = assembly.GetType(className);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 直接查找
|
||||||
|
type = assembly.GetType(className);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type == null)
|
||||||
|
{
|
||||||
|
Debug.LogError($"未定义的类型: {className}");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var constructor = type.GetConstructor(Type.EmptyTypes);
|
||||||
|
if (constructor == null)
|
||||||
|
{
|
||||||
|
Debug.LogError($"{className} 必须包含无参构造函数");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 创建实例
|
||||||
|
object instance;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
instance = Activator.CreateInstance(type);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Debug.LogError($"创建 {className} 实例失败: {ex.Message}");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 检查是否继承自 Define
|
||||||
|
if (instance is not Define define)
|
||||||
|
{
|
||||||
|
Debug.LogError($"{className} 必须继承自 Define");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (define.Init(defineDoc))
|
||||||
|
{
|
||||||
|
return define;
|
||||||
|
}
|
||||||
|
// 获取类的所有字段(不包括私有字段)
|
||||||
|
var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
|
||||||
|
|
||||||
|
// 遍历字段并尝试从 XElement 中赋值
|
||||||
|
foreach (var field in fields)
|
||||||
|
{
|
||||||
|
// 查找对应的 XElement 子元素
|
||||||
|
var element = defineDoc.Element(field.Name);
|
||||||
|
if (element != null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// 将子元素的值转换为目标类型并赋值
|
||||||
|
object value = Convert.ChangeType(element.Value, field.FieldType);
|
||||||
|
field.SetValue(define, value);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Debug.LogWarning($"Error setting field {field.Name}: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return define;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
// 对齐格式(左对齐,固定宽度)
|
||||||
|
const int labelWidth = -15;
|
||||||
|
const int valueWidth = -30;
|
||||||
|
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
|
||||||
|
// 基础字段
|
||||||
|
sb.AppendLine($"{"PackID:",labelWidth}{packID,valueWidth}");
|
||||||
|
sb.AppendLine();
|
||||||
|
|
||||||
|
// PackAbout 对象
|
||||||
|
sb.AppendLine("=== PackAbout ===");
|
||||||
|
sb.AppendLine(packAbout?.ToString() ?? "N/A"); // 调用 PackAbout 的 ToString()
|
||||||
|
sb.AppendLine();
|
||||||
|
|
||||||
|
// 字典字段(defines)
|
||||||
|
sb.AppendLine("=== Defines ===");
|
||||||
|
if (defines != null && defines.Count > 0)
|
||||||
|
{
|
||||||
|
foreach (var kvp in defines)
|
||||||
|
{
|
||||||
|
sb.AppendLine($"【{kvp.Key}】"); // 输出字典的键(类别名)
|
||||||
|
foreach (var define in kvp.Value) // 遍历该类别下的所有 Define 对象
|
||||||
|
{
|
||||||
|
sb.AppendLine(define.ToString()); // 调用 Define 的 ToString()
|
||||||
|
}
|
||||||
|
sb.AppendLine(); // 每个类别后空一行
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sb.AppendLine("No defines found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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