LocalConfiguration.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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)
  94. {
  95. var filename = EnsureFileName();
  96. var config = LoadCurrentConfig(filename);
  97. foreach(var (section, obj) in objs)
  98. {
  99. config[section] = obj;
  100. }
  101. var contents = SerializeConfiguration(config);
  102. File.WriteAllText(filename, contents);
  103. foreach(var (section, obj) in objs)
  104. {
  105. ConfigurationCache.Add(ConfigurationCacheType.Local, section, obj);
  106. }
  107. }
  108. public override void Delete(Action<Exception?>? callback = null)
  109. {
  110. var filename = EnsureFileName();
  111. var config = LoadCurrentConfig(filename);
  112. if (config.ContainsKey(Section))
  113. config.Remove(Section);
  114. if (config.Any())
  115. {
  116. var contents = SerializeConfiguration(config);
  117. File.WriteAllText(filename, contents);
  118. }
  119. else
  120. {
  121. if (File.Exists(filename))
  122. File.Delete(filename);
  123. }
  124. ConfigurationCache.Clear<T>(ConfigurationCacheType.Local, Section);
  125. callback?.Invoke(null);
  126. }
  127. public override string[] Sections()
  128. {
  129. if (!Directory.Exists(Folder))
  130. return new[] { "" };
  131. var filename = GetFileName();
  132. if (!File.Exists(filename))
  133. return new[] { "" };
  134. var config = DeserializeConfiguration(File.ReadAllText(filename));
  135. if (config == null)
  136. return new[] { "" };
  137. return config.Keys.ToArray();
  138. }
  139. public void Dispose()
  140. {
  141. }
  142. }
  143. public class EncryptedLocalConfiguration<T> : LocalConfiguration<T>
  144. where T : ILocalConfigurationSettings, new()
  145. {
  146. private byte[] Key;
  147. public EncryptedLocalConfiguration(byte[] key, string section = "") : base(section)
  148. {
  149. Key = key;
  150. }
  151. public EncryptedLocalConfiguration(byte[] key, string section, string folder) : base(section, folder)
  152. {
  153. Key = key;
  154. }
  155. public EncryptedLocalConfiguration(string key, string section = "") : this(ConvertKey(key), section) { }
  156. public EncryptedLocalConfiguration(string key, string section, string folder) : this(ConvertKey(key), section, folder) { }
  157. protected override string SerializeConfiguration(Dictionary<string, T> config)
  158. {
  159. return Encryption.EncryptV2(base.SerializeConfiguration(config), Key);
  160. }
  161. protected override Dictionary<string, T>? DeserializeConfiguration(string text)
  162. {
  163. return base.DeserializeConfiguration(Encryption.DecryptV2(text, Key));
  164. }
  165. private static byte[] ConvertKey(string key)
  166. {
  167. return Convert.FromBase64String(key);
  168. }
  169. }
  170. }