/// <summary> /// Load settings from a file /// </summary> /// <param name="settings">The filename to load from, a default will be used otherwise</param> /// <return>Settings</return> public Settings Load(String filename = null) { if (filename == null) filename = DefaultFilename; if (File.Exists(filename)) { Settings settings; Stream stream = File.OpenRead(filename); XmlSerializer serializer = new XmlSerializer(typeof(Settings)); try { settings = (Settings)serializer.Deserialize(stream); } catch { // Probably bad data in file, so provide the defaults. settings = new Settings(); } stream.Close(); // Validate settings if needed validateSettings(settings); settings.Save(); return settings; } // If we're here, no settings file exists, so save one and return defaults. this.Save(DefaultFilename); return this; }
/// <summary> /// A place to run any checks and restrict any values before saving the settings object. /// </summary> /// <param name="settings">The settings object</param> private void validateSettings(Settings settings) { settings.Scale = MathHelper.Clamp(settings.Scale, 0, 100); }