From 57eb3b2de4e015c94a6bdbb674dc1ec8c1c4fcc2 Mon Sep 17 00:00:00 2001 From: Arash Outadi Date: Sat, 10 Aug 2019 21:02:49 -0700 Subject: [PATCH] Add code path for invalid settings file --- voxygen/src/settings.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/voxygen/src/settings.rs b/voxygen/src/settings.rs index df23337137..bcb4b0c181 100644 --- a/voxygen/src/settings.rs +++ b/voxygen/src/settings.rs @@ -233,13 +233,26 @@ impl Default for Settings { impl Settings { pub fn load() -> Self { let path = Settings::get_settings_path(); - - // If file doesn't exist, use the default settings. - if let Ok(file) = fs::File::open(path) { - ron::de::from_reader(file).expect("Error parsing settings") - } else { - Self::default() + + if let Ok(file) = fs::File::open(&path) { + match ron::de::from_reader(file) { + Ok(s) => s, + Err(e) => { + log::warn!("Failed to parse setting file! Fallback to default. {}", e); + // Rename the corrupted settings file + let mut new_path = path.to_owned(); + new_path.pop(); + new_path.push("settings.invalid.ron"); + if let Err(err) = std::fs::rename(path, new_path) { + log::warn!("Failed to rename settings file. {}", err); + } + } + } } + let default_settings = Self::default(); + default_settings.save_to_file_warn(); + default_settings + } pub fn save_to_file_warn(&self) {