/// <summary> /// Loads in a preset from a file. /// </summary> /// <param name="presetFile">The file to load the preset from.</param> /// <returns>The parsed preset from the file, or null if the preset is invalid.</returns> public static Preset LoadPresetFile(string presetFile) { string extension = Path.GetExtension(presetFile); if (extension != null) { extension = extension.ToLowerInvariant(); } if (extension == ".xml") { throw new ArgumentException("Exported preset file is too old to open. Open with VidCoder 3.15 and export to upgrade it."); } if (extension != ".vjpreset") { throw new ArgumentException("File extension " + extension + " is not recognized."); } if (!File.Exists(presetFile)) { throw new ArgumentException("Preset file could not be found."); } PresetWrapper presetWrapper = JsonConvert.DeserializeObject <PresetWrapper>(File.ReadAllText(presetFile)); if (presetWrapper.Version < 20) { throw new ArgumentException("Exported preset file is too old to open. Open with VidCoder 3.15 and export to upgrade it."); } UpgradeEncodingProfile(presetWrapper.Preset.EncodingProfile, PresetToDbVersion(presetWrapper.Version)); return(presetWrapper.Preset); }
/// <summary> /// Loads in a preset from a file. /// </summary> /// <param name="presetFile">The file to load the preset from.</param> /// <returns>The parsed preset from the file, or null if the preset is invalid.</returns> public static Preset LoadPresetFile(string presetFile) { string extension = Path.GetExtension(presetFile); if (extension != null) { extension = extension.ToLowerInvariant(); } if (extension != ".xml" && extension != ".vjpreset") { throw new ArgumentException("File extension " + extension + " is not recognized."); } if (!File.Exists(presetFile)) { throw new ArgumentException("Preset file could not be found."); } if (extension == ".xml") { XDocument doc = XDocument.Load(presetFile); if (doc.Element("UserPreset") == null) { throw new ArgumentException("Preset file is malformed."); } XElement presetElement = doc.Element("UserPreset").Element("Preset"); int version = int.Parse(doc.Element("UserPreset").Attribute("Version").Value, CultureInfo.InvariantCulture); using (XmlReader reader = presetElement.CreateReader()) { XmlSerializer presetSerializer = new XmlSerializer(typeof(Preset)); var preset = presetSerializer.Deserialize(reader) as Preset; UpgradeEncodingProfile(preset.EncodingProfile, PresetToDbVersion(version)); return(preset); } } else { PresetWrapper presetWrapper = JsonConvert.DeserializeObject <PresetWrapper>(File.ReadAllText(presetFile)); UpgradeEncodingProfile(presetWrapper.Preset.EncodingProfile, PresetToDbVersion(presetWrapper.Version)); return(presetWrapper.Preset); } }
/// <summary> /// Saves a user preset to a file. /// </summary> /// <param name="preset">The preset to save.</param> /// <param name="filePath">The path to save the preset to.</param> /// <returns>True if the save succeeded.</returns> public static bool SavePresetToFile(Preset preset, string filePath) { try { var presetWrapper = new PresetWrapper { Version = CurrentPresetVersion, Preset = preset }; File.WriteAllText(filePath, JsonConvert.SerializeObject(presetWrapper)); return(true); } catch (Exception exception) { System.Windows.MessageBox.Show(string.Format(MainRes.CouldNotSavePresetMessage, preset.Name, Environment.NewLine, exception)); } return(false); }