2019-06-05 13:13:24 +00:00
|
|
|
use client::Client;
|
2019-06-05 15:22:06 +00:00
|
|
|
use common::clock::Clock;
|
2020-07-21 09:59:00 +00:00
|
|
|
use crossbeam::channel::{bounded, unbounded, Receiver, Sender, TryRecvError};
|
2020-10-06 02:59:47 +00:00
|
|
|
use server::{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::{
|
|
|
|
atomic::{AtomicBool, Ordering},
|
|
|
|
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
|
|
|
};
|
2020-10-10 23:23:41 +00:00
|
|
|
use tracing::{error, info, warn};
|
2019-04-17 13:41:34 +00:00
|
|
|
|
|
|
|
const TPS: u64 = 30;
|
|
|
|
|
2019-04-17 14:57:23 +00:00
|
|
|
enum Msg {
|
|
|
|
Stop,
|
|
|
|
}
|
|
|
|
|
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<()>,
|
2019-04-17 14:57:23 +00:00
|
|
|
sender: Sender<Msg>,
|
2020-07-21 09:59:00 +00:00
|
|
|
pub receiver: Receiver<Result<(), 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 {
|
2020-10-26 04:15:30 +00:00
|
|
|
pub fn new(client: Option<&Client>) -> Self {
|
2019-08-15 22:07:09 +00:00
|
|
|
let (sender, receiver) = 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 = {
|
2020-10-05 07:41:58 +00:00
|
|
|
let mut path = common::userdata_dir_workspace!();
|
|
|
|
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!(
|
|
|
|
"Saves folder doesn't exist, but there is one in the old saves location, copying \
|
|
|
|
it to the new location"
|
|
|
|
);
|
|
|
|
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 thread_pool = client.map(|c| c.thread_pool().clone());
|
|
|
|
let settings2 = settings.clone();
|
2019-05-21 22:31:17 +00:00
|
|
|
|
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);
|
|
|
|
|
2020-07-10 09:00:16 +00:00
|
|
|
let thread = thread::spawn(move || {
|
2020-08-12 14:43:08 +00:00
|
|
|
let mut server = None;
|
2020-10-06 02:59:47 +00:00
|
|
|
if let Err(e) = result_sender.send(
|
|
|
|
match Server::new(settings2, editable_settings, &server_data_dir) {
|
|
|
|
Ok(s) => {
|
|
|
|
server = Some(s);
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
Err(e) => Err(e),
|
2020-07-21 09:59:00 +00:00
|
|
|
},
|
2020-10-06 02:59:47 +00:00
|
|
|
) {
|
2020-08-12 14:43:08 +00:00
|
|
|
warn!(
|
|
|
|
?e,
|
|
|
|
"Failed to send singleplayer server initialization result. Most likely the \
|
|
|
|
channel was closed by cancelling server creation. Stopping Server"
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
let server = match server {
|
|
|
|
Some(s) => s,
|
|
|
|
None => return,
|
2020-07-21 09:59:00 +00:00
|
|
|
};
|
2019-10-18 20:05:37 +00:00
|
|
|
|
2020-07-10 09:00:16 +00:00
|
|
|
let server = match thread_pool {
|
|
|
|
Some(pool) => server.with_thread_pool(pool),
|
|
|
|
None => server,
|
|
|
|
};
|
2019-10-18 20:05:37 +00:00
|
|
|
|
2020-03-04 20:42:22 +00:00
|
|
|
run_server(server, receiver, paused1);
|
2019-04-17 14:57:23 +00:00
|
|
|
});
|
2019-05-06 14:26:10 +00:00
|
|
|
|
2020-10-26 04:15:30 +00:00
|
|
|
Singleplayer {
|
|
|
|
_server_thread: thread,
|
|
|
|
sender,
|
|
|
|
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
|
|
|
|
let _ = self.sender.send(Msg::Stop);
|
2019-04-17 14:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 20:42:22 +00:00
|
|
|
fn run_server(mut server: Server, rec: Receiver<Msg>, paused: Arc<AtomicBool>) {
|
2019-04-17 13:41:34 +00:00
|
|
|
info!("Starting server-cli...");
|
|
|
|
|
|
|
|
// Set up an fps clock
|
2019-07-01 20:42:43 +00:00
|
|
|
let mut clock = Clock::start();
|
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
|
2020-03-01 22:18:22 +00:00
|
|
|
match rec.try_recv() {
|
|
|
|
Ok(msg) => match msg {
|
|
|
|
Msg::Stop => break,
|
|
|
|
},
|
|
|
|
Err(err) => match err {
|
|
|
|
TryRecvError::Empty => (),
|
|
|
|
TryRecvError::Disconnected => break,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-03-03 19:51:15 +00:00
|
|
|
// Wait for the next tick.
|
|
|
|
clock.tick(Duration::from_millis(1000 / TPS));
|
|
|
|
|
|
|
|
// 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
|
|
|
|
.tick(Input::default(), clock.get_last_delta())
|
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();
|
|
|
|
}
|
|
|
|
}
|