/// <summary> /// Attempts to load the scripting settings from the defacto file at the given path. /// /// If the file does not exist, it will create a new, empty file there. /// </summary> /// <param name="path"></param> /// <returns></returns> public static ScriptingSettings LoadScriptingSettings(String path) { String targetPath = Path.Combine(path, SCRIPT_CONF_FILE_NAME); ScriptingSettings settings = new ScriptingSettings(); try { string raw = File.ReadAllText(targetPath); settings = JsonConvert.DeserializeObject<ScriptingSettings>(raw); if (settings.Version < ScriptingSettings.SchemaVersion) { MessageBox.Show("Your scripting parameters are outdated and have been set to defaults. Please updated them.", "Settings Outdated"); settings = new ScriptingSettings(); throw new SettingsFileOutdatedException(); } } catch(Exception ex) { try { WriteScriptingSettingsToFile(path, settings); } catch { throw new SettingFileException(string.Format("Could not access settings file at {0}", targetPath)); } } Console.WriteLine(settings); return settings; }
/// <summary> /// Write the given scripting settings to the defacto file at the given path. /// </summary> /// <param name="path"></param> /// <param name="settings"></param> /// <returns></returns> public static String WriteScriptingSettingsToFile(String path, ScriptingSettings settings) { String outputFile = Path.Combine(path, SCRIPT_CONF_FILE_NAME); File.Delete(outputFile); StreamWriter writer = new StreamWriter( new FileStream( outputFile, FileMode.OpenOrCreate, FileAccess.Write) ); String content = JsonConvert.SerializeObject(settings, Formatting.Indented); Console.WriteLine(content); writer.Write(content); writer.Close(); return outputFile; }