Files
godot-------/Script/Loader/InitResource.cs
m0_75251201 7700703099 初次提交
2025-07-12 11:30:22 +08:00

177 lines
5.6 KiB
C#

using Cosmobox.Def;
using Godot;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Xml.Linq;
namespace Cosmobox
{
public partial class InitResource : Node
{
public static InitResource instance;
#if DEBUG
static readonly string corePath = "D:\\godot_Project\\app\\打怪\\Data";
#else
static readonly string corePath = "Data";
#endif
static readonly string coreNamespace = "Cosmobox.Def.";
public Dictionary<string, Dictionary<string, Define>> defines = new();
public override void _Ready()
{
// 在节点准备就绪时调用初始化方法
Init();
instance = this;
}
public void Init()
{
// 获取程序所在的目录路径
string appDirectory = AppDomain.CurrentDomain.BaseDirectory;
// 构建Data目录的完整路径
string dataDirectory = Path.Combine(appDirectory, corePath);
try
{
// 检查Data目录是否存在
if (Directory.Exists(dataDirectory))
{
// 获取Data目录下的所有子文件夹
string[] subDirectories = Directory.GetDirectories(dataDirectory);
// 遍历并输出每个子文件夹的名称
foreach (string dir in subDirectories)
{
string[] xmlFiles = Directory.GetFiles(dir, "*.xml", SearchOption.AllDirectories);
foreach (string xmlFile in xmlFiles)
{
// GD.Print($"找到XML文件: {xmlFile}");
// 调用加载XML数据的方法
LoadXmlData(xmlFile);
}
}
}
else
{
GD.Print($"Data目录不存在: {dataDirectory}");
}
}
catch (Exception ex)
{
GD.Print($"发生错误: {ex.Message}");
}
}
public void LoadXmlData(string xmlFilePath)
{
// 检查文件是否存在
if (!File.Exists(xmlFilePath))
{
GD.Print($"XML文件不存在: {xmlFilePath}");
return;
}
XDocument xdoc = XDocument.Load(xmlFilePath);
// 解析Define节点
XElement rootElement = xdoc.Root;
if (rootElement != null)
{
if (rootElement.Name == "Define")
{
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<string, Define>());
defines[className][name] = (Define)def;
}
GD.Print(name, " ", def);
}
}
}
}
public object LoadDefine(XElement xElement)
{
string className = xElement.Name.ToString();
Assembly assembly = Assembly.GetExecutingAssembly();
Type type;
if (!className.Contains('.'))
{
// 尝试拼接默认命名空间
string fullClassName = coreNamespace + className;
type = assembly.GetType(fullClassName);
// 如果拼接命名空间后仍找不到,尝试直接查找(可能是全局命名空间下的类)
if (type == null)
{
type = assembly.GetType(className);
}
}
else
{
// 直接查找
type = assembly.GetType(className);
}
if (type == null)
{
GD.PrintErr($"未定义的类型: {className}");
return null;
}
var constructor = type.GetConstructor(Type.EmptyTypes);
if (constructor == null)
{
GD.PrintErr($"{className} 必须包含无参构造函数");
return null;
}
// 3. 创建实例
object instance;
try
{
instance = Activator.CreateInstance(type);
}
catch (Exception ex)
{
GD.PrintErr($"创建 {className} 实例失败: {ex.Message}");
return null;
}
// 4. 检查是否继承自 Define
if (instance is not Define define)
{
GD.PrintErr($"{className} 必须继承自 Define");
return null;
}
define.Init(xElement);
return define;
// SetField(define, "Address", "123 Main St");
}
public static void SetField(object obj, string fieldName, object value)
{
Type type = obj.GetType();
FieldInfo field = type.GetField(fieldName);
if (field != null)
{
field.SetValue(obj, Convert.ChangeType(value, field.FieldType));
}
else
{
Console.WriteLine($"Field '{fieldName}' not found.");
}
}
}
}