/// <summary> /// Saves a given configuration to the save file in parallel. /// </summary> public void SaveStart(ConfigT cfg, bool silent = false) { if (!SaveInProgress) { if (!silent) { ExceptionHandler.SendChatMessage("Saving configuration..."); } SaveInProgress = true; EnqueueTask(() => { cfg.Validate(); KnownException exception = TrySave(cfg); if (exception != null) { EnqueueAction(() => SaveFinish(false, silent)); throw exception; } else { EnqueueAction(() => SaveFinish(true, silent)); } }); } else { ExceptionHandler.SendChatMessage("Save operation already in progress."); } }
/// <summary> /// Attempts to save current configuration to a file. /// </summary> private KnownException TrySave(ConfigT cfg) { string xmlOut; KnownException exception = Utils.Xml.TrySerialize(cfg, out xmlOut); if (exception == null && xmlOut != null) { exception = cfgFile.TryWrite(xmlOut); } return(exception); }
/// <summary> /// Loads the current configuration in parallel. /// </summary> public void LoadStart(Action <ConfigT> UpdateConfig, bool silent = false) { if (!SaveInProgress) { SaveInProgress = true; if (!silent) { ExceptionHandler.SendChatMessage("Loading configuration..."); } EnqueueTask(() => { ConfigT cfg = null; KnownException loadException = null; // Load and validate if (cfgFile.FileExists) { loadException = TryLoad(out cfg); cfg = ValidateConfig(cfg); } else { cfg = Defaults; } // Enqueue callback EnqueueAction(() => UpdateConfig(cfg)); // Write validated config back to the file TrySave(cfg); if (loadException != null) { EnqueueAction(() => LoadFinish(false, silent)); throw loadException; } else { EnqueueAction(() => LoadFinish(true, silent)); } }); } else { ExceptionHandler.SendChatMessage("Save operation already in progress."); } }
/// <summary> /// Saves the current configuration synchronously. /// </summary> public void Save(ConfigT cfg) { if (!SaveInProgress) { cfg.Validate(); KnownException exception = TrySave(cfg); if (exception != null) { throw exception; } } }
/// <summary> /// Attempts to load config file and creates a new one if it can't. /// </summary> private KnownException TryLoad(out ConfigT cfg) { string data; KnownException exception = cfgFile.TryRead(out data); cfg = null; if (exception != null || data == null) { return(exception); } else { exception = Utils.Xml.TryDeserialize(data, out cfg); } return(exception); }
/// <summary> /// Loads the current configuration synchronously. /// </summary> public ConfigT Load(bool silent = false) { ConfigT cfg = null; KnownException loadException = null; if (!SaveInProgress) { SaveInProgress = true; if (!silent) { ExceptionHandler.SendChatMessage("Loading configuration..."); } if (cfgFile.FileExists) { loadException = TryLoad(out cfg); cfg = ValidateConfig(cfg); } else { cfg = Defaults; } TrySave(cfg); if (loadException != null) { ExceptionHandler.WriteToLog(loadException.ToString()); } else if (!silent) { ExceptionHandler.SendChatMessage("Configuration loaded."); } } else { ExceptionHandler.SendChatMessage("Save operation already in progress."); } SaveInProgress = false; return(cfg); }
private ConfigT ValidateConfig(ConfigT cfg) { if (cfg != null) { if (cfg.VersionID != Defaults.VersionID) { EnqueueAction(() => ExceptionHandler.SendChatMessage("Config version mismatch. Some settings may have " + "been reset. A backup of the original config file will be made.")); Backup(); } cfg.Validate(); return(cfg); } else { EnqueueAction(() => ExceptionHandler.SendChatMessage("Unable to load configuration.")); return(Defaults); } }