veloren/server/src/settings.rs

52 lines
1.5 KiB
Rust
Raw Normal View History

2019-06-29 16:41:26 +00:00
use serde_derive::{Deserialize, Serialize};
use std::{fs, io::prelude::*, path::PathBuf, net::SocketAddr};
/// `ControlSettings` contains keybindings.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default)]
pub struct ServerSettings {
pub address: SocketAddr,
//pub max_players: u64,
pub world_seed: u32,
//pub pvp_enabled: bool,
//pub serverinfo: whatever
//pub login_server: whatever
}
impl Default for ServerSettings {
fn default() -> Self {
Self {
address: SocketAddr::from(([0; 4], 59003)),
world_seed: 1337,
}
}
}
impl ServerSettings {
pub fn load() -> Self {
let path = ServerSettings::get_settings_path();
// If file doesn't exist, use the default settings.
if let Ok(file) = fs::File::open(path) {
// TODO: Replace expect with returning default?
ron::de::from_reader(file).expect("Error parsing settings")
} else {
// TODO: temporary
Self::default().save_to_file().unwrap();
Self::default()
}
}
pub fn save_to_file(&self) -> std::io::Result<()> {
let path = ServerSettings::get_settings_path();
let mut config_file = fs::File::create(path)?;
let s: &str = &ron::ser::to_string_pretty(self, ron::ser::PrettyConfig::default()).unwrap();
config_file.write_all(s.as_bytes()).unwrap();
Ok(())
}
fn get_settings_path() -> PathBuf {
PathBuf::from(r"settings.ron")
}
}