Merge branch 'confdir' into 'master'

Use the directory defined by the OS to store configuration files

See merge request veloren/veloren!72

Former-commit-id: 57968e44d31294b32095c97d351ecabcaf63e2fd
This commit is contained in:
Joshua Barretto 2019-04-25 16:51:28 +00:00
commit 837ba99d2a
3 changed files with 39 additions and 15 deletions

11
Cargo.lock generated
View File

@ -516,6 +516,15 @@ dependencies = [
"syn 0.15.32 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "directories"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "dirs"
version = "1.0.5"
@ -2319,6 +2328,7 @@ dependencies = [
"config 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)",
"conrod_core 0.63.0 (git+https://gitlab.com/veloren/conrod.git)",
"conrod_winit 0.63.0 (git+https://gitlab.com/veloren/conrod.git)",
"directories 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"dot_vox 4.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"euc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2602,6 +2612,7 @@ dependencies = [
"checksum daggy 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9293a0da7d1bc1f30090ece4d9f9de79a07be7302ddb00e5eb1fefb6ee6409e2"
"checksum deflate 0.7.19 (registry+https://github.com/rust-lang/crates.io-index)" = "8a6abb26e16e8d419b5c78662aa9f82857c2386a073da266840e474d5055ec86"
"checksum derivative 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6073e9676dbebdddeabaeb63e3b7cefd23c86f5c41d381ee1237cc77b1079898"
"checksum directories 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "72d337a64190607d4fcca2cb78982c5dd57f4916e19696b48a575fa746b6cb0f"
"checksum dirs 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3fd78930633bd1c6e35c4b42b1df7b0cbc6bc191146e512bb3bedf243fcc3901"
"checksum dlib 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "77e51249a9d823a4cb79e3eca6dcd756153e8ed0157b6c04775d04bf1b13b76a"
"checksum dot_vox 4.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "11afd3251e588f2770226659b2a1d55ec2f8aaf2ca42bdcdbd01ff53b4a81e70"

View File

@ -45,3 +45,4 @@ guillotiere = "0.4"
fnv = "1.0"
simplelog = "0.5"
msgbox = "0.1"
directories = "1.0"

View File

@ -1,15 +1,9 @@
use config::{
Config,
ConfigError,
};
use serde_derive::{Serialize, Deserialize};
use config::{Config, ConfigError};
use directories::ProjectDirs;
use glutin::VirtualKeyCode;
use serde_derive::{Deserialize, Serialize};
use std::{fs::File, io::prelude::*, path::PathBuf};
use toml;
use std::{
io::prelude::*,
fs::File,
path::PathBuf,
};
/// Settings contains everything that can be configured in the Settings.toml file
#[derive(Clone, Debug, Serialize, Deserialize)]
@ -76,7 +70,7 @@ impl Default for Settings {
},
networking: NetworkingSettings {
username: "Username".to_string(),
servers: vec!("server.veloren.net".to_string()),
servers: vec!["server.veloren.net".to_string()],
default_server: 0,
},
log: Log {
@ -84,21 +78,39 @@ impl Default for Settings {
},
}
}
}
impl Settings {
pub fn load() -> Result<Self, ConfigError> {
let mut config = Config::new();
config.merge(Config::try_from(&Settings::default())?);
config.merge(config::File::with_name("settings"))?;
config.merge(
Config::try_from(&Settings::default())
.expect("Default settings struct could not be converted to Config"),
);
let path = Settings::get_settings_path();
config.merge::<config::File<config::FileSourceFile>>(path.into())?;
config.try_into()
}
pub fn save_to_file(&self) -> std::io::Result<()> {
let mut config_file = File::create("settings.toml")?;
let path = Settings::get_settings_path();
let mut config_file = File::create(path)?;
let s: &str = &toml::to_string_pretty(self).unwrap();
config_file.write_all(s.as_bytes()).unwrap();
Ok(())
}
fn get_settings_path() -> PathBuf {
let proj_dirs =
ProjectDirs::from("net", "veloren", "voxygen").expect("No home directory defined.");
let path = proj_dirs.config_dir();
path.join("settings");
let path = path.with_extension("toml");
path
}
}