2021-05-04 19:41:08 +00:00
|
|
|
use common::{clock::Clock, consts::MIN_RECOMMENDED_TOKIO_THREADS};
|
2021-05-31 17:03:18 +00:00
|
|
|
use crossbeam_channel::{bounded, unbounded, Receiver, Sender, TryRecvError};
|
2021-04-13 22:05:47 +00:00
|
|
|
use server::{
|
|
|
|
persistence::{DatabaseSettings, SqlLogMode},
|
|
|
|
Error as ServerError, Event, Input, Server,
|
|
|
|
};
|
2019-05-06 14:26:10 +00:00
|
|
|
use std::{
|
2020-03-04 20:42:22 +00:00
|
|
|
sync::{
|
2021-02-19 09:55:43 +00:00
|
|
|
atomic::{AtomicBool, AtomicUsize, Ordering},
|
2020-03-04 20:42:22 +00:00
|
|
|
Arc,
|
|
|
|
},
|
2019-05-06 14:26:10 +00:00
|
|
|
thread::{self, JoinHandle},
|
2019-05-09 17:58:16 +00:00
|
|
|
time::Duration,
|
2019-05-06 14:26:10 +00:00
|
|
|
};
|
2021-02-19 09:55:43 +00:00
|
|
|
use tokio::runtime::Runtime;
|
|
|
|
use tracing::{debug, error, info, trace, warn};
|
2019-04-17 13:41:34 +00:00
|
|
|
|
|
|
|
const TPS: u64 = 30;
|
|
|
|
|
2019-04-17 15:22:26 +00:00
|
|
|
/// Used to start and stop the background thread running the server
|
|
|
|
/// when in singleplayer mode.
|
2019-04-17 14:57:23 +00:00
|
|
|
pub struct Singleplayer {
|
2019-07-02 21:29:38 +00:00
|
|
|
_server_thread: JoinHandle<()>,
|
2021-02-19 09:55:43 +00:00
|
|
|
stop_server_s: Sender<()>,
|
|
|
|
pub receiver: Receiver<Result<Arc<Runtime>, ServerError>>,
|
2020-03-03 19:51:15 +00:00
|
|
|
// Wether the server is stopped or not
|
2020-03-04 20:42:22 +00:00
|
|
|
paused: Arc<AtomicBool>,
|
2020-10-26 04:15:30 +00:00
|
|
|
// Settings that the server was started with
|
|
|
|
settings: server::Settings,
|
2019-04-17 14:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Singleplayer {
|
2021-02-19 09:55:43 +00:00
|
|
|
#[allow(clippy::new_without_default)]
|
|
|
|
pub fn new() -> Self {
|
|
|
|
let (stop_server_s, stop_server_r) = unbounded();
|
2019-05-06 14:26:10 +00:00
|
|
|
|
2020-10-05 07:41:58 +00:00
|
|
|
// Determine folder to save server data in
|
2020-10-06 02:59:47 +00:00
|
|
|
let server_data_dir = {
|
2021-03-08 22:40:02 +00:00
|
|
|
let mut path = common_base::userdata_dir_workspace!();
|
2020-10-05 07:41:58 +00:00
|
|
|
path.push("singleplayer");
|
|
|
|
path
|
2020-10-06 02:59:47 +00:00
|
|
|
};
|
2020-10-05 07:41:58 +00:00
|
|
|
|
2020-10-10 23:23:41 +00:00
|
|
|
// Copy saves from old folder if they don't exist in the new location
|
|
|
|
(|| {
|
|
|
|
let new_path = server_data_dir.join("saves");
|
|
|
|
if new_path.exists() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let working_dir = std::path::PathBuf::from("saves");
|
|
|
|
let config_dir = directories_next::ProjectDirs::from("net", "veloren", "voxygen")
|
|
|
|
.expect("System's $HOME directory path not found!")
|
|
|
|
.config_dir()
|
|
|
|
.join("saves");
|
|
|
|
let old_path = if working_dir.exists() {
|
|
|
|
working_dir
|
|
|
|
} else if config_dir.exists() {
|
|
|
|
config_dir
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
info!(
|
2021-04-14 06:15:51 +00:00
|
|
|
"Saves folder doesn't exist, but there is one in the old saves location ({}), \
|
|
|
|
copying it to the new location",
|
|
|
|
old_path.display()
|
2020-10-10 23:23:41 +00:00
|
|
|
);
|
|
|
|
if let Some(parent) = new_path.parent() {
|
|
|
|
if let Err(e) = std::fs::create_dir_all(parent) {
|
|
|
|
error!(?e, "Could not create folder to hold saves folder.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Err(e) = copy_dir::copy_dir(old_path, new_path) {
|
|
|
|
error!(?e, "Failed to copy saves from the old location");
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
2019-05-21 22:31:17 +00:00
|
|
|
// Create server
|
2020-10-06 02:59:47 +00:00
|
|
|
let settings = server::Settings::singleplayer(&server_data_dir);
|
|
|
|
let editable_settings = server::EditableSettings::singleplayer(&server_data_dir);
|
2019-06-05 13:13:24 +00:00
|
|
|
|
2021-05-04 19:41:08 +00:00
|
|
|
// TODO: evaluate std::thread::available_concurrency as a num_cpus replacement
|
2021-02-19 09:55:43 +00:00
|
|
|
let cores = num_cpus::get();
|
|
|
|
debug!("Creating a new runtime for server");
|
|
|
|
let runtime = Arc::new(
|
|
|
|
tokio::runtime::Builder::new_multi_thread()
|
|
|
|
.enable_all()
|
2021-05-04 19:41:08 +00:00
|
|
|
.worker_threads((cores / 4).max(MIN_RECOMMENDED_TOKIO_THREADS))
|
2021-02-19 09:55:43 +00:00
|
|
|
.thread_name_fn(|| {
|
|
|
|
static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0);
|
|
|
|
let id = ATOMIC_ID.fetch_add(1, Ordering::SeqCst);
|
|
|
|
format!("tokio-sp-{}", id)
|
|
|
|
})
|
|
|
|
.build()
|
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
|
2019-10-18 20:05:37 +00:00
|
|
|
let settings2 = settings.clone();
|
2019-05-21 22:31:17 +00:00
|
|
|
|
2021-04-13 22:05:47 +00:00
|
|
|
// Relative to data_dir
|
|
|
|
const PERSISTENCE_DB_DIR: &str = "saves";
|
|
|
|
|
|
|
|
let database_settings = DatabaseSettings {
|
|
|
|
db_dir: server_data_dir.join(PERSISTENCE_DB_DIR),
|
|
|
|
sql_log_mode: SqlLogMode::Disabled, /* Voxygen doesn't take in command-line arguments
|
|
|
|
* so SQL logging can't be enabled for
|
|
|
|
* singleplayer without changing this line
|
|
|
|
* manually */
|
|
|
|
};
|
|
|
|
|
2020-03-04 20:42:22 +00:00
|
|
|
let paused = Arc::new(AtomicBool::new(false));
|
2020-09-27 16:20:40 +00:00
|
|
|
let paused1 = Arc::clone(&paused);
|
2020-03-04 20:42:22 +00:00
|
|
|
|
2020-07-21 09:59:00 +00:00
|
|
|
let (result_sender, result_receiver) = bounded(1);
|
|
|
|
|
2021-02-18 00:01:57 +00:00
|
|
|
let builder = thread::Builder::new().name("singleplayer-server-thread".into());
|
|
|
|
let thread = builder
|
|
|
|
.spawn(move || {
|
|
|
|
trace!("starting singleplayer server thread");
|
|
|
|
let mut server = None;
|
|
|
|
if let Err(e) = result_sender.send(
|
2021-02-19 09:55:43 +00:00
|
|
|
match Server::new(
|
|
|
|
settings2,
|
|
|
|
editable_settings,
|
2021-04-13 22:05:47 +00:00
|
|
|
database_settings,
|
2021-02-19 09:55:43 +00:00
|
|
|
&server_data_dir,
|
|
|
|
Arc::clone(&runtime),
|
|
|
|
) {
|
2021-02-18 00:01:57 +00:00
|
|
|
Ok(s) => {
|
|
|
|
server = Some(s);
|
2021-02-19 09:55:43 +00:00
|
|
|
Ok(runtime)
|
2021-02-18 00:01:57 +00:00
|
|
|
},
|
|
|
|
Err(e) => Err(e),
|
2020-10-06 02:59:47 +00:00
|
|
|
},
|
2021-02-18 00:01:57 +00:00
|
|
|
) {
|
|
|
|
warn!(
|
|
|
|
?e,
|
|
|
|
"Failed to send singleplayer server initialization result. Most likely \
|
|
|
|
the channel was closed by cancelling server creation. Stopping Server"
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
};
|
2019-10-18 20:05:37 +00:00
|
|
|
|
2021-02-18 00:01:57 +00:00
|
|
|
let server = match server {
|
|
|
|
Some(s) => s,
|
|
|
|
None => return,
|
|
|
|
};
|
2019-10-18 20:05:37 +00:00
|
|
|
|
2021-02-19 09:55:43 +00:00
|
|
|
run_server(server, stop_server_r, paused1);
|
2021-02-18 00:01:57 +00:00
|
|
|
trace!("ending singleplayer server thread");
|
|
|
|
})
|
|
|
|
.unwrap();
|
2019-05-06 14:26:10 +00:00
|
|
|
|
2020-10-26 04:15:30 +00:00
|
|
|
Singleplayer {
|
|
|
|
_server_thread: thread,
|
2021-02-19 09:55:43 +00:00
|
|
|
stop_server_s,
|
2020-10-26 04:15:30 +00:00
|
|
|
receiver: result_receiver,
|
|
|
|
paused,
|
2019-10-11 12:19:55 +00:00
|
|
|
settings,
|
2020-10-26 04:15:30 +00:00
|
|
|
}
|
2019-04-17 14:57:23 +00:00
|
|
|
}
|
2020-03-01 22:18:22 +00:00
|
|
|
|
2020-10-26 04:15:30 +00:00
|
|
|
/// Returns reference to the settings the server was started with
|
|
|
|
pub fn settings(&self) -> &server::Settings { &self.settings }
|
|
|
|
|
2020-03-03 19:51:15 +00:00
|
|
|
/// Returns wether or not the server is paused
|
2020-03-03 21:26:59 +00:00
|
|
|
pub fn is_paused(&self) -> bool { self.paused.load(Ordering::SeqCst) }
|
2020-03-03 19:51:15 +00:00
|
|
|
|
|
|
|
/// Pauses if true is passed and unpauses if false (Does nothing if in that
|
|
|
|
/// state already)
|
2020-03-04 20:42:22 +00:00
|
|
|
pub fn pause(&self, state: bool) { self.paused.store(state, Ordering::SeqCst); }
|
2019-04-17 14:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Singleplayer {
|
|
|
|
fn drop(&mut self) {
|
2019-05-17 07:55:51 +00:00
|
|
|
// Ignore the result
|
2021-02-19 09:55:43 +00:00
|
|
|
let _ = self.stop_server_s.send(());
|
2019-04-17 14:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 09:55:43 +00:00
|
|
|
fn run_server(mut server: Server, stop_server_r: Receiver<()>, paused: Arc<AtomicBool>) {
|
2019-04-17 13:41:34 +00:00
|
|
|
info!("Starting server-cli...");
|
|
|
|
|
|
|
|
// Set up an fps clock
|
2020-11-12 02:47:22 +00:00
|
|
|
let mut clock = Clock::new(Duration::from_secs_f64(1.0 / TPS as f64));
|
2019-07-05 11:58:52 +00:00
|
|
|
|
2019-04-17 13:41:34 +00:00
|
|
|
loop {
|
2020-03-03 19:51:15 +00:00
|
|
|
// Check any event such as stopping and pausing
|
2021-02-19 09:55:43 +00:00
|
|
|
match stop_server_r.try_recv() {
|
|
|
|
Ok(()) => break,
|
|
|
|
Err(TryRecvError::Disconnected) => break,
|
|
|
|
Err(TryRecvError::Empty) => (),
|
2020-03-01 22:18:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 19:51:15 +00:00
|
|
|
// Wait for the next tick.
|
2020-11-10 12:30:01 +00:00
|
|
|
clock.tick();
|
2020-03-03 19:51:15 +00:00
|
|
|
|
|
|
|
// Skip updating the server if it's paused
|
2020-03-04 20:42:22 +00:00
|
|
|
if paused.load(Ordering::SeqCst) && server.number_of_players() < 2 {
|
2020-03-01 22:18:22 +00:00
|
|
|
continue;
|
2020-03-04 20:42:22 +00:00
|
|
|
} else if server.number_of_players() > 1 {
|
|
|
|
paused.store(false, Ordering::SeqCst);
|
2020-03-01 22:18:22 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 20:37:19 +00:00
|
|
|
let events = server
|
2020-11-10 12:30:01 +00:00
|
|
|
.tick(Input::default(), clock.dt())
|
2019-05-17 09:22:32 +00:00
|
|
|
.expect("Failed to tick server!");
|
2019-04-17 13:41:34 +00:00
|
|
|
|
|
|
|
for event in events {
|
|
|
|
match event {
|
2019-07-02 21:29:38 +00:00
|
|
|
Event::ClientConnected { .. } => info!("Client connected!"),
|
|
|
|
Event::ClientDisconnected { .. } => info!("Client disconnected!"),
|
|
|
|
Event::Chat { entity: _, msg } => info!("[Client] {}", msg),
|
2019-04-17 13:41:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Clean up the server after a tick.
|
2019-04-17 13:41:34 +00:00
|
|
|
server.cleanup();
|
|
|
|
}
|
|
|
|
}
|