From 92e3ff4a3491a75084d71a981cc11d29b048f370 Mon Sep 17 00:00:00 2001 From: Songtronix Date: Sat, 29 Jun 2019 18:41:26 +0200 Subject: [PATCH] add customizable port and worldseed --- Cargo.lock | 5 +++- server-cli/Cargo.toml | 2 +- server-cli/src/main.rs | 9 +++++-- server/Cargo.toml | 4 +++ server/src/lib.rs | 16 +++++++----- server/src/settings.rs | 52 +++++++++++++++++++++++++++++++++++++ voxygen/src/singleplayer.rs | 4 +-- 7 files changed, 79 insertions(+), 13 deletions(-) create mode 100644 server/src/settings.rs diff --git a/Cargo.lock b/Cargo.lock index 463af2c31a..2e207bd8b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2765,8 +2765,11 @@ name = "veloren-server" version = "0.2.0" dependencies = [ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "ron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "scan_fmt 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)", "specs 0.14.3 (registry+https://github.com/rust-lang/crates.io-index)", "uvth 3.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "vek 0.9.8 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/server-cli/Cargo.toml b/server-cli/Cargo.toml index 62ad0c6368..7e32d07f5f 100644 --- a/server-cli/Cargo.toml +++ b/server-cli/Cargo.toml @@ -9,4 +9,4 @@ server = { package = "veloren-server", path = "../server" } common = { package = "veloren-common", path = "../common" } log = "0.4" -pretty_env_logger = "0.3" +pretty_env_logger = "0.3" \ No newline at end of file diff --git a/server-cli/src/main.rs b/server-cli/src/main.rs index d53aca5d91..41f4949812 100644 --- a/server-cli/src/main.rs +++ b/server-cli/src/main.rs @@ -1,6 +1,6 @@ use common::clock::Clock; use log::info; -use server::{Event, Input, Server}; +use server::{Event, Input, Server, ServerSettings}; use std::time::Duration; const TPS: u64 = 30; @@ -13,9 +13,12 @@ fn main() { // Set up an fps clock let mut clock = Clock::start(); + + // Load settings + let settings = ServerSettings::load(); // Create server - let mut server = Server::new().expect("Failed to create server instance!"); + let mut server = Server::new(settings).expect("Failed to create server instance!"); loop { let events = server @@ -36,4 +39,6 @@ fn main() { // Wait for the next tick. clock.tick(Duration::from_millis(1000 / TPS)); } + + //settings.save_to_file().expect("failed to save the settings!"); } diff --git a/server/Cargo.toml b/server/Cargo.toml index bd5ab709cc..7fed15056d 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -14,3 +14,7 @@ vek = "0.9" uvth = "3.1.0" lazy_static = "1.3.0" scan_fmt = "0.1.3" + +ron = "0.5.1" +serde = "1.0" +serde_derive = "1.0" \ No newline at end of file diff --git a/server/src/lib.rs b/server/src/lib.rs index a0afc058c2..3ba39b0c29 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -4,9 +4,10 @@ pub mod client; pub mod cmd; pub mod error; pub mod input; +pub mod settings; // Reexports -pub use crate::{error::Error, input::Input}; +pub use crate::{error::Error, input::Input, settings::ServerSettings}; use crate::{ client::{Client, Clients}, @@ -36,8 +37,6 @@ use world::World; const CLIENT_TIMEOUT: f64 = 20.0; // Seconds -const DEFAULT_WORLD_SEED: u32 = 1337; - pub enum Event { ClientConnected { entity: EcsEntity, @@ -66,19 +65,20 @@ pub struct Server { chunk_rx: mpsc::Receiver<(Vec2, TerrainChunk)>, pending_chunks: HashSet>, + server_settings: ServerSettings, server_info: ServerInfo, } impl Server { /// Create a new `Server` bound to the default socket. #[allow(dead_code)] - pub fn new() -> Result { - Self::bind(SocketAddr::from(([0; 4], 59003))) + pub fn new(settings: ServerSettings) -> Result { + Self::bind(settings.address, settings) } /// Create a new server bound to the given socket. #[allow(dead_code)] - pub fn bind>(addrs: A) -> Result { + pub fn bind>(addrs: A, settings: ServerSettings) -> Result { let (chunk_tx, chunk_rx) = mpsc::channel(); let mut state = State::default(); @@ -88,7 +88,7 @@ impl Server { let this = Self { state, - world: Arc::new(World::generate(DEFAULT_WORLD_SEED)), + world: Arc::new(World::generate(settings.world_seed)), postoffice: PostOffice::bind(addrs.into())?, clients: Clients::empty(), @@ -100,7 +100,9 @@ impl Server { chunk_rx, pending_chunks: HashSet::new(), + server_settings: settings, server_info: ServerInfo { + // TODO: get from settings name: "Server name".to_owned(), description: "This is the best Veloren server.".to_owned(), }, diff --git a/server/src/settings.rs b/server/src/settings.rs new file mode 100644 index 0000000000..41f32902b2 --- /dev/null +++ b/server/src/settings.rs @@ -0,0 +1,52 @@ +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") + } +} \ No newline at end of file diff --git a/voxygen/src/singleplayer.rs b/voxygen/src/singleplayer.rs index ce300a3d5b..18975ad6d9 100644 --- a/voxygen/src/singleplayer.rs +++ b/voxygen/src/singleplayer.rs @@ -2,7 +2,7 @@ use client::Client; use common::clock::Clock; use log::info; use portpicker::pick_unused_port; -use server::{Event, Input, Server}; +use server::{Event, Input, Server, ServerSettings}; use std::{ net::SocketAddr, sync::mpsc::{channel, Receiver, Sender, TryRecvError}, @@ -36,7 +36,7 @@ impl Singleplayer { )); // Create server - let server = Server::bind(sock.clone()).expect("Failed to create server instance!"); + let server = Server::bind(sock.clone(), ServerSettings::default()).expect("Failed to create server instance!"); let server = match client { Some(client) => server.with_thread_pool(client.thread_pool().clone()),