2017-09-10 17:10:45 +00:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-09-10 17:10:45 +00:00
|
|
|
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/Config/GeckoCodeWidget.h"
|
2017-09-10 17:10:45 +00:00
|
|
|
|
2023-04-26 08:48:59 +00:00
|
|
|
#include <algorithm>
|
2021-06-06 19:25:37 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2019-06-21 07:28:24 +00:00
|
|
|
#include <QCursor>
|
2017-09-10 17:10:45 +00:00
|
|
|
#include <QFontDatabase>
|
|
|
|
#include <QFormLayout>
|
2018-01-24 12:36:16 +00:00
|
|
|
#include <QHBoxLayout>
|
2017-09-10 17:10:45 +00:00
|
|
|
#include <QLabel>
|
|
|
|
#include <QListWidget>
|
2019-06-21 07:28:24 +00:00
|
|
|
#include <QMenu>
|
2017-09-10 17:10:45 +00:00
|
|
|
#include <QPushButton>
|
|
|
|
#include <QTextEdit>
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/IniFile.h"
|
2018-05-28 01:48:04 +00:00
|
|
|
|
2017-09-10 17:10:45 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
2019-07-31 13:42:30 +00:00
|
|
|
#include "Core/GeckoCode.h"
|
2017-09-10 17:10:45 +00:00
|
|
|
#include "Core/GeckoCodeConfig.h"
|
2018-05-28 01:48:04 +00:00
|
|
|
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/Config/CheatCodeEditor.h"
|
|
|
|
#include "DolphinQt/Config/CheatWarningWidget.h"
|
2019-03-04 19:49:00 +00:00
|
|
|
#include "DolphinQt/QtUtils/ModalMessageBox.h"
|
2022-03-08 07:51:29 +00:00
|
|
|
#include "DolphinQt/QtUtils/NonDefaultQPushButton.h"
|
2018-05-28 01:48:04 +00:00
|
|
|
|
2017-12-31 19:33:36 +00:00
|
|
|
#include "UICommon/GameFile.h"
|
2017-09-10 17:10:45 +00:00
|
|
|
|
2021-06-06 19:25:37 +00:00
|
|
|
GeckoCodeWidget::GeckoCodeWidget(std::string game_id, std::string gametdb_id, u16 game_revision,
|
|
|
|
bool restart_required)
|
|
|
|
: m_game_id(std::move(game_id)), m_gametdb_id(std::move(gametdb_id)),
|
|
|
|
m_game_revision(game_revision), m_restart_required(restart_required)
|
2017-09-10 17:10:45 +00:00
|
|
|
{
|
|
|
|
CreateWidgets();
|
|
|
|
ConnectWidgets();
|
|
|
|
|
2021-09-16 05:17:37 +00:00
|
|
|
if (!m_game_id.empty())
|
|
|
|
{
|
2023-04-13 13:38:09 +00:00
|
|
|
Common::IniFile game_ini_local;
|
2017-09-10 17:10:45 +00:00
|
|
|
|
2021-09-16 05:17:37 +00:00
|
|
|
// We don't use LoadLocalGameIni() here because user cheat codes that are installed via the UI
|
|
|
|
// will always be stored in GS/${GAMEID}.ini
|
|
|
|
game_ini_local.Load(File::GetUserPath(D_GAMESETTINGS_IDX) + m_game_id + ".ini");
|
2017-09-10 17:10:45 +00:00
|
|
|
|
2023-04-13 13:38:09 +00:00
|
|
|
const Common::IniFile game_ini_default =
|
|
|
|
SConfig::LoadDefaultGameIni(m_game_id, m_game_revision);
|
2021-09-16 05:17:37 +00:00
|
|
|
m_gecko_codes = Gecko::LoadCodes(game_ini_default, game_ini_local);
|
|
|
|
}
|
2017-09-10 17:10:45 +00:00
|
|
|
|
|
|
|
UpdateList();
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:42:30 +00:00
|
|
|
GeckoCodeWidget::~GeckoCodeWidget() = default;
|
|
|
|
|
2017-09-10 17:10:45 +00:00
|
|
|
void GeckoCodeWidget::CreateWidgets()
|
|
|
|
{
|
2018-05-18 05:17:30 +00:00
|
|
|
m_warning = new CheatWarningWidget(m_game_id, m_restart_required, this);
|
2017-09-10 17:10:45 +00:00
|
|
|
m_code_list = new QListWidget;
|
|
|
|
m_name_label = new QLabel;
|
|
|
|
m_creator_label = new QLabel;
|
|
|
|
|
2019-06-21 07:28:24 +00:00
|
|
|
m_code_list->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
|
2017-09-10 17:10:45 +00:00
|
|
|
QFont monospace(QFontDatabase::systemFont(QFontDatabase::FixedFont).family());
|
|
|
|
|
|
|
|
const auto line_height = QFontMetrics(font()).lineSpacing();
|
|
|
|
|
|
|
|
m_code_description = new QTextEdit;
|
|
|
|
m_code_description->setFont(monospace);
|
|
|
|
m_code_description->setReadOnly(true);
|
|
|
|
m_code_description->setFixedHeight(line_height * 5);
|
|
|
|
|
|
|
|
m_code_view = new QTextEdit;
|
|
|
|
m_code_view->setFont(monospace);
|
|
|
|
m_code_view->setReadOnly(true);
|
|
|
|
m_code_view->setFixedHeight(line_height * 10);
|
|
|
|
|
2022-03-08 07:51:29 +00:00
|
|
|
m_add_code = new NonDefaultQPushButton(tr("&Add New Code..."));
|
|
|
|
m_edit_code = new NonDefaultQPushButton(tr("&Edit Code..."));
|
|
|
|
m_remove_code = new NonDefaultQPushButton(tr("&Remove Code"));
|
|
|
|
m_download_codes = new NonDefaultQPushButton(tr("Download Codes"));
|
2018-03-17 15:07:51 +00:00
|
|
|
|
|
|
|
m_download_codes->setToolTip(tr("Download Codes from the WiiRD Database"));
|
2018-01-24 12:36:16 +00:00
|
|
|
|
2021-09-16 05:17:37 +00:00
|
|
|
m_code_list->setEnabled(!m_game_id.empty());
|
|
|
|
m_name_label->setEnabled(!m_game_id.empty());
|
|
|
|
m_creator_label->setEnabled(!m_game_id.empty());
|
|
|
|
m_code_description->setEnabled(!m_game_id.empty());
|
|
|
|
m_code_view->setEnabled(!m_game_id.empty());
|
|
|
|
|
|
|
|
m_add_code->setEnabled(!m_game_id.empty());
|
2018-01-24 12:36:16 +00:00
|
|
|
m_edit_code->setEnabled(false);
|
|
|
|
m_remove_code->setEnabled(false);
|
2021-09-16 05:17:37 +00:00
|
|
|
m_download_codes->setEnabled(!m_game_id.empty());
|
2017-09-10 17:10:45 +00:00
|
|
|
|
|
|
|
auto* layout = new QVBoxLayout;
|
|
|
|
|
|
|
|
layout->addWidget(m_warning);
|
|
|
|
layout->addWidget(m_code_list);
|
|
|
|
|
|
|
|
auto* info_layout = new QFormLayout;
|
|
|
|
|
|
|
|
info_layout->addRow(tr("Name:"), m_name_label);
|
|
|
|
info_layout->addRow(tr("Creator:"), m_creator_label);
|
|
|
|
info_layout->addRow(tr("Description:"), static_cast<QWidget*>(nullptr));
|
|
|
|
|
|
|
|
info_layout->setFormAlignment(Qt::AlignLeft | Qt::AlignTop);
|
|
|
|
|
|
|
|
for (QLabel* label : {m_name_label, m_creator_label})
|
|
|
|
{
|
|
|
|
label->setTextInteractionFlags(Qt::TextSelectableByMouse);
|
|
|
|
label->setCursor(Qt::IBeamCursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
layout->addLayout(info_layout);
|
|
|
|
layout->addWidget(m_code_description);
|
|
|
|
layout->addWidget(m_code_view);
|
2018-01-24 12:36:16 +00:00
|
|
|
|
|
|
|
QHBoxLayout* btn_layout = new QHBoxLayout;
|
|
|
|
|
|
|
|
btn_layout->addWidget(m_add_code);
|
|
|
|
btn_layout->addWidget(m_edit_code);
|
|
|
|
btn_layout->addWidget(m_remove_code);
|
|
|
|
btn_layout->addWidget(m_download_codes);
|
|
|
|
|
|
|
|
layout->addLayout(btn_layout);
|
2017-09-10 17:10:45 +00:00
|
|
|
|
|
|
|
setLayout(layout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GeckoCodeWidget::ConnectWidgets()
|
|
|
|
{
|
|
|
|
connect(m_code_list, &QListWidget::itemSelectionChanged, this,
|
|
|
|
&GeckoCodeWidget::OnSelectionChanged);
|
|
|
|
connect(m_code_list, &QListWidget::itemChanged, this, &GeckoCodeWidget::OnItemChanged);
|
2019-06-20 23:40:37 +00:00
|
|
|
connect(m_code_list->model(), &QAbstractItemModel::rowsMoved, this,
|
|
|
|
&GeckoCodeWidget::OnListReordered);
|
2019-06-21 07:28:24 +00:00
|
|
|
connect(m_code_list, &QListWidget::customContextMenuRequested, this,
|
|
|
|
&GeckoCodeWidget::OnContextMenuRequested);
|
2017-09-10 17:10:45 +00:00
|
|
|
|
2019-07-23 22:18:58 +00:00
|
|
|
connect(m_add_code, &QPushButton::clicked, this, &GeckoCodeWidget::AddCode);
|
|
|
|
connect(m_remove_code, &QPushButton::clicked, this, &GeckoCodeWidget::RemoveCode);
|
|
|
|
connect(m_edit_code, &QPushButton::clicked, this, &GeckoCodeWidget::EditCode);
|
|
|
|
connect(m_download_codes, &QPushButton::clicked, this, &GeckoCodeWidget::DownloadCodes);
|
2017-09-10 17:10:45 +00:00
|
|
|
connect(m_warning, &CheatWarningWidget::OpenCheatEnableSettings, this,
|
|
|
|
&GeckoCodeWidget::OpenGeneralSettings);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GeckoCodeWidget::OnSelectionChanged()
|
|
|
|
{
|
|
|
|
auto items = m_code_list->selectedItems();
|
|
|
|
|
2019-06-20 23:40:37 +00:00
|
|
|
const bool empty = items.empty();
|
|
|
|
|
|
|
|
m_edit_code->setEnabled(!empty);
|
|
|
|
m_remove_code->setEnabled(!empty);
|
2018-01-24 12:36:16 +00:00
|
|
|
|
2017-09-10 17:10:45 +00:00
|
|
|
if (items.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto selected = items[0];
|
|
|
|
|
2018-05-29 01:36:35 +00:00
|
|
|
const int index = selected->data(Qt::UserRole).toInt();
|
|
|
|
|
|
|
|
const auto& code = m_gecko_codes[index];
|
2017-09-10 17:10:45 +00:00
|
|
|
|
|
|
|
m_name_label->setText(QString::fromStdString(code.name));
|
|
|
|
m_creator_label->setText(QString::fromStdString(code.creator));
|
|
|
|
|
|
|
|
m_code_description->clear();
|
|
|
|
|
|
|
|
for (const auto& line : code.notes)
|
|
|
|
m_code_description->append(QString::fromStdString(line));
|
|
|
|
|
|
|
|
m_code_view->clear();
|
|
|
|
|
|
|
|
for (const auto& c : code.codes)
|
2021-08-05 17:05:12 +00:00
|
|
|
m_code_view->append(QString::fromStdString(c.original_line));
|
2017-09-10 17:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GeckoCodeWidget::OnItemChanged(QListWidgetItem* item)
|
|
|
|
{
|
2018-05-29 01:36:35 +00:00
|
|
|
const int index = item->data(Qt::UserRole).toInt();
|
|
|
|
m_gecko_codes[index].enabled = (item->checkState() == Qt::Checked);
|
2017-09-10 17:10:45 +00:00
|
|
|
|
2018-03-26 02:17:47 +00:00
|
|
|
if (!m_restart_required)
|
|
|
|
Gecko::SetActiveCodes(m_gecko_codes);
|
|
|
|
|
2017-09-10 17:10:45 +00:00
|
|
|
SaveCodes();
|
|
|
|
}
|
|
|
|
|
2018-01-24 12:36:16 +00:00
|
|
|
void GeckoCodeWidget::AddCode()
|
|
|
|
{
|
|
|
|
Gecko::GeckoCode code;
|
|
|
|
code.enabled = true;
|
|
|
|
|
2018-05-06 16:02:39 +00:00
|
|
|
CheatCodeEditor ed(this);
|
2018-01-24 12:36:16 +00:00
|
|
|
ed.SetGeckoCode(&code);
|
2019-07-31 13:16:01 +00:00
|
|
|
if (ed.exec() == QDialog::Rejected)
|
|
|
|
return;
|
2018-01-24 12:36:16 +00:00
|
|
|
|
2019-07-31 13:16:01 +00:00
|
|
|
m_gecko_codes.push_back(std::move(code));
|
|
|
|
SaveCodes();
|
|
|
|
UpdateList();
|
2018-01-24 12:36:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GeckoCodeWidget::EditCode()
|
|
|
|
{
|
|
|
|
const auto* item = m_code_list->currentItem();
|
|
|
|
if (item == nullptr)
|
|
|
|
return;
|
|
|
|
|
2018-05-29 01:36:35 +00:00
|
|
|
const int index = item->data(Qt::UserRole).toInt();
|
2018-01-24 12:36:16 +00:00
|
|
|
|
2018-05-06 16:02:39 +00:00
|
|
|
CheatCodeEditor ed(this);
|
2018-05-29 01:36:35 +00:00
|
|
|
ed.SetGeckoCode(&m_gecko_codes[index]);
|
2019-07-31 13:16:01 +00:00
|
|
|
if (ed.exec() == QDialog::Rejected)
|
|
|
|
return;
|
2018-01-24 12:36:16 +00:00
|
|
|
|
2019-07-31 13:16:01 +00:00
|
|
|
SaveCodes();
|
|
|
|
UpdateList();
|
2018-01-24 12:36:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GeckoCodeWidget::RemoveCode()
|
|
|
|
{
|
|
|
|
const auto* item = m_code_list->currentItem();
|
|
|
|
|
|
|
|
if (item == nullptr)
|
|
|
|
return;
|
|
|
|
|
2018-05-29 01:36:35 +00:00
|
|
|
m_gecko_codes.erase(m_gecko_codes.begin() + item->data(Qt::UserRole).toInt());
|
2018-01-24 12:36:16 +00:00
|
|
|
|
|
|
|
UpdateList();
|
|
|
|
SaveCodes();
|
|
|
|
}
|
|
|
|
|
2017-09-10 17:10:45 +00:00
|
|
|
void GeckoCodeWidget::SaveCodes()
|
|
|
|
{
|
2021-09-16 05:17:37 +00:00
|
|
|
if (m_game_id.empty())
|
|
|
|
return;
|
|
|
|
|
2019-07-31 13:11:44 +00:00
|
|
|
const auto ini_path =
|
|
|
|
std::string(File::GetUserPath(D_GAMESETTINGS_IDX)).append(m_game_id).append(".ini");
|
2019-06-20 23:40:37 +00:00
|
|
|
|
2023-04-13 13:38:09 +00:00
|
|
|
Common::IniFile game_ini_local;
|
2019-07-31 13:11:44 +00:00
|
|
|
game_ini_local.Load(ini_path);
|
2017-09-10 17:10:45 +00:00
|
|
|
Gecko::SaveCodes(game_ini_local, m_gecko_codes);
|
2019-07-31 13:11:44 +00:00
|
|
|
game_ini_local.Save(ini_path);
|
2017-09-10 17:10:45 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 07:28:24 +00:00
|
|
|
void GeckoCodeWidget::OnContextMenuRequested()
|
|
|
|
{
|
|
|
|
QMenu menu;
|
|
|
|
|
|
|
|
menu.addAction(tr("Sort Alphabetically"), this, &GeckoCodeWidget::SortAlphabetically);
|
2023-04-26 08:48:59 +00:00
|
|
|
menu.addAction(tr("Show Enabled Codes First"), this, &GeckoCodeWidget::SortEnabledCodesFirst);
|
|
|
|
menu.addAction(tr("Show Disabled Codes First"), this, &GeckoCodeWidget::SortDisabledCodesFirst);
|
2019-06-21 07:28:24 +00:00
|
|
|
|
|
|
|
menu.exec(QCursor::pos());
|
|
|
|
}
|
|
|
|
|
|
|
|
void GeckoCodeWidget::SortAlphabetically()
|
|
|
|
{
|
|
|
|
m_code_list->sortItems();
|
|
|
|
OnListReordered();
|
|
|
|
}
|
|
|
|
|
2023-04-26 08:48:59 +00:00
|
|
|
void GeckoCodeWidget::SortEnabledCodesFirst()
|
|
|
|
{
|
|
|
|
std::stable_sort(m_gecko_codes.begin(), m_gecko_codes.end(), [](const auto& a, const auto& b) {
|
|
|
|
return a.enabled && a.enabled != b.enabled;
|
|
|
|
});
|
|
|
|
|
|
|
|
UpdateList();
|
|
|
|
SaveCodes();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GeckoCodeWidget::SortDisabledCodesFirst()
|
|
|
|
{
|
|
|
|
std::stable_sort(m_gecko_codes.begin(), m_gecko_codes.end(), [](const auto& a, const auto& b) {
|
|
|
|
return !a.enabled && a.enabled != b.enabled;
|
|
|
|
});
|
|
|
|
|
|
|
|
UpdateList();
|
|
|
|
SaveCodes();
|
|
|
|
}
|
|
|
|
|
2019-06-20 23:40:37 +00:00
|
|
|
void GeckoCodeWidget::OnListReordered()
|
|
|
|
{
|
|
|
|
// Reorder codes based on the indices of table item
|
|
|
|
std::vector<Gecko::GeckoCode> codes;
|
|
|
|
codes.reserve(m_gecko_codes.size());
|
|
|
|
|
|
|
|
for (int i = 0; i < m_code_list->count(); i++)
|
|
|
|
{
|
|
|
|
const int index = m_code_list->item(i)->data(Qt::UserRole).toInt();
|
|
|
|
|
|
|
|
codes.push_back(std::move(m_gecko_codes[index]));
|
|
|
|
}
|
|
|
|
|
|
|
|
m_gecko_codes = std::move(codes);
|
|
|
|
|
|
|
|
UpdateList();
|
|
|
|
SaveCodes();
|
|
|
|
}
|
|
|
|
|
2017-09-10 17:10:45 +00:00
|
|
|
void GeckoCodeWidget::UpdateList()
|
|
|
|
{
|
|
|
|
m_code_list->clear();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m_gecko_codes.size(); i++)
|
|
|
|
{
|
|
|
|
const auto& code = m_gecko_codes[i];
|
|
|
|
|
|
|
|
auto* item = new QListWidgetItem(QString::fromStdString(code.name)
|
2019-07-30 11:57:06 +00:00
|
|
|
.replace(QStringLiteral("<"), QChar::fromLatin1('<'))
|
|
|
|
.replace(QStringLiteral(">"), QChar::fromLatin1('>')));
|
2017-09-10 17:10:45 +00:00
|
|
|
|
2019-06-20 23:40:37 +00:00
|
|
|
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable |
|
|
|
|
Qt::ItemIsDragEnabled);
|
2017-09-10 17:10:45 +00:00
|
|
|
item->setCheckState(code.enabled ? Qt::Checked : Qt::Unchecked);
|
2018-05-29 01:36:35 +00:00
|
|
|
item->setData(Qt::UserRole, static_cast<int>(i));
|
2017-09-10 17:10:45 +00:00
|
|
|
|
|
|
|
m_code_list->addItem(item);
|
|
|
|
}
|
2019-06-20 23:40:37 +00:00
|
|
|
|
|
|
|
m_code_list->setDragDropMode(QAbstractItemView::InternalMove);
|
2017-09-10 17:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GeckoCodeWidget::DownloadCodes()
|
|
|
|
{
|
|
|
|
bool success;
|
|
|
|
|
2019-02-23 16:49:06 +00:00
|
|
|
std::vector<Gecko::GeckoCode> codes = Gecko::DownloadCodes(m_gametdb_id, &success);
|
2017-09-10 17:10:45 +00:00
|
|
|
|
|
|
|
if (!success)
|
|
|
|
{
|
2019-03-04 19:49:00 +00:00
|
|
|
ModalMessageBox::critical(this, tr("Error"), tr("Failed to download codes."));
|
2017-09-10 17:10:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (codes.empty())
|
|
|
|
{
|
2019-03-04 19:49:00 +00:00
|
|
|
ModalMessageBox::critical(this, tr("Error"), tr("File contained no codes."));
|
2017-09-10 17:10:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t added_count = 0;
|
|
|
|
|
|
|
|
for (const auto& code : codes)
|
|
|
|
{
|
|
|
|
auto it = std::find(m_gecko_codes.begin(), m_gecko_codes.end(), code);
|
|
|
|
|
|
|
|
if (it == m_gecko_codes.end())
|
|
|
|
{
|
|
|
|
m_gecko_codes.push_back(code);
|
|
|
|
added_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateList();
|
|
|
|
SaveCodes();
|
|
|
|
|
2019-03-04 19:49:00 +00:00
|
|
|
ModalMessageBox::information(
|
|
|
|
this, tr("Download complete"),
|
|
|
|
tr("Downloaded %1 codes. (added %2)")
|
|
|
|
.arg(QString::number(codes.size()), QString::number(added_count)));
|
2017-09-10 17:10:45 +00:00
|
|
|
}
|