(client) chore:将DefineManager的获取路径下xml文件的功能移植到fileHander

This commit is contained in:
m0_75251201
2025-07-10 22:36:02 +08:00
parent 8a52f8fb3a
commit 6d064fbab0
4 changed files with 71 additions and 34 deletions

View File

@ -4,10 +4,11 @@ using System.IO;
using System.Reflection;
using System.Xml.Linq;
using UnityEngine;
using Utils;
namespace Data
{
public class InitResource:Utils.Singleton<InitResource>
public class DefineManager:Utils.Singleton<DefineManager>
{
static readonly string[] dataSetFilePath = { "Data", "Mod" };
@ -17,35 +18,13 @@ namespace Data
public void Init()
{
foreach (var path in dataSetFilePath)
{
// 获取程序所在的目录路径
var appDirectory = AppDomain.CurrentDomain.BaseDirectory;
// 加载所有 XML 文件路径
var xmlFilePaths = FileHandler.LoadXmlFilesFromPaths(dataSetFilePath);
// 构建Data目录的完整路径
var dataDirectory = Path.Combine(appDirectory, path);
try
{
// 检查目录是否存在
if (Directory.Exists(dataDirectory))
{
// 获取目录下的所有子文件夹
var subDirectories = Directory.GetDirectories(dataDirectory);
// 遍历并输出每个子文件夹的名称
foreach (var dir in subDirectories)
{
var xmlFiles = Directory.GetFiles(dir, "*.xml", SearchOption.AllDirectories);
foreach (var xmlFile in xmlFiles)
{
LoadXmlData(xmlFile);
}
}
}
}
catch (Exception ex)
{
Debug.LogError($"发生错误: {ex.Message}");
}
// 遍历并加载每个 XML 文件
foreach (var xmlFilePath in xmlFilePaths)
{
LoadXmlData(xmlFilePath);
}
}
@ -57,9 +36,9 @@ namespace Data
Debug.LogWarning($"XML文件不存在: {xmlFilePath}");
return;
}
XDocument xdoc = XDocument.Load(xmlFilePath);
var xdoc = XDocument.Load(xmlFilePath);
// 解析Define节点
XElement rootElement = xdoc.Root;
var rootElement = xdoc.Root;
if (rootElement != null)
{
@ -168,8 +147,8 @@ namespace Data
public static void SetField(object obj, string fieldName, object value)
{
Type type = obj.GetType();
FieldInfo field = type.GetField(fieldName);
var type = obj.GetType();
var field = type.GetField(fieldName);
if (field != null)
{
@ -177,8 +156,9 @@ namespace Data
}
else
{
Console.WriteLine($"Field '{fieldName}' not found.");
Debug.LogWarning($"Field '{fieldName}' not found.");
}
}
}
}

View File

@ -0,0 +1,12 @@
namespace Data
{
public class DefineSourceData
{
public string className;
public string defName;
public string label;
public string discription;
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e7aa794debf245aa82e09f42b92c9b52
timeCreated: 1752156768

View File

@ -152,6 +152,48 @@ namespace Utils
return null;
}
}
/// <summary>
/// 获取指定路径下的所有xml文件
/// </summary>
/// <param name="paths">文件夹路径</param>
/// <returns></returns>
public static List<string> LoadXmlFilesFromPaths(string[] paths)
{
var xmlFilePaths = new List<string>();
foreach (var path in paths)
{
// 获取程序所在的目录路径
var appDirectory = AppDomain.CurrentDomain.BaseDirectory;
// 构建目标目录的完整路径
var dataDirectory = Path.Combine(appDirectory, path);
try
{
// 检查目录是否存在
if (Directory.Exists(dataDirectory))
{
// 获取目录下的所有子文件夹
var subDirectories = Directory.GetDirectories(dataDirectory);
// 遍历并收集每个子文件夹中的 XML 文件
foreach (var dir in subDirectories)
{
var xmlFiles = Directory.GetFiles(dir, "*.xml", SearchOption.AllDirectories);
xmlFilePaths.AddRange(xmlFiles);
}
}
}
catch (Exception ex)
{
Debug.LogError($"加载文件时发生错误: {ex.Message}");
}
}
return xmlFilePaths;
}
}
}