Add auto generation of settings.toml if it does not already exist

Former-commit-id: 941b81a8faeff89096256666c56ae472d9b569d5
This commit is contained in:
Louis Pearson 2019-04-16 10:06:23 -06:00
parent 9592eb67c5
commit 515039c62d
4 changed files with 18 additions and 20 deletions

1
.gitignore vendored
View File

@ -17,6 +17,7 @@
# Veloren
**/server_conf.toml
**/keybinds.toml
**/settings.toml
assets/voxygen
*.rar

View File

@ -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"

View File

@ -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");

View File

@ -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<Self, ConfigError> {
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(())
}
}