| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/NetPlay/NetPlayDialog.h" |
| 5 | |
| 6 | #include <QAction> |
| 7 | #include <QActionGroup> |
| 8 | #include <QApplication> |
| 9 | #include <QClipboard> |
| 10 | #include <QComboBox> |
| 11 | #include <QFileDialog> |
| 12 | #include <QGridLayout> |
| 13 | #include <QGroupBox> |
| 14 | #include <QHeaderView> |
| 15 | #include <QLabel> |
| 16 | #include <QLineEdit> |
| 17 | #include <QMenu> |
| 18 | #include <QPushButton> |
| 19 | #include <QSignalBlocker> |
| 20 | #include <QSpinBox> |
| 21 | #include <QSplitter> |
| 22 | #include <QTableWidget> |
| 23 | #include <QTextBrowser> |
| 24 | |
| 25 | #include <algorithm> |
| 26 | #include <utility> |
| 27 | |
| 28 | #ifdef HAS_LIBMGBA |
| 29 | #include <fmt/ranges.h> |
| 30 | #endif |
| 31 | |
| 32 | #include "Common/Config/Config.h" |
| 33 | #include "Common/HttpRequest.h" |
| 34 | #include "Common/Logging/Log.h" |
| 35 | #include "Common/TraversalClient.h" |
| 36 | |
| 37 | #include "Core/Boot/Boot.h" |
| 38 | #include "Core/Config/GraphicsSettings.h" |
| 39 | #include "Core/Config/NetplaySettings.h" |
| 40 | #include "Core/ConfigManager.h" |
| 41 | #include "Core/Core.h" |
| 42 | #ifdef HAS_LIBMGBA |
| 43 | #include "Core/HW/GBACore.h" |
| 44 | #endif |
| 45 | #include "Core/IOS/FS/FileSystem.h" |
| 46 | #include "Core/NetPlayServer.h" |
| 47 | #include "Core/SyncIdentifier.h" |
| 48 | #include "Core/System.h" |
| 49 | |
| 50 | #include "DolphinQt/NetPlay/ChunkedProgressDialog.h" |
| 51 | #include "DolphinQt/NetPlay/ClickBlurLabel.h" |
| 52 | #include "DolphinQt/NetPlay/GameDigestDialog.h" |
| 53 | #include "DolphinQt/NetPlay/GameListDialog.h" |
| 54 | #include "DolphinQt/NetPlay/PadMappingDialog.h" |
| 55 | #include "DolphinQt/QtUtils/ModalMessageBox.h" |
| 56 | #include "DolphinQt/QtUtils/QueueOnObject.h" |
| 57 | #include "DolphinQt/QtUtils/RunOnObject.h" |
| 58 | #include "DolphinQt/Resources.h" |
| 59 | #include "DolphinQt/Settings.h" |
| 60 | #include "DolphinQt/Settings/GameCubePane.h" |
| 61 | |
| 62 | #include "UICommon/DiscordPresence.h" |
| 63 | #include "UICommon/GameFile.h" |
| 64 | #include "UICommon/UICommon.h" |
| 65 | |
| 66 | #include "VideoCommon/NetPlayChatUI.h" |
| 67 | #include "VideoCommon/NetPlayGolfUI.h" |
| 68 | |
| 69 | namespace |
| 70 | { |
| 71 | QString InetAddressToString(const Common::TraversalInetAddress& addr) |
| 72 | { |
| 73 | QString ip; |
| 74 | |
| 75 | if (addr.isIPV6) |
| 76 | { |
| 77 | ip = QStringLiteral("IPv6-Not-Implemented"); |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | const auto ipv4 = reinterpret_cast<const u8*>(addr.address); |
| 82 | ip = QString::number(ipv4[0]); |
| 83 | for (u32 i = 1; i != 4; ++i) |
| 84 | { |
| 85 | ip += QStringLiteral("."); |
| 86 | ip += QString::number(ipv4[i]); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return QStringLiteral("%1:%2").arg(ip, QString::number(ntohs(addr.port))); |
| 91 | } |
| 92 | } // namespace |
| 93 | |
| 94 | NetPlayDialog::NetPlayDialog(const GameListModel& game_list_model, |
| 95 | StartGameCallback start_game_callback, QWidget* parent) |
| 96 | : QDialog(parent), m_game_list_model(game_list_model), |
| 97 | m_start_game_callback(std::move(start_game_callback)) |
| 98 | { |
| 99 | setWindowTitle(tr("NetPlay")); |
| 100 | setWindowIcon(Resources::GetAppIcon()); |
| 101 | |
| 102 | m_pad_mapping = new PadMappingDialog(this); |
| 103 | m_game_digest_dialog = new GameDigestDialog(this); |
| 104 | m_chunked_progress_dialog = new ChunkedProgressDialog(this); |
| 105 | |
| 106 | ResetExternalIP(); |
| 107 | CreateChatLayout(); |
| 108 | CreatePlayersLayout(); |
| 109 | CreateMainLayout(); |
| 110 | LoadSettings(); |
| 111 | ConnectWidgets(); |
| 112 | |
| 113 | const auto& settings = Settings::Instance().GetQSettings(); |
| 114 | |
| 115 | restoreGeometry(settings.value(QStringLiteral("netplaydialog/geometry")).toByteArray()); |
| 116 | m_splitter->restoreState(settings.value(QStringLiteral("netplaydialog/splitter")).toByteArray()); |
| 117 | } |
| 118 | |
| 119 | NetPlayDialog::~NetPlayDialog() |
| 120 | { |
| 121 | auto& settings = Settings::Instance().GetQSettings(); |
| 122 | |
| 123 | settings.setValue(QStringLiteral("netplaydialog/geometry"), saveGeometry()); |
| 124 | settings.setValue(QStringLiteral("netplaydialog/splitter"), m_splitter->saveState()); |
| 125 | } |
| 126 | |
| 127 | void NetPlayDialog::CreateMainLayout() |
| 128 | { |
| 129 | m_main_layout = new QGridLayout; |
| 130 | m_game_button = new QPushButton; |
| 131 | m_start_button = new QPushButton(tr("Start")); |
| 132 | m_buffer_size_box = new QSpinBox; |
| 133 | m_buffer_label = new QLabel(tr("Buffer:")); |
| 134 | m_quit_button = new QPushButton(tr("Quit")); |
| 135 | m_splitter = new QSplitter(Qt::Horizontal); |
| 136 | m_menu_bar = new QMenuBar(this); |
| 137 | |
| 138 | m_data_menu = m_menu_bar->addMenu(tr("Data")); |
| 139 | m_data_menu->setToolTipsVisible(true); |
| 140 | |
| 141 | m_savedata_none_action = m_data_menu->addAction(tr("No Save Data")); |
| 142 | m_savedata_none_action->setToolTip( |
| 143 | tr("Netplay will start without any save data, and any created save data will be discarded at " |
| 144 | "the end of the Netplay session.")); |
| 145 | m_savedata_none_action->setCheckable(true); |
| 146 | m_savedata_load_only_action = m_data_menu->addAction(tr("Load Host's Save Data Only")); |
| 147 | m_savedata_load_only_action->setToolTip(tr( |
| 148 | "Netplay will start using the Host's save data, but any save data created or modified during " |
| 149 | "the Netplay session will be discarded at the end of the session.")); |
| 150 | m_savedata_load_only_action->setCheckable(true); |
| 151 | m_savedata_load_and_write_action = m_data_menu->addAction(tr("Load and Write Host's Save Data")); |
| 152 | m_savedata_load_and_write_action->setToolTip( |
| 153 | tr("Netplay will start using the Host's save data, and any save data created or modified " |
| 154 | "during the Netplay session will remain in the Host's local saves.")); |
| 155 | m_savedata_load_and_write_action->setCheckable(true); |
| 156 | |
| 157 | m_savedata_style_group = new QActionGroup(this); |
| 158 | m_savedata_style_group->setExclusive(true); |
| 159 | m_savedata_style_group->addAction(m_savedata_none_action); |
| 160 | m_savedata_style_group->addAction(m_savedata_load_only_action); |
| 161 | m_savedata_style_group->addAction(m_savedata_load_and_write_action); |
| 162 | |
| 163 | m_data_menu->addSeparator(); |
| 164 | |
| 165 | m_savedata_all_wii_saves_action = m_data_menu->addAction(tr("Use All Wii Save Data")); |
| 166 | m_savedata_all_wii_saves_action->setToolTip(tr( |
| 167 | "If checked, all Wii saves will be used instead of only the save of the game being started. " |
| 168 | "Useful when switching games mid-session. Has no effect if No Save Data is selected.")); |
| 169 | m_savedata_all_wii_saves_action->setCheckable(true); |
| 170 | |
| 171 | m_data_menu->addSeparator(); |
| 172 | |
| 173 | m_sync_codes_action = m_data_menu->addAction(tr("Sync AR/Gecko Codes")); |
| 174 | m_sync_codes_action->setCheckable(true); |
| 175 | m_strict_settings_sync_action = m_data_menu->addAction(tr("Strict Settings Sync")); |
| 176 | m_strict_settings_sync_action->setToolTip( |
| 177 | tr("This will sync additional graphics settings, and force everyone to the same internal " |
| 178 | "resolution.\nMay prevent desync in some games that use EFB reads. Please ensure everyone " |
| 179 | "uses the same video backend.")); |
| 180 | m_strict_settings_sync_action->setCheckable(true); |
| 181 | |
| 182 | m_network_menu = m_menu_bar->addMenu(tr("Network")); |
| 183 | m_network_menu->setToolTipsVisible(true); |
| 184 | m_fixed_delay_action = m_network_menu->addAction(tr("Fair Input Delay")); |
| 185 | m_fixed_delay_action->setToolTip( |
| 186 | tr("Each player sends their own inputs to the game, with equal buffer size for all players, " |
| 187 | "configured by the host.\nSuitable for competitive games where fairness and minimal " |
| 188 | "latency are most important.")); |
| 189 | m_fixed_delay_action->setCheckable(true); |
| 190 | m_host_input_authority_action = m_network_menu->addAction(tr("Host Input Authority")); |
| 191 | m_host_input_authority_action->setToolTip( |
| 192 | tr("Host has control of sending all inputs to the game, as received from other players, " |
| 193 | "giving the host zero latency but increasing latency for others.\nSuitable for casual " |
| 194 | "games with 3+ players, possibly on unstable or high latency connections.")); |
| 195 | m_host_input_authority_action->setCheckable(true); |
| 196 | m_golf_mode_action = m_network_menu->addAction(tr("Golf Mode")); |
| 197 | m_golf_mode_action->setToolTip( |
| 198 | tr("Identical to Host Input Authority, except the \"Host\" (who has zero latency) can be " |
| 199 | "switched at any time.\nSuitable for turn-based games with timing-sensitive controls, " |
| 200 | "such as golf.")); |
| 201 | m_golf_mode_action->setCheckable(true); |
| 202 | |
| 203 | m_network_mode_group = new QActionGroup(this); |
| 204 | m_network_mode_group->setExclusive(true); |
| 205 | m_network_mode_group->addAction(m_fixed_delay_action); |
| 206 | m_network_mode_group->addAction(m_host_input_authority_action); |
| 207 | m_network_mode_group->addAction(m_golf_mode_action); |
| 208 | m_fixed_delay_action->setChecked(true); |
| 209 | |
| 210 | m_game_digest_menu = m_menu_bar->addMenu(tr("Checksum")); |
| 211 | m_game_digest_menu->addAction(tr("Current game"), this, [this] { |
| 212 | Settings::Instance().GetNetPlayServer()->ComputeGameDigest(m_current_game_identifier); |
| 213 | }); |
| 214 | m_game_digest_menu->addAction(tr("Other game..."), this, [this] { |
| 215 | GameListDialog gld(m_game_list_model, this); |
| 216 | |
| 217 | if (gld.exec() != QDialog::Accepted) |
| 218 | return; |
| 219 | Settings::Instance().GetNetPlayServer()->ComputeGameDigest( |
| 220 | gld.GetSelectedGame().GetSyncIdentifier()); |
| 221 | }); |
| 222 | m_game_digest_menu->addAction(tr("SD Card"), this, [] { |
| 223 | Settings::Instance().GetNetPlayServer()->ComputeGameDigest( |
| 224 | NetPlay::NetPlayClient::GetSDCardIdentifier()); |
| 225 | }); |
| 226 | |
| 227 | m_other_menu = m_menu_bar->addMenu(tr("Other")); |
| 228 | m_record_input_action = m_other_menu->addAction(tr("Record Inputs")); |
| 229 | m_record_input_action->setCheckable(true); |
| 230 | m_golf_mode_overlay_action = m_other_menu->addAction(tr("Show Golf Mode Overlay")); |
| 231 | m_golf_mode_overlay_action->setCheckable(true); |
| 232 | m_hide_remote_gbas_action = m_other_menu->addAction(tr("Hide Remote GBAs")); |
| 233 | m_hide_remote_gbas_action->setCheckable(true); |
| 234 | |
| 235 | m_game_button->setDefault(false); |
| 236 | m_game_button->setAutoDefault(false); |
| 237 | |
| 238 | m_savedata_load_only_action->setChecked(true); |
| 239 | m_sync_codes_action->setChecked(true); |
| 240 | |
| 241 | m_main_layout->setMenuBar(m_menu_bar); |
| 242 | |
| 243 | m_main_layout->addWidget(m_game_button, 0, 0, 1, -1); |
| 244 | m_main_layout->addWidget(m_splitter, 1, 0, 1, -1); |
| 245 | |
| 246 | m_splitter->addWidget(m_chat_box); |
| 247 | m_splitter->addWidget(m_players_box); |
| 248 | |
| 249 | auto* options_widget = new QGridLayout; |
| 250 | |
| 251 | options_widget->addWidget(m_start_button, 0, 0, Qt::AlignVCenter); |
| 252 | options_widget->addWidget(m_buffer_label, 0, 1, Qt::AlignVCenter); |
| 253 | options_widget->addWidget(m_buffer_size_box, 0, 2, Qt::AlignVCenter); |
| 254 | options_widget->addWidget(m_quit_button, 0, 3, Qt::AlignVCenter | Qt::AlignRight); |
| 255 | options_widget->setColumnStretch(3, 1000); |
| 256 | |
| 257 | m_main_layout->addLayout(options_widget, 2, 0, 1, -1, Qt::AlignRight); |
| 258 | m_main_layout->setRowStretch(1, 1000); |
| 259 | |
| 260 | setLayout(m_main_layout); |
| 261 | } |
| 262 | |
| 263 | void NetPlayDialog::CreateChatLayout() |
| 264 | { |
| 265 | m_chat_box = new QGroupBox(tr("Chat")); |
| 266 | m_chat_edit = new QTextBrowser; |
| 267 | m_chat_type_edit = new QLineEdit; |
| 268 | m_chat_send_button = new QPushButton(tr("Send")); |
| 269 | |
| 270 | // This button will get re-enabled when something gets entered into the chat box |
| 271 | m_chat_send_button->setEnabled(false); |
| 272 | m_chat_send_button->setDefault(false); |
| 273 | m_chat_send_button->setAutoDefault(false); |
| 274 | |
| 275 | m_chat_edit->setReadOnly(true); |
| 276 | |
| 277 | auto* layout = new QGridLayout; |
| 278 | |
| 279 | layout->addWidget(m_chat_edit, 0, 0, 1, -1); |
| 280 | layout->addWidget(m_chat_type_edit, 1, 0); |
| 281 | layout->addWidget(m_chat_send_button, 1, 1); |
| 282 | |
| 283 | m_chat_box->setLayout(layout); |
| 284 | } |
| 285 | |
| 286 | void NetPlayDialog::CreatePlayersLayout() |
| 287 | { |
| 288 | m_players_box = new QGroupBox(tr("Players")); |
| 289 | m_room_box = new QComboBox; |
| 290 | m_hostcode_label = new ClickBlurLabel; |
| 291 | m_hostcode_action_button = new QPushButton(tr("Copy")); |
| 292 | m_players_list = new QTableWidget; |
| 293 | m_kick_button = new QPushButton(tr("Kick Player")); |
| 294 | m_assign_ports_button = new QPushButton(tr("Assign Controller Ports")); |
| 295 | |
| 296 | m_players_list->setTabKeyNavigation(false); |
| 297 | m_players_list->setColumnCount(5); |
| 298 | m_players_list->verticalHeader()->hide(); |
| 299 | m_players_list->setSelectionBehavior(QAbstractItemView::SelectRows); |
| 300 | m_players_list->horizontalHeader()->setStretchLastSection(true); |
| 301 | m_players_list->horizontalHeader()->setHighlightSections(false); |
| 302 | |
| 303 | for (int i = 0; i < 4; i++) |
| 304 | m_players_list->horizontalHeader()->setSectionResizeMode(i, QHeaderView::ResizeToContents); |
| 305 | |
| 306 | auto* layout = new QGridLayout; |
| 307 | |
| 308 | layout->addWidget(m_room_box, 0, 0); |
| 309 | layout->addWidget(m_hostcode_label, 0, 1); |
| 310 | layout->addWidget(m_hostcode_action_button, 0, 2); |
| 311 | layout->addWidget(m_players_list, 1, 0, 1, -1); |
| 312 | layout->addWidget(m_kick_button, 2, 0, 1, -1); |
| 313 | layout->addWidget(m_assign_ports_button, 3, 0, 1, -1); |
| 314 | |
| 315 | m_players_box->setLayout(layout); |
| 316 | } |
| 317 | |
| 318 | void NetPlayDialog::ConnectWidgets() |
| 319 | { |
| 320 | // Players |
| 321 | connect(m_room_box, &QComboBox::currentIndexChanged, this, &NetPlayDialog::UpdateGUI); |
| 322 | connect(m_hostcode_action_button, &QPushButton::clicked, [this] { |
| 323 | if (m_is_copy_button_retry) |
| 324 | Common::g_TraversalClient->ReconnectToServer(); |
| 325 | else |
| 326 | QApplication::clipboard()->setText(m_hostcode_label->text()); |
| 327 | }); |
| 328 | connect(m_players_list, &QTableWidget::itemSelectionChanged, [this] { |
| 329 | const int row = m_players_list->currentRow(); |
| 330 | m_kick_button->setEnabled(row > 0 && |
| 331 | !m_players_list->currentItem()->data(Qt::UserRole).isNull()); |
| 332 | }); |
| 333 | connect(m_kick_button, &QPushButton::clicked, [this] { |
| 334 | const auto id = m_players_list->currentItem()->data(Qt::UserRole).toInt(); |
| 335 | Settings::Instance().GetNetPlayServer()->KickPlayer(id); |
| 336 | }); |
| 337 | connect(m_assign_ports_button, &QPushButton::clicked, [this] { |
| 338 | m_pad_mapping->exec(); |
| 339 | |
| 340 | Settings::Instance().GetNetPlayServer()->SetPadMapping(m_pad_mapping->GetGCPadArray()); |
| 341 | Settings::Instance().GetNetPlayServer()->SetGBAConfig(m_pad_mapping->GetGBAArray(), true); |
| 342 | Settings::Instance().GetNetPlayServer()->SetWiimoteMapping(m_pad_mapping->GetWiimoteArray()); |
| 343 | }); |
| 344 | |
| 345 | // Chat |
| 346 | connect(m_chat_send_button, &QPushButton::clicked, this, &NetPlayDialog::OnChat); |
| 347 | connect(m_chat_type_edit, &QLineEdit::returnPressed, this, &NetPlayDialog::OnChat); |
| 348 | connect(m_chat_type_edit, &QLineEdit::textChanged, this, |
| 349 | [this] { m_chat_send_button->setEnabled(!m_chat_type_edit->text().isEmpty()); }); |
| 350 | |
| 351 | // Other |
| 352 | connect(m_buffer_size_box, &QSpinBox::valueChanged, [this](int value) { |
| 353 | if (value == m_buffer_size) |
| 354 | return; |
| 355 | |
| 356 | const auto client = Settings::Instance().GetNetPlayClient(); |
| 357 | const auto server = Settings::Instance().GetNetPlayServer(); |
| 358 | if (server && !m_host_input_authority) |
| 359 | server->AdjustPadBufferSize(value); |
| 360 | else |
| 361 | client->AdjustPadBufferSize(value); |
| 362 | }); |
| 363 | |
| 364 | const auto hia_function = [this](bool enable) { |
| 365 | if (m_host_input_authority != enable) |
| 366 | { |
| 367 | const auto server = Settings::Instance().GetNetPlayServer(); |
| 368 | if (server) |
| 369 | server->SetHostInputAuthority(enable); |
| 370 | } |
| 371 | }; |
| 372 | |
| 373 | connect(m_host_input_authority_action, &QAction::toggled, this, |
| 374 | [hia_function] { hia_function(true); }); |
| 375 | connect(m_golf_mode_action, &QAction::toggled, this, [hia_function] { hia_function(true); }); |
| 376 | connect(m_fixed_delay_action, &QAction::toggled, this, [hia_function] { hia_function(false); }); |
| 377 | |
| 378 | connect(m_start_button, &QPushButton::clicked, this, &NetPlayDialog::OnStart); |
| 379 | connect(m_quit_button, &QPushButton::clicked, this, &NetPlayDialog::reject); |
| 380 | |
| 381 | connect(m_game_button, &QPushButton::clicked, [this] { |
| 382 | GameListDialog gld(m_game_list_model, this); |
| 383 | if (gld.exec() == QDialog::Accepted) |
| 384 | { |
| 385 | Settings& settings = Settings::Instance(); |
| 386 | |
| 387 | const UICommon::GameFile& game = gld.GetSelectedGame(); |
| 388 | const std::string netplay_name = m_game_list_model.GetNetPlayName(game); |
| 389 | |
| 390 | settings.GetNetPlayServer()->ChangeGame(game.GetSyncIdentifier(), netplay_name); |
| 391 | Settings::GetQSettings().setValue(QStringLiteral("netplay/hostgame"), |
| 392 | QString::fromStdString(netplay_name)); |
| 393 | } |
| 394 | }); |
| 395 | |
| 396 | connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, [this](Core::State state) { |
| 397 | if (isVisible()) |
| 398 | { |
| 399 | GameStatusChanged(state != Core::State::Uninitialized); |
| 400 | if ((state == Core::State::Uninitialized || state == Core::State::Stopping) && |
| 401 | !m_got_stop_request) |
| 402 | { |
| 403 | Settings::Instance().GetNetPlayClient()->RequestStopGame(); |
| 404 | } |
| 405 | if (state == Core::State::Uninitialized) |
| 406 | DisplayMessage(tr("Stopped game"), "red"); |
| 407 | } |
| 408 | }); |
| 409 | |
| 410 | // SaveSettings() - Save Hosting-Dialog Settings |
| 411 | |
| 412 | connect(m_buffer_size_box, &QSpinBox::valueChanged, this, &NetPlayDialog::SaveSettings); |
| 413 | connect(m_savedata_none_action, &QAction::toggled, this, &NetPlayDialog::SaveSettings); |
| 414 | connect(m_savedata_load_only_action, &QAction::toggled, this, &NetPlayDialog::SaveSettings); |
| 415 | connect(m_savedata_load_and_write_action, &QAction::toggled, this, &NetPlayDialog::SaveSettings); |
| 416 | connect(m_savedata_all_wii_saves_action, &QAction::toggled, this, &NetPlayDialog::SaveSettings); |
| 417 | connect(m_sync_codes_action, &QAction::toggled, this, &NetPlayDialog::SaveSettings); |
| 418 | connect(m_record_input_action, &QAction::toggled, this, &NetPlayDialog::SaveSettings); |
| 419 | connect(m_strict_settings_sync_action, &QAction::toggled, this, &NetPlayDialog::SaveSettings); |
| 420 | connect(m_host_input_authority_action, &QAction::toggled, this, &NetPlayDialog::SaveSettings); |
| 421 | connect(m_golf_mode_action, &QAction::toggled, this, &NetPlayDialog::SaveSettings); |
| 422 | connect(m_golf_mode_overlay_action, &QAction::toggled, this, &NetPlayDialog::SaveSettings); |
| 423 | connect(m_fixed_delay_action, &QAction::toggled, this, &NetPlayDialog::SaveSettings); |
| 424 | connect(m_hide_remote_gbas_action, &QAction::toggled, this, &NetPlayDialog::SaveSettings); |
| 425 | } |
| 426 | |
| 427 | void NetPlayDialog::SendMessage(const std::string& msg) |
| 428 | { |
| 429 | Settings::Instance().GetNetPlayClient()->SendChatMessage(msg); |
| 430 | |
| 431 | DisplayMessage( |
| 432 | QStringLiteral("%1: %2").arg(QString::fromStdString(m_nickname), QString::fromStdString(msg)), |
| 433 | ""); |
| 434 | } |
| 435 | |
| 436 | void NetPlayDialog::OnChat() |
| 437 | { |
| 438 | QueueOnObject(this, [this] { |
| 439 | const auto msg = m_chat_type_edit->text().toStdString(); |
| 440 | |
| 441 | if (msg.empty()) |
| 442 | return; |
| 443 | |
| 444 | m_chat_type_edit->clear(); |
| 445 | |
| 446 | SendMessage(msg); |
| 447 | }); |
| 448 | } |
| 449 | |
| 450 | void NetPlayDialog::OnIndexAdded(bool success, const std::string error) |
| 451 | { |
| 452 | DisplayMessage(success ? tr("Successfully added to the NetPlay index") : |
| 453 | tr("Failed to add this session to the NetPlay index: %1") |
| 454 | .arg(QString::fromStdString(error)), |
| 455 | success ? "green" : "red"); |
| 456 | } |
| 457 | |
| 458 | void NetPlayDialog::OnIndexRefreshFailed(const std::string error) |
| 459 | { |
| 460 | DisplayMessage(QString::fromStdString(error), "red"); |
| 461 | } |
| 462 | |
| 463 | void NetPlayDialog::OnStart() |
| 464 | { |
| 465 | if (!Settings::Instance().GetNetPlayClient()->DoAllPlayersHaveGame()) |
| 466 | { |
| 467 | if (ModalMessageBox::question( |
| 468 | this, tr("Warning"), |
| 469 | tr("Not all players have the game. Do you really want to start?")) == QMessageBox::No) |
| 470 | return; |
| 471 | } |
| 472 | |
| 473 | if (m_strict_settings_sync_action->isChecked() && Config::Get(Config::GFX_EFB_SCALE) == 0) |
| 474 | { |
| 475 | ModalMessageBox::critical( |
| 476 | this, tr("Error"), |
| 477 | tr("Auto internal resolution is not allowed in strict sync mode, as it depends on window " |
| 478 | "size.\n\nPlease select a specific internal resolution.")); |
| 479 | return; |
| 480 | } |
| 481 | |
| 482 | const auto game = FindGameFile(m_current_game_identifier); |
| 483 | if (!game) |
| 484 | { |
| 485 | PanicAlertFmtT("Selected game doesn't exist in game list!"); |
| 486 | return; |
| 487 | } |
| 488 | |
| 489 | if (Settings::Instance().GetNetPlayServer()->RequestStartGame()) |
| 490 | SetOptionsEnabled(false); |
| 491 | } |
| 492 | |
| 493 | void NetPlayDialog::reject() |
| 494 | { |
| 495 | if (ModalMessageBox::question(this, tr("Confirmation"), |
| 496 | tr("Are you sure you want to quit NetPlay?")) == QMessageBox::Yes) |
| 497 | { |
| 498 | QDialog::reject(); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | void NetPlayDialog::show(std::string nickname, bool use_traversal) |
| 503 | { |
| 504 | m_nickname = std::move(nickname); |
| 505 | m_use_traversal = use_traversal; |
| 506 | m_buffer_size = 0; |
| 507 | m_old_player_count = 0; |
| 508 | |
| 509 | m_room_box->clear(); |
| 510 | m_chat_edit->clear(); |
| 511 | m_chat_type_edit->clear(); |
| 512 | |
| 513 | const bool is_hosting = Settings::Instance().GetNetPlayServer() != nullptr; |
| 514 | |
| 515 | if (is_hosting) |
| 516 | { |
| 517 | if (use_traversal) |
| 518 | m_room_box->addItem(tr("Room ID")); |
| 519 | m_room_box->addItem(tr("External")); |
| 520 | |
| 521 | for (const auto& iface : Settings::Instance().GetNetPlayServer()->GetInterfaceSet()) |
| 522 | { |
| 523 | const auto interface = QString::fromStdString(iface); |
| 524 | m_room_box->addItem(iface == "!local!" ? tr("Local") : interface, interface); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | m_data_menu->menuAction()->setVisible(is_hosting); |
| 529 | m_network_menu->menuAction()->setVisible(is_hosting); |
| 530 | m_game_digest_menu->menuAction()->setVisible(is_hosting); |
| 531 | #ifdef HAS_LIBMGBA |
| 532 | m_hide_remote_gbas_action->setVisible(is_hosting); |
| 533 | #else |
| 534 | m_hide_remote_gbas_action->setVisible(false); |
| 535 | #endif |
| 536 | m_start_button->setHidden(!is_hosting); |
| 537 | m_kick_button->setHidden(!is_hosting); |
| 538 | m_assign_ports_button->setHidden(!is_hosting); |
| 539 | m_room_box->setHidden(!is_hosting); |
| 540 | m_hostcode_label->setHidden(!is_hosting); |
| 541 | m_hostcode_action_button->setHidden(!is_hosting); |
| 542 | m_game_button->setEnabled(is_hosting); |
| 543 | m_kick_button->setEnabled(false); |
| 544 | |
| 545 | SetOptionsEnabled(true); |
| 546 | |
| 547 | QDialog::show(); |
| 548 | UpdateGUI(); |
| 549 | } |
| 550 | |
| 551 | void NetPlayDialog::ResetExternalIP() |
| 552 | { |
| 553 | m_external_ip_address = Common::Lazy<std::string>([]() -> std::string { |
| 554 | Common::HttpRequest request; |
| 555 | // ENet does not support IPv6, so IPv4 has to be used |
| 556 | request.UseIPv4(); |
| 557 | Common::HttpRequest::Response response = |
| 558 | request.Get("https://ip.dolphin-emu.org/", {{"X-Is-Dolphin", "1"}}); |
| 559 | |
| 560 | if (response.has_value()) |
| 561 | return std::string(response->begin(), response->end()); |
| 562 | return ""; |
| 563 | }); |
| 564 | } |
| 565 | |
| 566 | void NetPlayDialog::UpdateDiscordPresence() |
| 567 | { |
| 568 | #ifdef USE_DISCORD_PRESENCE |
| 569 | // both m_current_game and m_player_count need to be set for the status to be displayed correctly |
| 570 | if (m_player_count == 0 || m_current_game_name.empty()) |
| 571 | return; |
| 572 | |
| 573 | const auto use_default = [this] { |
| 574 | Discord::UpdateDiscordPresence(m_player_count, Discord::SecretType::Empty, "", |
| 575 | m_current_game_name); |
| 576 | }; |
| 577 | |
| 578 | if (Core::IsRunning(Core::System::GetInstance())) |
| 579 | return use_default(); |
| 580 | |
| 581 | if (IsHosting()) |
| 582 | { |
| 583 | if (Common::g_TraversalClient) |
| 584 | { |
| 585 | const auto host_id = Common::g_TraversalClient->GetHostID(); |
| 586 | if (host_id[0] == '\0') |
| 587 | return use_default(); |
| 588 | |
| 589 | Discord::UpdateDiscordPresence(m_player_count, Discord::SecretType::RoomID, |
| 590 | std::string(host_id.begin(), host_id.end()), |
| 591 | m_current_game_name); |
| 592 | } |
| 593 | else |
| 594 | { |
| 595 | if (m_external_ip_address->empty()) |
| 596 | return use_default(); |
| 597 | const int port = Settings::Instance().GetNetPlayServer()->GetPort(); |
| 598 | |
| 599 | Discord::UpdateDiscordPresence( |
| 600 | m_player_count, Discord::SecretType::IPAddress, |
| 601 | Discord::CreateSecretFromIPAddress(*m_external_ip_address, port), m_current_game_name); |
| 602 | } |
| 603 | } |
| 604 | else |
| 605 | { |
| 606 | use_default(); |
| 607 | } |
| 608 | #endif |
| 609 | } |
| 610 | |
| 611 | void NetPlayDialog::UpdateGUI() |
| 612 | { |
| 613 | const auto client = Settings::Instance().GetNetPlayClient(); |
| 614 | const auto server = Settings::Instance().GetNetPlayServer(); |
| 615 | if (!client) |
| 616 | return; |
| 617 | |
| 618 | // Update Player List |
| 619 | const auto players = client->GetPlayers(); |
| 620 | |
| 621 | if (static_cast<int>(players.size()) != m_player_count && m_player_count != 0) |
| 622 | QApplication::alert(this); |
| 623 | |
| 624 | m_player_count = static_cast<int>(players.size()); |
| 625 | |
| 626 | const int selection_pid = m_players_list->currentItem() ? |
| 627 | m_players_list->currentItem()->data(Qt::UserRole).toInt() : |
| 628 | -1; |
| 629 | |
| 630 | m_players_list->clear(); |
| 631 | m_players_list->setHorizontalHeaderLabels( |
| 632 | {tr("Player"), tr("Game Status"), tr("Ping"), tr("Mapping"), tr("Revision")}); |
| 633 | m_players_list->setRowCount(m_player_count); |
| 634 | |
| 635 | static const std::map<NetPlay::SyncIdentifierComparison, std::pair<QString, QString>> |
| 636 | player_status{ |
| 637 | {NetPlay::SyncIdentifierComparison::SameGame, {tr("OK"), tr("OK")}}, |
| 638 | {NetPlay::SyncIdentifierComparison::DifferentHash, |
| 639 | {tr("Wrong hash"), |
| 640 | tr("Game file has a different hash; right-click it, select Properties, switch to the " |
| 641 | "Verify tab, and select Verify Integrity to check the hash")}}, |
| 642 | {NetPlay::SyncIdentifierComparison::DifferentDiscNumber, |
| 643 | {tr("Wrong disc number"), tr("Game has a different disc number")}}, |
| 644 | {NetPlay::SyncIdentifierComparison::DifferentRevision, |
| 645 | {tr("Wrong revision"), tr("Game has a different revision")}}, |
| 646 | {NetPlay::SyncIdentifierComparison::DifferentRegion, |
| 647 | {tr("Wrong region"), tr("Game region does not match")}}, |
| 648 | {NetPlay::SyncIdentifierComparison::DifferentGame, |
| 649 | {tr("Not found"), tr("No matching game was found")}}, |
| 650 | }; |
| 651 | |
| 652 | for (int i = 0; i < m_player_count; i++) |
| 653 | { |
| 654 | const auto* p = players[i]; |
| 655 | |
| 656 | auto* name_item = new QTableWidgetItem(QString::fromStdString(p->name)); |
| 657 | name_item->setToolTip(name_item->text()); |
| 658 | const auto it = player_status.find(p->game_status); |
| 659 | const auto& status_info = it != player_status.end() ? |
| 660 | it->second : |
| 661 | std::make_pair(QStringLiteral("?"), QStringLiteral("?")); |
| 662 | auto* status_item = new QTableWidgetItem(status_info.first); |
| 663 | status_item->setToolTip(status_info.second); |
| 664 | auto* ping_item = new QTableWidgetItem(QStringLiteral("%1 ms").arg(p->ping)); |
| 665 | ping_item->setToolTip(ping_item->text()); |
| 666 | auto* mapping_item = |
| 667 | new QTableWidgetItem(QString::fromStdString(NetPlay::GetPlayerMappingString( |
| 668 | p->pid, client->GetPadMapping(), client->GetGBAConfig(), client->GetWiimoteMapping()))); |
| 669 | mapping_item->setToolTip(mapping_item->text()); |
| 670 | auto* revision_item = new QTableWidgetItem(QString::fromStdString(p->revision)); |
| 671 | revision_item->setToolTip(revision_item->text()); |
| 672 | |
| 673 | for (auto* item : {name_item, status_item, ping_item, mapping_item, revision_item}) |
| 674 | { |
| 675 | item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); |
| 676 | item->setData(Qt::UserRole, static_cast<int>(p->pid)); |
| 677 | } |
| 678 | |
| 679 | m_players_list->setItem(i, 0, name_item); |
| 680 | m_players_list->setItem(i, 1, status_item); |
| 681 | m_players_list->setItem(i, 2, ping_item); |
| 682 | m_players_list->setItem(i, 3, mapping_item); |
| 683 | m_players_list->setItem(i, 4, revision_item); |
| 684 | |
| 685 | if (p->pid == selection_pid) |
| 686 | m_players_list->selectRow(i); |
| 687 | } |
| 688 | |
| 689 | if (m_old_player_count != m_player_count) |
| 690 | { |
| 691 | UpdateDiscordPresence(); |
| 692 | m_old_player_count = m_player_count; |
| 693 | } |
| 694 | |
| 695 | if (!server) |
| 696 | return; |
| 697 | |
| 698 | const bool is_local_ip_selected = m_room_box->currentIndex() > (m_use_traversal ? 1 : 0); |
| 699 | if (is_local_ip_selected) |
| 700 | { |
| 701 | m_hostcode_label->setText(QString::fromStdString( |
| 702 | server->GetInterfaceHost(m_room_box->currentData().toString().toStdString()))); |
| 703 | m_hostcode_action_button->setEnabled(true); |
| 704 | m_hostcode_action_button->setText(tr("Copy")); |
| 705 | m_is_copy_button_retry = false; |
| 706 | } |
| 707 | else if (m_use_traversal) |
| 708 | { |
| 709 | switch (Common::g_TraversalClient->GetState()) |
| 710 | { |
| 711 | case Common::TraversalClient::State::Connecting: |
| 712 | m_hostcode_label->setText(tr("Connecting")); |
| 713 | m_hostcode_action_button->setEnabled(false); |
| 714 | m_hostcode_action_button->setText(tr("...")); |
| 715 | break; |
| 716 | case Common::TraversalClient::State::Connected: |
| 717 | { |
| 718 | if (m_room_box->currentIndex() == 0) |
| 719 | { |
| 720 | // Display Room ID. |
| 721 | const auto host_id = Common::g_TraversalClient->GetHostID(); |
| 722 | m_hostcode_label->setText( |
| 723 | QString::fromStdString(std::string(host_id.begin(), host_id.end()))); |
| 724 | } |
| 725 | else |
| 726 | { |
| 727 | // Externally mapped IP and port are known when using the traversal server. |
| 728 | m_hostcode_label->setText( |
| 729 | InetAddressToString(Common::g_TraversalClient->GetExternalAddress())); |
| 730 | } |
| 731 | |
| 732 | m_hostcode_action_button->setEnabled(true); |
| 733 | m_hostcode_action_button->setText(tr("Copy")); |
| 734 | m_is_copy_button_retry = false; |
| 735 | break; |
| 736 | } |
| 737 | case Common::TraversalClient::State::Failure: |
| 738 | m_hostcode_label->setText(tr("Error")); |
| 739 | m_hostcode_action_button->setText(tr("Retry")); |
| 740 | m_hostcode_action_button->setEnabled(true); |
| 741 | m_is_copy_button_retry = true; |
| 742 | break; |
| 743 | } |
| 744 | } |
| 745 | else |
| 746 | { |
| 747 | // Display External IP. |
| 748 | if (!m_external_ip_address->empty()) |
| 749 | { |
| 750 | const int port = Settings::Instance().GetNetPlayServer()->GetPort(); |
| 751 | m_hostcode_label->setText(QStringLiteral("%1:%2").arg( |
| 752 | QString::fromStdString(*m_external_ip_address), QString::number(port))); |
| 753 | m_hostcode_action_button->setEnabled(true); |
| 754 | } |
| 755 | else |
| 756 | { |
| 757 | m_hostcode_label->setText(tr("Unknown")); |
| 758 | m_hostcode_action_button->setEnabled(false); |
| 759 | } |
| 760 | |
| 761 | m_hostcode_action_button->setText(tr("Copy")); |
| 762 | m_is_copy_button_retry = false; |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | // NetPlayUI methods |
| 767 | |
| 768 | void NetPlayDialog::BootGame(const std::string& filename, |
| 769 | std::unique_ptr<BootSessionData> boot_session_data) |
| 770 | { |
| 771 | m_got_stop_request = false; |
| 772 | m_start_game_callback(filename, std::move(boot_session_data)); |
| 773 | } |
| 774 | |
| 775 | void NetPlayDialog::StopGame() |
| 776 | { |
| 777 | if (m_got_stop_request) |
| 778 | return; |
| 779 | |
| 780 | m_got_stop_request = true; |
| 781 | emit Stop(); |
| 782 | } |
| 783 | |
| 784 | bool NetPlayDialog::IsHosting() const |
| 785 | { |
| 786 | return Settings::Instance().GetNetPlayServer() != nullptr; |
| 787 | } |
| 788 | |
| 789 | void NetPlayDialog::Update() |
| 790 | { |
| 791 | QueueOnObject(this, &NetPlayDialog::UpdateGUI); |
| 792 | } |
| 793 | |
| 794 | void NetPlayDialog::DisplayMessage(const QString& msg, const std::string& color, int duration) |
| 795 | { |
| 796 | QueueOnObject(m_chat_edit, [this, color, msg] { |
| 797 | m_chat_edit->append(QStringLiteral("<font color='%1'>%2</font>") |
| 798 | .arg(QString::fromStdString(color), msg.toHtmlEscaped())); |
| 799 | }); |
| 800 | |
| 801 | const QColor c(color.empty() ? QStringLiteral("white") : QString::fromStdString(color)); |
| 802 | |
| 803 | if (Config::Get(Config::GFX_SHOW_NETPLAY_MESSAGES) && |
| 804 | Core::IsRunning(Core::System::GetInstance())) |
| 805 | { |
| 806 | g_netplay_chat_ui->AppendChat(msg.toStdString(), |
| 807 | {static_cast<float>(c.redF()), static_cast<float>(c.greenF()), |
| 808 | static_cast<float>(c.blueF())}); |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | void NetPlayDialog::AppendChat(const std::string& msg) |
| 813 | { |
| 814 | DisplayMessage(QString::fromStdString(msg), ""); |
| 815 | QApplication::alert(this); |
| 816 | } |
| 817 | |
| 818 | void NetPlayDialog::OnMsgChangeGame(const NetPlay::SyncIdentifier& sync_identifier, |
| 819 | const std::string& netplay_name) |
| 820 | { |
| 821 | QString qname = QString::fromStdString(netplay_name); |
| 822 | QueueOnObject(this, [this, qname, netplay_name, &sync_identifier] { |
| 823 | m_game_button->setText(qname); |
| 824 | m_current_game_identifier = sync_identifier; |
| 825 | m_current_game_name = netplay_name; |
| 826 | UpdateDiscordPresence(); |
| 827 | }); |
| 828 | DisplayMessage(tr("Game changed to \"%1\"").arg(qname), "magenta"); |
| 829 | } |
| 830 | |
| 831 | void NetPlayDialog::OnMsgChangeGBARom(int pad, const NetPlay::GBAConfig& config) |
| 832 | { |
| 833 | if (config.has_rom) |
| 834 | { |
| 835 | DisplayMessage( |
| 836 | tr("GBA%1 ROM changed to \"%2\"").arg(pad + 1).arg(QString::fromStdString(config.title)), |
| 837 | "magenta"); |
| 838 | } |
| 839 | else |
| 840 | { |
| 841 | DisplayMessage(tr("GBA%1 ROM disabled").arg(pad + 1), "magenta"); |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | void NetPlayDialog::GameStatusChanged(bool running) |
| 846 | { |
| 847 | QueueOnObject(this, [this, running] { SetOptionsEnabled(!running); }); |
| 848 | } |
| 849 | |
| 850 | void NetPlayDialog::SetOptionsEnabled(bool enabled) |
| 851 | { |
| 852 | if (Settings::Instance().GetNetPlayServer()) |
| 853 | { |
| 854 | m_start_button->setEnabled(enabled); |
| 855 | m_game_button->setEnabled(enabled); |
| 856 | m_savedata_none_action->setEnabled(enabled); |
| 857 | m_savedata_load_only_action->setEnabled(enabled); |
| 858 | m_savedata_load_and_write_action->setEnabled(enabled); |
| 859 | m_savedata_all_wii_saves_action->setEnabled(enabled); |
| 860 | m_sync_codes_action->setEnabled(enabled); |
| 861 | m_assign_ports_button->setEnabled(enabled); |
| 862 | m_strict_settings_sync_action->setEnabled(enabled); |
| 863 | m_host_input_authority_action->setEnabled(enabled); |
| 864 | m_golf_mode_action->setEnabled(enabled); |
| 865 | m_fixed_delay_action->setEnabled(enabled); |
| 866 | } |
| 867 | |
| 868 | m_record_input_action->setEnabled(enabled); |
| 869 | } |
| 870 | |
| 871 | void NetPlayDialog::OnMsgStartGame() |
| 872 | { |
| 873 | DisplayMessage(tr("Started game"), "green"); |
| 874 | |
| 875 | g_netplay_chat_ui = |
| 876 | std::make_unique<NetPlayChatUI>([this](const std::string& message) { SendMessage(message); }); |
| 877 | |
| 878 | if (m_host_input_authority && Settings::Instance().GetNetPlayClient()->GetNetSettings().golf_mode) |
| 879 | { |
| 880 | g_netplay_golf_ui = std::make_unique<NetPlayGolfUI>(Settings::Instance().GetNetPlayClient()); |
| 881 | } |
| 882 | |
| 883 | QueueOnObject(this, [this] { |
| 884 | const auto client = Settings::Instance().GetNetPlayClient(); |
| 885 | |
| 886 | if (client) |
| 887 | { |
| 888 | if (const auto game = FindGameFile(m_current_game_identifier)) |
| 889 | client->StartGame(game->GetFilePath()); |
| 890 | else |
| 891 | PanicAlertFmtT("Selected game doesn't exist in game list!"); |
| 892 | } |
| 893 | UpdateDiscordPresence(); |
| 894 | }); |
| 895 | } |
| 896 | |
| 897 | void NetPlayDialog::OnMsgStopGame() |
| 898 | { |
| 899 | g_netplay_chat_ui.reset(); |
| 900 | g_netplay_golf_ui.reset(); |
| 901 | QueueOnObject(this, [this] { UpdateDiscordPresence(); }); |
| 902 | } |
| 903 | |
| 904 | void NetPlayDialog::OnMsgPowerButton() |
| 905 | { |
| 906 | if (!Core::IsRunning(Core::System::GetInstance())) |
| 907 | return; |
| 908 | QueueOnObject(this, [] { UICommon::TriggerSTMPowerEvent(); }); |
| 909 | } |
| 910 | |
| 911 | void NetPlayDialog::OnPlayerConnect(const std::string& player) |
| 912 | { |
| 913 | DisplayMessage(tr("%1 has joined").arg(QString::fromStdString(player)), "darkcyan"); |
| 914 | } |
| 915 | |
| 916 | void NetPlayDialog::OnPlayerDisconnect(const std::string& player) |
| 917 | { |
| 918 | DisplayMessage(tr("%1 has left").arg(QString::fromStdString(player)), "darkcyan"); |
| 919 | } |
| 920 | |
| 921 | void NetPlayDialog::OnPadBufferChanged(u32 buffer) |
| 922 | { |
| 923 | QueueOnObject(this, [this, buffer] { |
| 924 | const QSignalBlocker blocker(m_buffer_size_box); |
| 925 | m_buffer_size_box->setValue(buffer); |
| 926 | }); |
| 927 | DisplayMessage(m_host_input_authority ? tr("Max buffer size changed to %1").arg(buffer) : |
| 928 | tr("Buffer size changed to %1").arg(buffer), |
| 929 | "darkcyan"); |
| 930 | |
| 931 | m_buffer_size = static_cast<int>(buffer); |
| 932 | } |
| 933 | |
| 934 | void NetPlayDialog::OnHostInputAuthorityChanged(bool enabled) |
| 935 | { |
| 936 | m_host_input_authority = enabled; |
| 937 | DisplayMessage(enabled ? tr("Host input authority enabled") : tr("Host input authority disabled"), |
| 938 | ""); |
| 939 | |
| 940 | QueueOnObject(this, [this, enabled] { |
| 941 | const bool is_hosting = IsHosting(); |
| 942 | const bool enable_buffer = is_hosting != enabled; |
| 943 | |
| 944 | if (is_hosting) |
| 945 | { |
| 946 | m_buffer_size_box->setEnabled(enable_buffer); |
| 947 | m_buffer_label->setEnabled(enable_buffer); |
| 948 | m_buffer_size_box->setHidden(false); |
| 949 | m_buffer_label->setHidden(false); |
| 950 | } |
| 951 | else |
| 952 | { |
| 953 | m_buffer_size_box->setEnabled(true); |
| 954 | m_buffer_label->setEnabled(true); |
| 955 | m_buffer_size_box->setHidden(!enable_buffer); |
| 956 | m_buffer_label->setHidden(!enable_buffer); |
| 957 | } |
| 958 | |
| 959 | m_buffer_label->setText(enabled ? tr("Max Buffer:") : tr("Buffer:")); |
| 960 | if (enabled) |
| 961 | { |
| 962 | const QSignalBlocker blocker(m_buffer_size_box); |
| 963 | m_buffer_size_box->setValue(Config::Get(Config::NETPLAY_CLIENT_BUFFER_SIZE)); |
| 964 | } |
| 965 | }); |
| 966 | } |
| 967 | |
| 968 | void NetPlayDialog::OnDesync(u32 frame, const std::string& player) |
| 969 | { |
| 970 | DisplayMessage(tr("Possible desync detected: %1 might have desynced at frame %2") |
| 971 | .arg(QString::fromStdString(player), QString::number(frame)), |
| 972 | "red", OSD::Duration::VERY_LONG); |
| 973 | } |
| 974 | |
| 975 | void NetPlayDialog::OnConnectionLost() |
| 976 | { |
| 977 | DisplayMessage(tr("Lost connection to NetPlay server..."), "red"); |
| 978 | } |
| 979 | |
| 980 | void NetPlayDialog::OnConnectionError(const std::string& message) |
| 981 | { |
| 982 | QueueOnObject(this, [this, message] { |
| 983 | ModalMessageBox::critical(this, tr("Error"), |
| 984 | tr("Failed to connect to server: %1").arg(tr(message.c_str()))); |
| 985 | }); |
| 986 | } |
| 987 | |
| 988 | void NetPlayDialog::OnTraversalError(Common::TraversalClient::FailureReason error) |
| 989 | { |
| 990 | QueueOnObject(this, [this, error] { |
| 991 | switch (error) |
| 992 | { |
| 993 | case Common::TraversalClient::FailureReason::BadHost: |
| 994 | ModalMessageBox::critical(this, tr("Traversal Error"), tr("Couldn't look up central server")); |
| 995 | QDialog::reject(); |
| 996 | break; |
| 997 | case Common::TraversalClient::FailureReason::VersionTooOld: |
| 998 | ModalMessageBox::critical(this, tr("Traversal Error"), |
| 999 | tr("Dolphin is too old for traversal server")); |
| 1000 | QDialog::reject(); |
| 1001 | break; |
| 1002 | case Common::TraversalClient::FailureReason::ServerForgotAboutUs: |
| 1003 | case Common::TraversalClient::FailureReason::SocketSendError: |
| 1004 | case Common::TraversalClient::FailureReason::ResendTimeout: |
| 1005 | UpdateGUI(); |
| 1006 | break; |
| 1007 | } |
| 1008 | }); |
| 1009 | } |
| 1010 | |
| 1011 | void NetPlayDialog::OnTraversalStateChanged(Common::TraversalClient::State state) |
| 1012 | { |
| 1013 | switch (state) |
| 1014 | { |
| 1015 | case Common::TraversalClient::State::Connected: |
| 1016 | case Common::TraversalClient::State::Failure: |
| 1017 | UpdateDiscordPresence(); |
| 1018 | break; |
| 1019 | default: |
| 1020 | break; |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | void NetPlayDialog::OnGameStartAborted() |
| 1025 | { |
| 1026 | QueueOnObject(this, [this] { SetOptionsEnabled(true); }); |
| 1027 | } |
| 1028 | |
| 1029 | void NetPlayDialog::OnGolferChanged(const bool is_golfer, const std::string& golfer_name) |
| 1030 | { |
| 1031 | if (m_host_input_authority) |
| 1032 | { |
| 1033 | QueueOnObject(this, [this, is_golfer] { |
| 1034 | m_buffer_size_box->setEnabled(!is_golfer); |
| 1035 | m_buffer_label->setEnabled(!is_golfer); |
| 1036 | }); |
| 1037 | } |
| 1038 | |
| 1039 | if (!golfer_name.empty()) |
| 1040 | DisplayMessage(tr("%1 is now golfing").arg(QString::fromStdString(golfer_name)), ""); |
| 1041 | } |
| 1042 | |
| 1043 | void NetPlayDialog::OnTtlDetermined(u8 ttl) |
| 1044 | { |
| 1045 | DisplayMessage(tr("Using TTL %1 for probe packet").arg(QString::number(ttl)), ""); |
| 1046 | } |
| 1047 | |
| 1048 | bool NetPlayDialog::IsRecording() |
| 1049 | { |
| 1050 | const std::optional<bool> is_recording = RunOnObject(m_record_input_action, &QAction::isChecked); |
| 1051 | if (is_recording) |
| 1052 | return *is_recording; |
| 1053 | return false; |
| 1054 | } |
| 1055 | |
| 1056 | std::shared_ptr<const UICommon::GameFile> |
| 1057 | NetPlayDialog::FindGameFile(const NetPlay::SyncIdentifier& sync_identifier, |
| 1058 | NetPlay::SyncIdentifierComparison* found) |
| 1059 | { |
| 1060 | NetPlay::SyncIdentifierComparison temp; |
| 1061 | if (!found) |
| 1062 | found = &temp; |
| 1063 | |
| 1064 | *found = NetPlay::SyncIdentifierComparison::DifferentGame; |
| 1065 | |
| 1066 | const std::optional<std::shared_ptr<const UICommon::GameFile>> game_file = |
| 1067 | RunOnObject(this, [this, &sync_identifier, found] { |
| 1068 | for (int i = 0; i < m_game_list_model.rowCount(QModelIndex()); i++) |
| 1069 | { |
| 1070 | auto file = m_game_list_model.GetGameFile(i); |
| 1071 | *found = std::min(*found, file->CompareSyncIdentifier(sync_identifier)); |
| 1072 | if (*found == NetPlay::SyncIdentifierComparison::SameGame) |
| 1073 | return file; |
| 1074 | } |
| 1075 | return static_cast<std::shared_ptr<const UICommon::GameFile>>(nullptr); |
| 1076 | }); |
| 1077 | if (game_file) |
| 1078 | return *game_file; |
| 1079 | return nullptr; |
| 1080 | } |
| 1081 | |
| 1082 | std::string NetPlayDialog::FindGBARomPath(const std::array<u8, 20>& hash, std::string_view title, |
| 1083 | int device_number) |
| 1084 | { |
| 1085 | #ifdef HAS_LIBMGBA |
| 1086 | const auto result = RunOnObject(this, [&, this] { |
| 1087 | std::string rom_path; |
| 1088 | std::array<u8, 20> rom_hash; |
| 1089 | std::string rom_title; |
| 1090 | for (size_t i = device_number; i < static_cast<size_t>(device_number) + 4; ++i) |
| 1091 | { |
| 1092 | rom_path = Config::Get(Config::MAIN_GBA_ROM_PATHS[i % 4]); |
| 1093 | if (!rom_path.empty() && HW::GBA::Core::GetRomInfo(rom_path.c_str(), rom_hash, rom_title) && |
| 1094 | rom_hash == hash && rom_title == title) |
| 1095 | { |
| 1096 | return rom_path; |
| 1097 | } |
| 1098 | } |
| 1099 | while (!(rom_path = GameCubePane::GetOpenGBARom(title)).empty()) |
| 1100 | { |
| 1101 | if (HW::GBA::Core::GetRomInfo(rom_path.c_str(), rom_hash, rom_title)) |
| 1102 | { |
| 1103 | if (rom_hash == hash && rom_title == title) |
| 1104 | return rom_path; |
| 1105 | ModalMessageBox::critical( |
| 1106 | this, tr("Error"), |
| 1107 | QString::fromStdString(Common::FmtFormatT( |
| 1108 | "Mismatched ROMs\n" |
| 1109 | "Selected: {0}\n- Title: {1}\n- Hash: {2:02X}\n" |
| 1110 | "Expected:\n- Title: {3}\n- Hash: {4:02X}", |
| 1111 | rom_path, rom_title, fmt::join(rom_hash, ""), title, fmt::join(hash, "")))); |
| 1112 | } |
| 1113 | else |
| 1114 | { |
| 1115 | ModalMessageBox::critical( |
| 1116 | this, tr("Error"), tr("%1 is not a valid ROM").arg(QString::fromStdString(rom_path))); |
| 1117 | } |
| 1118 | } |
| 1119 | return std::string(); |
| 1120 | }); |
| 1121 | if (result) |
| 1122 | return *result; |
| 1123 | #endif |
| 1124 | return {}; |
| 1125 | } |
| 1126 | |
| 1127 | void NetPlayDialog::LoadSettings() |
| 1128 | { |
| 1129 | const int buffer_size = Config::Get(Config::NETPLAY_BUFFER_SIZE); |
| 1130 | const bool savedata_load = Config::Get(Config::NETPLAY_SAVEDATA_LOAD); |
| 1131 | const bool savedata_write = Config::Get(Config::NETPLAY_SAVEDATA_WRITE); |
| 1132 | const bool sync_all_wii_saves = Config::Get(Config::NETPLAY_SAVEDATA_SYNC_ALL_WII); |
| 1133 | const bool sync_codes = Config::Get(Config::NETPLAY_SYNC_CODES); |
| 1134 | const bool record_inputs = Config::Get(Config::NETPLAY_RECORD_INPUTS); |
| 1135 | const bool strict_settings_sync = Config::Get(Config::NETPLAY_STRICT_SETTINGS_SYNC); |
| 1136 | const bool golf_mode_overlay = Config::Get(Config::NETPLAY_GOLF_MODE_OVERLAY); |
| 1137 | const bool hide_remote_gbas = Config::Get(Config::NETPLAY_HIDE_REMOTE_GBAS); |
| 1138 | |
| 1139 | m_buffer_size_box->setValue(buffer_size); |
| 1140 | |
| 1141 | if (!savedata_load) |
| 1142 | m_savedata_none_action->setChecked(true); |
| 1143 | else if (!savedata_write) |
| 1144 | m_savedata_load_only_action->setChecked(true); |
| 1145 | else |
| 1146 | m_savedata_load_and_write_action->setChecked(true); |
| 1147 | m_savedata_all_wii_saves_action->setChecked(sync_all_wii_saves); |
| 1148 | |
| 1149 | m_sync_codes_action->setChecked(sync_codes); |
| 1150 | m_record_input_action->setChecked(record_inputs); |
| 1151 | m_strict_settings_sync_action->setChecked(strict_settings_sync); |
| 1152 | m_golf_mode_overlay_action->setChecked(golf_mode_overlay); |
| 1153 | m_hide_remote_gbas_action->setChecked(hide_remote_gbas); |
| 1154 | |
| 1155 | const std::string network_mode = Config::Get(Config::NETPLAY_NETWORK_MODE); |
| 1156 | |
| 1157 | if (network_mode == "fixeddelay") |
| 1158 | { |
| 1159 | m_fixed_delay_action->setChecked(true); |
| 1160 | } |
| 1161 | else if (network_mode == "hostinputauthority") |
| 1162 | { |
| 1163 | m_host_input_authority_action->setChecked(true); |
| 1164 | } |
| 1165 | else if (network_mode == "golf") |
| 1166 | { |
| 1167 | m_golf_mode_action->setChecked(true); |
| 1168 | } |
| 1169 | else |
| 1170 | { |
| 1171 | WARN_LOG_FMT(NETPLAY, "Unknown network mode '{}', using 'fixeddelay'", network_mode); |
| 1172 | m_fixed_delay_action->setChecked(true); |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | void NetPlayDialog::SaveSettings() |
| 1177 | { |
| 1178 | Config::ConfigChangeCallbackGuard config_guard; |
| 1179 | |
| 1180 | if (m_host_input_authority) |
| 1181 | Config::SetBase(Config::NETPLAY_CLIENT_BUFFER_SIZE, m_buffer_size_box->value()); |
| 1182 | else |
| 1183 | Config::SetBase(Config::NETPLAY_BUFFER_SIZE, m_buffer_size_box->value()); |
| 1184 | |
| 1185 | const bool write_savedata = m_savedata_load_and_write_action->isChecked(); |
| 1186 | const bool load_savedata = write_savedata || m_savedata_load_only_action->isChecked(); |
| 1187 | Config::SetBase(Config::NETPLAY_SAVEDATA_LOAD, load_savedata); |
| 1188 | Config::SetBase(Config::NETPLAY_SAVEDATA_WRITE, write_savedata); |
| 1189 | |
| 1190 | Config::SetBase(Config::NETPLAY_SAVEDATA_SYNC_ALL_WII, |
| 1191 | m_savedata_all_wii_saves_action->isChecked()); |
| 1192 | Config::SetBase(Config::NETPLAY_SYNC_CODES, m_sync_codes_action->isChecked()); |
| 1193 | Config::SetBase(Config::NETPLAY_RECORD_INPUTS, m_record_input_action->isChecked()); |
| 1194 | Config::SetBase(Config::NETPLAY_STRICT_SETTINGS_SYNC, m_strict_settings_sync_action->isChecked()); |
| 1195 | Config::SetBase(Config::NETPLAY_GOLF_MODE_OVERLAY, m_golf_mode_overlay_action->isChecked()); |
| 1196 | Config::SetBase(Config::NETPLAY_HIDE_REMOTE_GBAS, m_hide_remote_gbas_action->isChecked()); |
| 1197 | |
| 1198 | std::string network_mode; |
| 1199 | if (m_fixed_delay_action->isChecked()) |
| 1200 | { |
| 1201 | network_mode = "fixeddelay"; |
| 1202 | } |
| 1203 | else if (m_host_input_authority_action->isChecked()) |
| 1204 | { |
| 1205 | network_mode = "hostinputauthority"; |
| 1206 | } |
| 1207 | else if (m_golf_mode_action->isChecked()) |
| 1208 | { |
| 1209 | network_mode = "golf"; |
| 1210 | } |
| 1211 | |
| 1212 | Config::SetBase(Config::NETPLAY_NETWORK_MODE, network_mode); |
| 1213 | } |
| 1214 | |
| 1215 | void NetPlayDialog::ShowGameDigestDialog(const std::string& title) |
| 1216 | { |
| 1217 | QueueOnObject(this, [this, title] { |
| 1218 | m_game_digest_menu->setEnabled(false); |
| 1219 | |
| 1220 | if (m_game_digest_dialog->isVisible()) |
| 1221 | m_game_digest_dialog->close(); |
| 1222 | |
| 1223 | m_game_digest_dialog->show(QString::fromStdString(title)); |
| 1224 | }); |
| 1225 | } |
| 1226 | |
| 1227 | void NetPlayDialog::SetGameDigestProgress(int pid, int progress) |
| 1228 | { |
| 1229 | QueueOnObject(this, [this, pid, progress] { |
| 1230 | if (m_game_digest_dialog->isVisible()) |
| 1231 | m_game_digest_dialog->SetProgress(pid, progress); |
| 1232 | }); |
| 1233 | } |
| 1234 | |
| 1235 | void NetPlayDialog::SetGameDigestResult(int pid, const std::string& result) |
| 1236 | { |
| 1237 | QueueOnObject(this, [this, pid, result] { |
| 1238 | m_game_digest_dialog->SetResult(pid, result); |
| 1239 | m_game_digest_menu->setEnabled(true); |
| 1240 | }); |
| 1241 | } |
| 1242 | |
| 1243 | void NetPlayDialog::AbortGameDigest() |
| 1244 | { |
| 1245 | QueueOnObject(this, [this] { |
| 1246 | m_game_digest_dialog->close(); |
| 1247 | m_game_digest_menu->setEnabled(true); |
| 1248 | }); |
| 1249 | } |
| 1250 | |
| 1251 | void NetPlayDialog::ShowChunkedProgressDialog(const std::string& title, const u64 data_size, |
| 1252 | std::span<const int> players) |
| 1253 | { |
| 1254 | QueueOnObject(this, [this, title, data_size, players] { |
| 1255 | if (m_chunked_progress_dialog->isVisible()) |
| 1256 | m_chunked_progress_dialog->done(QDialog::Accepted); |
| 1257 | |
| 1258 | m_chunked_progress_dialog->show(QString::fromStdString(title), data_size, players); |
| 1259 | }); |
| 1260 | } |
| 1261 | |
| 1262 | void NetPlayDialog::HideChunkedProgressDialog() |
| 1263 | { |
| 1264 | QueueOnObject(this, [this] { m_chunked_progress_dialog->done(QDialog::Accepted); }); |
| 1265 | } |
| 1266 | |
| 1267 | void NetPlayDialog::SetChunkedProgress(const int pid, const u64 progress) |
| 1268 | { |
| 1269 | QueueOnObject(this, [this, pid, progress] { |
| 1270 | if (m_chunked_progress_dialog->isVisible()) |
| 1271 | m_chunked_progress_dialog->SetProgress(pid, progress); |
| 1272 | }); |
| 1273 | } |
| 1274 | |
| 1275 | void NetPlayDialog::SetHostWiiSyncData(std::vector<u64> titles, std::string redirect_folder) |
| 1276 | { |
| 1277 | const auto client = Settings::Instance().GetNetPlayClient(); |
| 1278 | if (client) |
| 1279 | client->SetWiiSyncData(nullptr, std::move(titles), std::move(redirect_folder)); |
| 1280 | } |