From 515039c62db2680a3fdf8aff1b0d7c71c370c75c Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Tue, 16 Apr 2019 10:06:23 -0600 Subject: [PATCH] Add auto generation of settings.toml if it does not already exist Former-commit-id: 941b81a8faeff89096256666c56ae472d9b569d5 --- .gitignore | 1 + voxygen/Settings.toml | 17 ----------------- voxygen/src/main.rs | 6 +++++- voxygen/src/settings.rs | 14 ++++++++++++-- 4 files changed, 18 insertions(+), 20 deletions(-) delete mode 100644 voxygen/Settings.toml diff --git a/.gitignore b/.gitignore index fbac64f787..96ef2e499f 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,7 @@ # Veloren **/server_conf.toml **/keybinds.toml +**/settings.toml assets/voxygen *.rar diff --git a/voxygen/Settings.toml b/voxygen/Settings.toml deleted file mode 100644 index 238f3fd281..0000000000 --- a/voxygen/Settings.toml +++ /dev/null @@ -1,17 +0,0 @@ -[controls] -toggle_cursor = "tab" -escape = "escape" -enter = "return" -move_forward = "w" -move_left = "a" -move_back = "s" -move_right = "d" -map = "m" -bag = "b" -quest_log = "l" -character_window = "c" -social = "o" -spellbook = "p" -settings = "n" -help = "f1" -interface = "f2" diff --git a/voxygen/src/main.rs b/voxygen/src/main.rs index 41e5bd041a..b31bef5e61 100644 --- a/voxygen/src/main.rs +++ b/voxygen/src/main.rs @@ -77,7 +77,11 @@ fn main() { // Set up the global state let settings = match Settings::load() { Ok(settings) => settings, - Err(err) => Settings::default(), + Err(err) => { + let settings = Settings::default(); + settings.save_to_file(); + settings + }, }; let window = Window::new(&settings).expect("Failed to create window"); diff --git a/voxygen/src/settings.rs b/voxygen/src/settings.rs index 3678b879f4..d72e5c5da4 100644 --- a/voxygen/src/settings.rs +++ b/voxygen/src/settings.rs @@ -3,8 +3,10 @@ use config::{ ConfigError, }; use serde_derive::{Serialize, Deserialize}; - use glutin::VirtualKeyCode; +use toml; +use std::fs::File; +use std::io::prelude::*; /// Settings contains everything that can be configured in the Settings.toml file #[derive(Clone, Debug, Serialize, Deserialize)] @@ -56,9 +58,17 @@ impl Settings { }, } } + pub fn load() -> Result { let mut config = Config::new(); - config.merge(config::File::with_name("Settings"))?; + config.merge(config::File::with_name("settings"))?; config.try_into() } + + pub fn save_to_file(&self) -> std::io::Result<()> { + let mut config_file = File::create("settings.toml")?; + let s: &str = &toml::to_string_pretty(self).unwrap(); + config_file.write_all(s.as_bytes()).unwrap(); + Ok(()) + } }