using System; using System.Collections.Generic; using System.IO; using System.Xml.Linq; using Newtonsoft.Json; using UnityEngine; using Formatting = Newtonsoft.Json.Formatting; namespace Configs { public static class ConfigProcessor { // 保存文件的默认文件夹路径 private const string FolderPath = "save"; /// /// 初始化文件夹 /// static ConfigProcessor() { if (!Directory.Exists(FolderPath)) Directory.CreateDirectory(FolderPath); } /// /// 保存单个类为 JSON 文件 /// /// 要保存的类对象 /// 可选的文件名(不包括扩展名) public static bool SaveFile(T obj, string fileName = null) { try { // 如果未提供文件名,则使用类名作为文件名 if (string.IsNullOrEmpty(fileName)) fileName = typeof(T).Name; // 构建完整的文件路径 var filePath = Path.Combine(FolderPath, fileName + ".json"); // 将对象序列化为 JSON 字符串 var jsonContent = JsonConvert.SerializeObject(obj, Formatting.Indented); // 写入文件 File.WriteAllText(filePath, jsonContent); Debug.Log($"Saved file: {filePath}"); return true; } catch (Exception ex) { Debug.LogError($"Failed to save file: {ex.Message}"); return false; } } /// /// 从 JSON 文件读取单个类 /// /// 文件名(不包括扩展名) public static T LoadFile(string fileName) { try { // 构建完整的文件路径 var filePath = Path.Combine(FolderPath, fileName + ".json"); // 检查文件是否存在 if (!File.Exists(filePath)) { Debug.LogError($"File not found: {filePath}"); return default; } // 读取文件内容 var jsonContent = File.ReadAllText(filePath); // 反序列化为指定类型的对象 var obj = JsonConvert.DeserializeObject(jsonContent); Debug.Log($"Loaded file: {filePath}"); return obj; } catch (Exception ex) { Debug.LogError($"Failed to load file: {ex.Message}"); return default; } } /// /// 打包多个类并保存为一个存档文件 /// /// 存档文件名(不包括扩展名) /// 要保存的类对象字典 public static bool SaveArchive(string archiveName, Dictionary objects) { try { // 构建完整的文件路径 var filePath = Path.Combine(FolderPath, archiveName + ".archive.json"); // 将字典中的对象序列化为 JSON var jsonContent = JsonConvert.SerializeObject(objects, Formatting.Indented); // 写入文件 File.WriteAllText(filePath, jsonContent); Debug.Log($"Saved archive: {filePath}"); return true; } catch (Exception ex) { Debug.LogError($"Failed to save archive: {ex.Message}"); return false; } } /// /// 从存档文件中读取多个类 /// /// 存档文件名(不包括扩展名) public static Dictionary LoadArchive(string archiveName) { try { // 构建完整的文件路径 var filePath = Path.Combine(FolderPath, archiveName + ".archive.json"); // 检查文件是否存在 if (!File.Exists(filePath)) { Debug.LogError($"Archive not found: {filePath}"); return null; } // 读取文件内容 var jsonContent = File.ReadAllText(filePath); // 反序列化为字典 var objects = JsonConvert.DeserializeObject>(jsonContent); Debug.Log($"Loaded archive: {filePath}"); return objects; } catch (Exception ex) { Debug.LogError($"Failed to load archive: {ex.Message}"); return null; } } /// /// 获取指定路径下的所有xml文件 /// /// 文件夹路径 /// public static List GetXmlFilePathsFromPaths(string[] paths) { var xmlFilePaths = new List(); foreach (var path in paths) { try { // 检查目录是否存在 if (!Directory.Exists(path)) { continue; } // 获取目录下的所有子文件夹 var subDirectories = Directory.GetDirectories(path); // 遍历并收集每个子文件夹中的 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; } /// /// 从指定路径加载所有 XML 文件并解析为 XDocument 对象。 /// /// 文件夹路径数组。 /// 包含所有解析后的 XDocument 对象的列表。 public static List LoadXmlFromPaths(string[] paths) { var xDocuments = new List(); var xmlFilePaths = GetXmlFilePathsFromPaths(paths); foreach (var filePath in xmlFilePaths) try { var xDoc = XDocument.Load(filePath); xDocuments.Add(xDoc); } catch (Exception ex) { Debug.LogError($"加载 XML 文件 {filePath} 时发生错误: {ex.Message}"); } return xDocuments; } /// /// 获取指定单个路径下的所有 XML 文件。 /// /// 文件夹路径。 /// 包含所有 XML 文件路径的列表。 public static List GetXmlFilePathsFromPath(string path) { return GetXmlFilePathsFromPaths(new[] { path }); } /// /// 从指定单个路径加载所有 XML 文件并解析为 XDocument 对象。 /// /// 文件夹路径。 /// 包含所有解析后的 XDocument 对象的列表。 public static List LoadXmlFromPath(string path) { return LoadXmlFromPaths(new[] { path }); } /// /// 获取文件夹列表中所有直接子文件夹的路径。 /// /// 文件夹路径列表。 /// 包含所有子文件夹路径的列表。 public static List GetSubFolders(List folderPaths) { var result = new List(); foreach (var folderPath in folderPaths) if (Directory.Exists(folderPath)) { // 获取当前文件夹的直接子文件夹 var subFolders = Directory.GetDirectories(folderPath); // 将子文件夹路径添加到结果列表中 result.AddRange(subFolders); } else { Debug.LogWarning($"警告: 文件夹不存在 - {folderPath}"); } return result; } } }