2022-07-25 10:18:30 -05:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2021-12-25 13:27:52 -06:00
|
|
|
|
|
|
|
#include <QComboBox>
|
|
|
|
#include <QFuture>
|
|
|
|
#include <QIntValidator>
|
2022-10-17 18:55:40 -05:00
|
|
|
#include <QRegularExpressionValidator>
|
2021-12-25 13:27:52 -06:00
|
|
|
#include <QString>
|
|
|
|
#include <QtConcurrent/QtConcurrentRun>
|
|
|
|
#include "common/settings.h"
|
2022-08-28 12:31:16 -05:00
|
|
|
#include "core/core.h"
|
2022-08-26 20:41:19 -05:00
|
|
|
#include "core/internal_network/network_interface.h"
|
2021-12-25 13:27:52 -06:00
|
|
|
#include "network/network.h"
|
|
|
|
#include "ui_direct_connect.h"
|
|
|
|
#include "yuzu/main.h"
|
|
|
|
#include "yuzu/multiplayer/client_room.h"
|
|
|
|
#include "yuzu/multiplayer/direct_connect.h"
|
|
|
|
#include "yuzu/multiplayer/message.h"
|
|
|
|
#include "yuzu/multiplayer/state.h"
|
|
|
|
#include "yuzu/multiplayer/validation.h"
|
|
|
|
#include "yuzu/uisettings.h"
|
|
|
|
|
|
|
|
enum class ConnectionType : u8 { TraversalServer, IP };
|
|
|
|
|
2022-08-26 20:41:19 -05:00
|
|
|
DirectConnectWindow::DirectConnectWindow(Core::System& system_, QWidget* parent)
|
2021-12-25 13:27:52 -06:00
|
|
|
: QDialog(parent, Qt::WindowTitleHint | Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint),
|
2022-08-26 20:41:19 -05:00
|
|
|
ui(std::make_unique<Ui::DirectConnect>()), system{system_}, room_network{
|
|
|
|
system.GetRoomNetwork()} {
|
2021-12-25 13:27:52 -06:00
|
|
|
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
// setup the watcher for background connections
|
|
|
|
watcher = new QFutureWatcher<void>;
|
|
|
|
connect(watcher, &QFutureWatcher<void>::finished, this, &DirectConnectWindow::OnConnection);
|
|
|
|
|
|
|
|
ui->nickname->setValidator(validation.GetNickname());
|
2023-06-05 19:45:33 -05:00
|
|
|
ui->nickname->setText(
|
|
|
|
QString::fromStdString(UISettings::values.multiplayer_nickname.GetValue()));
|
2021-12-25 13:27:52 -06:00
|
|
|
if (ui->nickname->text().isEmpty() && !Settings::values.yuzu_username.GetValue().empty()) {
|
|
|
|
// Use yuzu Web Service user name as nickname by default
|
|
|
|
ui->nickname->setText(QString::fromStdString(Settings::values.yuzu_username.GetValue()));
|
|
|
|
}
|
|
|
|
ui->ip->setValidator(validation.GetIP());
|
2023-06-05 19:45:33 -05:00
|
|
|
ui->ip->setText(QString::fromStdString(UISettings::values.multiplayer_ip.GetValue()));
|
2021-12-25 13:27:52 -06:00
|
|
|
ui->port->setValidator(validation.GetPort());
|
2022-07-15 12:45:35 -05:00
|
|
|
ui->port->setText(QString::number(UISettings::values.multiplayer_port.GetValue()));
|
2021-12-25 13:27:52 -06:00
|
|
|
|
|
|
|
// TODO(jroweboy): Show or hide the connection options based on the current value of the combo
|
|
|
|
// box. Add this back in when the traversal server support is added.
|
|
|
|
connect(ui->connect, &QPushButton::clicked, this, &DirectConnectWindow::Connect);
|
|
|
|
}
|
|
|
|
|
|
|
|
DirectConnectWindow::~DirectConnectWindow() = default;
|
|
|
|
|
|
|
|
void DirectConnectWindow::RetranslateUi() {
|
|
|
|
ui->retranslateUi(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirectConnectWindow::Connect() {
|
2022-08-26 20:41:19 -05:00
|
|
|
if (!Network::GetSelectedNetworkInterface()) {
|
|
|
|
NetworkMessage::ErrorManager::ShowError(
|
|
|
|
NetworkMessage::ErrorManager::NO_INTERFACE_SELECTED);
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 13:27:52 -06:00
|
|
|
if (!ui->nickname->hasAcceptableInput()) {
|
|
|
|
NetworkMessage::ErrorManager::ShowError(NetworkMessage::ErrorManager::USERNAME_NOT_VALID);
|
|
|
|
return;
|
|
|
|
}
|
2022-08-26 20:41:19 -05:00
|
|
|
if (system.IsPoweredOn()) {
|
|
|
|
if (!NetworkMessage::WarnGameRunning()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2022-07-22 09:31:13 -05:00
|
|
|
if (const auto member = room_network.GetRoomMember().lock()) {
|
2021-12-25 13:27:52 -06:00
|
|
|
// Prevent the user from trying to join a room while they are already joining.
|
|
|
|
if (member->GetState() == Network::RoomMember::State::Joining) {
|
|
|
|
return;
|
|
|
|
} else if (member->IsConnected()) {
|
|
|
|
// And ask if they want to leave the room if they are already in one.
|
|
|
|
if (!NetworkMessage::WarnDisconnect()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-17 08:43:39 -06:00
|
|
|
if (!ui->ip->hasAcceptableInput()) {
|
|
|
|
NetworkMessage::ErrorManager::ShowError(NetworkMessage::ErrorManager::IP_ADDRESS_NOT_VALID);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!ui->port->hasAcceptableInput()) {
|
|
|
|
NetworkMessage::ErrorManager::ShowError(NetworkMessage::ErrorManager::PORT_NOT_VALID);
|
|
|
|
return;
|
2021-12-25 13:27:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store settings
|
2023-06-05 19:45:33 -05:00
|
|
|
UISettings::values.multiplayer_nickname = ui->nickname->text().toStdString();
|
|
|
|
UISettings::values.multiplayer_ip = ui->ip->text().toStdString();
|
2022-07-15 12:45:35 -05:00
|
|
|
if (ui->port->isModified() && !ui->port->text().isEmpty()) {
|
|
|
|
UISettings::values.multiplayer_port = ui->port->text().toInt();
|
|
|
|
} else {
|
|
|
|
UISettings::values.multiplayer_port = UISettings::values.multiplayer_port.GetDefault();
|
|
|
|
}
|
2021-12-25 13:27:52 -06:00
|
|
|
|
2022-09-09 15:29:22 -05:00
|
|
|
emit SaveConfig();
|
|
|
|
|
2021-12-25 13:27:52 -06:00
|
|
|
// attempt to connect in a different thread
|
|
|
|
QFuture<void> f = QtConcurrent::run([&] {
|
2022-07-22 09:31:13 -05:00
|
|
|
if (auto room_member = room_network.GetRoomMember().lock()) {
|
2022-07-15 12:45:35 -05:00
|
|
|
auto port = UISettings::values.multiplayer_port.GetValue();
|
2022-07-29 22:58:23 -05:00
|
|
|
room_member->Join(ui->nickname->text().toStdString(),
|
|
|
|
ui->ip->text().toStdString().c_str(), port, 0, Network::NoPreferredIP,
|
|
|
|
ui->password->text().toStdString().c_str());
|
2021-12-25 13:27:52 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
watcher->setFuture(f);
|
|
|
|
// and disable widgets and display a connecting while we wait
|
|
|
|
BeginConnecting();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirectConnectWindow::BeginConnecting() {
|
|
|
|
ui->connect->setEnabled(false);
|
|
|
|
ui->connect->setText(tr("Connecting"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirectConnectWindow::EndConnecting() {
|
|
|
|
ui->connect->setEnabled(true);
|
|
|
|
ui->connect->setText(tr("Connect"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirectConnectWindow::OnConnection() {
|
|
|
|
EndConnecting();
|
|
|
|
|
2022-07-22 09:31:13 -05:00
|
|
|
if (auto room_member = room_network.GetRoomMember().lock()) {
|
2022-07-29 22:58:23 -05:00
|
|
|
if (room_member->IsConnected()) {
|
2021-12-25 13:27:52 -06:00
|
|
|
close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|