2019-04-14 23:28:29 +00:00
|
|
|
mod client_init;
|
2019-10-27 07:11:18 +00:00
|
|
|
mod ui;
|
2019-01-02 22:08:13 +00:00
|
|
|
|
2019-03-17 17:52:54 +00:00
|
|
|
use super::char_selection::CharSelectionState;
|
2019-10-27 07:11:18 +00:00
|
|
|
#[cfg(feature = "singleplayer")]
|
|
|
|
use crate::singleplayer::Singleplayer;
|
2019-10-18 20:05:37 +00:00
|
|
|
use crate::{
|
2019-10-27 07:11:18 +00:00
|
|
|
render::Renderer, settings::Settings, window::Event, Direction, GlobalState, PlayState,
|
|
|
|
PlayStateResult,
|
2019-10-18 20:05:37 +00:00
|
|
|
};
|
2020-01-02 08:43:45 +00:00
|
|
|
use client_init::{ClientInit, Error as InitError, Msg as InitMsg};
|
2020-08-28 01:02:17 +00:00
|
|
|
use common::{assets::Asset, comp};
|
2020-06-21 10:22:26 +00:00
|
|
|
use tracing::{error, warn};
|
2019-03-17 17:52:54 +00:00
|
|
|
use ui::{Event as MainMenuEvent, MainMenuUi};
|
2019-01-02 21:25:01 +00:00
|
|
|
|
2019-03-04 07:28:16 +00:00
|
|
|
pub struct MainMenuState {
|
|
|
|
main_menu_ui: MainMenuUi,
|
2019-10-27 07:11:18 +00:00
|
|
|
// Used for client creation.
|
|
|
|
client_init: Option<ClientInit>,
|
2019-01-30 12:11:34 +00:00
|
|
|
}
|
2019-01-02 21:25:01 +00:00
|
|
|
|
2019-03-04 07:28:16 +00:00
|
|
|
impl MainMenuState {
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Create a new `MainMenuState`.
|
2019-04-18 17:40:29 +00:00
|
|
|
pub fn new(global_state: &mut GlobalState) -> Self {
|
2019-01-30 12:11:34 +00:00
|
|
|
Self {
|
2019-04-18 17:40:29 +00:00
|
|
|
main_menu_ui: MainMenuUi::new(global_state),
|
2019-10-27 07:11:18 +00:00
|
|
|
client_init: None,
|
2019-01-30 12:11:34 +00:00
|
|
|
}
|
2019-01-02 21:25:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-20 11:59:35 +00:00
|
|
|
const DEFAULT_PORT: u16 = 14004;
|
2019-04-27 20:55:30 +00:00
|
|
|
|
2019-03-04 07:28:16 +00:00
|
|
|
impl PlayState for MainMenuState {
|
2019-10-27 07:11:18 +00:00
|
|
|
fn enter(&mut self, global_state: &mut GlobalState, _: Direction) {
|
2019-10-04 17:31:24 +00:00
|
|
|
// Kick off title music
|
2020-03-31 18:12:49 +00:00
|
|
|
if global_state.settings.audio.output.is_enabled() && global_state.audio.music_enabled() {
|
2020-02-15 21:30:44 +00:00
|
|
|
global_state.audio.play_title_music();
|
2019-10-17 07:10:24 +00:00
|
|
|
}
|
2019-10-04 17:31:24 +00:00
|
|
|
|
2019-10-18 20:05:37 +00:00
|
|
|
// Reset singleplayer server if it was running already
|
2019-10-27 07:11:18 +00:00
|
|
|
#[cfg(feature = "singleplayer")]
|
|
|
|
{
|
|
|
|
global_state.singleplayer = None;
|
|
|
|
}
|
|
|
|
}
|
2019-10-18 20:05:37 +00:00
|
|
|
|
2019-10-27 07:11:18 +00:00
|
|
|
fn tick(&mut self, global_state: &mut GlobalState, events: Vec<Event>) -> PlayStateResult {
|
2020-08-28 01:02:17 +00:00
|
|
|
let localized_strings = crate::i18n::VoxygenLocalization::load_expect(
|
2020-02-02 08:05:58 +00:00
|
|
|
&crate::i18n::i18n_asset_key(&global_state.settings.language.selected_language),
|
|
|
|
);
|
|
|
|
|
2020-07-21 09:59:00 +00:00
|
|
|
//Poll server creation
|
|
|
|
#[cfg(feature = "singleplayer")]
|
|
|
|
{
|
|
|
|
if let Some(singleplayer) = &global_state.singleplayer {
|
|
|
|
if let Ok(result) = singleplayer.receiver.try_recv() {
|
|
|
|
if let Err(error) = result {
|
|
|
|
tracing::error!(?error, "Could not start server");
|
|
|
|
global_state.singleplayer = None;
|
|
|
|
self.client_init = None;
|
|
|
|
self.main_menu_ui.cancel_connection();
|
|
|
|
self.main_menu_ui.show_info(format!("Error: {:?}", error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-27 07:11:18 +00:00
|
|
|
// Handle window events.
|
|
|
|
for event in events {
|
|
|
|
match event {
|
|
|
|
Event::Close => return PlayStateResult::Shutdown,
|
|
|
|
// Pass events to ui.
|
|
|
|
Event::Ui(event) => {
|
|
|
|
self.main_menu_ui.handle_event(event);
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-10-27 07:11:18 +00:00
|
|
|
// Ignore all other events.
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Poll client creation.
|
|
|
|
match self.client_init.as_ref().and_then(|init| init.poll()) {
|
|
|
|
Some(InitMsg::Done(Ok(mut client))) => {
|
2020-07-17 00:14:34 +00:00
|
|
|
self.client_init = None;
|
2019-10-27 07:11:18 +00:00
|
|
|
self.main_menu_ui.connected();
|
|
|
|
// Register voxygen components / resources
|
|
|
|
crate::ecs::init(client.state_mut().ecs_mut());
|
|
|
|
return PlayStateResult::Push(Box::new(CharSelectionState::new(
|
|
|
|
global_state,
|
|
|
|
std::rc::Rc::new(std::cell::RefCell::new(client)),
|
|
|
|
)));
|
|
|
|
},
|
|
|
|
Some(InitMsg::Done(Err(err))) => {
|
|
|
|
self.client_init = None;
|
|
|
|
global_state.info_message = Some({
|
|
|
|
let err = match err {
|
|
|
|
InitError::BadAddress(_) | InitError::NoAddress => {
|
|
|
|
localized_strings.get("main.login.server_not_found").into()
|
|
|
|
},
|
|
|
|
InitError::ClientError(err) => match err {
|
|
|
|
client::Error::AuthErr(e) => format!(
|
|
|
|
"{}: {}",
|
|
|
|
localized_strings.get("main.login.authentication_error"),
|
|
|
|
e
|
|
|
|
),
|
|
|
|
client::Error::TooManyPlayers => {
|
|
|
|
localized_strings.get("main.login.server_full").into()
|
|
|
|
},
|
|
|
|
client::Error::AuthServerNotTrusted => localized_strings
|
|
|
|
.get("main.login.untrusted_auth_server")
|
|
|
|
.into(),
|
|
|
|
client::Error::ServerWentMad => localized_strings
|
|
|
|
.get("main.login.outdated_client_or_server")
|
|
|
|
.into(),
|
|
|
|
client::Error::ServerTimeout => {
|
|
|
|
localized_strings.get("main.login.timeout").into()
|
2020-01-04 10:21:59 +00:00
|
|
|
},
|
2019-10-27 07:11:18 +00:00
|
|
|
client::Error::ServerShutdown => {
|
|
|
|
localized_strings.get("main.login.server_shut_down").into()
|
|
|
|
},
|
|
|
|
client::Error::AlreadyLoggedIn => {
|
|
|
|
localized_strings.get("main.login.already_logged_in").into()
|
|
|
|
},
|
|
|
|
client::Error::NotOnWhitelist => {
|
|
|
|
localized_strings.get("main.login.not_on_whitelist").into()
|
|
|
|
},
|
|
|
|
client::Error::InvalidCharacter => {
|
|
|
|
localized_strings.get("main.login.invalid_character").into()
|
|
|
|
},
|
|
|
|
client::Error::NetworkErr(e) => format!(
|
|
|
|
"{}: {:?}",
|
|
|
|
localized_strings.get("main.login.network_error"),
|
|
|
|
e
|
|
|
|
),
|
|
|
|
client::Error::ParticipantErr(e) => format!(
|
|
|
|
"{}: {:?}",
|
|
|
|
localized_strings.get("main.login.network_error"),
|
|
|
|
e
|
|
|
|
),
|
|
|
|
client::Error::StreamErr(e) => format!(
|
|
|
|
"{}: {:?}",
|
|
|
|
localized_strings.get("main.login.network_error"),
|
|
|
|
e
|
|
|
|
),
|
|
|
|
client::Error::Other(e) => {
|
|
|
|
format!("{}: {}", localized_strings.get("common.error"), e)
|
|
|
|
},
|
|
|
|
client::Error::AuthClientError(e) => match e {
|
|
|
|
client::AuthClientError::JsonError(e) => format!(
|
2020-02-02 08:05:58 +00:00
|
|
|
"{}: {}",
|
2019-10-27 07:11:18 +00:00
|
|
|
localized_strings.get("common.fatal_error"),
|
2020-02-02 08:05:58 +00:00
|
|
|
e
|
|
|
|
),
|
2019-10-27 07:11:18 +00:00
|
|
|
// TODO: remove parentheses
|
2020-07-29 17:28:22 +00:00
|
|
|
client::AuthClientError::RequestError(e) => format!(
|
|
|
|
"{}: {}",
|
|
|
|
localized_strings.get("main.login.failed_sending_request"),
|
|
|
|
e
|
|
|
|
),
|
2020-07-13 03:30:46 +00:00
|
|
|
client::AuthClientError::ServerError(_, e) => e,
|
2020-01-02 08:43:45 +00:00
|
|
|
},
|
2019-10-27 07:11:18 +00:00
|
|
|
},
|
|
|
|
InitError::ClientCrashed => {
|
|
|
|
localized_strings.get("main.login.client_crashed").into()
|
|
|
|
},
|
|
|
|
};
|
|
|
|
// Log error for possible additional use later or incase that the error
|
|
|
|
// displayed is cut of.
|
|
|
|
error!("{}", err);
|
|
|
|
err
|
|
|
|
});
|
|
|
|
},
|
|
|
|
Some(InitMsg::IsAuthTrusted(auth_server)) => {
|
|
|
|
if global_state
|
|
|
|
.settings
|
|
|
|
.networking
|
|
|
|
.trusted_auth_servers
|
|
|
|
.contains(&auth_server)
|
|
|
|
{
|
|
|
|
// Can't fail since we just polled it, it must be Some
|
|
|
|
self.client_init
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.auth_trust(auth_server, true);
|
|
|
|
} else {
|
|
|
|
// Show warning that auth server is not trusted and prompt for approval
|
|
|
|
self.main_menu_ui.auth_trust_prompt(auth_server);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => {},
|
|
|
|
}
|
2019-05-18 20:10:02 +00:00
|
|
|
|
2019-10-27 07:11:18 +00:00
|
|
|
// Maintain the UI.
|
|
|
|
for event in self
|
|
|
|
.main_menu_ui
|
|
|
|
.maintain(global_state, global_state.clock.get_last_delta())
|
|
|
|
{
|
|
|
|
match event {
|
|
|
|
MainMenuEvent::LoginAttempt {
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
server_address,
|
|
|
|
} => {
|
|
|
|
attempt_login(
|
|
|
|
global_state,
|
2019-04-14 23:28:29 +00:00
|
|
|
username,
|
2019-08-07 19:42:44 +00:00
|
|
|
password,
|
2019-04-14 23:28:29 +00:00
|
|
|
server_address,
|
2019-10-27 07:11:18 +00:00
|
|
|
DEFAULT_PORT,
|
|
|
|
&mut self.client_init,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
MainMenuEvent::CancelLoginAttempt => {
|
|
|
|
// client_init contains Some(ClientInit), which spawns a thread which contains a
|
|
|
|
// TcpStream::connect() call This call is blocking
|
|
|
|
// TODO fix when the network rework happens
|
2019-09-15 20:41:47 +00:00
|
|
|
#[cfg(feature = "singleplayer")]
|
2019-10-27 07:11:18 +00:00
|
|
|
{
|
|
|
|
global_state.singleplayer = None;
|
|
|
|
}
|
|
|
|
self.client_init = None;
|
|
|
|
self.main_menu_ui.cancel_connection();
|
|
|
|
},
|
|
|
|
#[cfg(feature = "singleplayer")]
|
|
|
|
MainMenuEvent::StartSingleplayer => {
|
|
|
|
let (singleplayer, server_settings) = Singleplayer::new(None); // TODO: Make client and server use the same thread pool
|
2019-01-30 12:11:34 +00:00
|
|
|
|
2020-02-08 03:26:17 +00:00
|
|
|
global_state.singleplayer = Some(singleplayer);
|
|
|
|
|
2019-10-27 07:11:18 +00:00
|
|
|
attempt_login(
|
|
|
|
global_state,
|
|
|
|
"singleplayer".to_owned(),
|
|
|
|
"".to_owned(),
|
|
|
|
server_settings.gameserver_address.ip().to_string(),
|
|
|
|
server_settings.gameserver_address.port(),
|
|
|
|
&mut self.client_init,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
MainMenuEvent::Settings => {}, // TODO
|
|
|
|
MainMenuEvent::Quit => return PlayStateResult::Shutdown,
|
|
|
|
/*MainMenuEvent::DisclaimerClosed => {
|
|
|
|
global_state.settings.show_disclaimer = false
|
|
|
|
},*/
|
|
|
|
MainMenuEvent::AuthServerTrust(auth_server, trust) => {
|
|
|
|
if trust {
|
|
|
|
global_state
|
|
|
|
.settings
|
|
|
|
.networking
|
|
|
|
.trusted_auth_servers
|
|
|
|
.insert(auth_server.clone());
|
|
|
|
global_state.settings.save_to_file_warn();
|
|
|
|
}
|
|
|
|
self.client_init
|
|
|
|
.as_ref()
|
|
|
|
.map(|init| init.auth_trust(auth_server, trust));
|
|
|
|
},
|
2019-10-18 11:24:18 +00:00
|
|
|
}
|
2019-10-27 07:11:18 +00:00
|
|
|
}
|
2019-10-18 11:24:18 +00:00
|
|
|
|
2019-10-27 07:11:18 +00:00
|
|
|
if let Some(info) = global_state.info_message.take() {
|
|
|
|
self.main_menu_ui.show_info(info);
|
2019-01-02 21:25:01 +00:00
|
|
|
}
|
2019-10-27 07:11:18 +00:00
|
|
|
|
|
|
|
PlayStateResult::Continue
|
2019-01-02 21:25:01 +00:00
|
|
|
}
|
2019-01-11 23:18:34 +00:00
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
fn name(&self) -> &'static str { "Title" }
|
2019-10-27 07:11:18 +00:00
|
|
|
|
|
|
|
fn render(&mut self, renderer: &mut Renderer, _: &Settings) {
|
|
|
|
// Draw the UI to the screen.
|
|
|
|
self.main_menu_ui.render(renderer);
|
|
|
|
}
|
2019-01-02 21:25:01 +00:00
|
|
|
}
|
2019-10-18 20:05:37 +00:00
|
|
|
|
|
|
|
fn attempt_login(
|
|
|
|
global_state: &mut GlobalState,
|
|
|
|
username: String,
|
|
|
|
password: String,
|
|
|
|
server_address: String,
|
|
|
|
server_port: u16,
|
|
|
|
client_init: &mut Option<ClientInit>,
|
|
|
|
) {
|
|
|
|
let mut net_settings = &mut global_state.settings.networking;
|
|
|
|
net_settings.username = username.clone();
|
|
|
|
if !net_settings.servers.contains(&server_address) {
|
|
|
|
net_settings.servers.push(server_address.clone());
|
|
|
|
}
|
2020-06-21 21:47:49 +00:00
|
|
|
if let Err(e) = global_state.settings.save_to_file() {
|
|
|
|
warn!(?e, "Failed to save settings");
|
2019-10-18 20:05:37 +00:00
|
|
|
}
|
|
|
|
|
2020-01-11 21:04:49 +00:00
|
|
|
if comp::Player::alias_is_valid(&username) {
|
2019-10-18 20:05:37 +00:00
|
|
|
// Don't try to connect if there is already a connection in progress.
|
|
|
|
if client_init.is_none() {
|
|
|
|
*client_init = Some(ClientInit::new(
|
|
|
|
(server_address, server_port, false),
|
2020-01-11 21:04:49 +00:00
|
|
|
username,
|
|
|
|
Some(global_state.settings.graphics.view_distance),
|
2020-04-21 20:04:55 +00:00
|
|
|
password,
|
2019-10-18 20:05:37 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-11 21:04:49 +00:00
|
|
|
global_state.info_message = Some("Invalid username".to_string());
|
2019-10-18 20:05:37 +00:00
|
|
|
}
|
|
|
|
}
|