/// <summary> /// Saves the given settings profile to a file at the given path. /// </summary> /// <param name="profile">The profile to save.</param> /// <param name="filePath">The path of the file.</param> /// <returns><c>true</c> if the file was correctly saved, <c>false</c> otherwise.</returns> public bool SaveSettingsProfile(SettingsProfile profile, UFile filePath) { if (profile == null) throw new ArgumentNullException("profile"); try { profile.Saving = true; Directory.CreateDirectory(filePath.GetFullDirectory()); var settingsFile = new SettingsFile(); EncodeSettings(profile, settingsFile.Settings); using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Write)) { YamlSerializer.Serialize(stream, settingsFile); } } catch (Exception e) { Logger.Error("Error while saving settings file [{0}]: {1}", e, filePath, e.FormatForReport()); return false; } finally { profile.Saving = false; } return true; }
/// <summary> /// Reloads a profile from its file, updating the value that have changed. /// </summary> /// <param name="profile">The profile to reload.</param> public void ReloadSettingsProfile(SettingsProfile profile) { var filePath = profile.FilePath; if (filePath == null) throw new ArgumentException("profile"); if (!File.Exists(filePath)) { Logger.Error("Settings file [{0}] was not found", filePath); throw new ArgumentException("profile"); } try { var settingsFile = new SettingsFile(profile); using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { YamlSerializer.Deserialize(stream, settingsFile); } } catch (Exception e) { Logger.Error("Error while loading settings file [{0}]: {1}", e, filePath, e.FormatForReport()); } var handler = SettingsFileLoaded; handler?.Invoke(null, new SettingsFileLoadedEventArgs(filePath)); }
/// <summary> /// Loads a settings profile from the given file. /// </summary> /// <param name="filePath">The path of the file from which to load settings.</param> /// <param name="setAsCurrent">If <c>true</c>, the loaded profile will also be set as <see cref="CurrentProfile"/>.</param> /// <param name="parent">The profile to use as parent for the loaded profile. If <c>null</c>, a default profile will be used.</param> /// <returns><c>true</c> if settings were correctly loaded, <c>false</c> otherwise.</returns> public SettingsProfile LoadSettingsProfile(UFile filePath, bool setAsCurrent, SettingsProfile parent = null) { if (filePath == null) throw new ArgumentNullException(nameof(filePath)); if (!File.Exists(filePath)) { Logger.Error("Settings file [{0}] was not found", filePath); return null; } var profile = new SettingsProfile(this, parent ?? rootProfile) { FilePath = filePath }; try { var settingsFile = new SettingsFile(profile); using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { YamlSerializer.Deserialize(stream, settingsFile); } } catch (Exception e) { Logger.Error("Error while loading settings file [{0}]: {1}", e, filePath, e.FormatForReport()); return null; } lock (SettingsLock) { profileList.Add(profile); if (setAsCurrent) { CurrentProfile = profile; } } var handler = SettingsFileLoaded; handler?.Invoke(null, new SettingsFileLoadedEventArgs(filePath)); return profile; }
/// <summary> /// Saves the given settings profile to a file at the given path. /// </summary> /// <param name="profile">The profile to save.</param> /// <param name="filePath">The path of the file.</param> /// <returns><c>true</c> if the file was correctly saved, <c>false</c> otherwise.</returns> public bool SaveSettingsProfile(SettingsProfile profile, UFile filePath) { if (profile == null) throw new ArgumentNullException(nameof(profile)); try { profile.Saving = true; Directory.CreateDirectory(filePath.GetFullDirectory()); var settingsFile = new SettingsFile(profile); using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Write)) { SettingsYamlSerializer.Default.Serialize(stream, settingsFile); } if (filePath != profile.FilePath) { if (File.Exists(profile.FilePath)) { File.Delete(profile.FilePath); } profile.FilePath = filePath; } } catch (Exception e) { Logger.Error("Error while saving settings file [{0}]: {1}", e, filePath, e.FormatFull()); return false; } finally { profile.Saving = false; } return true; }
/// <summary> /// Loads a settings profile from the given file. /// </summary> /// <param name="filePath">The path of the file from which to load settings.</param> /// <param name="setAsCurrent">If <c>true</c>, the loaded profile will also be set as <see cref="CurrentProfile"/>.</param> /// <param name="parent">The profile to use as parent for the loaded profile. If <c>null</c>, a default profile will be used.</param> /// <param name="registerInContainer">If true, the profile will be registered in this container. Otherwise it will be disconnected from the container.</param> /// <returns><c>true</c> if settings were correctly loaded, <c>false</c> otherwise.</returns> /// <remarks> /// If the profile is not registered to the container, it won't be able to receive <see cref="SettingsKey"/> that are registered after its /// creation. If the profile is registered to the container, <see cref="UnloadSettingsProfile"/> must be call in order to unregister it. /// </remarks> public SettingsProfile LoadSettingsProfile(UFile filePath, bool setAsCurrent, SettingsProfile parent = null, bool registerInContainer = true) { if (filePath == null) throw new ArgumentNullException(nameof(filePath)); if (setAsCurrent && !registerInContainer) throw new ArgumentException(@"Cannot set the profile as current if it's not registered to the container", nameof(setAsCurrent)); if (!File.Exists(filePath)) { Logger.Error("Settings file [{0}] was not found", filePath); return null; } var profile = new SettingsProfile(this, parent ?? RootProfile) { FilePath = filePath }; try { var settingsFile = new SettingsFile(profile); using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { SettingsYamlSerializer.Default.Deserialize(stream, settingsFile); } } catch (Exception e) { Logger.Error("Error while loading settings file [{0}]: {1}", e, filePath, e.FormatFull()); return null; } if (registerInContainer) { lock (SettingsLock) { profileList.Add(profile); if (setAsCurrent) { CurrentProfile = profile; } } } var handler = SettingsFileLoaded; handler?.Invoke(null, new SettingsFileLoadedEventArgs(filePath)); return profile; }
/// <summary> /// Saves the given settings profile to a file at the given path. /// </summary> /// <param name="profile">The profile to save.</param> /// <param name="filePath">The path of the file.</param> /// <returns><c>true</c> if the file was correctly saved, <c>false</c> otherwise.</returns> public bool SaveSettingsProfile(SettingsProfile profile, UFile filePath) { if (profile == null) throw new ArgumentNullException("profile"); try { profile.Saving = true; Directory.CreateDirectory(filePath.GetFullDirectory()); var settingsFile = new SettingsFile(); foreach (var entry in profile.Settings.Values) { try { // Find key SettingsKey key; settingsKeys.TryGetValue(entry.Name, out key); settingsFile.Settings.Add(entry.Name, entry.GetSerializableValue(key)); } catch (Exception) { } } using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Write)) { YamlSerializer.Serialize(stream, settingsFile); } } catch (Exception e) { Logger.Error("Error while saving settings file [{0}]: {1}", e, filePath, e.FormatForReport()); return false; } finally { profile.Saving = false; } return true; }