LocalConfiguration.cs 7.8 KB

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