2018-02-16 06:53:52 -07:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-02-16 06:53:52 -07:00
|
|
|
|
2018-07-06 16:40:15 -06:00
|
|
|
#include "DolphinQt/Config/PatchesWidget.h"
|
2018-02-16 06:53:52 -07:00
|
|
|
|
|
|
|
#include <QGridLayout>
|
|
|
|
#include <QListWidget>
|
|
|
|
#include <QPushButton>
|
|
|
|
|
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/IniFile.h"
|
|
|
|
#include "Common/StringUtil.h"
|
2018-05-13 13:38:55 -06:00
|
|
|
|
2018-02-16 06:53:52 -07:00
|
|
|
#include "Core/ConfigManager.h"
|
2018-05-13 13:38:55 -06:00
|
|
|
#include "Core/PatchEngine.h"
|
|
|
|
|
2023-11-15 10:03:21 -07:00
|
|
|
#include "DolphinQt/Config/HardcoreWarningWidget.h"
|
2018-07-06 16:40:15 -06:00
|
|
|
#include "DolphinQt/Config/NewPatchDialog.h"
|
2023-07-30 16:42:15 -06:00
|
|
|
#include "DolphinQt/QtUtils/SetWindowDecorations.h"
|
2018-02-16 06:53:52 -07:00
|
|
|
|
2018-05-13 13:38:55 -06:00
|
|
|
#include "UICommon/GameFile.h"
|
|
|
|
|
2017-12-31 12:33:36 -07:00
|
|
|
PatchesWidget::PatchesWidget(const UICommon::GameFile& game)
|
2018-07-06 17:58:01 -06:00
|
|
|
: m_game_id(game.GetGameID()), m_game_revision(game.GetRevision())
|
2018-02-16 06:53:52 -07:00
|
|
|
{
|
2023-04-13 07:38:09 -06:00
|
|
|
Common::IniFile game_ini_local;
|
2018-02-16 06:53:52 -07:00
|
|
|
game_ini_local.Load(File::GetUserPath(D_GAMESETTINGS_IDX) + m_game_id + ".ini");
|
|
|
|
|
2023-04-13 07:38:09 -06:00
|
|
|
Common::IniFile game_ini_default =
|
|
|
|
SConfig::GetInstance().LoadDefaultGameIni(m_game_id, m_game_revision);
|
2018-02-16 06:53:52 -07:00
|
|
|
|
2021-06-21 11:56:27 -06:00
|
|
|
PatchEngine::LoadPatchSection("OnFrame", &m_patches, game_ini_default, game_ini_local);
|
2018-02-16 06:53:52 -07:00
|
|
|
|
|
|
|
CreateWidgets();
|
|
|
|
ConnectWidgets();
|
|
|
|
|
|
|
|
Update();
|
|
|
|
|
|
|
|
UpdateActions();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PatchesWidget::CreateWidgets()
|
|
|
|
{
|
2023-11-15 10:03:21 -07:00
|
|
|
#ifdef USE_RETRO_ACHIEVEMENTS
|
|
|
|
m_hc_warning = new HardcoreWarningWidget(this);
|
|
|
|
#endif // USE_RETRO_ACHIEVEMENTS
|
2018-02-16 06:53:52 -07:00
|
|
|
m_list = new QListWidget;
|
|
|
|
m_add_button = new QPushButton(tr("&Add..."));
|
|
|
|
m_edit_button = new QPushButton();
|
|
|
|
m_remove_button = new QPushButton(tr("&Remove"));
|
|
|
|
|
2023-11-15 10:03:21 -07:00
|
|
|
auto* grid_layout = new QGridLayout;
|
2018-02-16 06:53:52 -07:00
|
|
|
|
2023-11-15 10:03:21 -07:00
|
|
|
grid_layout->addWidget(m_list, 0, 0, 1, -1);
|
|
|
|
grid_layout->addWidget(m_add_button, 1, 0);
|
|
|
|
grid_layout->addWidget(m_edit_button, 1, 2);
|
|
|
|
grid_layout->addWidget(m_remove_button, 1, 1);
|
|
|
|
|
|
|
|
auto* layout = new QVBoxLayout;
|
|
|
|
|
|
|
|
#ifdef USE_RETRO_ACHIEVEMENTS
|
|
|
|
layout->addWidget(m_hc_warning);
|
|
|
|
#endif // USE_RETRO_ACHIEVEMENTS
|
|
|
|
layout->addLayout(grid_layout);
|
2018-02-16 06:53:52 -07:00
|
|
|
|
|
|
|
setLayout(layout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PatchesWidget::ConnectWidgets()
|
|
|
|
{
|
2023-11-15 10:03:21 -07:00
|
|
|
#ifdef USE_RETRO_ACHIEVEMENTS
|
|
|
|
connect(m_hc_warning, &HardcoreWarningWidget::OpenAchievementSettings, this,
|
|
|
|
&PatchesWidget::OpenAchievementSettings);
|
|
|
|
#endif // USE_RETRO_ACHIEVEMENTS
|
|
|
|
|
2018-02-16 06:53:52 -07:00
|
|
|
connect(m_list, &QListWidget::itemSelectionChanged, this, &PatchesWidget::UpdateActions);
|
|
|
|
connect(m_list, &QListWidget::itemChanged, this, &PatchesWidget::OnItemChanged);
|
2019-07-23 16:18:58 -06:00
|
|
|
connect(m_remove_button, &QPushButton::clicked, this, &PatchesWidget::OnRemove);
|
|
|
|
connect(m_add_button, &QPushButton::clicked, this, &PatchesWidget::OnAdd);
|
|
|
|
connect(m_edit_button, &QPushButton::clicked, this, &PatchesWidget::OnEdit);
|
2018-02-16 06:53:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void PatchesWidget::OnItemChanged(QListWidgetItem* item)
|
|
|
|
{
|
2020-12-10 04:58:27 -07:00
|
|
|
m_patches[m_list->row(item)].enabled = (item->checkState() == Qt::Checked);
|
2018-02-16 06:53:52 -07:00
|
|
|
SavePatches();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PatchesWidget::OnAdd()
|
|
|
|
{
|
|
|
|
PatchEngine::Patch patch;
|
|
|
|
patch.user_defined = true;
|
|
|
|
|
2023-07-30 16:42:15 -06:00
|
|
|
bool new_patch_confirmed = false;
|
|
|
|
{
|
|
|
|
NewPatchDialog dialog(this, patch);
|
|
|
|
SetQWidgetWindowDecorations(&dialog);
|
|
|
|
new_patch_confirmed = dialog.exec();
|
|
|
|
}
|
|
|
|
if (new_patch_confirmed)
|
2018-02-16 06:53:52 -07:00
|
|
|
{
|
|
|
|
m_patches.push_back(patch);
|
|
|
|
SavePatches();
|
|
|
|
Update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PatchesWidget::OnEdit()
|
|
|
|
{
|
|
|
|
if (m_list->selectedItems().isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto* item = m_list->selectedItems()[0];
|
|
|
|
|
|
|
|
auto patch = m_patches[m_list->row(item)];
|
|
|
|
|
|
|
|
if (!patch.user_defined)
|
2018-03-04 11:08:57 -07:00
|
|
|
{
|
|
|
|
// i18n: If there is a pre-defined patch with the name %1 and the user wants to edit it,
|
|
|
|
// a copy of it gets created with this name
|
|
|
|
patch.name = tr("%1 (Copy)").arg(QString::fromStdString(patch.name)).toStdString();
|
|
|
|
}
|
2018-02-16 06:53:52 -07:00
|
|
|
|
2023-07-30 16:42:15 -06:00
|
|
|
bool new_patch_confirmed = false;
|
|
|
|
{
|
|
|
|
NewPatchDialog dialog(this, patch);
|
|
|
|
SetQWidgetWindowDecorations(&dialog);
|
|
|
|
new_patch_confirmed = dialog.exec();
|
|
|
|
}
|
|
|
|
if (new_patch_confirmed)
|
2018-02-16 06:53:52 -07:00
|
|
|
{
|
|
|
|
if (patch.user_defined)
|
|
|
|
{
|
|
|
|
m_patches[m_list->row(item)] = patch;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
patch.user_defined = true;
|
|
|
|
m_patches.push_back(patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
SavePatches();
|
|
|
|
Update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PatchesWidget::OnRemove()
|
|
|
|
{
|
|
|
|
if (m_list->selectedItems().isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_patches.erase(m_patches.begin() + m_list->row(m_list->selectedItems()[0]));
|
|
|
|
SavePatches();
|
|
|
|
Update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PatchesWidget::SavePatches()
|
|
|
|
{
|
2021-06-21 11:56:27 -06:00
|
|
|
const std::string ini_path = File::GetUserPath(D_GAMESETTINGS_IDX) + m_game_id + ".ini";
|
2018-02-16 06:53:52 -07:00
|
|
|
|
2023-04-13 07:38:09 -06:00
|
|
|
Common::IniFile game_ini_local;
|
2021-06-21 11:56:27 -06:00
|
|
|
game_ini_local.Load(ini_path);
|
|
|
|
PatchEngine::SavePatchSection(&game_ini_local, m_patches);
|
|
|
|
game_ini_local.Save(ini_path);
|
2018-02-16 06:53:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void PatchesWidget::Update()
|
|
|
|
{
|
|
|
|
m_list->clear();
|
|
|
|
|
|
|
|
for (const auto& patch : m_patches)
|
|
|
|
{
|
|
|
|
auto* item = new QListWidgetItem(QString::fromStdString(patch.name));
|
|
|
|
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
|
2020-12-10 04:58:27 -07:00
|
|
|
item->setCheckState(patch.enabled ? Qt::Checked : Qt::Unchecked);
|
2018-02-16 06:53:52 -07:00
|
|
|
item->setData(Qt::UserRole, patch.user_defined);
|
|
|
|
|
|
|
|
m_list->addItem(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PatchesWidget::UpdateActions()
|
|
|
|
{
|
|
|
|
bool selected = !m_list->selectedItems().isEmpty();
|
2018-03-29 13:22:16 -06:00
|
|
|
|
|
|
|
auto* item = selected ? m_list->selectedItems()[0] : nullptr;
|
2018-02-16 06:53:52 -07:00
|
|
|
|
|
|
|
bool user_defined = selected ? item->data(Qt::UserRole).toBool() : true;
|
|
|
|
|
|
|
|
m_edit_button->setEnabled(selected);
|
|
|
|
m_edit_button->setText(user_defined ? tr("&Edit...") : tr("&Clone..."));
|
|
|
|
m_remove_button->setEnabled(selected && user_defined);
|
|
|
|
}
|