LocalConfiguration.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using InABox.Core;
  7. namespace InABox.Configuration
  8. {
  9. public interface ILocalConfigurationSettings : IConfigurationSettings
  10. {
  11. }
  12. public class LocalConfiguration<T> : Configuration<T>, IDisposable
  13. where T : ILocalConfigurationSettings, new()
  14. {
  15. public LocalConfiguration(string section = "") : base(section)
  16. {
  17. Folder = GetPath();
  18. }
  19. public LocalConfiguration(string folder, string section) : base(section)
  20. {
  21. Folder = folder;
  22. }
  23. public string Folder { get; set; }
  24. private string GetPath() => CoreUtils.GetPath();
  25. public string GetFileName()
  26. {
  27. return Path.Combine(
  28. Folder,
  29. typeof(T).Name + ".settings"
  30. );
  31. }
  32. private string EnsureFileName()
  33. {
  34. if (!Directory.Exists(Folder))
  35. Directory.CreateDirectory(Folder);
  36. return GetFileName();
  37. }
  38. protected virtual string SerializeConfiguration(Dictionary<string, T> config)
  39. {
  40. return Serialization.Serialize(config, true);
  41. }
  42. protected virtual Dictionary<string, T>? DeserializeConfiguration(string text)
  43. {
  44. return Serialization.Deserialize<Dictionary<string, T>>(text);
  45. }
  46. private Dictionary<string, T> LoadCurrentConfig(string filename)
  47. {
  48. Dictionary<string, T>? config = null;
  49. if (File.Exists(filename))
  50. config = DeserializeConfiguration(File.ReadAllText(filename));
  51. return config ?? new Dictionary<string, T>();
  52. }
  53. public override T Load(bool useCache = true)
  54. {
  55. if (useCache)
  56. {
  57. var cached = ConfigurationCache.Check<T>(ConfigurationCacheType.Local, Section);
  58. if (cached != null)
  59. //Logger.Send(LogType.Information, "", "Local Config: Returning Cached Configuration Data");
  60. return cached;
  61. }
  62. //Logger.Send(LogType.Information, "", "Local Config: Creating "+typeof(T).EntityName().Split('.').Last());
  63. var result = new T();
  64. var filename = EnsureFileName();
  65. //Logger.Send(LogType.Information, "", "Local Config: FileName is ["+filename+"]");
  66. if (File.Exists(filename))
  67. {
  68. //Logger.Send(LogType.Information, "", "Local Config: Deserializing Configuration");
  69. var config = DeserializeConfiguration(File.ReadAllText(filename)); //Toml.ReadString<T>(File.ReadAllText(FileName));
  70. if (config != null && config.ContainsKey(Section))
  71. //Logger.Send(LogType.Information, "", "Local Config: Found Section ["+Section+"}");
  72. if (config[Section] != null)
  73. //Logger.Send(LogType.Information, "", "Local Config: ["+Section+"] is not null");
  74. result = config[Section];
  75. }
  76. //Logger.Send(LogType.Information, "", "Local Config: Adding Configuration to Cache");
  77. ConfigurationCache.Add(ConfigurationCacheType.Local, Section, result);
  78. return result;
  79. }
  80. public override Dictionary<string, T> LoadAll()
  81. {
  82. return LoadCurrentConfig(EnsureFileName());
  83. }
  84. public override void Save(T obj)
  85. {
  86. var filename = EnsureFileName();
  87. var config = LoadCurrentConfig(filename);
  88. config[Section] = obj;
  89. var contents = SerializeConfiguration(config);
  90. File.WriteAllText(filename, contents);
  91. ConfigurationCache.Add(ConfigurationCacheType.Local, Section, obj);
  92. }
  93. public override void SaveAll(Dictionary<string, T> objs, bool reset = false)
  94. {
  95. var filename = EnsureFileName();
  96. var config = reset
  97. ? new Dictionary<string,T>()
  98. : LoadCurrentConfig(filename);
  99. foreach(var (section, obj) in objs)
  100. {
  101. config[section] = obj;
  102. }
  103. var contents = SerializeConfiguration(config);
  104. File.WriteAllText(filename, contents);
  105. foreach(var (section, obj) in objs)
  106. {
  107. ConfigurationCache.Add(ConfigurationCacheType.Local, section, obj);
  108. }
  109. }
  110. public override void Delete(Action<Exception?>? callback = null)
  111. {
  112. var filename = EnsureFileName();
  113. var config = LoadCurrentConfig(filename);
  114. if (config.ContainsKey(Section))
  115. config.Remove(Section);
  116. if (config.Any())
  117. {
  118. var contents = SerializeConfiguration(config);
  119. File.WriteAllText(filename, contents);
  120. }
  121. else
  122. {
  123. if (File.Exists(filename))
  124. File.Delete(filename);
  125. }
  126. ConfigurationCache.Clear<T>(ConfigurationCacheType.Local, Section);
  127. callback?.Invoke(null);
  128. }
  129. public override string[] Sections()
  130. {
  131. if (!Directory.Exists(Folder))
  132. return new[] { "" };
  133. var filename = GetFileName();
  134. if (!File.Exists(filename))
  135. return new[] { "" };
  136. var config = DeserializeConfiguration(File.ReadAllText(filename));
  137. if (config == null)
  138. return new[] { "" };
  139. return config.Keys.ToArray();
  140. }
  141. public void Dispose()
  142. {
  143. }
  144. }
  145. public class EncryptedLocalConfiguration<T> : LocalConfiguration<T>
  146. where T : ILocalConfigurationSettings, new()
  147. {
  148. private byte[] Key;
  149. public EncryptedLocalConfiguration(byte[] key, string section = "") : base(section)
  150. {
  151. Key = key;
  152. }
  153. public EncryptedLocalConfiguration(byte[] key, string section, string folder) : base(section, folder)
  154. {
  155. Key = key;
  156. }
  157. public EncryptedLocalConfiguration(string key, string section = "") : this(ConvertKey(key), section) { }
  158. public EncryptedLocalConfiguration(string key, string section, string folder) : this(ConvertKey(key), section, folder) { }
  159. protected override string SerializeConfiguration(Dictionary<string, T> config)
  160. {
  161. return Encryption.EncryptV2(base.SerializeConfiguration(config), Key);
  162. }
  163. protected override Dictionary<string, T>? DeserializeConfiguration(string text)
  164. {
  165. return base.DeserializeConfiguration(Encryption.DecryptV2(text, Key));
  166. }
  167. private static byte[] ConvertKey(string key)
  168. {
  169. return Convert.FromBase64String(key);
  170. }
  171. }
  172. }