2018-02-16 13:53:52 +00:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/Config/PatchesWidget.h"
|
2018-02-16 13:53:52 +00:00
|
|
|
|
|
|
|
#include <QGridLayout>
|
|
|
|
#include <QListWidget>
|
|
|
|
#include <QPushButton>
|
|
|
|
|
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/IniFile.h"
|
|
|
|
#include "Common/StringUtil.h"
|
2018-05-13 19:38:55 +00:00
|
|
|
|
2018-02-16 13:53:52 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
2018-05-13 19:38:55 +00:00
|
|
|
#include "Core/PatchEngine.h"
|
|
|
|
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/Config/NewPatchDialog.h"
|
2018-02-16 13:53:52 +00:00
|
|
|
|
2018-05-13 19:38:55 +00:00
|
|
|
#include "UICommon/GameFile.h"
|
|
|
|
|
2017-12-31 19:33:36 +00:00
|
|
|
PatchesWidget::PatchesWidget(const UICommon::GameFile& game)
|
2018-07-06 23:58:01 +00:00
|
|
|
: m_game_id(game.GetGameID()), m_game_revision(game.GetRevision())
|
2018-02-16 13:53:52 +00:00
|
|
|
{
|
|
|
|
IniFile game_ini_local;
|
|
|
|
game_ini_local.Load(File::GetUserPath(D_GAMESETTINGS_IDX) + m_game_id + ".ini");
|
|
|
|
|
|
|
|
IniFile game_ini_default = SConfig::GetInstance().LoadDefaultGameIni(m_game_id, m_game_revision);
|
|
|
|
|
|
|
|
PatchEngine::LoadPatchSection("OnFrame", m_patches, game_ini_default, game_ini_local);
|
|
|
|
|
|
|
|
CreateWidgets();
|
|
|
|
ConnectWidgets();
|
|
|
|
|
|
|
|
Update();
|
|
|
|
|
|
|
|
UpdateActions();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PatchesWidget::CreateWidgets()
|
|
|
|
{
|
|
|
|
m_list = new QListWidget;
|
|
|
|
m_add_button = new QPushButton(tr("&Add..."));
|
|
|
|
m_edit_button = new QPushButton();
|
|
|
|
m_remove_button = new QPushButton(tr("&Remove"));
|
|
|
|
|
|
|
|
auto* layout = new QGridLayout;
|
|
|
|
|
|
|
|
layout->addWidget(m_list, 0, 0, 1, -1);
|
|
|
|
layout->addWidget(m_add_button, 1, 0);
|
|
|
|
layout->addWidget(m_edit_button, 1, 2);
|
|
|
|
layout->addWidget(m_remove_button, 1, 1);
|
|
|
|
|
|
|
|
setLayout(layout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PatchesWidget::ConnectWidgets()
|
|
|
|
{
|
|
|
|
connect(m_list, &QListWidget::itemSelectionChanged, this, &PatchesWidget::UpdateActions);
|
|
|
|
connect(m_list, &QListWidget::itemChanged, this, &PatchesWidget::OnItemChanged);
|
2019-07-23 22:18:58 +00: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 13:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PatchesWidget::OnItemChanged(QListWidgetItem* item)
|
|
|
|
{
|
2020-12-10 11:58:27 +00:00
|
|
|
m_patches[m_list->row(item)].enabled = (item->checkState() == Qt::Checked);
|
2018-02-16 13:53:52 +00:00
|
|
|
SavePatches();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PatchesWidget::OnAdd()
|
|
|
|
{
|
|
|
|
PatchEngine::Patch patch;
|
|
|
|
patch.user_defined = true;
|
|
|
|
|
2018-05-06 16:02:39 +00:00
|
|
|
if (NewPatchDialog(this, patch).exec())
|
2018-02-16 13:53:52 +00: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 18:08:57 +00: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 13:53:52 +00:00
|
|
|
|
2018-05-06 16:02:39 +00:00
|
|
|
if (NewPatchDialog(this, patch).exec())
|
2018-02-16 13:53:52 +00: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()
|
|
|
|
{
|
|
|
|
std::vector<std::string> lines;
|
|
|
|
std::vector<std::string> lines_enabled;
|
|
|
|
|
|
|
|
for (const auto& patch : m_patches)
|
|
|
|
{
|
2020-12-10 11:58:27 +00:00
|
|
|
if (patch.enabled)
|
2018-02-16 13:53:52 +00:00
|
|
|
lines_enabled.push_back("$" + patch.name);
|
|
|
|
|
|
|
|
if (!patch.user_defined)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
lines.push_back("$" + patch.name);
|
|
|
|
|
|
|
|
for (const auto& entry : patch.entries)
|
|
|
|
{
|
|
|
|
lines.push_back(StringFromFormat("0x%08X:%s:0x%08X", entry.address,
|
2018-05-13 18:10:58 +00:00
|
|
|
PatchEngine::PatchTypeAsString(entry.type), entry.value));
|
2018-02-16 13:53:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IniFile game_ini_local;
|
|
|
|
game_ini_local.Load(File::GetUserPath(D_GAMESETTINGS_IDX) + m_game_id + ".ini");
|
|
|
|
|
|
|
|
game_ini_local.SetLines("OnFrame_Enabled", lines_enabled);
|
|
|
|
game_ini_local.SetLines("OnFrame", lines);
|
|
|
|
|
|
|
|
game_ini_local.Save(File::GetUserPath(D_GAMESETTINGS_IDX) + m_game_id + ".ini");
|
|
|
|
}
|
|
|
|
|
|
|
|
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 11:58:27 +00:00
|
|
|
item->setCheckState(patch.enabled ? Qt::Checked : Qt::Unchecked);
|
2018-02-16 13:53:52 +00:00
|
|
|
item->setData(Qt::UserRole, patch.user_defined);
|
|
|
|
|
|
|
|
m_list->addItem(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PatchesWidget::UpdateActions()
|
|
|
|
{
|
|
|
|
bool selected = !m_list->selectedItems().isEmpty();
|
2018-03-29 19:22:16 +00:00
|
|
|
|
|
|
|
auto* item = selected ? m_list->selectedItems()[0] : nullptr;
|
2018-02-16 13:53:52 +00: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);
|
|
|
|
}
|