mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Merge branch 'arashout/213-settings-crash' into 'master'
Add code path for invalid settings file See merge request veloren/veloren!428
This commit is contained in:
commit
c5dc2fe765
@ -234,12 +234,27 @@ 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) => return 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// This is reached if either:
|
||||
// - The file can't be opened (presumably it doesn't exist)
|
||||
// - Or there was an error parsing the file
|
||||
let default_settings = Self::default();
|
||||
default_settings.save_to_file_warn();
|
||||
default_settings
|
||||
}
|
||||
|
||||
pub fn save_to_file_warn(&self) {
|
||||
|
Loading…
Reference in New Issue
Block a user