JsonAppConfig.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using Azylee.Core.AppUtils.AppConfigUtils;
  2. using Azylee.Core.AppUtils.AppConfigUtils.AppConfigInterfaces;
  3. using Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels;
  4. using Azylee.Core.DataUtils.DateTimeUtils;
  5. using Azylee.Core.IOUtils.FileUtils;
  6. using Azylee.Core.IOUtils.TxtUtils;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. namespace Azylee.Jsons.JsonAppConfigUtils
  13. {
  14. /// <summary>
  15. /// Json 配置管理器
  16. ///
  17. /// 如何使用:
  18. /// 1. 根据自己需求,创建配置类,实现IAppConfigModel接口
  19. /// 2. 使用配置管理器,new出创建的配置类
  20. /// 3. 通过配置管理器,管理配置信息即可
  21. /// PS. 建议直接使用静态变量创建
  22. /// </summary>
  23. /// <typeparam name="T"></typeparam>
  24. public class JsonAppConfig<T> : AppConfig<T> where T : IAppConfigModel, new()
  25. {
  26. private bool IsReadonly = false;
  27. private string FilePath;
  28. private string FilePathBackup;
  29. private string FilePathReadonly;
  30. private JsonAppConfig() { }
  31. /// <summary>
  32. /// 构造配置管理器
  33. /// </summary>
  34. /// <param name="filepath">配置文件路径</param>
  35. /// <param name="isReadonly">是否只读</param>
  36. public JsonAppConfig(string filepath, bool isReadonly = false)
  37. {
  38. this.FilePath = filepath;
  39. this.FilePathBackup = filepath + ".backup";
  40. this.FilePathReadonly = filepath + ".readonly";
  41. if (isReadonly)
  42. {
  43. this.IsReadonly = isReadonly;
  44. this.FilePath = FilePathReadonly;
  45. }
  46. // 配置初始化
  47. OnCreate();
  48. // 重置配置项
  49. this.Config.ForceSet();
  50. // 保存配置
  51. DoSave();
  52. }
  53. public override bool OnCreate()
  54. {
  55. // 读取默认配置文件
  56. if (File.Exists(this.FilePath))
  57. {
  58. this.Config = Json.File2Object<T>(this.FilePath);
  59. }
  60. // 读取备份的配置文件
  61. if (this.Config == null)
  62. {
  63. if (File.Exists(this.FilePathBackup))
  64. {
  65. this.Config = Json.File2Object<T>(this.FilePathBackup);
  66. }
  67. }
  68. if (this.Config == null)
  69. {
  70. // 配置读取为空,有可能是配置内容发生结构性改变,JSON解析失败
  71. // 所以,此处对原配置文件先进行备份,防止内容丢失
  72. FileTool.Copy(this.FilePath, this.FilePath + ".backup" + "." + DateTimeFormatter.Compact(DateTime.Now), true);
  73. this.Config = new T();
  74. }
  75. return true;
  76. }
  77. public override bool OnDestroy()
  78. {
  79. return DoSave();
  80. }
  81. /// <summary>
  82. /// 保存配置信息
  83. /// </summary>
  84. /// <returns></returns>
  85. public override bool DoSave()
  86. {
  87. // 如果是只读模式,则直接返回保存失败
  88. if (this.IsReadonly) return false;
  89. string s = Json.Object2String(this.Config);
  90. s = JsonFormat.Format(s);
  91. TxtTool.Create(this.FilePath, s);
  92. bool result = TxtTool.Create(this.FilePathBackup, s);
  93. if (result)
  94. {
  95. if (Json.File2Object<T>(this.FilePathBackup) != null)
  96. {
  97. if (FileTool.Copy(this.FilePathBackup, this.FilePath, true))
  98. {
  99. FileTool.Copy(this.FilePath, this.FilePathReadonly, true);
  100. FileTool.Delete(this.FilePathBackup);
  101. }
  102. }
  103. }
  104. return result;
  105. }
  106. }
  107. }