LocalConfiguration.cs 6.7 KB

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