2019-08-19 12:39:23 +00:00
|
|
|
#![deny(unsafe_code)]
|
2020-09-27 16:20:40 +00:00
|
|
|
#![deny(clippy::clone_on_ref_ptr)]
|
2020-09-20 02:40:26 +00:00
|
|
|
#![feature(bool_to_option)]
|
2019-08-19 12:39:23 +00:00
|
|
|
|
2020-10-05 07:41:58 +00:00
|
|
|
mod logging;
|
2020-10-05 08:35:24 +00:00
|
|
|
mod settings;
|
2020-10-03 19:10:34 +00:00
|
|
|
mod shutdown_coordinator;
|
2020-08-31 08:41:11 +00:00
|
|
|
mod tui_runner;
|
|
|
|
mod tuilog;
|
|
|
|
|
|
|
|
use crate::{
|
2020-10-03 19:10:34 +00:00
|
|
|
shutdown_coordinator::ShutdownCoordinator,
|
2020-08-31 08:41:11 +00:00
|
|
|
tui_runner::{Message, Tui},
|
|
|
|
};
|
2020-10-05 07:41:58 +00:00
|
|
|
use clap::{App, Arg};
|
2019-01-30 12:11:34 +00:00
|
|
|
use common::clock::Clock;
|
2020-10-05 07:41:58 +00:00
|
|
|
use server::{DataDir, Event, Input, Server, ServerSettings};
|
2020-10-03 19:10:34 +00:00
|
|
|
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
|
|
|
use signal_hook::SIGUSR1;
|
|
|
|
use std::{
|
|
|
|
io,
|
|
|
|
sync::{atomic::AtomicBool, mpsc, Arc},
|
|
|
|
time::Duration,
|
|
|
|
};
|
2020-10-05 07:41:58 +00:00
|
|
|
use tracing::info;
|
2020-07-27 16:26:34 +00:00
|
|
|
|
2019-03-19 19:53:35 +00:00
|
|
|
const TPS: u64 = 30;
|
2020-07-27 16:26:34 +00:00
|
|
|
|
|
|
|
fn main() -> io::Result<()> {
|
2020-07-28 13:47:01 +00:00
|
|
|
let matches = App::new("Veloren server cli")
|
2020-09-16 10:50:55 +00:00
|
|
|
.version(common::util::DISPLAY_VERSION_LONG.as_str())
|
2020-07-28 13:47:01 +00:00
|
|
|
.author("The veloren devs <https://gitlab.com/veloren/veloren>")
|
2020-07-28 15:02:36 +00:00
|
|
|
.about("The veloren server cli provides an easy to use interface to start a veloren server")
|
2020-09-20 04:51:20 +00:00
|
|
|
.args(&[
|
2020-07-28 13:47:01 +00:00
|
|
|
Arg::with_name("basic")
|
|
|
|
.short("b")
|
|
|
|
.long("basic")
|
|
|
|
.help("Disables the tui")
|
|
|
|
.takes_value(false),
|
2020-09-20 02:40:26 +00:00
|
|
|
Arg::with_name("interactive")
|
|
|
|
.short("i")
|
|
|
|
.long("interactive")
|
|
|
|
.help("Enables command input for basic mode")
|
|
|
|
.takes_value(false),
|
2020-09-20 04:51:20 +00:00
|
|
|
Arg::with_name("no-auth")
|
|
|
|
.long("no-auth")
|
|
|
|
.help("Runs without auth enabled"),
|
|
|
|
])
|
2020-07-28 13:47:01 +00:00
|
|
|
.get_matches();
|
|
|
|
|
|
|
|
let basic = matches.is_present("basic");
|
2020-09-20 02:40:26 +00:00
|
|
|
let interactive = matches.is_present("interactive");
|
2020-09-20 04:51:20 +00:00
|
|
|
let no_auth = matches.is_present("no-auth");
|
2020-10-03 19:10:34 +00:00
|
|
|
|
|
|
|
let sigusr1_signal = Arc::new(AtomicBool::new(false));
|
|
|
|
|
|
|
|
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
|
|
|
let _ = signal_hook::flag::register(SIGUSR1, Arc::clone(&sigusr1_signal));
|
|
|
|
|
2020-10-05 07:41:58 +00:00
|
|
|
logging::init(basic);
|
2019-01-30 12:11:34 +00:00
|
|
|
|
2020-10-05 08:35:24 +00:00
|
|
|
// Load settings
|
|
|
|
let settings = settings::Settings::load();
|
|
|
|
|
2020-10-03 19:10:34 +00:00
|
|
|
// Panic hook to ensure that console mode is set back correctly if in non-basic
|
|
|
|
// mode
|
|
|
|
let hook = std::panic::take_hook();
|
|
|
|
std::panic::set_hook(Box::new(move |info| {
|
|
|
|
Tui::shutdown(basic);
|
|
|
|
hook(info);
|
|
|
|
}));
|
|
|
|
|
2020-09-20 02:40:26 +00:00
|
|
|
let tui = (!basic || interactive).then(|| Tui::run(basic));
|
2020-07-28 10:02:48 +00:00
|
|
|
|
|
|
|
info!("Starting server...");
|
|
|
|
|
|
|
|
// Set up an fps clock
|
|
|
|
let mut clock = Clock::start();
|
|
|
|
|
2020-10-05 07:41:58 +00:00
|
|
|
// Determine folder to save server data in
|
|
|
|
let server_data_dir = DataDir::from({
|
|
|
|
let mut path = common::userdata_dir_workspace!();
|
|
|
|
path.push(server::DEFAULT_DATA_DIR_NAME);
|
|
|
|
path
|
|
|
|
});
|
|
|
|
|
2020-10-05 08:35:24 +00:00
|
|
|
// Load server settings
|
|
|
|
let mut server_settings = ServerSettings::load(server_data_dir.as_ref());
|
2020-10-05 07:41:58 +00:00
|
|
|
|
2020-09-20 04:51:20 +00:00
|
|
|
if no_auth {
|
2020-10-05 08:35:24 +00:00
|
|
|
server_settings.auth_server_address = None;
|
2020-09-20 04:51:20 +00:00
|
|
|
}
|
2020-10-05 07:41:58 +00:00
|
|
|
|
2020-10-05 08:35:24 +00:00
|
|
|
let server_port = &server_settings.gameserver_address.port();
|
|
|
|
let metrics_port = &server_settings.metrics_address.port();
|
2020-07-28 10:02:48 +00:00
|
|
|
// Create server
|
2020-10-05 07:41:58 +00:00
|
|
|
let mut server =
|
2020-10-05 08:35:24 +00:00
|
|
|
Server::new(server_settings, server_data_dir).expect("Failed to create server instance!");
|
2020-07-28 10:02:48 +00:00
|
|
|
|
2020-10-03 19:10:34 +00:00
|
|
|
info!(
|
|
|
|
?server_port,
|
|
|
|
?metrics_port,
|
|
|
|
"Server is ready to accept connections."
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut shutdown_coordinator = ShutdownCoordinator::new(Arc::clone(&sigusr1_signal));
|
2020-07-28 10:02:48 +00:00
|
|
|
|
|
|
|
loop {
|
2020-10-03 19:10:34 +00:00
|
|
|
// Terminate the server if instructed to do so by the shutdown coordinator
|
2020-10-05 08:35:24 +00:00
|
|
|
if shutdown_coordinator.check(&mut server, &settings) {
|
2020-10-03 19:10:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-07-28 10:02:48 +00:00
|
|
|
let events = server
|
|
|
|
.tick(Input::default(), clock.get_last_delta())
|
|
|
|
.expect("Failed to tick server");
|
|
|
|
|
|
|
|
for event in events {
|
|
|
|
match event {
|
|
|
|
Event::ClientConnected { entity: _ } => info!("Client connected!"),
|
|
|
|
Event::ClientDisconnected { entity: _ } => info!("Client disconnected!"),
|
|
|
|
Event::Chat { entity: _, msg } => info!("[Client] {}", msg),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up the server after a tick.
|
|
|
|
server.cleanup();
|
2020-08-29 07:00:26 +00:00
|
|
|
#[cfg(feature = "tracy")]
|
|
|
|
common::util::tracy_client::finish_continuous_frame!();
|
2020-07-28 10:02:48 +00:00
|
|
|
|
2020-09-20 02:40:26 +00:00
|
|
|
if let Some(tui) = tui.as_ref() {
|
|
|
|
match tui.msg_r.try_recv() {
|
|
|
|
Ok(msg) => match msg {
|
|
|
|
Message::AbortShutdown => shutdown_coordinator.abort_shutdown(&mut server),
|
|
|
|
Message::Shutdown { grace_period } => {
|
|
|
|
// TODO: The TUI parser doesn't support quoted strings so it is not
|
|
|
|
// currently possible to provide a shutdown reason
|
|
|
|
// from the console.
|
|
|
|
let message = "The server is shutting down".to_owned();
|
|
|
|
shutdown_coordinator.initiate_shutdown(&mut server, grace_period, message);
|
|
|
|
},
|
|
|
|
Message::Quit => {
|
|
|
|
info!("Closing the server");
|
|
|
|
break;
|
|
|
|
},
|
2020-10-03 19:10:34 +00:00
|
|
|
},
|
2020-09-20 02:40:26 +00:00
|
|
|
Err(mpsc::TryRecvError::Empty) | Err(mpsc::TryRecvError::Disconnected) => {},
|
|
|
|
}
|
|
|
|
}
|
2020-07-28 10:02:48 +00:00
|
|
|
|
|
|
|
// Wait for the next tick.
|
|
|
|
clock.tick(Duration::from_millis(1000 / TPS));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|