using System; using System.Collections.Generic; using System.Text; using System.IO; using Newtonsoft.Json; using EliteBGS.BGS; namespace EliteBGS.Util { public class Config { private string config_folder = null; private string objectives_file = null; private string config_file = null; private AppConfig global_config = new AppConfig(); public Config() { DetermineConfigFolder(); global_config.PropertyChanged += Global_config_PropertyChanged; } private void Global_config_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { try { SaveGlobal(); } catch (Exception) { /* ignored */ } } public string ConfigPath => config_folder; public AppConfig Global => global_config; private void DetermineConfigFolder() { string folder = Environment.ExpandEnvironmentVariables("%appdata%\\EliteBGS"); if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } config_folder = folder; objectives_file = Path.Combine(config_folder, "objectives.json"); config_file = Path.Combine(config_folder, "config.json"); } public void SaveGlobal() { var serializer = JsonSerializer.CreateDefault(); using (var file = new StreamWriter(File.OpenWrite(config_file), Encoding.UTF8)) { var stream = new JsonTextWriter(file); serializer.Serialize(stream, global_config); } } public void LoadGlobal() { var serializer = JsonSerializer.CreateDefault(); using (var file = new StreamReader(File.OpenRead(config_file), Encoding.UTF8)) { var stream = new JsonTextReader(file); var app = serializer.Deserialize(stream); if (app != null) { this.global_config = app; global_config.PropertyChanged += Global_config_PropertyChanged; } } } public void SaveObjectives(Report report) { var serializer = JsonSerializer.CreateDefault(); using (var file = new StreamWriter(File.OpenWrite(objectives_file), Encoding.UTF8)) { var stream = new JsonTextWriter(file); serializer.Serialize(stream, report.Objectives); } } public void LoadObjectives(Report report) { var serializer = JsonSerializer.CreateDefault(); using (var file = new StreamReader(File.OpenRead(objectives_file), Encoding.UTF8)) { var stream = new JsonTextReader(file); var objectives = serializer.Deserialize>(stream); report.Objectives = objectives; } } } }