| 1 | // Copyright 2023 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/InfinityBase/InfinityBaseWindow.h" |
| 5 | |
| 6 | #include <string> |
| 7 | |
| 8 | #include <QCheckBox> |
| 9 | #include <QComboBox> |
| 10 | #include <QCompleter> |
| 11 | #include <QDialogButtonBox> |
| 12 | #include <QGroupBox> |
| 13 | #include <QLabel> |
| 14 | #include <QLineEdit> |
| 15 | #include <QMessageBox> |
| 16 | #include <QPushButton> |
| 17 | #include <QScrollArea> |
| 18 | #include <QString> |
| 19 | #include <QVBoxLayout> |
| 20 | |
| 21 | #include "Common/IOFile.h" |
| 22 | |
| 23 | #include "Core/Config/MainSettings.h" |
| 24 | #include "Core/Core.h" |
| 25 | #include "Core/IOS/USB/Emulated/Infinity.h" |
| 26 | #include "Core/System.h" |
| 27 | |
| 28 | #include "DolphinQt/QtUtils/DolphinFileDialog.h" |
| 29 | #include "DolphinQt/Resources.h" |
| 30 | #include "DolphinQt/Settings.h" |
| 31 | |
| 32 | // Qt is not guaranteed to keep track of file paths using native file pickers, so we use this |
| 33 | // static variable to ensure we open at the most recent figure file location |
| 34 | static QString s_last_figure_path; |
| 35 | |
| 36 | using FigureUIPosition = IOS::HLE::USB::FigureUIPosition; |
| 37 | |
| 38 | InfinityBaseWindow::InfinityBaseWindow(QWidget* parent) : QWidget(parent) |
| 39 | { |
| 40 | // i18n: Window for managing Disney Infinity figures |
| 41 | setWindowTitle(tr("Infinity Manager")); |
| 42 | setWindowIcon(Resources::GetAppIcon()); |
| 43 | setObjectName(QStringLiteral("infinity_manager")); |
| 44 | setMinimumSize(QSize(700, 200)); |
| 45 | |
| 46 | CreateMainWindow(); |
| 47 | |
| 48 | connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, |
| 49 | &InfinityBaseWindow::OnEmulationStateChanged); |
| 50 | |
| 51 | installEventFilter(this); |
| 52 | |
| 53 | OnEmulationStateChanged(Core::GetState(Core::System::GetInstance())); |
| 54 | } |
| 55 | |
| 56 | InfinityBaseWindow::~InfinityBaseWindow() = default; |
| 57 | |
| 58 | void InfinityBaseWindow::CreateMainWindow() |
| 59 | { |
| 60 | auto* main_layout = new QVBoxLayout(); |
| 61 | |
| 62 | auto* checkbox_group = new QGroupBox(); |
| 63 | auto* checkbox_layout = new QHBoxLayout(); |
| 64 | checkbox_layout->setAlignment(Qt::AlignHCenter); |
| 65 | m_checkbox = new QCheckBox(tr("Emulate Infinity Base"), this); |
| 66 | m_checkbox->setChecked(Config::Get(Config::MAIN_EMULATE_INFINITY_BASE)); |
| 67 | connect(m_checkbox, &QCheckBox::toggled, this, &InfinityBaseWindow::EmulateBase); |
| 68 | checkbox_layout->addWidget(m_checkbox); |
| 69 | checkbox_group->setLayout(checkbox_layout); |
| 70 | main_layout->addWidget(checkbox_group); |
| 71 | |
| 72 | auto add_line = [](QVBoxLayout* vbox) { |
| 73 | auto* line = new QFrame(); |
| 74 | line->setFrameShape(QFrame::HLine); |
| 75 | line->setFrameShadow(QFrame::Sunken); |
| 76 | vbox->addWidget(line); |
| 77 | }; |
| 78 | |
| 79 | m_group_figures = new QGroupBox(tr("Active Infinity Figures:")); |
| 80 | auto* vbox_group = new QVBoxLayout(); |
| 81 | auto* scroll_area = new QScrollArea(); |
| 82 | |
| 83 | AddFigureSlot(vbox_group, tr("Play Set/Power Disc"), FigureUIPosition::HexagonDiscOne); |
| 84 | add_line(vbox_group); |
| 85 | AddFigureSlot(vbox_group, tr("Power Disc Two"), FigureUIPosition::HexagonDiscTwo); |
| 86 | add_line(vbox_group); |
| 87 | AddFigureSlot(vbox_group, tr("Power Disc Three"), FigureUIPosition::HexagonDiscThree); |
| 88 | add_line(vbox_group); |
| 89 | AddFigureSlot(vbox_group, tr("Player One"), FigureUIPosition::PlayerOne); |
| 90 | add_line(vbox_group); |
| 91 | AddFigureSlot(vbox_group, tr("Player One Ability One"), FigureUIPosition::P1AbilityOne); |
| 92 | add_line(vbox_group); |
| 93 | AddFigureSlot(vbox_group, tr("Player One Ability Two"), FigureUIPosition::P1AbilityTwo); |
| 94 | add_line(vbox_group); |
| 95 | AddFigureSlot(vbox_group, tr("Player Two"), FigureUIPosition::PlayerTwo); |
| 96 | add_line(vbox_group); |
| 97 | AddFigureSlot(vbox_group, tr("Player Two Ability One"), FigureUIPosition::P2AbilityOne); |
| 98 | add_line(vbox_group); |
| 99 | AddFigureSlot(vbox_group, tr("Player Two Ability Two"), FigureUIPosition::P2AbilityTwo); |
| 100 | |
| 101 | m_group_figures->setLayout(vbox_group); |
| 102 | scroll_area->setWidget(m_group_figures); |
| 103 | scroll_area->setWidgetResizable(true); |
| 104 | m_group_figures->setVisible(Config::Get(Config::MAIN_EMULATE_INFINITY_BASE)); |
| 105 | main_layout->addWidget(scroll_area); |
| 106 | setLayout(main_layout); |
| 107 | } |
| 108 | |
| 109 | void InfinityBaseWindow::AddFigureSlot(QVBoxLayout* vbox_group, const QString& name, |
| 110 | FigureUIPosition slot) |
| 111 | { |
| 112 | auto* hbox_infinity = new QHBoxLayout(); |
| 113 | |
| 114 | auto* label_skyname = new QLabel(name); |
| 115 | |
| 116 | auto* clear_btn = new QPushButton(tr("Clear")); |
| 117 | auto* create_btn = new QPushButton(tr("Create")); |
| 118 | auto* load_btn = new QPushButton(tr("Load")); |
| 119 | m_edit_figures[static_cast<u8>(slot)] = new QLineEdit(); |
| 120 | m_edit_figures[static_cast<u8>(slot)]->setEnabled(false); |
| 121 | m_edit_figures[static_cast<u8>(slot)]->setText(tr("None")); |
| 122 | |
| 123 | connect(clear_btn, &QAbstractButton::clicked, this, [this, slot] { ClearFigure(slot); }); |
| 124 | connect(create_btn, &QAbstractButton::clicked, this, [this, slot] { CreateFigure(slot); }); |
| 125 | connect(load_btn, &QAbstractButton::clicked, this, [this, slot] { LoadFigure(slot); }); |
| 126 | |
| 127 | hbox_infinity->addWidget(label_skyname); |
| 128 | hbox_infinity->addWidget(m_edit_figures[static_cast<u8>(slot)]); |
| 129 | hbox_infinity->addWidget(clear_btn); |
| 130 | hbox_infinity->addWidget(create_btn); |
| 131 | hbox_infinity->addWidget(load_btn); |
| 132 | |
| 133 | vbox_group->addLayout(hbox_infinity); |
| 134 | } |
| 135 | |
| 136 | void InfinityBaseWindow::ClearFigure(FigureUIPosition slot) |
| 137 | { |
| 138 | auto& system = Core::System::GetInstance(); |
| 139 | m_edit_figures[static_cast<u8>(slot)]->setText(tr("None")); |
| 140 | |
| 141 | system.GetInfinityBase().RemoveFigure(slot); |
| 142 | } |
| 143 | |
| 144 | void InfinityBaseWindow::LoadFigure(FigureUIPosition slot) |
| 145 | { |
| 146 | const QString file_path = |
| 147 | DolphinFileDialog::getOpenFileName(this, tr("Select Figure File"), s_last_figure_path, |
| 148 | QStringLiteral("Infinity Figure (*.bin);;")); |
| 149 | if (file_path.isEmpty()) |
| 150 | { |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | s_last_figure_path = QFileInfo(file_path).absolutePath() + QLatin1Char('/'); |
| 155 | |
| 156 | LoadFigurePath(slot, file_path); |
| 157 | } |
| 158 | |
| 159 | void InfinityBaseWindow::CreateFigure(FigureUIPosition slot) |
| 160 | { |
| 161 | CreateFigureDialog create_dlg(this, slot); |
| 162 | if (create_dlg.exec() == CreateFigureDialog::Accepted) |
| 163 | { |
| 164 | LoadFigurePath(slot, create_dlg.GetFilePath()); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void InfinityBaseWindow::LoadFigurePath(FigureUIPosition slot, const QString& path) |
| 169 | { |
| 170 | File::IOFile inf_file(path.toStdString(), "r+b"); |
| 171 | if (!inf_file) |
| 172 | { |
| 173 | QMessageBox::warning( |
| 174 | this, tr("Failed to open the Infinity file!"), |
| 175 | tr("Failed to open the Infinity file:\n%1\n\nThe file may already be in use on the base.") |
| 176 | .arg(path), |
| 177 | QMessageBox::Ok); |
| 178 | return; |
| 179 | } |
| 180 | std::array<u8, IOS::HLE::USB::INFINITY_NUM_BLOCKS * IOS::HLE::USB::INFINITY_BLOCK_SIZE> file_data; |
| 181 | if (!inf_file.ReadBytes(file_data.data(), file_data.size())) |
| 182 | { |
| 183 | QMessageBox::warning( |
| 184 | this, tr("Failed to read the Infinity file!"), |
| 185 | tr("Failed to read the Infinity file:\n%1\n\nThe file was too small.").arg(path), |
| 186 | QMessageBox::Ok); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | auto& system = Core::System::GetInstance(); |
| 191 | |
| 192 | system.GetInfinityBase().RemoveFigure(slot); |
| 193 | m_edit_figures[static_cast<u8>(slot)]->setText(QString::fromStdString( |
| 194 | system.GetInfinityBase().LoadFigure(file_data, std::move(inf_file), slot))); |
| 195 | } |
| 196 | |
| 197 | CreateFigureDialog::CreateFigureDialog(QWidget* parent, FigureUIPosition slot) : QDialog(parent) |
| 198 | { |
| 199 | setWindowTitle(tr("Infinity Figure Creator")); |
| 200 | setObjectName(QStringLiteral("infinity_creator")); |
| 201 | setMinimumSize(QSize(500, 150)); |
| 202 | auto* layout = new QVBoxLayout; |
| 203 | |
| 204 | auto* combo_figlist = new QComboBox(); |
| 205 | QStringList filterlist; |
| 206 | u32 first_entry = 0; |
| 207 | for (const auto& entry : IOS::HLE::USB::InfinityBase::GetFigureList()) |
| 208 | { |
| 209 | const auto figure = entry.second; |
| 210 | // Only display entry if it is a piece appropriate for the slot |
| 211 | if ((slot == FigureUIPosition::HexagonDiscOne && |
| 212 | ((figure > 0x1E8480 && figure < 0x2DC6BF) || (figure > 0x3D0900 && figure < 0x4C4B3F))) || |
| 213 | ((slot == FigureUIPosition::HexagonDiscTwo || slot == FigureUIPosition::HexagonDiscThree) && |
| 214 | (figure > 0x3D0900 && figure < 0x4C4B3F)) || |
| 215 | ((slot == FigureUIPosition::PlayerOne || slot == FigureUIPosition::PlayerTwo) && |
| 216 | figure < 0x1E847F) || |
| 217 | ((slot == FigureUIPosition::P1AbilityOne || slot == FigureUIPosition::P1AbilityTwo || |
| 218 | slot == FigureUIPosition::P2AbilityOne || slot == FigureUIPosition::P2AbilityTwo) && |
| 219 | (figure > 0x2DC6C0 && figure < 0x3D08FF))) |
| 220 | { |
| 221 | const auto figure_name = QString::fromStdString(entry.first); |
| 222 | combo_figlist->addItem(figure_name, QVariant(figure)); |
| 223 | filterlist << figure_name; |
| 224 | if (first_entry == 0) |
| 225 | { |
| 226 | first_entry = figure; |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | combo_figlist->addItem(tr("--Unknown--"), QVariant(0xFFFF)); |
| 231 | combo_figlist->setEditable(true); |
| 232 | combo_figlist->setInsertPolicy(QComboBox::NoInsert); |
| 233 | |
| 234 | auto* co_compl = new QCompleter(filterlist, this); |
| 235 | co_compl->setCaseSensitivity(Qt::CaseInsensitive); |
| 236 | co_compl->setCompletionMode(QCompleter::PopupCompletion); |
| 237 | co_compl->setFilterMode(Qt::MatchContains); |
| 238 | combo_figlist->setCompleter(co_compl); |
| 239 | |
| 240 | layout->addWidget(combo_figlist); |
| 241 | |
| 242 | auto* line = new QFrame(); |
| 243 | line->setFrameShape(QFrame::HLine); |
| 244 | line->setFrameShadow(QFrame::Sunken); |
| 245 | layout->addWidget(line); |
| 246 | |
| 247 | auto* hbox_idvar = new QHBoxLayout(); |
| 248 | auto* label_id = new QLabel(tr("Figure Number:")); |
| 249 | auto* edit_num = new QLineEdit(QString::number(first_entry)); |
| 250 | auto* rxv = new QRegularExpressionValidator(QRegularExpression(QStringLiteral("\\d*")), this); |
| 251 | edit_num->setValidator(rxv); |
| 252 | hbox_idvar->addWidget(label_id); |
| 253 | hbox_idvar->addWidget(edit_num); |
| 254 | layout->addLayout(hbox_idvar); |
| 255 | |
| 256 | auto* buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); |
| 257 | buttons->button(QDialogButtonBox::Ok)->setText(tr("Create")); |
| 258 | layout->addWidget(buttons); |
| 259 | |
| 260 | setLayout(layout); |
| 261 | |
| 262 | connect(combo_figlist, &QComboBox::currentIndexChanged, [=](int index) { |
| 263 | const u32 char_info = combo_figlist->itemData(index).toUInt(); |
| 264 | if (char_info != 0xFFFFFFFF) |
| 265 | { |
| 266 | edit_num->setText(QString::number(char_info)); |
| 267 | } |
| 268 | }); |
| 269 | |
| 270 | connect(buttons, &QDialogButtonBox::accepted, this, [=, this] { |
| 271 | bool ok_char = false; |
| 272 | const u32 char_number = edit_num->text().toULong(&ok_char); |
| 273 | if (!ok_char) |
| 274 | { |
| 275 | QMessageBox::warning(this, tr("Error converting value"), tr("Character entered is invalid!"), |
| 276 | QMessageBox::Ok); |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | QString predef_name = s_last_figure_path; |
| 281 | |
| 282 | auto& system = Core::System::GetInstance(); |
| 283 | const auto found_fig = system.GetInfinityBase().FindFigure(char_number); |
| 284 | if (!found_fig.empty()) |
| 285 | { |
| 286 | predef_name += QString::fromStdString(found_fig + ".bin"); |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | // i18n: This is used to create a file name. The string must end in ".bin". |
| 291 | QString str = tr("Unknown(%1).bin"); |
| 292 | predef_name += str.arg(char_number); |
| 293 | } |
| 294 | |
| 295 | m_file_path = DolphinFileDialog::getSaveFileName(this, tr("Create Infinity File"), predef_name, |
| 296 | tr("Infinity Object (*.bin);;")); |
| 297 | if (m_file_path.isEmpty()) |
| 298 | { |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | if (!system.GetInfinityBase().CreateFigure(m_file_path.toStdString(), char_number)) |
| 303 | { |
| 304 | QMessageBox::warning( |
| 305 | this, tr("Failed to create Infinity file"), |
| 306 | tr("Blank figure creation failed at:\n%1\n\nTry again with a different character.") |
| 307 | .arg(m_file_path), |
| 308 | QMessageBox::Ok); |
| 309 | return; |
| 310 | } |
| 311 | s_last_figure_path = QFileInfo(m_file_path).absolutePath() + QLatin1Char('/'); |
| 312 | accept(); |
| 313 | }); |
| 314 | |
| 315 | connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject); |
| 316 | |
| 317 | connect(co_compl, QOverload<const QString&>::of(&QCompleter::activated), |
| 318 | [=](const QString& text) { |
| 319 | combo_figlist->setCurrentText(text); |
| 320 | combo_figlist->setCurrentIndex(combo_figlist->findText(text)); |
| 321 | }); |
| 322 | } |
| 323 | |
| 324 | QString CreateFigureDialog::GetFilePath() const |
| 325 | { |
| 326 | return m_file_path; |
| 327 | } |
| 328 | |
| 329 | void InfinityBaseWindow::EmulateBase(bool emulate) |
| 330 | { |
| 331 | Config::SetBaseOrCurrent(Config::MAIN_EMULATE_INFINITY_BASE, emulate); |
| 332 | m_group_figures->setVisible(emulate); |
| 333 | } |
| 334 | |
| 335 | void InfinityBaseWindow::OnEmulationStateChanged(Core::State state) |
| 336 | { |
| 337 | const bool running = state != Core::State::Uninitialized; |
| 338 | |
| 339 | m_checkbox->setEnabled(!running); |
| 340 | } |