2019-06-29 16:41:26 +00:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
2019-07-01 09:37:17 +00:00
|
|
|
use std::{fs, io::prelude::*, net::SocketAddr, path::PathBuf};
|
2019-06-29 16:41:26 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
#[serde(default)]
|
|
|
|
pub struct ServerSettings {
|
|
|
|
pub address: SocketAddr,
|
2019-07-01 11:19:26 +00:00
|
|
|
pub max_players: usize,
|
2019-06-29 16:41:26 +00:00
|
|
|
pub world_seed: u32,
|
|
|
|
//pub pvp_enabled: bool,
|
2019-07-01 09:37:17 +00:00
|
|
|
pub server_name: String,
|
|
|
|
pub server_description: String,
|
2019-06-29 16:41:26 +00:00
|
|
|
//pub login_server: whatever
|
2019-07-12 13:03:35 +00:00
|
|
|
pub start_time: f64,
|
2019-06-29 16:41:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ServerSettings {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2019-07-20 11:59:35 +00:00
|
|
|
address: SocketAddr::from(([0; 4], 14004)),
|
2019-06-29 16:41:26 +00:00
|
|
|
world_seed: 1337,
|
2019-07-01 09:37:17 +00:00
|
|
|
server_name: "Server name".to_owned(),
|
|
|
|
server_description: "This is the best Veloren server.".to_owned(),
|
2019-07-01 11:19:26 +00:00
|
|
|
max_players: 16,
|
2019-07-12 13:03:35 +00:00
|
|
|
start_time: 0.0,
|
2019-06-29 16:41:26 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-01 09:37:17 +00:00
|
|
|
}
|
2019-06-29 16:41:26 +00:00
|
|
|
|
|
|
|
impl ServerSettings {
|
|
|
|
pub fn load() -> Self {
|
|
|
|
let path = ServerSettings::get_settings_path();
|
|
|
|
|
|
|
|
if let Ok(file) = fs::File::open(path) {
|
2019-07-04 17:37:56 +00:00
|
|
|
match ron::de::from_reader(file) {
|
|
|
|
Ok(x) => x,
|
|
|
|
Err(e) => {
|
|
|
|
log::warn!("Failed to parse setting file! Fallback to default. {}", e);
|
|
|
|
Self::default()
|
|
|
|
}
|
|
|
|
}
|
2019-06-29 16:41:26 +00:00
|
|
|
} else {
|
2019-07-04 17:37:56 +00:00
|
|
|
let default_settings = Self::default();
|
|
|
|
|
|
|
|
match default_settings.save_to_file() {
|
|
|
|
Err(e) => log::error!("Failed to create default setting file! {}", e),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
default_settings
|
2019-06-29 16:41:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
2019-07-01 09:37:17 +00:00
|
|
|
}
|