From f146150faa8f6822f107b65c36e966b152bfa4d1 Mon Sep 17 00:00:00 2001 From: Jack <3486688394@qq.com> Date: Sun, 18 Jan 2026 21:36:24 +0800 Subject: [PATCH] =?UTF-8?q?feat(config):=20=E6=B7=BB=E5=8A=A0=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E8=AF=BB=E5=8F=96=E5=92=8C=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 引入 ConfigReader 和 ConfigWriter 委托类型定义 - 在 Config 构造函数中添加自定义读取和保存方法参数 - 实现 ReadConfigFile 和 WriteConfigFile 辅助方法 - 修改文件操作使用自定义方法或默认方法 - 更新测试程序添加多种自定义方法使用示例 - 在文档中添加自定义方法使用的代码示例 --- JackCraft.Config/Config.cs | 39 ++++++++++---- Readme.MD | 33 ++++++++++++ Test/Program.cs | 106 ++++++++++++++++++++++++++++++++++--- 3 files changed, 161 insertions(+), 17 deletions(-) diff --git a/JackCraft.Config/Config.cs b/JackCraft.Config/Config.cs index d65928b..a491faf 100644 --- a/JackCraft.Config/Config.cs +++ b/JackCraft.Config/Config.cs @@ -5,21 +5,25 @@ namespace JackCraft.Config; public class Config { + public delegate string ConfigReader(string filePath); + + public delegate void ConfigWriter(string filePath, string content); + private readonly FileInfo _configFile; - public readonly string ConfigFileName; - public readonly string ConfigFilePath; + private readonly ConfigReader? _customReader; + private readonly ConfigWriter? _customWriter; private Dictionary _configData = new(); - public Config(FileInfo configFile) + public Config(FileInfo configFile, ConfigReader? customReader = null, ConfigWriter? customWriter = null) { _configFile = configFile; - ConfigFilePath = configFile.DirectoryName ?? string.Empty; - ConfigFileName = configFile.Name; + _customReader = customReader; + _customWriter = customWriter; + if (!configFile.Exists) { configFile.Directory?.Create(); - using var stream = configFile.Create(); - File.WriteAllText(configFile.FullName, "{}"); + WriteConfigFile(configFile.FullName, "{}"); } LoadConfigData(); @@ -29,7 +33,7 @@ public class Config { if (File.Exists(_configFile.FullName)) { - var existingJson = File.ReadAllText(_configFile.FullName); + var existingJson = ReadConfigFile(_configFile.FullName); _configData = JsonConvert.DeserializeObject>(existingJson) ?? new Dictionary(); } @@ -39,6 +43,21 @@ public class Config } } + // 读取配置文件的辅助方法,使用自定义读取器或默认方法 + private string ReadConfigFile(string filePath) + { + return _customReader != null ? _customReader(filePath) : File.ReadAllText(filePath); + } + + // 写入配置文件的辅助方法,使用自定义写入器或默认方法 + private void WriteConfigFile(string filePath, string content) + { + if (_customWriter != null) + _customWriter(filePath, content); + else + File.WriteAllText(filePath, content); + } + public T? Get(string key) { return Get(key, default); @@ -104,7 +123,7 @@ public class Config currentData[finalKey] = value!; var json = JsonConvert.SerializeObject(_configData, Formatting.Indented); - File.WriteAllText(_configFile.FullName, json); + WriteConfigFile(_configFile.FullName, json); } public void Delete(string key) @@ -130,6 +149,6 @@ public class Config currentData.Remove(finalKey); var json = JsonConvert.SerializeObject(_configData, Formatting.Indented); - File.WriteAllText(_configFile.FullName, json); + WriteConfigFile(_configFile.FullName, json); } } \ No newline at end of file diff --git a/Readme.MD b/Readme.MD index 4ee1ec3..78ba696 100644 --- a/Readme.MD +++ b/Readme.MD @@ -13,6 +13,39 @@ var config = new Config(new FileInfo("./config.json")); ``` +```csharp +// 定义自定义读取方法 +Config.ConfigReader customReader = (filePath) => { + Console.WriteLine($"正在读取文件: {filePath}"); + if (File.Exists(filePath)) + { + var content = File.ReadAllText(filePath); + Console.WriteLine($"文件内容: {content}"); + return content; + } + return "{}"; +}; + +// 定义自定义保存方法 +Config.ConfigWriter customWriter = (filePath, content) => { + Console.WriteLine($"正在保存到文件: {filePath}"); + + // 创建备份 + if (File.Exists(filePath)) + { + var backupPath = filePath + ".backup"; + File.Copy(filePath, backupPath, true); + Console.WriteLine($"已创建备份: {backupPath}"); + } + + File.WriteAllText(filePath, content); + Console.WriteLine("文件保存完成"); +}; + +// 使用自定义方法创建 Config 实例 +var config = new Config(new FileInfo("./config.json"), customReader, customWriter); +``` + ### 读取键值 > 若键不存在则返回默认值,并且同时也会写入键值(若没有提供默认值则写入空) diff --git a/Test/Program.cs b/Test/Program.cs index 887fdca..27cf7e0 100644 --- a/Test/Program.cs +++ b/Test/Program.cs @@ -1,4 +1,5 @@ -using JackCraft.Config; +using System.Text; +using JackCraft.Config; namespace Test; @@ -12,13 +13,104 @@ internal static class Program Test = "test", Abc = 123 }; - var config = new Config(new FileInfo("./config.json")); - var a = config.Get("aaa.www.www", "555"); - var b = config.Get("aaa.bbb", "1"); - var c = config.Get("aaa.sss", "1"); - var d = config.Get("bbb.sss", "1"); - config.Delete("aaa.www"); + // 示例1:使用默认的文件读取和保存方法 + Console.WriteLine("\n=== 使用默认方法 ==="); + var config1 = new Config(new FileInfo("./config1.json")); + config1.Set("app.name", "MyApp"); + config1.Set("app.version", "1.0.0"); + Console.WriteLine($"App Name: {config1.Get("app.name")}"); + Console.WriteLine($"App Version: {config1.Get("app.version")}"); + + // 示例2:使用自定义的读取和保存方法 + Console.WriteLine("\n=== 使用自定义方法 ==="); + + // 自定义读取方法 - 添加日志 + Config.ConfigReader customReader = filePath => + { + Console.WriteLine($"[自定义读取] 正在读取文件: {filePath}"); + if (File.Exists(filePath)) + { + var content = File.ReadAllText(filePath); + Console.WriteLine($"[自定义读取] 文件内容: {content}"); + return content; + } + + Console.WriteLine("[自定义读取] 文件不存在,返回空字符串"); + return "{}"; + }; + + // 自定义保存方法 - 添加日志和备份 + Config.ConfigWriter customWriter = (filePath, content) => + { + Console.WriteLine($"[自定义保存] 正在保存到文件: {filePath}"); + Console.WriteLine($"[自定义保存] 文件内容: {content}"); + + // 创建备份 + if (File.Exists(filePath)) + { + var backupPath = filePath + ".backup"; + File.Copy(filePath, backupPath, true); + Console.WriteLine($"[自定义保存] 已创建备份: {backupPath}"); + } + + File.WriteAllText(filePath, content); + Console.WriteLine("[自定义保存] 文件保存完成"); + }; + + var config2 = new Config(new FileInfo("./config2.json"), customReader, customWriter); + config2.Set("app.name", "MyCustomApp"); + config2.Set("app.version", "2.0.0"); + config2.Set("app.author", "Jack"); + + Console.WriteLine($"App Name: {config2.Get("app.name")}"); + Console.WriteLine($"App Version: {config2.Get("app.version")}"); + Console.WriteLine($"App Author: {config2.Get("app.author")}"); + + // 示例3:使用加密的自定义读取和保存方法 + Console.WriteLine("\n=== 使用加密的自定义方法 ==="); + + // 简单的Base64编码/解码作为加密示例 + Config.ConfigReader encryptedReader = filePath => + { + Console.WriteLine($"[加密读取] 正在读取加密文件: {filePath}"); + if (File.Exists(filePath)) + { + var encryptedContent = File.ReadAllText(filePath); + try + { + var decodedBytes = Convert.FromBase64String(encryptedContent); + var decodedContent = Encoding.UTF8.GetString(decodedBytes); + Console.WriteLine("[加密读取] 解密成功"); + return decodedContent; + } + catch + { + Console.WriteLine("[加密读取] 解密失败,返回空配置"); + return "{}"; + } + } + + return "{}"; + }; + + Config.ConfigWriter encryptedWriter = (filePath, content) => + { + Console.WriteLine($"[加密保存] 正在加密保存到文件: {filePath}"); + var contentBytes = Encoding.UTF8.GetBytes(content); + var encryptedContent = Convert.ToBase64String(contentBytes); + File.WriteAllText(filePath, encryptedContent); + Console.WriteLine("[加密保存] 加密保存完成"); + }; + + var config3 = new Config(new FileInfo("./config3.encrypted"), encryptedReader, encryptedWriter); + config3.Set("secret.apiKey", "12345-ABCDE"); + config3.Set("secret.password", "mySecretPassword"); + + Console.WriteLine($"API Key: {config3.Get("secret.apiKey")}"); + Console.WriteLine($"Password: {config3.Get("secret.password")}"); + + Console.WriteLine("\n所有示例执行完成!"); } }