2019-04-25 16:24:00 +00:00
|
|
|
use config::{Config, ConfigError};
|
|
|
|
use directories::ProjectDirs;
|
2019-04-15 17:51:26 +00:00
|
|
|
use glutin::VirtualKeyCode;
|
2019-04-25 16:24:00 +00:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
2019-05-20 21:32:16 +00:00
|
|
|
use std::{fs, io::prelude::*, path::PathBuf};
|
2019-04-16 16:06:23 +00:00
|
|
|
use toml;
|
2019-04-15 17:51:26 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
/// `Settings` contains everything that can be configured in the Settings.toml file.
|
2019-04-15 17:51:26 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2019-04-16 17:41:39 +00:00
|
|
|
#[serde(default)]
|
2019-04-15 17:51:26 +00:00
|
|
|
pub struct Settings {
|
|
|
|
pub controls: ControlSettings,
|
2019-04-18 17:40:29 +00:00
|
|
|
pub networking: NetworkingSettings,
|
2019-04-25 11:49:36 +00:00
|
|
|
pub log: Log,
|
2019-05-20 17:40:35 +00:00
|
|
|
pub graphics: GraphicsSettings,
|
2019-05-20 16:54:54 +00:00
|
|
|
pub audio: AudioSettings,
|
2019-04-15 17:51:26 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
/// `ControlSettings` contains keybindings.
|
2019-04-15 17:51:26 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct ControlSettings {
|
|
|
|
pub toggle_cursor: VirtualKeyCode,
|
|
|
|
pub escape: VirtualKeyCode,
|
|
|
|
pub enter: VirtualKeyCode,
|
|
|
|
pub move_forward: VirtualKeyCode,
|
|
|
|
pub move_left: VirtualKeyCode,
|
|
|
|
pub move_back: VirtualKeyCode,
|
|
|
|
pub move_right: VirtualKeyCode,
|
2019-05-01 15:51:25 +00:00
|
|
|
pub jump: VirtualKeyCode,
|
2019-05-13 20:59:42 +00:00
|
|
|
pub glide: VirtualKeyCode,
|
2019-04-15 17:51:26 +00:00
|
|
|
pub map: VirtualKeyCode,
|
|
|
|
pub bag: VirtualKeyCode,
|
|
|
|
pub quest_log: VirtualKeyCode,
|
|
|
|
pub character_window: VirtualKeyCode,
|
|
|
|
pub social: VirtualKeyCode,
|
|
|
|
pub spellbook: VirtualKeyCode,
|
|
|
|
pub settings: VirtualKeyCode,
|
|
|
|
pub help: VirtualKeyCode,
|
2019-04-20 15:17:29 +00:00
|
|
|
pub toggle_interface: VirtualKeyCode,
|
2019-05-18 21:16:35 +00:00
|
|
|
pub toggle_debug: VirtualKeyCode,
|
|
|
|
pub fullscreen: VirtualKeyCode,
|
|
|
|
pub screenshot: VirtualKeyCode,
|
2019-05-21 04:55:20 +00:00
|
|
|
pub toggle_ingame_ui: VirtualKeyCode,
|
2019-05-25 02:30:56 +00:00
|
|
|
pub pan_sensitivity: f32,
|
|
|
|
pub zoom_sensitivity: f32,
|
2019-04-15 17:51:26 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 17:40:29 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct NetworkingSettings {
|
|
|
|
pub username: String,
|
|
|
|
pub servers: Vec<String>,
|
|
|
|
pub default_server: usize,
|
|
|
|
}
|
|
|
|
|
2019-04-25 11:49:36 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Log {
|
|
|
|
pub file: PathBuf,
|
|
|
|
}
|
|
|
|
|
2019-05-20 17:40:35 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct GraphicsSettings {
|
|
|
|
pub view_distance: u32,
|
|
|
|
}
|
|
|
|
|
2019-05-20 16:54:54 +00:00
|
|
|
/// AudioSettings controls the volume of different audio subsystems and which
|
2019-05-22 11:29:38 +00:00
|
|
|
/// device is used.
|
2019-05-20 16:54:54 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct AudioSettings {
|
|
|
|
pub music_volume: f32,
|
|
|
|
pub sfx_volume: f32,
|
|
|
|
|
2019-05-22 11:29:38 +00:00
|
|
|
/// Audio Device that Voxygen will use to play audio.
|
|
|
|
pub audio_device: Option<String>,
|
2019-05-20 16:54:54 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 17:41:39 +00:00
|
|
|
impl Default for Settings {
|
|
|
|
fn default() -> Self {
|
2019-04-16 08:56:47 +00:00
|
|
|
Settings {
|
|
|
|
controls: ControlSettings {
|
|
|
|
toggle_cursor: VirtualKeyCode::Tab,
|
|
|
|
escape: VirtualKeyCode::Escape,
|
|
|
|
enter: VirtualKeyCode::Return,
|
|
|
|
move_forward: VirtualKeyCode::W,
|
|
|
|
move_left: VirtualKeyCode::A,
|
|
|
|
move_back: VirtualKeyCode::S,
|
|
|
|
move_right: VirtualKeyCode::D,
|
2019-05-01 15:51:25 +00:00
|
|
|
jump: VirtualKeyCode::Space,
|
2019-05-13 20:59:42 +00:00
|
|
|
glide: VirtualKeyCode::LShift,
|
2019-04-16 08:56:47 +00:00
|
|
|
map: VirtualKeyCode::M,
|
|
|
|
bag: VirtualKeyCode::B,
|
|
|
|
quest_log: VirtualKeyCode::L,
|
|
|
|
character_window: VirtualKeyCode::C,
|
|
|
|
social: VirtualKeyCode::O,
|
|
|
|
spellbook: VirtualKeyCode::P,
|
|
|
|
settings: VirtualKeyCode::N,
|
2019-04-16 17:41:39 +00:00
|
|
|
help: VirtualKeyCode::F1,
|
2019-04-20 15:17:29 +00:00
|
|
|
toggle_interface: VirtualKeyCode::F2,
|
2019-05-18 21:16:35 +00:00
|
|
|
toggle_debug: VirtualKeyCode::F3,
|
|
|
|
fullscreen: VirtualKeyCode::F11,
|
|
|
|
screenshot: VirtualKeyCode::F4,
|
2019-05-21 04:55:20 +00:00
|
|
|
toggle_ingame_ui: VirtualKeyCode::F6,
|
2019-05-25 02:30:56 +00:00
|
|
|
pan_sensitivity: 1.0,
|
|
|
|
zoom_sensitivity: 1.0,
|
2019-04-16 08:56:47 +00:00
|
|
|
},
|
2019-04-18 17:40:29 +00:00
|
|
|
networking: NetworkingSettings {
|
|
|
|
username: "Username".to_string(),
|
2019-04-25 16:24:00 +00:00
|
|
|
servers: vec!["server.veloren.net".to_string()],
|
2019-04-18 17:40:29 +00:00
|
|
|
default_server: 0,
|
|
|
|
},
|
2019-04-25 11:49:36 +00:00
|
|
|
log: Log {
|
|
|
|
file: "voxygen.log".into(),
|
|
|
|
},
|
2019-05-20 17:40:35 +00:00
|
|
|
graphics: GraphicsSettings { view_distance: 5 },
|
2019-05-20 16:54:54 +00:00
|
|
|
audio: AudioSettings {
|
|
|
|
music_volume: 0.5,
|
|
|
|
sfx_volume: 0.5,
|
2019-05-22 11:29:38 +00:00
|
|
|
audio_device: None,
|
2019-05-20 16:54:54 +00:00
|
|
|
},
|
2019-04-16 08:56:47 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-16 17:41:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Settings {
|
2019-05-17 07:55:51 +00:00
|
|
|
pub fn load() -> Self {
|
|
|
|
let default_settings = Settings::default();
|
2019-04-25 16:24:00 +00:00
|
|
|
|
|
|
|
let path = Settings::get_settings_path();
|
|
|
|
|
2019-05-17 07:55:51 +00:00
|
|
|
let mut config = Config::new();
|
|
|
|
|
|
|
|
config
|
|
|
|
.merge(
|
|
|
|
Config::try_from(&default_settings)
|
2019-05-17 09:22:32 +00:00
|
|
|
.expect("Default settings struct could not be converted to Config!"),
|
2019-05-17 07:55:51 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2019-04-25 16:24:00 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// TODO: Log errors here.
|
|
|
|
// If merge or try_into fail, use the default settings.
|
2019-05-17 07:55:51 +00:00
|
|
|
match config.merge::<config::File<_>>(path.into()) {
|
|
|
|
Ok(_) => match config.try_into() {
|
|
|
|
Ok(settings) => settings,
|
|
|
|
Err(_) => default_settings,
|
|
|
|
},
|
|
|
|
Err(_) => {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Maybe the file didn't exist.
|
|
|
|
// TODO: Handle this result.
|
2019-05-17 07:55:51 +00:00
|
|
|
default_settings.save_to_file();
|
|
|
|
default_settings
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 17:51:26 +00:00
|
|
|
}
|
2019-04-16 16:06:23 +00:00
|
|
|
|
|
|
|
pub fn save_to_file(&self) -> std::io::Result<()> {
|
2019-04-25 16:24:00 +00:00
|
|
|
let path = Settings::get_settings_path();
|
|
|
|
|
2019-05-20 21:32:16 +00:00
|
|
|
if let Some(dir) = path.parent() {
|
|
|
|
fs::create_dir_all(dir)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut config_file = fs::File::create(path)?;
|
2019-04-16 16:06:23 +00:00
|
|
|
let s: &str = &toml::to_string_pretty(self).unwrap();
|
|
|
|
config_file.write_all(s.as_bytes()).unwrap();
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-04-25 16:24:00 +00:00
|
|
|
|
|
|
|
fn get_settings_path() -> PathBuf {
|
|
|
|
let proj_dirs =
|
2019-05-17 09:22:32 +00:00
|
|
|
ProjectDirs::from("net", "veloren", "voxygen").expect("No home directory defined!");
|
2019-04-25 16:24:00 +00:00
|
|
|
let path = proj_dirs.config_dir();
|
|
|
|
path.join("settings");
|
|
|
|
let path = path.with_extension("toml");
|
|
|
|
path
|
|
|
|
}
|
2019-04-15 17:51:26 +00:00
|
|
|
}
|