(client) feat:健康给予,路径优化,结算界面,商店界面 (#60)

Co-authored-by: m0_75251201 <m0_75251201@noreply.gitcode.com>
Reviewed-on: http://47.107.252.169:3000/Roguelite-Game-Developing-Team/Gen_Hack-and-Slash-Roguelite/pulls/60
This commit is contained in:
2025-10-10 14:08:23 +08:00
parent 9a797479ff
commit 16b49f3d3a
1900 changed files with 114053 additions and 34157 deletions

View File

@@ -1,30 +1,21 @@
using Configs;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml.Linq;
using Configs;
using UnityEngine;
namespace Data
{
/// <summary>
/// 表示模组包的基本信息
/// 表示模组包的基本信息
/// </summary>
public class PackAbout
{
public string Name { get; private set; }
public string Description { get; private set; }
public string Author { get; private set; }
public string Version { get; private set; }
public string PackID { get; private set; }
public string[] Necessary { get; private set; }
public string[] After { get; private set; }
public string[] Before { get; private set; }
private const string RootElementName = "About";
private const string ElementName_Name = "name";
private const string ElementName_Description = "description";
@@ -35,16 +26,27 @@ namespace Data
private const string ElementName_Before = "before";
private const string ElementName_After = "after";
private const string ElementName_Necessary = "necessary";
public string Name { get; private set; }
public string Description { get; private set; }
public string Author { get; private set; }
public string Version { get; private set; }
public string PackID { get; private set; }
public string[] Necessary { get; private set; }
public string[] After { get; private set; }
public string[] Before { get; private set; }
/// <summary>
/// 使用静态方法从 XML 文档创建 PackAbout 实例。
/// </summary>
/// <param name="doc">XML 文档。</param>
/// <returns>初始化的 PackAbout 实例。</returns>
/// <exception cref="ArgumentException">当 XML 文档无效,根节点为空或不是 'About' 时抛出。</exception>
public static PackAbout FromXDocument(XDocument doc)
{
var aboutElement = doc.Element(RootElementName);
if (aboutElement == null) throw new ArgumentException($"XML 文档无效,根节点为空或不是 '{RootElementName}'。");
if (aboutElement == null)
throw new ArgumentException($"<color=red>XML 文档无效,根节点为空或不是 '{RootElementName}'。</color>");
PackAbout result = new();
result.Name = aboutElement.Element(ElementName_Name)?.Value ?? "Unknown";
@@ -74,7 +76,7 @@ namespace Data
/// 获取指定 XElement 下所有子元素的值并返回为字符串数组。
/// </summary>
/// <param name="element">父 XElement。</param>
/// <returns>字符串数组。</returns>
/// <returns>包含子元素值的字符串数组。</returns>
private static string[] GetElementValues(XElement element)
{
if (element == null || !element.HasElements) return Array.Empty<string>();
@@ -86,21 +88,20 @@ namespace Data
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}");
@@ -112,36 +113,22 @@ namespace Data
/// <summary>
/// 表示一个模组包的定义集合
/// 表示一个模组包的定义集合
/// </summary>
public class DefinePack
{
private const string CoreNamespace = "Data.";
// 优化点7将魔法字符串转换为常量
private const string RootElementName_About = "About";
private const string RootElementName_Define = "Define";
// 优化点1和2反射缓存和改进的类型查找
// 反射缓存
private static readonly Dictionary<string, Type> _typeCache = new();
private static readonly Dictionary<Type, ConstructorInfo> _constructorCache = new();
private static readonly Dictionary<Type, FieldInfo[]> _fieldCache = new();
private static readonly List<Assembly> _assembliesToSearch = new(); // 缓存要搜索的程序集
static DefinePack()
{
// 优化点2一次性初始化要搜索的程序集。
// 为简单起见,我们将扫描所有当前加载的程序集。
// 在实际游戏中,您可能希望显式添加特定的程序集
// 如 Assembly.Load("YourGameLogicAssembly") 或按名称过滤。
_assembliesToSearch.AddRange(AppDomain.CurrentDomain.GetAssemblies());
// 可选:过滤或添加特定程序集:
// _assembliesToSearch.Add(Assembly.GetExecutingAssembly()); // 添加当前程序集
// _assembliesToSearch.Add(Assembly.Load("AnotherAssemblyContainingDefines"));
}
/// <summary>
/// define类别及其定义
/// 定义类别及其定义
/// </summary>
public Dictionary<string, List<Define>> defines = new();
@@ -151,47 +138,62 @@ namespace Data
public int priority = -1;
public string Name
static DefinePack()
{
get
{
// 优化点5Name属性的空值安全性
return packAbout?.Name ?? "Unnamed Pack"; // 使用 PackAbout.Name 属性
}
// 初始化要搜索的程序集。
_assembliesToSearch.AddRange(AppDomain.CurrentDomain.GetAssemblies());
}
public string Name =>
// 使用 PackAbout.Name 属性,若为空则返回默认值。
packAbout?.Name ?? "Unnamed Pack";
/// <summary>
/// 加载模组包。
/// </summary>
/// <param name="packPath">模组包的路径。</param>
/// <returns>如果模组包加载成功,则为 true否则为 false。</returns>
public bool LoadPack(string packPath)
{
packRootPath = System.IO.Path.GetFullPath(packPath);
packRootPath = Path.GetFullPath(packPath);
var packDatas = ConfigProcessor.LoadXmlFromPath(packPath);
// 优化点7使用常量
var aboutXmls = FindDocumentsWithRootName(packDatas, RootElementName_About);
if (aboutXmls == null || aboutXmls.Count < 1)
{
Debug.LogError("包缺少配置文件,加载跳过");
Debug.LogError("<color=red>包缺少配置文件,加载跳过。</color>");
return false;
}
var aboutXml = aboutXmls[0];
packAbout = PackAbout.FromXDocument(aboutXml);
// 优化点3使用 PackAbout.PackID 属性
packID = packAbout.PackID;
// 优化点3使用 PackAbout.Name 属性
if (aboutXmls.Count > 1) Debug.LogWarning($"{packAbout.Name}包拥有多个配置文件,系统选择了加载序的第一个,请避免这种情况");
if (aboutXmls.Count > 1)
Debug.LogWarning($"<color=yellow>{packAbout.Name} 包拥有多个配置文件,系统选择了加载序的第一个,请避免这种情况。</color>");
// 优化点7使用常量
var defineXmls = FindDocumentsWithRootName(packDatas, RootElementName_Define);
// Debug.Log($"Define文件数量{defineXmls.Count}");
foreach (var defineXml in defineXmls) LoadDefines(defineXml);
return true;
}
/// <summary>
/// 加载定义。
/// </summary>
/// <param name="defineDoc">包含定义的 XML 文档。</param>
private void LoadDefines(XDocument defineDoc)
{
var rootElement = defineDoc.Root;
// 优化点7使用常量
if (rootElement == null || rootElement.Name != RootElementName_Define)
if (rootElement == null)
{
Debug.LogWarning("<color=yellow>加载定义时发现一个XML文档的根元素为空跳过此文档。</color>");
return;
}
if (rootElement.Name != RootElementName_Define)
{
Debug.LogWarning(
$"<color=yellow>加载定义时发现XML文档根元素名称 '{rootElement.Name}' 不符合预期 '{RootElementName_Define}',跳过此文档。</color>");
return;
}
foreach (var element in rootElement.Elements())
{
@@ -209,75 +211,66 @@ namespace Data
}
/// <summary>
/// 根据指定的 XML 元素 (<paramref name="defineDoc"/>) 和类名 (<paramref name="className"/>),
/// 动态加载并初始化一个继承自 <see cref="Define"/> 的类实例。
/// 根据指定的 XML 元素 (<paramref name="defineDoc" />) 和类名 (<paramref name="className" />),
/// 动态加载并初始化一个继承自 <see cref="Define" /> 的类实例。
/// </summary>
/// <param name="defineDoc">包含类定义的 XML 元素 (<see cref="XElement"/>)。</param>
/// <param name="defineDoc">包含类定义的 XML 元素 (<see cref="XElement" />)。</param>
/// <param name="className">目标类的全限定名或简短名称。</param>
/// <returns>
/// 如果成功加载并初始化,则返回对应的 <see cref="Define"/> 类实例;
/// 否则返回 null。
/// 如果成功加载并初始化,则返回对应的 <see cref="Define" /> 类实例;
/// 否则返回 null。
/// </returns>
/// <exception cref="ArgumentNullException">
/// 如果 <paramref name="defineDoc"/> 或 <paramref name="className"/> 为 null 或空字符串,则抛出此异常。
/// </exception>
/// <remarks>
/// 该方法通过反射动态加载指定类,并检查其是否继承自 <see cref="Define"/>。
/// 如果类存在且满足条件,则尝试调用其 <see cref="Define.Init(XElement)"/> 方法进行初始化。
/// 如果初始化失败,则使用默认初始化方法 (<see cref="DefaultInitDefine(Define, XElement, Type)"/>)。
/// </remarks>
public static Define LoadDefineClass(XElement defineDoc, string className)
{
// 优化点1和2反射缓存和改进的类型查找
// 反射缓存和改进的类型查找
if (!_typeCache.TryGetValue(className, out var type))
{
// 首先尝试使用 CoreNamespace
// 尝试 CoreNamespace + className
var fullClassName = CoreNamespace + className;
type = _assembliesToSearch.Select(a => a.GetType(fullClassName)).FirstOrDefault(t => t != null);
// 如果未找到,尝试不使用 CoreNamespace(可能在全局命名空间中或已经是完全限定名)
// 如果未找到,尝试不使用 CoreNamespace
if (type == null)
{
type = _assembliesToSearch.Select(a => a.GetType(className)).FirstOrDefault(t => t != null);
}
if (type == null)
{
Debug.LogError($"未定义的类型: {className}");
Debug.LogError($"<color=red>未定义的类型: {className}。</color>");
return null;
}
_typeCache[className] = type;
}
// 优化点1构造函数缓存
// 构造函数缓存
if (!_constructorCache.TryGetValue(type, out var constructor))
{
constructor = type.GetConstructor(Type.EmptyTypes);
if (constructor == null)
{
Debug.LogError($"{className} 必须包含无参构造函数");
Debug.LogError($"<color=red>类 {className} 必须包含无参构造函数。</color>");
return null;
}
_constructorCache[type] = constructor;
}
// 3. 创建实例
// 创建实例
object instance;
try
{
instance = constructor.Invoke(null); // 使用缓存的构造函数
instance = constructor.Invoke(null);
}
catch (Exception ex)
{
Debug.LogError($"创建 {className} 实例失败: {ex.Message}");
Debug.LogError($"<color=red>创建 {className} 实例失败: {ex.Message}。</color>");
return null;
}
// 4. 检查是否继承自 Define
// 检查是否继承自 Define
if (instance is not Define define)
{
Debug.LogError($"{className} 必须继承自 Define");
Debug.LogError($"<color=red>类 {className} 必须继承自 Define。</color>");
return null;
}
@@ -288,24 +281,15 @@ namespace Data
}
/// <summary>
/// 初始化指定的 <paramref name="define"/> 对象,根据 <paramref name="defineDoc"/> 中的 XML 元素内容,
/// 将对应的字段值赋给 <paramref name="define"/> 对象。
/// 初始化指定的 <paramref name="define" /> 对象,根据 <paramref name="defineDoc" /> 中的 XML 元素内容,
/// 将对应的字段值赋给 <paramref name="define" /> 对象。
/// </summary>
/// <param name="define">需要初始化的对象实例。</param>
/// <param name="defineDoc">包含字段定义的 XML 元素 (<see cref="XElement"/>)。</param>
/// <param name="defineType">目标对象的类型 (<see cref="Type"/>)。</param>
/// <exception cref="ArgumentNullException">
/// 如果 <paramref name="define"/>、<paramref name="defineDoc"/> 或 <paramref name="defineType"/> 为 null则抛出此异常。
/// </exception>
/// <remarks>
/// 该方法会遍历 <paramref name="defineType"/> 的所有字段(包括公共和非公共字段),
/// 并尝试从 <paramref name="defineDoc"/> 中找到与字段名称匹配的子元素。
/// 如果找到匹配的子元素,则将其值转换为字段的类型并赋值给字段。
/// 如果字段类型继承自 <see cref="Define"/>,则递归调用 <see cref="LoadDefineClass(XElement, string)"/> 方法进行加载。
/// </remarks>
/// <param name="defineDoc">包含字段定义的 XML 元素 (<see cref="XElement" />)。</param>
/// <param name="defineType">目标对象的类型 (<see cref="Type" />)。</param>
public static void DefaultInitDefine(Define define, XElement defineDoc, Type defineType)
{
// 优化点1FieldInfo 缓存
// FieldInfo 缓存
if (!_fieldCache.TryGetValue(defineType, out var fields))
{
fields = defineType.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
@@ -320,62 +304,82 @@ namespace Data
if (element != null)
try
{
// 优化点4重构 ProcessArrayField 并引入通用转换辅助方法
var value = ConvertXElementValueToType(element, field.FieldType);
field.SetValue(define, value);
}
catch (Exception ex)
{
Debug.LogWarning($"设置字段时出错,字段名:{field.Name}; 值: {element.Value}; 错误: {ex.Message}");
Debug.LogWarning(
$"<color=yellow>设置字段时出错,字段名: {field.Name}; 值: {element.Value}; 错误: {ex.Message}。</color>");
}
}
var properties = defineType
.GetProperties(BindingFlags.Public | BindingFlags.Instance |
BindingFlags.NonPublic) // 也获取私有属性,但要求私有属性有 setter
.Where(p => p.CanWrite); // 只要可写就可以设置
foreach (var property in properties)
{
var element = defineDoc.Element(property.Name);
if (element != null)
try
{
var value = ConvertXElementValueToType(element, property.PropertyType);
property.SetValue(define, value);
}
catch (Exception ex)
{
Debug.LogWarning(
$"<color=yellow>设置属性 '{property.Name}' 时出错,值: '{element.Value}',错误: {ex.Message}。</color>");
}
}
}
// 优化点4新的辅助方法用于将 XElement 值转换为特定类型
/// <summary>
/// 将 XElement 值转换为特定类型。
/// </summary>
/// <param name="element">XElement 元素。</param>
/// <param name="targetType">目标类型。</param>
/// <returns>转换后的值。</returns>
private static object ConvertXElementValueToType(XElement element, Type targetType)
{
if (IsTypeInheritedFrom(targetType, typeof(Define)))
{
if (element.HasElements)
{
return LoadDefineClass(element, targetType.Name);
}
else
{
// 引用另一个 Define
var reference = (Define)Activator.CreateInstance(targetType);
reference.isReferene = true;
reference.description = targetType.Name;
reference.label = element.Name.LocalName; // 使用元素的名称作为标签
reference.defName = element.Value;
return reference;
}
if (element.HasElements) return LoadDefineClass(element, targetType.Name);
// 引用另一个 Define
var reference = (Define)Activator.CreateInstance(targetType);
reference.isReferene = true;
reference.description = targetType.Name;
reference.label = element.Name.LocalName; // 使用元素的名称作为标签
reference.defName = element.Value;
return reference;
}
else if (targetType.IsArray || typeof(IList).IsAssignableFrom(targetType))
{
if (targetType.IsArray || typeof(IList).IsAssignableFrom(targetType))
return ProcessArrayField(targetType, element);
}
else if (targetType.IsEnum)
{
return Enum.Parse(targetType, element.Value);
}
else
{
return Convert.ChangeType(element.Value, targetType);
}
if (targetType.IsEnum) return Enum.Parse(targetType, element.Value);
return Convert.ChangeType(element.Value, targetType);
}
// 优化点4修改 ProcessArrayField 以直接接受 Type
/// <summary>
/// 处理数组字段。
/// </summary>
/// <param name="fieldType">字段类型。</param>
/// <param name="element">XElement 元素。</param>
/// <returns>处理后的数组对象。</returns>
private static object ProcessArrayField(Type fieldType, XElement element)
{
// 获取集合的元素类型
var elementType = fieldType.IsArray
? fieldType.GetElementType()
: fieldType.GetGenericArguments().FirstOrDefault(); // 使用 FirstOrDefault 以确保安全
: fieldType.GetGenericArguments().FirstOrDefault();
if (elementType == null)
{
Debug.LogWarning($"无法确定类型为 {fieldType.Name} 的集合字段的元素类型");
Debug.LogWarning($"<color=yellow>无法确定类型为 {fieldType.Name} 的集合字段的元素类型。</color>");
return null;
}
@@ -383,29 +387,21 @@ namespace Data
// 遍历 XML 子元素
foreach (var liElement in element.Elements())
{
// 对每个项目使用新的辅助方法
arrayElements.Add(ConvertXElementValueToType(liElement, elementType));
}
// 根据目标字段的类型构造结果
if (fieldType.IsArray)
{
var resultArray = Array.CreateInstance(elementType, arrayElements.Count);
for (var i = 0; i < arrayElements.Count; i++)
{
resultArray.SetValue(arrayElements[i], i);
}
for (var i = 0; i < arrayElements.Count; i++) resultArray.SetValue(arrayElements[i], i);
return resultArray;
}
else if (typeof(IList).IsAssignableFrom(fieldType))
if (typeof(IList).IsAssignableFrom(fieldType))
{
var listType = typeof(List<>).MakeGenericType(elementType);
var resultList = (IList)Activator.CreateInstance(listType);
foreach (var item in arrayElements)
{
resultList.Add(item);
}
foreach (var item in arrayElements) resultList.Add(item);
return resultList;
}
@@ -414,14 +410,13 @@ namespace Data
/// <summary>
/// 从 List<c>XDocument</c> 中查找指定根元素名称的文档。
/// 从 <see cref="List{XDocument}" /> 中查找指定根元素名称的文档。
/// </summary>
/// <param name="xmlDocuments">XML 文档列表。</param>
/// <param name="rootName">目标根元素名称。</param>
/// <returns>符合条件的 XML 文档列表。</returns>
public static List<XDocument> FindDocumentsWithRootName(List<XDocument> xmlDocuments, string rootName)
{
// 使用 LINQ to Objects 实现更简洁的解决方案
var result = xmlDocuments
.Where(doc => doc.Root != null && doc.Root.Name.LocalName == rootName)
.ToList();
@@ -443,7 +438,6 @@ namespace Data
// PackAbout 对象
sb.AppendLine("=== PackAbout ===");
// 优化点3使用 PackAbout.ToString()
sb.AppendLine(packAbout?.ToString() ?? "N/A");
sb.AppendLine();
@@ -464,32 +458,27 @@ namespace Data
}
/// <summary>
/// 检查字段的类型是否继承自指定的类 (严格派生,不包括基类本身)
/// 检查字段的类型是否继承自指定的类严格派生,不包括基类本身)。
/// </summary>
/// <param name="field">字段信息</param>
/// <param name="baseType">要检查的基类类型</param>
/// <returns>如果字段的类型是基类的严格派生类,则返回 true</returns>
// 优化点6为 IsFieldTypeInheritedFrom 进行语义澄清
/// <param name="field">字段信息</param>
/// <param name="baseType">要检查的基类类型</param>
/// <returns>如果字段的类型是基类的严格派生类,则返回 true;否则返回 false。</returns>
public static bool IsFieldTypeInheritedFrom(FieldInfo field, Type baseType)
{
// 获取字段的类型
var fieldType = field.FieldType;
// 如果字段的类型为 null 或不是基类的派生类,则返回 false
// 严格派生:不包括基类本身
return fieldType != null && fieldType != baseType && baseType.IsAssignableFrom(fieldType);
}
/// <summary>
/// 检查一个类型是否继承自指定的基类 (严格派生,不包括基类本身)
/// 检查一个类型是否继承自指定的基类严格派生,不包括基类本身)。
/// </summary>
/// <param name="type">要检查的类型</param>
/// <param name="baseType">要检查的基类类型</param>
/// <returns>如果类型是基类的严格派生类,则返回 true</returns>
// 在 ConvertXElementValueToType 中使用的新类型检查辅助方法
/// <param name="type">要检查的类型</param>
/// <param name="baseType">要检查的基类类型</param>
/// <returns>如果类型是基类的严格派生类,则返回 true;否则返回 false。</returns>
public static bool IsTypeInheritedFrom(Type type, Type baseType)
{
return type != null && type != baseType && baseType.IsAssignableFrom(type);
}
}
}