From 16fef536259a45c0571a93f61e7b59691f759e7c Mon Sep 17 00:00:00 2001 From: m0_75251201 Date: Sat, 12 Jul 2025 16:06:17 +0800 Subject: [PATCH] =?UTF-8?q?(client)=20chore:=E5=B0=86=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E5=88=9D=E6=AC=A1=E5=8A=A0=E8=BD=BD=E7=A7=BB=E5=8A=A8=E5=88=B0?= =?UTF-8?q?DefinePack?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Client/Assets/Scripts/Data/DefineManager.cs | 144 ++------------------ Client/Assets/Scripts/Data/DefinePack.cs | 119 ++++++++++++++-- 2 files changed, 119 insertions(+), 144 deletions(-) diff --git a/Client/Assets/Scripts/Data/DefineManager.cs b/Client/Assets/Scripts/Data/DefineManager.cs index 80c1422..8eba878 100644 --- a/Client/Assets/Scripts/Data/DefineManager.cs +++ b/Client/Assets/Scripts/Data/DefineManager.cs @@ -1,9 +1,11 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Reflection; using System.Xml.Linq; using UnityEngine; +using UnityEngine.Rendering; using Utils; namespace Data @@ -15,149 +17,19 @@ namespace Data const string coreNamespace = "Data"; public Dictionary> defines = new(); + public Dictionary packs = new(); public void Init() { - // 加载所有 XML 文件路径 - var xmlFilePaths = FileHandler.GetXmlFilePathsFromPaths(dataSetFilePath); - - // 遍历并加载每个 XML 文件 - foreach (var xmlFilePath in xmlFilePaths) + var packFolder = Directory.GetDirectories(dataSetFilePath[0]); + foreach (var folder in packFolder) { - LoadXmlData(xmlFilePath); - } - - } - 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") + var pack=new DefinePack(); + if (pack.LoadPack(folder)) { - foreach (var element in rootElement.Elements()) - { - 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()); - defines[className][name] = (Define)def; - } - } + packs.Add(pack.packID, pack); } } - - } - 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."); - } } } diff --git a/Client/Assets/Scripts/Data/DefinePack.cs b/Client/Assets/Scripts/Data/DefinePack.cs index b230161..b8a1a89 100644 --- a/Client/Assets/Scripts/Data/DefinePack.cs +++ b/Client/Assets/Scripts/Data/DefinePack.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Xml.Linq; using UnityEngine; @@ -27,23 +28,19 @@ namespace Data /// 初始化的 PackAbout 实例。 public static PackAbout FromXDocument(XDocument doc) { - var aboutElement = doc.Element("About"); // Assuming "about" is the root element name + var aboutElement = doc.Element("About"); 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.author = aboutElement.Element("author")?.Value ?? "Unknown"; 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) { @@ -83,8 +80,14 @@ namespace Data { public string packID; public PackAbout packAbout; - public List defines; + /// + /// define类别及其定义 + /// + public Dictionary> defines; + private const string CoreNamespace = "Data."; + + public bool LoadPack(string packPath) { var packDatas=Utils.FileHandler.LoadXmlFromPath(packPath); @@ -113,7 +116,107 @@ namespace Data 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()); + 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; } ///