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)]
|
2019-08-19 12:39:23 +00:00
|
|
|
|
2020-08-31 08:41:11 +00:00
|
|
|
mod tui_runner;
|
|
|
|
mod tuilog;
|
|
|
|
|
2020-07-27 16:26:34 +00:00
|
|
|
#[macro_use] extern crate lazy_static;
|
|
|
|
|
2020-08-31 08:41:11 +00:00
|
|
|
use crate::{
|
|
|
|
tui_runner::{Message, Tui},
|
|
|
|
tuilog::TuiLog,
|
|
|
|
};
|
2019-01-30 12:11:34 +00:00
|
|
|
use common::clock::Clock;
|
2019-06-29 16:41:26 +00:00
|
|
|
use server::{Event, Input, Server, ServerSettings};
|
2020-08-31 12:38:54 +00:00
|
|
|
use tracing::{info, Level};
|
2020-06-29 08:53:43 +00:00
|
|
|
use tracing_subscriber::{filter::LevelFilter, EnvFilter, FmtSubscriber};
|
2020-08-29 07:00:26 +00:00
|
|
|
#[cfg(feature = "tracy")]
|
|
|
|
use tracing_subscriber::{layer::SubscriberExt, prelude::*};
|
2019-01-30 12:11:34 +00:00
|
|
|
|
2020-07-28 13:47:01 +00:00
|
|
|
use clap::{App, Arg};
|
2020-08-31 08:41:11 +00:00
|
|
|
use std::{io, sync::mpsc, time::Duration};
|
2020-07-27 16:26:34 +00:00
|
|
|
|
2019-03-19 19:53:35 +00:00
|
|
|
const TPS: u64 = 30;
|
2020-06-29 08:53:43 +00:00
|
|
|
const RUST_LOG_ENV: &str = "RUST_LOG";
|
2019-01-30 12:11:34 +00:00
|
|
|
|
2020-07-27 16:26:34 +00:00
|
|
|
lazy_static! {
|
|
|
|
static ref LOG: TuiLog<'static> = TuiLog::default();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-28 13:47:01 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("basic")
|
|
|
|
.short("b")
|
|
|
|
.long("basic")
|
|
|
|
.help("Disables the tui")
|
|
|
|
.takes_value(false),
|
|
|
|
)
|
|
|
|
.get_matches();
|
|
|
|
|
|
|
|
let basic = matches.is_present("basic");
|
2020-08-31 08:41:11 +00:00
|
|
|
let (mut tui, msg_r) = Tui::new();
|
2020-07-28 13:47:01 +00:00
|
|
|
|
2019-01-30 12:11:34 +00:00
|
|
|
// Init logging
|
2020-09-16 08:13:37 +00:00
|
|
|
let base_exceptions = |env: EnvFilter| {
|
|
|
|
env.add_directive("veloren_world::sim=info".parse().unwrap())
|
|
|
|
.add_directive("veloren_world::civ=info".parse().unwrap())
|
|
|
|
.add_directive("uvth=warn".parse().unwrap())
|
|
|
|
.add_directive("tiny_http=warn".parse().unwrap())
|
|
|
|
.add_directive("mio::sys::windows=debug".parse().unwrap())
|
|
|
|
.add_directive(LevelFilter::INFO.into())
|
|
|
|
};
|
|
|
|
|
2020-08-29 04:33:20 +00:00
|
|
|
#[cfg(not(feature = "tracy"))]
|
2020-06-29 08:53:43 +00:00
|
|
|
let filter = match std::env::var_os(RUST_LOG_ENV).map(|s| s.into_string()) {
|
|
|
|
Some(Ok(env)) => {
|
2020-09-16 08:13:37 +00:00
|
|
|
let mut filter = base_exceptions(EnvFilter::new(""));
|
2020-06-29 08:53:43 +00:00
|
|
|
for s in env.split(',').into_iter() {
|
|
|
|
match s.parse() {
|
|
|
|
Ok(d) => filter = filter.add_directive(d),
|
|
|
|
Err(err) => println!("WARN ignoring log directive: `{}`: {}", s, err),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
filter
|
|
|
|
},
|
2020-09-16 08:13:37 +00:00
|
|
|
_ => base_exceptions(EnvFilter::from_env(RUST_LOG_ENV)),
|
2020-06-29 08:53:43 +00:00
|
|
|
};
|
2020-06-21 14:26:06 +00:00
|
|
|
|
2020-08-29 04:33:20 +00:00
|
|
|
#[cfg(feature = "tracy")]
|
|
|
|
tracing_subscriber::registry()
|
|
|
|
.with(tracing_tracy::TracyLayer::new().with_stackdepth(0))
|
|
|
|
.init();
|
|
|
|
|
|
|
|
#[cfg(not(feature = "tracy"))]
|
2020-08-29 07:00:26 +00:00
|
|
|
// TODO: when tracing gets per Layer filters re-enable this when the tracy feature is being
|
|
|
|
// used (and do the same in voxygen)
|
2020-08-29 04:33:20 +00:00
|
|
|
{
|
|
|
|
let subscriber = FmtSubscriber::builder()
|
|
|
|
.with_max_level(Level::ERROR)
|
|
|
|
.with_env_filter(filter);
|
|
|
|
|
|
|
|
if basic {
|
|
|
|
subscriber.init();
|
|
|
|
} else {
|
|
|
|
subscriber.with_writer(|| LOG.clone()).init();
|
|
|
|
}
|
2020-07-28 13:47:01 +00:00
|
|
|
}
|
2019-01-30 12:11:34 +00:00
|
|
|
|
2020-09-02 11:47:17 +00:00
|
|
|
tui.run(basic);
|
2020-07-28 10:02:48 +00:00
|
|
|
|
|
|
|
info!("Starting server...");
|
|
|
|
|
|
|
|
// Set up an fps clock
|
|
|
|
let mut clock = Clock::start();
|
|
|
|
|
|
|
|
// Load settings
|
|
|
|
let settings = ServerSettings::load();
|
|
|
|
let server_port = &settings.gameserver_address.port();
|
|
|
|
let metrics_port = &settings.metrics_address.port();
|
|
|
|
// Create server
|
|
|
|
let mut server = Server::new(settings).expect("Failed to create server instance!");
|
|
|
|
|
|
|
|
info!("Server is ready to accept connections.");
|
|
|
|
info!(?metrics_port, "starting metrics at port");
|
|
|
|
info!(?server_port, "starting server at port");
|
|
|
|
|
|
|
|
loop {
|
|
|
|
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-08-31 08:41:11 +00:00
|
|
|
match msg_r.try_recv() {
|
2020-07-28 10:02:48 +00:00
|
|
|
Ok(msg) => match msg {
|
|
|
|
Message::Quit => {
|
|
|
|
info!("Closing the server");
|
|
|
|
break;
|
|
|
|
},
|
|
|
|
},
|
2020-09-07 13:50:48 +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(())
|
|
|
|
}
|