2018-04-01 07:06:48 +01:00
|
|
|
// Copyright 2018 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-04-17 19:01:14 +01:00
|
|
|
#include <QAction>
|
2018-12-01 01:28:55 +00:00
|
|
|
#include <QApplication>
|
2018-04-01 07:06:48 +01:00
|
|
|
#include <QIcon>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QStandardItemModel>
|
|
|
|
#include "citra_qt/game_list.h"
|
|
|
|
#include "citra_qt/multiplayer/client_room.h"
|
|
|
|
#include "citra_qt/multiplayer/direct_connect.h"
|
|
|
|
#include "citra_qt/multiplayer/host_room.h"
|
|
|
|
#include "citra_qt/multiplayer/lobby.h"
|
|
|
|
#include "citra_qt/multiplayer/message.h"
|
|
|
|
#include "citra_qt/multiplayer/state.h"
|
2018-11-24 08:22:49 +00:00
|
|
|
#include "citra_qt/ui_settings.h"
|
2018-04-01 07:06:48 +01:00
|
|
|
#include "citra_qt/util/clickable_label.h"
|
|
|
|
#include "common/announce_multiplayer_room.h"
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
|
2018-04-17 19:01:14 +01:00
|
|
|
MultiplayerState::MultiplayerState(QWidget* parent, QStandardItemModel* game_list_model,
|
|
|
|
QAction* leave_room, QAction* show_room)
|
|
|
|
: QWidget(parent), game_list_model(game_list_model), leave_room(leave_room),
|
|
|
|
show_room(show_room) {
|
2018-04-01 07:06:48 +01:00
|
|
|
if (auto member = Network::GetRoomMember().lock()) {
|
|
|
|
// register the network structs to use in slots and signals
|
|
|
|
state_callback_handle = member->BindOnStateChanged(
|
|
|
|
[this](const Network::RoomMember::State& state) { emit NetworkStateChanged(state); });
|
|
|
|
connect(this, &MultiplayerState::NetworkStateChanged, this,
|
|
|
|
&MultiplayerState::OnNetworkStateChanged);
|
2018-11-24 08:13:46 +00:00
|
|
|
error_callback_handle = member->BindOnError(
|
|
|
|
[this](const Network::RoomMember::Error& error) { emit NetworkError(error); });
|
|
|
|
connect(this, &MultiplayerState::NetworkError, this, &MultiplayerState::OnNetworkError);
|
2018-04-01 07:06:48 +01:00
|
|
|
}
|
|
|
|
|
2018-04-05 19:07:11 +01:00
|
|
|
qRegisterMetaType<Network::RoomMember::State>();
|
2018-11-24 08:13:46 +00:00
|
|
|
qRegisterMetaType<Network::RoomMember::Error>();
|
2018-04-01 07:06:48 +01:00
|
|
|
qRegisterMetaType<Common::WebResult>();
|
|
|
|
announce_multiplayer_session = std::make_shared<Core::AnnounceMultiplayerSession>();
|
|
|
|
announce_multiplayer_session->BindErrorCallback(
|
|
|
|
[this](const Common::WebResult& result) { emit AnnounceFailed(result); });
|
|
|
|
connect(this, &MultiplayerState::AnnounceFailed, this, &MultiplayerState::OnAnnounceFailed);
|
|
|
|
|
|
|
|
status_text = new ClickableLabel(this);
|
|
|
|
status_icon = new ClickableLabel(this);
|
|
|
|
status_text->setToolTip(tr("Current connection status"));
|
|
|
|
status_text->setText(tr("Not Connected. Click here to find a room!"));
|
|
|
|
status_icon->setPixmap(QIcon::fromTheme("disconnected").pixmap(16));
|
|
|
|
|
|
|
|
connect(status_text, &ClickableLabel::clicked, this, &MultiplayerState::OnOpenNetworkRoom);
|
|
|
|
connect(status_icon, &ClickableLabel::clicked, this, &MultiplayerState::OnOpenNetworkRoom);
|
2018-12-01 01:28:55 +00:00
|
|
|
|
|
|
|
connect(static_cast<QApplication*>(QApplication::instance()), &QApplication::focusChanged, this,
|
|
|
|
[this](QWidget* /*old*/, QWidget* now) {
|
|
|
|
if (client_room && client_room->isAncestorOf(now)) {
|
|
|
|
HideNotification();
|
|
|
|
}
|
|
|
|
});
|
2018-04-01 07:06:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MultiplayerState::~MultiplayerState() {
|
|
|
|
if (state_callback_handle) {
|
|
|
|
if (auto member = Network::GetRoomMember().lock()) {
|
|
|
|
member->Unbind(state_callback_handle);
|
|
|
|
}
|
|
|
|
}
|
2018-11-24 08:13:46 +00:00
|
|
|
|
|
|
|
if (error_callback_handle) {
|
|
|
|
if (auto member = Network::GetRoomMember().lock()) {
|
|
|
|
member->Unbind(error_callback_handle);
|
|
|
|
}
|
|
|
|
}
|
2018-04-01 07:06:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MultiplayerState::Close() {
|
|
|
|
if (host_room)
|
|
|
|
host_room->close();
|
|
|
|
if (direct_connect)
|
|
|
|
direct_connect->close();
|
|
|
|
if (client_room)
|
|
|
|
client_room->close();
|
|
|
|
if (lobby)
|
|
|
|
lobby->close();
|
|
|
|
}
|
|
|
|
|
2018-07-24 11:57:45 +01:00
|
|
|
void MultiplayerState::retranslateUi() {
|
|
|
|
status_text->setToolTip(tr("Current connection status"));
|
|
|
|
|
|
|
|
if (current_state == Network::RoomMember::State::Uninitialized) {
|
|
|
|
status_text->setText(tr("Not Connected. Click here to find a room!"));
|
2018-12-15 09:13:46 +00:00
|
|
|
} else if (current_state == Network::RoomMember::State::Joined ||
|
|
|
|
current_state == Network::RoomMember::State::Moderator) {
|
|
|
|
|
2018-07-24 11:57:45 +01:00
|
|
|
status_text->setText(tr("Connected"));
|
|
|
|
} else {
|
|
|
|
status_text->setText(tr("Not Connected"));
|
|
|
|
}
|
2018-10-09 16:08:33 +01:00
|
|
|
|
|
|
|
if (lobby)
|
|
|
|
lobby->RetranslateUi();
|
|
|
|
if (host_room)
|
|
|
|
host_room->RetranslateUi();
|
|
|
|
if (client_room)
|
|
|
|
client_room->RetranslateUi();
|
|
|
|
if (direct_connect)
|
|
|
|
direct_connect->RetranslateUi();
|
2018-07-24 11:57:45 +01:00
|
|
|
}
|
|
|
|
|
2018-04-01 07:06:48 +01:00
|
|
|
void MultiplayerState::OnNetworkStateChanged(const Network::RoomMember::State& state) {
|
2018-06-29 12:18:07 +01:00
|
|
|
LOG_DEBUG(Frontend, "Network State: {}", Network::GetStateStr(state));
|
2018-12-15 09:13:46 +00:00
|
|
|
if (state == Network::RoomMember::State::Joined ||
|
|
|
|
state == Network::RoomMember::State::Moderator) {
|
|
|
|
|
2018-11-24 08:13:46 +00:00
|
|
|
OnOpenNetworkRoom();
|
|
|
|
status_icon->setPixmap(QIcon::fromTheme("connected").pixmap(16));
|
|
|
|
status_text->setText(tr("Connected"));
|
|
|
|
leave_room->setEnabled(true);
|
|
|
|
show_room->setEnabled(true);
|
|
|
|
} else {
|
|
|
|
status_icon->setPixmap(QIcon::fromTheme("disconnected").pixmap(16));
|
|
|
|
status_text->setText(tr("Not Connected"));
|
|
|
|
leave_room->setEnabled(false);
|
|
|
|
show_room->setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
current_state = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiplayerState::OnNetworkError(const Network::RoomMember::Error& error) {
|
|
|
|
LOG_DEBUG(Frontend, "Network Error: {}", Network::GetErrorStr(error));
|
|
|
|
switch (error) {
|
|
|
|
case Network::RoomMember::Error::LostConnection:
|
2018-04-19 07:47:11 +01:00
|
|
|
NetworkMessage::ShowError(NetworkMessage::LOST_CONNECTION);
|
|
|
|
break;
|
2018-11-24 08:13:46 +00:00
|
|
|
case Network::RoomMember::Error::HostKicked:
|
|
|
|
NetworkMessage::ShowError(NetworkMessage::HOST_KICKED);
|
|
|
|
break;
|
|
|
|
case Network::RoomMember::Error::CouldNotConnect:
|
2018-04-19 07:47:11 +01:00
|
|
|
NetworkMessage::ShowError(NetworkMessage::UNABLE_TO_CONNECT);
|
|
|
|
break;
|
2018-11-24 08:13:46 +00:00
|
|
|
case Network::RoomMember::Error::NameCollision:
|
2018-11-02 12:17:25 +00:00
|
|
|
NetworkMessage::ShowError(NetworkMessage::USERNAME_NOT_VALID_SERVER);
|
2018-04-19 07:47:11 +01:00
|
|
|
break;
|
2018-11-24 08:13:46 +00:00
|
|
|
case Network::RoomMember::Error::MacCollision:
|
2018-04-19 07:47:11 +01:00
|
|
|
NetworkMessage::ShowError(NetworkMessage::MAC_COLLISION);
|
|
|
|
break;
|
2018-11-24 08:13:46 +00:00
|
|
|
case Network::RoomMember::Error::ConsoleIdCollision:
|
2018-10-31 15:07:03 +00:00
|
|
|
NetworkMessage::ShowError(NetworkMessage::CONSOLE_ID_COLLISION);
|
|
|
|
break;
|
2018-11-24 08:13:46 +00:00
|
|
|
case Network::RoomMember::Error::RoomIsFull:
|
2018-04-20 08:34:37 +01:00
|
|
|
NetworkMessage::ShowError(NetworkMessage::ROOM_IS_FULL);
|
|
|
|
break;
|
2018-11-24 08:13:46 +00:00
|
|
|
case Network::RoomMember::Error::WrongPassword:
|
2018-04-19 07:47:11 +01:00
|
|
|
NetworkMessage::ShowError(NetworkMessage::WRONG_PASSWORD);
|
|
|
|
break;
|
2018-11-24 08:13:46 +00:00
|
|
|
case Network::RoomMember::Error::WrongVersion:
|
2018-04-19 07:47:11 +01:00
|
|
|
NetworkMessage::ShowError(NetworkMessage::WRONG_VERSION);
|
|
|
|
break;
|
2018-11-24 08:13:46 +00:00
|
|
|
case Network::RoomMember::Error::HostBanned:
|
|
|
|
NetworkMessage::ShowError(NetworkMessage::HOST_BANNED);
|
|
|
|
break;
|
|
|
|
case Network::RoomMember::Error::UnknownError:
|
2018-04-19 07:47:11 +01:00
|
|
|
NetworkMessage::ShowError(NetworkMessage::UNABLE_TO_CONNECT);
|
|
|
|
break;
|
2018-11-24 08:13:46 +00:00
|
|
|
case Network::RoomMember::Error::PermissionDenied:
|
|
|
|
NetworkMessage::ShowError(NetworkMessage::PERMISSION_DENIED);
|
|
|
|
break;
|
|
|
|
case Network::RoomMember::Error::NoSuchUser:
|
|
|
|
NetworkMessage::ShowError(NetworkMessage::NO_SUCH_USER);
|
2018-04-19 07:47:11 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-04-01 07:06:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MultiplayerState::OnAnnounceFailed(const Common::WebResult& result) {
|
|
|
|
announce_multiplayer_session->Stop();
|
2019-04-19 15:55:49 +01:00
|
|
|
QMessageBox::warning(this, tr("Error"),
|
|
|
|
tr("Failed to update the room information. Please check your Internet "
|
|
|
|
"connection and try hosting the room again.\nDebug Message: ") +
|
|
|
|
QString::fromStdString(result.result_string),
|
|
|
|
QMessageBox::Ok);
|
2018-04-01 07:06:48 +01:00
|
|
|
}
|
|
|
|
|
2018-07-27 03:51:33 +01:00
|
|
|
void MultiplayerState::UpdateThemedIcons() {
|
2018-12-01 01:28:55 +00:00
|
|
|
if (show_notification) {
|
|
|
|
status_icon->setPixmap(QIcon::fromTheme("connected_notification").pixmap(16));
|
2018-12-15 09:13:46 +00:00
|
|
|
} else if (current_state == Network::RoomMember::State::Joined ||
|
|
|
|
current_state == Network::RoomMember::State::Moderator) {
|
|
|
|
|
2018-07-27 03:51:33 +01:00
|
|
|
status_icon->setPixmap(QIcon::fromTheme("connected").pixmap(16));
|
|
|
|
} else {
|
|
|
|
status_icon->setPixmap(QIcon::fromTheme("disconnected").pixmap(16));
|
|
|
|
}
|
2019-01-17 19:25:17 +00:00
|
|
|
if (client_room)
|
|
|
|
client_room->UpdateIconDisplay();
|
2018-07-27 03:51:33 +01:00
|
|
|
}
|
|
|
|
|
2018-04-01 07:06:48 +01:00
|
|
|
static void BringWidgetToFront(QWidget* widget) {
|
|
|
|
widget->show();
|
|
|
|
widget->activateWindow();
|
|
|
|
widget->raise();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiplayerState::OnViewLobby() {
|
|
|
|
if (lobby == nullptr) {
|
|
|
|
lobby = new Lobby(this, game_list_model, announce_multiplayer_session);
|
|
|
|
}
|
|
|
|
BringWidgetToFront(lobby);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiplayerState::OnCreateRoom() {
|
|
|
|
if (host_room == nullptr) {
|
|
|
|
host_room = new HostRoomWindow(this, game_list_model, announce_multiplayer_session);
|
|
|
|
}
|
|
|
|
BringWidgetToFront(host_room);
|
|
|
|
}
|
|
|
|
|
2018-04-18 18:04:32 +01:00
|
|
|
bool MultiplayerState::OnCloseRoom() {
|
2018-04-17 19:01:14 +01:00
|
|
|
if (!NetworkMessage::WarnCloseRoom())
|
2018-04-18 18:04:32 +01:00
|
|
|
return false;
|
2018-04-01 07:06:48 +01:00
|
|
|
if (auto room = Network::GetRoom().lock()) {
|
2018-04-17 19:01:14 +01:00
|
|
|
// if you are in a room, leave it
|
|
|
|
if (auto member = Network::GetRoomMember().lock()) {
|
|
|
|
member->Leave();
|
2018-06-29 12:18:07 +01:00
|
|
|
LOG_DEBUG(Frontend, "Left the room (as a client)");
|
2018-04-17 19:01:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// if you are hosting a room, also stop hosting
|
2018-04-09 18:00:56 +01:00
|
|
|
if (room->GetState() != Network::Room::State::Open) {
|
2018-04-18 18:04:32 +01:00
|
|
|
return true;
|
2018-04-09 18:00:56 +01:00
|
|
|
}
|
2018-11-24 08:22:49 +00:00
|
|
|
// Save ban list
|
|
|
|
if (auto room = Network::GetRoom().lock()) {
|
|
|
|
UISettings::values.ban_list = std::move(room->GetBanList());
|
|
|
|
}
|
2018-04-17 19:01:14 +01:00
|
|
|
room->Destroy();
|
|
|
|
announce_multiplayer_session->Stop();
|
2018-06-29 12:18:07 +01:00
|
|
|
LOG_DEBUG(Frontend, "Closed the room (as a server)");
|
2018-04-01 07:06:48 +01:00
|
|
|
}
|
2018-04-18 18:04:32 +01:00
|
|
|
return true;
|
2018-04-01 07:06:48 +01:00
|
|
|
}
|
|
|
|
|
2018-12-01 01:28:55 +00:00
|
|
|
void MultiplayerState::ShowNotification() {
|
|
|
|
if (client_room && client_room->isAncestorOf(QApplication::focusWidget()))
|
|
|
|
return; // Do not show notification if the chat window currently has focus
|
|
|
|
show_notification = true;
|
|
|
|
QApplication::alert(nullptr);
|
|
|
|
status_icon->setPixmap(QIcon::fromTheme("connected_notification").pixmap(16));
|
|
|
|
status_text->setText(tr("New Messages Received"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiplayerState::HideNotification() {
|
|
|
|
show_notification = false;
|
|
|
|
status_icon->setPixmap(QIcon::fromTheme("connected").pixmap(16));
|
|
|
|
status_text->setText(tr("Connected"));
|
|
|
|
}
|
|
|
|
|
2018-04-01 07:06:48 +01:00
|
|
|
void MultiplayerState::OnOpenNetworkRoom() {
|
|
|
|
if (auto member = Network::GetRoomMember().lock()) {
|
|
|
|
if (member->IsConnected()) {
|
|
|
|
if (client_room == nullptr) {
|
|
|
|
client_room = new ClientRoomWindow(this);
|
2018-12-01 01:28:55 +00:00
|
|
|
connect(client_room, &ClientRoomWindow::ShowNotification, this,
|
|
|
|
&MultiplayerState::ShowNotification);
|
2018-04-01 07:06:48 +01:00
|
|
|
}
|
|
|
|
BringWidgetToFront(client_room);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If the user is not a member of a room, show the lobby instead.
|
|
|
|
// This is currently only used on the clickable label in the status bar
|
|
|
|
OnViewLobby();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiplayerState::OnDirectConnectToRoom() {
|
|
|
|
if (direct_connect == nullptr) {
|
|
|
|
direct_connect = new DirectConnectWindow(this);
|
|
|
|
}
|
|
|
|
BringWidgetToFront(direct_connect);
|
|
|
|
}
|
2019-04-20 03:42:20 +01:00
|
|
|
|
|
|
|
bool MultiplayerState::IsHostingPublicRoom() const {
|
|
|
|
return announce_multiplayer_session->IsRunning();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiplayerState::UpdateCredentials() {
|
|
|
|
announce_multiplayer_session->UpdateCredentials();
|
|
|
|
}
|
2019-04-20 05:38:19 +01:00
|
|
|
|
|
|
|
void MultiplayerState::UpdateGameList(QStandardItemModel* game_list) {
|
|
|
|
game_list_model = game_list;
|
|
|
|
if (lobby) {
|
|
|
|
lobby->UpdateGameList(game_list);
|
|
|
|
}
|
|
|
|
if (host_room) {
|
|
|
|
host_room->UpdateGameList(game_list);
|
|
|
|
}
|
|
|
|
}
|