using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; namespace DiscordBotCore.Others; public class SettingsDictionary { private string _File { get; } protected IDictionary _Dictionary; public SettingsDictionary(string file) { this._File = file; _Dictionary = null!; } public async Task SaveToFile() { if (!string.IsNullOrEmpty(_File)) await JsonManager.SaveToJsonFile(_File, _Dictionary); } public IEnumerator> GetEnumerator() { return _Dictionary.GetEnumerator(); } public async Task LoadFromFile() { if (string.IsNullOrEmpty(_File)) return false; if(!File.Exists(_File)) { _Dictionary = new Dictionary(); await SaveToFile(); return false; } string fileAsText = await File.ReadAllTextAsync(_File); if(string.IsNullOrEmpty(fileAsText) || string.IsNullOrWhiteSpace(fileAsText)) { _Dictionary = new Dictionary(); await SaveToFile(); return false; } _Dictionary = await JsonManager.ConvertFromJson>(fileAsText); if (_Dictionary.Keys.Count == 0) return false; return true; } public void Add(TKey key, TValue value) { _Dictionary.Add(key, value); } public bool ContainsAllKeys(params TKey[] keys) { return keys.All(key => _Dictionary.ContainsKey(key)); } public bool ContainsKey(TKey key) { return _Dictionary.ContainsKey(key); } public bool Remove(TKey key) { return _Dictionary.Remove(key); } public TValue this[TKey key] { get { if(!_Dictionary.ContainsKey(key)) throw new System.Exception($"The key {key} ({typeof(TKey)}) (file: {this._File}) was not present in the dictionary"); if(_Dictionary[key] is not TValue) throw new System.Exception("The dictionary is corrupted. This error is critical !"); return _Dictionary[key]; } set => _Dictionary[key] = value; } // First public KeyValuePair FirstOrDefault(Func, bool> predicate) { return _Dictionary.FirstOrDefault(predicate); } // Where public IEnumerable> Where(Func, bool> predicate) { return _Dictionary.Where(predicate); } public void Clear() { _Dictionary.Clear(); } public IEnumerable Keys => _Dictionary.Keys; public IEnumerable Values => _Dictionary.Values; }