123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using InABox.Core;
- namespace InABox.Configuration
- {
- public interface ILocalConfigurationSettings : IConfigurationSettings
- {
- }
- public class LocalConfiguration<T> : Configuration<T> where T : ILocalConfigurationSettings, new()
- {
- public LocalConfiguration(string section = "") : base(section)
- {
- Folder = GetPath();
- }
- public LocalConfiguration(string folder, string section) : base(section)
- {
- Folder = folder;
- }
- public string Folder { get; set; }
- private string GetPath() => CoreUtils.GetPath();
- public string GetFileName()
- {
- return Path.Combine(
- Folder,
- typeof(T).Name + ".settings"
- );
- }
- private string EnsureFileName()
- {
- if (!Directory.Exists(Folder))
- Directory.CreateDirectory(Folder);
- return GetFileName();
- }
- protected virtual string SerializeConfiguration(Dictionary<string, T> config)
- {
- return Serialization.Serialize(config, true);
- }
- protected virtual Dictionary<string, T>? DeserializeConfiguration(string text)
- {
- return Serialization.Deserialize<Dictionary<string, T>>(text);
- }
- private Dictionary<string, T> LoadCurrentConfig(string filename)
- {
- Dictionary<string, T>? config = null;
- if (File.Exists(filename))
- config = DeserializeConfiguration(File.ReadAllText(filename));
- return config ?? new Dictionary<string, T>();
- }
- public override T Load(bool useCache = true)
- {
- if (useCache)
- {
- var cached = ConfigurationCache.Check<T>(ConfigurationCacheType.Local, Section);
- if (cached != null)
- //Logger.Send(LogType.Information, "", "Local Config: Returning Cached Configuration Data");
- return cached;
- }
- //Logger.Send(LogType.Information, "", "Local Config: Creating "+typeof(T).EntityName().Split('.').Last());
- var result = new T();
- var filename = EnsureFileName();
- //Logger.Send(LogType.Information, "", "Local Config: FileName is ["+filename+"]");
- if (File.Exists(filename))
- {
- //Logger.Send(LogType.Information, "", "Local Config: Deserializing Configuration");
- var config = DeserializeConfiguration(File.ReadAllText(filename)); //Toml.ReadString<T>(File.ReadAllText(FileName));
- if (config != null && config.ContainsKey(Section))
- //Logger.Send(LogType.Information, "", "Local Config: Found Section ["+Section+"}");
- if (config[Section] != null)
- //Logger.Send(LogType.Information, "", "Local Config: ["+Section+"] is not null");
- result = config[Section];
- }
- //Logger.Send(LogType.Information, "", "Local Config: Adding Configuration to Cache");
- ConfigurationCache.Add(ConfigurationCacheType.Local, Section, result);
- return result;
- }
- public override Dictionary<string, T> LoadAll()
- {
- return LoadCurrentConfig(EnsureFileName());
- }
- public override void Save(T obj)
- {
- var filename = EnsureFileName();
- var config = LoadCurrentConfig(filename);
- config[Section] = obj;
- var contents = SerializeConfiguration(config);
- File.WriteAllText(filename, contents);
- ConfigurationCache.Add(ConfigurationCacheType.Local, Section, obj);
- }
- public override void SaveAll(Dictionary<string, T> objs)
- {
- var filename = EnsureFileName();
- var config = LoadCurrentConfig(filename);
- foreach(var (section, obj) in objs)
- {
- config[section] = obj;
- }
- var contents = SerializeConfiguration(config);
- File.WriteAllText(filename, contents);
- foreach(var (section, obj) in objs)
- {
- ConfigurationCache.Add(ConfigurationCacheType.Local, section, obj);
- }
- }
- public override void Delete(Action<Exception?>? callback = null)
- {
- var filename = EnsureFileName();
- var config = LoadCurrentConfig(filename);
- if (config.ContainsKey(Section))
- config.Remove(Section);
- if (config.Any())
- {
- var contents = SerializeConfiguration(config);
- File.WriteAllText(filename, contents);
- }
- else
- {
- if (File.Exists(filename))
- File.Delete(filename);
- }
- ConfigurationCache.Clear<T>(ConfigurationCacheType.Local, Section);
- callback?.Invoke(null);
- }
- public override string[] Sections()
- {
- if (!Directory.Exists(Folder))
- return new[] { "" };
- var filename = GetFileName();
- if (!File.Exists(filename))
- return new[] { "" };
- var config = DeserializeConfiguration(File.ReadAllText(filename));
- if (config == null)
- return new[] { "" };
- return config.Keys.ToArray();
- }
- }
- public class EncryptedLocalConfiguration<T> : LocalConfiguration<T>
- where T : ILocalConfigurationSettings, new()
- {
- private byte[] Key;
- public EncryptedLocalConfiguration(byte[] key, string section = "") : base(section)
- {
- Key = key;
- }
- public EncryptedLocalConfiguration(byte[] key, string section, string folder) : base(section, folder)
- {
- Key = key;
- }
- public EncryptedLocalConfiguration(string key, string section = "") : this(ConvertKey(key), section) { }
- public EncryptedLocalConfiguration(string key, string section, string folder) : this(ConvertKey(key), section, folder) { }
- protected override string SerializeConfiguration(Dictionary<string, T> config)
- {
- return Encryption.EncryptV2(base.SerializeConfiguration(config), Key);
- }
- protected override Dictionary<string, T>? DeserializeConfiguration(string text)
- {
- return base.DeserializeConfiguration(Encryption.DecryptV2(text, Key));
- }
- private static byte[] ConvertKey(string key)
- {
- return Convert.FromBase64String(key);
- }
- }
- }
|