diff --git a/Client/Assets/Plugins/Newtonsoft.Json.dll b/Client/Assets/Plugins/Newtonsoft.Json.dll new file mode 100644 index 0000000..1aa22bf Binary files /dev/null and b/Client/Assets/Plugins/Newtonsoft.Json.dll differ diff --git a/Client/Assets/Plugins/Newtonsoft.Json.dll.meta b/Client/Assets/Plugins/Newtonsoft.Json.dll.meta new file mode 100644 index 0000000..a0b6b6f --- /dev/null +++ b/Client/Assets/Plugins/Newtonsoft.Json.dll.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 13521751152941a6835e2c81be4e6137 \ No newline at end of file diff --git a/Client/Assets/Scripts/Utils/FileHandler.cs b/Client/Assets/Scripts/Utils/FileHandler.cs new file mode 100644 index 0000000..2cf5914 --- /dev/null +++ b/Client/Assets/Scripts/Utils/FileHandler.cs @@ -0,0 +1,157 @@ +using System; +using System.Collections.Generic; +using System.IO; +using Newtonsoft.Json; +using UnityEngine; + +namespace Utils +{ + public static class FileHandler + { + // 保存文件的默认文件夹路径 + private const string FolderPath = "save"; + + /// + /// 初始化文件夹 + /// + static FileHandler() + { + 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); + + // 反序列化为字典 + Dictionary objects = JsonConvert.DeserializeObject>(jsonContent); + + Debug.Log($"Loaded archive: {filePath}"); + return objects; + } + catch (Exception ex) + { + Debug.LogError($"Failed to load archive: {ex.Message}"); + return null; + } + } + } + +} \ No newline at end of file diff --git a/Client/Assets/Scripts/Utils/FileHandler.cs.meta b/Client/Assets/Scripts/Utils/FileHandler.cs.meta new file mode 100644 index 0000000..f7a0aa8 --- /dev/null +++ b/Client/Assets/Scripts/Utils/FileHandler.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 3b277e204485b3b47b6de15948e9156e \ No newline at end of file