2021-08-10 13:55:58 +00:00
|
|
|
use common::clock::Clock;
|
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-08-10 13:55:58 +00:00
|
|
|
atomic::{AtomicBool, 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-08-10 13:55:58 +00:00
|
|
|
use tracing::{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<()>,
|
2022-08-25 04:43:14 +00:00
|
|
|
pub receiver: Receiver<Result<common_state::Pools, 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 {
|
2022-09-13 03:26:22 +00:00
|
|
|
pub fn new() -> Self {
|
2021-02-19 09:55:43 +00:00
|
|
|
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
|
|
|
|
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");
|
2021-08-11 07:42:29 +00:00
|
|
|
|
2022-09-13 03:26:22 +00:00
|
|
|
let pools = common_state::State::pools(
|
|
|
|
common::resources::GameMode::Singleplayer,
|
|
|
|
tokio::runtime::Builder::new_multi_thread(),
|
|
|
|
);
|
2021-08-11 07:42:29 +00:00
|
|
|
let (server, init_result) = match Server::new(
|
|
|
|
settings2,
|
|
|
|
editable_settings,
|
|
|
|
database_settings,
|
|
|
|
&server_data_dir,
|
2022-08-25 04:43:14 +00:00
|
|
|
pools.clone(),
|
2021-02-18 00:01:57 +00:00
|
|
|
) {
|
2022-08-25 04:43:14 +00:00
|
|
|
Ok(server) => (Some(server), Ok(pools)),
|
2021-08-11 07:42:29 +00:00
|
|
|
Err(err) => (None, Err(err)),
|
|
|
|
};
|
|
|
|
|
|
|
|
match (result_sender.send(init_result), server) {
|
|
|
|
(Err(e), _) => warn!(
|
2021-02-18 00:01:57 +00:00
|
|
|
?e,
|
|
|
|
"Failed to send singleplayer server initialization result. Most likely \
|
|
|
|
the channel was closed by cancelling server creation. Stopping Server"
|
2021-08-11 07:42:29 +00:00
|
|
|
),
|
|
|
|
(Ok(()), None) => (),
|
|
|
|
(Ok(()), Some(server)) => run_server(server, stop_server_r, paused1),
|
|
|
|
}
|
2019-10-18 20:05:37 +00:00
|
|
|
|
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...");
|
|
|
|
|
2022-09-14 08:00:01 +00:00
|
|
|
Arc::clone(&server.runtime()).block_on(async {
|
2019-04-17 13:41:34 +00:00
|
|
|
// 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.
|
2022-09-14 08:00:01 +00:00
|
|
|
clock.tick().await;
|
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();
|
|
|
|
}
|
2022-09-14 08:00:01 +00:00
|
|
|
});
|
2019-04-17 13:41:34 +00:00
|
|
|
}
|