veloren_voxygen/settings/
mod.rs

1use directories_next::UserDirs;
2use serde::{Deserialize, Serialize};
3use std::{
4    fs,
5    path::{Path, PathBuf},
6};
7use tracing::warn;
8
9pub mod audio;
10pub mod chat;
11pub mod control;
12pub mod controller;
13pub mod gameplay;
14pub mod graphics;
15pub mod interface;
16pub mod inventory;
17pub mod language;
18pub mod networking;
19
20pub use audio::{AudioOutput, AudioSettings};
21pub use chat::ChatSettings;
22pub use control::ControlSettings;
23pub use controller::{Button, ControllerSettings};
24pub use gameplay::GameplaySettings;
25pub use graphics::{Fps, GraphicsSettings, get_fps};
26pub use interface::InterfaceSettings;
27pub use inventory::InventorySettings;
28pub use language::LanguageSettings;
29pub use networking::NetworkingSettings;
30
31/// `Settings` contains everything that can be configured in the settings.ron
32/// file.
33#[derive(Clone, Debug, Serialize, Deserialize)]
34#[serde(default)]
35pub struct Settings {
36    pub chat: ChatSettings,
37    pub controls: ControlSettings,
38    pub controller: ControllerSettings,
39    pub interface: InterfaceSettings,
40    pub gameplay: GameplaySettings,
41    pub networking: NetworkingSettings,
42    pub graphics: GraphicsSettings,
43    pub audio: AudioSettings,
44    pub show_disclaimer: bool,
45    pub send_logon_commands: bool,
46    // TODO: Remove at a later date, for dev testing
47    pub logon_commands: Vec<String>,
48    pub language: LanguageSettings,
49    pub screenshots_path: PathBuf,
50    pub inventory: InventorySettings,
51}
52
53impl Default for Settings {
54    fn default() -> Self {
55        let user_dirs = UserDirs::new().expect("System's $HOME directory path not found!");
56
57        // Chooses a path to store the screenshots by the following order:
58        //  - The VOXYGEN_SCREENSHOT environment variable
59        //  - The user's picture directory
60        //  - The executable's directory
61        // This only selects if there isn't already an entry in the settings file
62        let screenshots_path = std::env::var_os("VOXYGEN_SCREENSHOT")
63            .map(PathBuf::from)
64            .or_else(|| user_dirs.picture_dir().map(|dir| dir.join("veloren")))
65            .or_else(|| {
66                std::env::current_exe()
67                    .ok()
68                    .and_then(|dir| dir.parent().map(PathBuf::from))
69            })
70            .expect("Couldn't choose a place to store the screenshots");
71
72        Settings {
73            chat: ChatSettings::default(),
74            controls: ControlSettings::default(),
75            controller: ControllerSettings::default(),
76            interface: InterfaceSettings::default(),
77            gameplay: GameplaySettings::default(),
78            networking: NetworkingSettings::default(),
79            graphics: GraphicsSettings::default(),
80            audio: AudioSettings::default(),
81            show_disclaimer: true,
82            send_logon_commands: false,
83            logon_commands: Vec::new(),
84            language: LanguageSettings::default(),
85            screenshots_path,
86            inventory: InventorySettings::default(),
87        }
88    }
89}
90
91impl Settings {
92    pub fn load(config_dir: &Path) -> Self {
93        let path = Self::get_path(config_dir);
94
95        let settings = common::util::ron_from_path_recoverable::<Self>(&path);
96        // Save settings to add new fields or create the file if it is not already there
97        settings.save_to_file_warn(config_dir);
98        settings
99    }
100
101    pub fn save_to_file_warn(&self, config_dir: &Path) {
102        if let Err(e) = self.save_to_file(config_dir) {
103            warn!(?e, "Failed to save settings");
104        }
105    }
106
107    pub fn save_to_file(&self, config_dir: &Path) -> std::io::Result<()> {
108        let path = Self::get_path(config_dir);
109        if let Some(dir) = path.parent() {
110            fs::create_dir_all(dir)?;
111        }
112
113        let ron = ron::ser::to_string_pretty(self, ron::ser::PrettyConfig::default()).unwrap();
114        fs::write(path, ron.as_bytes())
115    }
116
117    fn get_path(config_dir: &Path) -> PathBuf { config_dir.join("settings.ron") }
118
119    pub fn display_warnings(&self) {
120        if !self.graphics.render_mode.experimental_shaders.is_empty() {
121            warn!(
122                "One or more experimental shaders are enabled, all rendering guarantees are off. \
123                 Experimental shaders may be unmaintained, mutually-incompatible, entirely \
124                 broken, or may cause your GPU to explode. You have been warned!"
125            );
126        }
127    }
128}