(client) chore: Rearrange script structures
This commit is contained in:
8
Client/Assets/Scripts/Configs.meta
Normal file
8
Client/Assets/Scripts/Configs.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3589427d099ead34ba56425c6227bd21
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -6,7 +6,7 @@ using Newtonsoft.Json;
|
||||
using UnityEngine;
|
||||
using Formatting = Newtonsoft.Json.Formatting;
|
||||
|
||||
namespace Utils
|
||||
namespace Configs
|
||||
{
|
||||
public static class FileHandler
|
||||
{
|
||||
@ -14,18 +14,15 @@ namespace Utils
|
||||
private const string FolderPath = "save";
|
||||
|
||||
/// <summary>
|
||||
/// 初始化文件夹
|
||||
/// 初始化文件夹
|
||||
/// </summary>
|
||||
static FileHandler()
|
||||
{
|
||||
if (!Directory.Exists(FolderPath))
|
||||
{
|
||||
Directory.CreateDirectory(FolderPath);
|
||||
}
|
||||
if (!Directory.Exists(FolderPath)) Directory.CreateDirectory(FolderPath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存单个类为 JSON 文件
|
||||
/// 保存单个类为 JSON 文件
|
||||
/// </summary>
|
||||
/// <param name="obj">要保存的类对象</param>
|
||||
/// <param name="fileName">可选的文件名(不包括扩展名)</param>
|
||||
@ -34,10 +31,7 @@ namespace Utils
|
||||
try
|
||||
{
|
||||
// 如果未提供文件名,则使用类名作为文件名
|
||||
if (string.IsNullOrEmpty(fileName))
|
||||
{
|
||||
fileName = typeof(T).Name;
|
||||
}
|
||||
if (string.IsNullOrEmpty(fileName)) fileName = typeof(T).Name;
|
||||
|
||||
// 构建完整的文件路径
|
||||
var filePath = Path.Combine(FolderPath, fileName + ".json");
|
||||
@ -49,7 +43,7 @@ namespace Utils
|
||||
File.WriteAllText(filePath, jsonContent);
|
||||
|
||||
Debug.Log($"Saved file: {filePath}");
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -60,7 +54,7 @@ namespace Utils
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从 JSON 文件读取单个类
|
||||
/// 从 JSON 文件读取单个类
|
||||
/// </summary>
|
||||
/// <param name="fileName">文件名(不包括扩展名)</param>
|
||||
public static T LoadFile<T>(string fileName)
|
||||
@ -94,7 +88,7 @@ namespace Utils
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打包多个类并保存为一个存档文件
|
||||
/// 打包多个类并保存为一个存档文件
|
||||
/// </summary>
|
||||
/// <param name="archiveName">存档文件名(不包括扩展名)</param>
|
||||
/// <param name="objects">要保存的类对象字典</param>
|
||||
@ -122,7 +116,7 @@ namespace Utils
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从存档文件中读取多个类
|
||||
/// 从存档文件中读取多个类
|
||||
/// </summary>
|
||||
/// <param name="archiveName">存档文件名(不包括扩展名)</param>
|
||||
public static Dictionary<string, object> LoadArchive(string archiveName)
|
||||
@ -143,7 +137,7 @@ namespace Utils
|
||||
var jsonContent = File.ReadAllText(filePath);
|
||||
|
||||
// 反序列化为字典
|
||||
Dictionary<string, object> objects = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonContent);
|
||||
var objects = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonContent);
|
||||
|
||||
Debug.Log($"Loaded archive: {filePath}");
|
||||
return objects;
|
||||
@ -154,9 +148,9 @@ namespace Utils
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定路径下的所有xml文件
|
||||
/// 获取指定路径下的所有xml文件
|
||||
/// </summary>
|
||||
/// <param name="paths">文件夹路径</param>
|
||||
/// <returns></returns>
|
||||
@ -196,8 +190,9 @@ namespace Utils
|
||||
|
||||
return xmlFilePaths;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从指定路径加载所有 XML 文件并解析为 XDocument 对象。
|
||||
/// 从指定路径加载所有 XML 文件并解析为 XDocument 对象。
|
||||
/// </summary>
|
||||
/// <param name="paths">文件夹路径数组。</param>
|
||||
/// <returns>包含所有解析后的 XDocument 对象的列表。</returns>
|
||||
@ -207,22 +202,21 @@ namespace Utils
|
||||
var xmlFilePaths = GetXmlFilePathsFromPaths(paths);
|
||||
|
||||
foreach (var filePath in xmlFilePaths)
|
||||
{
|
||||
try
|
||||
{
|
||||
var xDoc = XDocument.Load(filePath);
|
||||
xDocuments.Add(xDoc);
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogError($"加载 XML 文件 {filePath} 时发生错误: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
return xDocuments;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定单个路径下的所有 XML 文件。
|
||||
/// 获取指定单个路径下的所有 XML 文件。
|
||||
/// </summary>
|
||||
/// <param name="path">文件夹路径。</param>
|
||||
/// <returns>包含所有 XML 文件路径的列表。</returns>
|
||||
@ -230,8 +224,9 @@ namespace Utils
|
||||
{
|
||||
return GetXmlFilePathsFromPaths(new[] { path });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从指定单个路径加载所有 XML 文件并解析为 XDocument 对象。
|
||||
/// 从指定单个路径加载所有 XML 文件并解析为 XDocument 对象。
|
||||
/// </summary>
|
||||
/// <param name="path">文件夹路径。</param>
|
||||
/// <returns>包含所有解析后的 XDocument 对象的列表。</returns>
|
||||
@ -239,22 +234,21 @@ namespace Utils
|
||||
{
|
||||
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>();
|
||||
var result = new List<string>();
|
||||
|
||||
foreach (string folderPath in folderPaths)
|
||||
{
|
||||
foreach (var folderPath in folderPaths)
|
||||
if (Directory.Exists(folderPath))
|
||||
{
|
||||
// 获取当前文件夹的直接子文件夹
|
||||
string[] subFolders = Directory.GetDirectories(folderPath);
|
||||
var subFolders = Directory.GetDirectories(folderPath);
|
||||
|
||||
// 将子文件夹路径添加到结果列表中
|
||||
result.AddRange(subFolders);
|
||||
@ -263,10 +257,8 @@ namespace Utils
|
||||
{
|
||||
Debug.LogWarning($"警告: 文件夹不存在 - {folderPath}");
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
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
|
||||
{
|
||||
public class DefineManager:Utils.Singleton<DefineManager>
|
||||
{
|
||||
|
||||
static readonly string[] dataSetFilePath = { "Data", "Mod" };
|
||||
const string coreNamespace = "Data";
|
||||
|
||||
public Dictionary<string, Dictionary<string, Define>> defines = new();
|
||||
public Dictionary<string, DefinePack> packs = new();
|
||||
|
||||
public void Init()
|
||||
{
|
||||
var packFolder = Directory.GetDirectories(dataSetFilePath[0]);
|
||||
foreach (var folder in packFolder)
|
||||
{
|
||||
var pack=new DefinePack();
|
||||
if (pack.LoadPack(folder))
|
||||
{
|
||||
packs.Add(pack.packID, pack);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -4,45 +4,41 @@ using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Xml.Linq;
|
||||
using Configs;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Data
|
||||
{
|
||||
|
||||
|
||||
public class PackAbout
|
||||
{
|
||||
public string name;
|
||||
public string description;
|
||||
public string[] after;
|
||||
public string author;
|
||||
public string version;
|
||||
public string packID;
|
||||
public string[] before;
|
||||
public string description;
|
||||
public string name;
|
||||
|
||||
public string[] necessary;
|
||||
public string[] after;
|
||||
public string[] before;
|
||||
public string packID;
|
||||
public string version;
|
||||
|
||||
/// <summary>
|
||||
/// 使用静态方法从 XML 文档创建 PackAbout 实例。
|
||||
/// 使用静态方法从 XML 文档创建 PackAbout 实例。
|
||||
/// </summary>
|
||||
/// <param name="doc">XML 文档。</param>
|
||||
/// <returns>初始化的 PackAbout 实例。</returns>
|
||||
public static PackAbout FromXDocument(XDocument doc)
|
||||
{
|
||||
var aboutElement = doc.Element("About");
|
||||
if (aboutElement == null)
|
||||
{
|
||||
throw new ArgumentException("XML 文档无效,根节点为空或不是 'About'。");
|
||||
}
|
||||
if (aboutElement == null) throw new ArgumentException("XML 文档无效,根节点为空或不是 'About'。");
|
||||
PackAbout result = new();
|
||||
|
||||
result.name = aboutElement.Element("name")?.Value ?? "Unknown";
|
||||
result.description = aboutElement.Element("description")?.Value ?? "Unknown";
|
||||
result.author = aboutElement.Element("author")?.Value ?? "Unknown";
|
||||
result.author = aboutElement.Element("author")?.Value ?? "Unknown";
|
||||
result.version = aboutElement.Element("version")?.Value ?? "Unknown";
|
||||
result.packID = aboutElement.Element("packID")?.Value ?? "Unknown";
|
||||
|
||||
XElement sortElement = aboutElement.Element("sort");
|
||||
|
||||
var sortElement = aboutElement.Element("sort");
|
||||
if (sortElement != null)
|
||||
{
|
||||
result.before = GetElementValues(sortElement.Element("before"));
|
||||
@ -51,40 +47,37 @@ namespace Data
|
||||
}
|
||||
else
|
||||
{
|
||||
result.before = System.Array.Empty<string>();
|
||||
result.after = System.Array.Empty<string>();
|
||||
result.necessary = System.Array.Empty<string>();
|
||||
result.before = Array.Empty<string>();
|
||||
result.after = Array.Empty<string>();
|
||||
result.necessary = Array.Empty<string>();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定 XElement 下所有子元素的值并返回为字符串数组。
|
||||
/// 获取指定 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>();
|
||||
}
|
||||
if (element == null || !element.HasElements) return Array.Empty<string>();
|
||||
|
||||
return element.Elements()
|
||||
.Select(e => e.Value.Trim())
|
||||
.ToArray();
|
||||
.Select(e => e.Value.Trim())
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
// 定义字段标签和值的对齐格式(如左对齐,固定宽度)
|
||||
const int labelWidth = -12; // 负数为左对齐
|
||||
const int valueWidth = -30; // 负数为左对齐
|
||||
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}");
|
||||
@ -93,7 +86,8 @@ namespace Data
|
||||
sb.AppendLine($"{"PackID:",labelWidth}{packID,valueWidth}");
|
||||
|
||||
// 数组字段(多行,每项缩进)
|
||||
sb.AppendLine($"{"Necessary:",labelWidth}{string.Join(", ", necessary ?? Array.Empty<string>()),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}");
|
||||
|
||||
@ -103,48 +97,44 @@ namespace Data
|
||||
|
||||
public class DefinePack
|
||||
{
|
||||
public string packID;
|
||||
public PackAbout packAbout;
|
||||
/// <summary>
|
||||
/// define类别及其定义
|
||||
/// </summary>
|
||||
public Dictionary<string,List<Define>> defines;
|
||||
|
||||
private const string CoreNamespace = "Data.";
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// define类别及其定义
|
||||
/// </summary>
|
||||
public Dictionary<string, List<Define>> defines;
|
||||
|
||||
public PackAbout packAbout;
|
||||
public string packID;
|
||||
|
||||
|
||||
public bool LoadPack(string packPath)
|
||||
{
|
||||
var packDatas=Utils.FileHandler.LoadXmlFromPath(packPath);
|
||||
var aboutXmls=FindDocumentsWithRootName(packDatas,"About");
|
||||
var packDatas = 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);
|
||||
}
|
||||
|
||||
|
||||
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")
|
||||
if (rootElement == null || rootElement.Name != "Define")
|
||||
return;
|
||||
|
||||
|
||||
foreach (var element in rootElement.Elements())
|
||||
{
|
||||
var className = element.Name.ToString();
|
||||
@ -154,13 +144,13 @@ namespace Data
|
||||
if (def == null)
|
||||
continue;
|
||||
if (!defines.ContainsKey(className))
|
||||
defines.Add(className,new List<Define>());
|
||||
defines.Add(className, new List<Define>());
|
||||
defines[className].Add(def);
|
||||
}
|
||||
}
|
||||
|
||||
private Define LoadDefineClass(XElement defineDoc)
|
||||
{
|
||||
{
|
||||
var className = defineDoc.Name.ToString();
|
||||
var assembly = Assembly.GetExecutingAssembly();
|
||||
|
||||
@ -172,10 +162,7 @@ namespace Data
|
||||
type = assembly.GetType(fullClassName);
|
||||
|
||||
// 如果拼接命名空间后仍找不到,尝试直接查找(可能是全局命名空间下的类)
|
||||
if (type == null)
|
||||
{
|
||||
type = assembly.GetType(className);
|
||||
}
|
||||
if (type == null) type = assembly.GetType(className);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -215,10 +202,7 @@ namespace Data
|
||||
return null;
|
||||
}
|
||||
|
||||
if (define.Init(defineDoc))
|
||||
{
|
||||
return define;
|
||||
}
|
||||
if (define.Init(defineDoc)) return define;
|
||||
// 获取类的所有字段(不包括私有字段)
|
||||
var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
|
||||
@ -228,24 +212,23 @@ namespace Data
|
||||
// 查找对应的 XElement 子元素
|
||||
var element = defineDoc.Element(field.Name);
|
||||
if (element != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 将子元素的值转换为目标类型并赋值
|
||||
object value = Convert.ChangeType(element.Value, field.FieldType);
|
||||
var 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> 中查找指定根元素名称的文档。
|
||||
/// 从 List<c>XDocument</c> 中查找指定根元素名称的文档。
|
||||
/// </summary>
|
||||
/// <param name="xmlDocuments">XML 文档列表。</param>
|
||||
/// <param name="rootName">目标根元素名称。</param>
|
||||
@ -267,7 +250,7 @@ namespace Data
|
||||
const int valueWidth = -30;
|
||||
|
||||
var sb = new StringBuilder();
|
||||
|
||||
|
||||
// 基础字段
|
||||
sb.AppendLine($"{"PackID:",labelWidth}{packID,valueWidth}");
|
||||
sb.AppendLine();
|
||||
@ -280,21 +263,15 @@ namespace Data
|
||||
// 字典字段(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();
|
||||
}
|
||||
|
26
Client/Assets/Scripts/Managers/DefineManager.cs
Normal file
26
Client/Assets/Scripts/Managers/DefineManager.cs
Normal file
@ -0,0 +1,26 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Data;
|
||||
using Utils;
|
||||
|
||||
namespace Managers
|
||||
{
|
||||
public class DefineManager : Singleton<DefineManager>
|
||||
{
|
||||
private const string coreNamespace = "Data";
|
||||
private static readonly string[] dataSetFilePath = { "Data", "Mod" };
|
||||
|
||||
public Dictionary<string, Dictionary<string, Define>> defines = new();
|
||||
public Dictionary<string, DefinePack> packs = new();
|
||||
|
||||
public void Init()
|
||||
{
|
||||
var packFolder = Directory.GetDirectories(dataSetFilePath[0]);
|
||||
foreach (var folder in packFolder)
|
||||
{
|
||||
var pack = new DefinePack();
|
||||
if (pack.LoadPack(folder)) packs.Add(pack.packID, pack);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user