Skip to content

Commit

Permalink
CheatsManager: Create ARCodeWidget and GeckoCodeWidget only once.
Browse files Browse the repository at this point in the history
Create ARCodeWidget and GeckoCodeWidget once on startup rather than
every time a game is launched or shutdown.

In addition to losing focus on the tab (since the previous widget and
tab no longer existed), the behavior prior to this commit could cause a
crash if the user initiated a game shutdown and then opened a code edit
window since the AR/GeckoCodeWidget would get deleted in the meantime.
  • Loading branch information
Dentomologist committed Jul 31, 2024
1 parent eaa34dd commit ab304fb
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 51 deletions.
80 changes: 30 additions & 50 deletions Source/Core/DolphinQt/CheatsManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ CheatsManager::CheatsManager(Core::System& system, QWidget* parent)
CreateWidgets();
ConnectWidgets();

RefreshCodeTabs(Core::GetState(m_system), true);

auto& settings = Settings::GetQSettings();
restoreGeometry(settings.value(QStringLiteral("cheatsmanager/geometry")).toByteArray());
}
Expand All @@ -50,7 +48,7 @@ CheatsManager::~CheatsManager()

void CheatsManager::OnStateChanged(Core::State state)
{
RefreshCodeTabs(state, false);
RefreshCodeTabs(state);
if (state == Core::State::Paused)
UpdateAllCheatSearchWidgetCurrentValues();
}
Expand Down Expand Up @@ -99,67 +97,45 @@ void CheatsManager::showEvent(QShowEvent* event)
RegisterAfterFrameEventCallback();
}

void CheatsManager::RefreshCodeTabs(Core::State state, bool force)
void CheatsManager::RefreshCodeTabs(Core::State state)
{
if (!force && (state == Core::State::Starting || state == Core::State::Stopping))
if (state == Core::State::Starting || state == Core::State::Stopping)
return;

const auto& game_id =
state != Core::State::Uninitialized ? SConfig::GetInstance().GetGameID() : std::string();
const auto& game_tdb_id = SConfig::GetInstance().GetGameTDBID();
const u16 revision = SConfig::GetInstance().GetRevision();

if (!force && m_game_id == game_id && m_game_tdb_id == game_tdb_id && m_revision == revision)
if (m_game_id == game_id && m_game_tdb_id == game_tdb_id && m_revision == revision)
return;

m_game_id = game_id;
m_game_tdb_id = game_tdb_id;
m_revision = revision;

if (m_ar_code)
{
const int tab_index = m_tab_widget->indexOf(m_ar_code);
if (tab_index != -1)
m_tab_widget->removeTab(tab_index);
m_ar_code->deleteLater();
m_ar_code = nullptr;
}

if (m_gecko_code)
{
const int tab_index = m_tab_widget->indexOf(m_gecko_code);
if (tab_index != -1)
m_tab_widget->removeTab(tab_index);
m_gecko_code->deleteLater();
m_gecko_code = nullptr;
}

m_ar_code = new ARCodeWidget(m_game_id, m_revision, false);
m_gecko_code = new GeckoCodeWidget(m_game_id, m_game_tdb_id, m_revision, false);
m_tab_widget->insertTab(0, m_ar_code, tr("AR Code"));
m_tab_widget->insertTab(1, m_gecko_code, tr("Gecko Codes"));
m_tab_widget->setTabUnclosable(0);
m_tab_widget->setTabUnclosable(1);

connect(m_ar_code, &ARCodeWidget::OpenGeneralSettings, this, &CheatsManager::OpenGeneralSettings);
connect(m_gecko_code, &GeckoCodeWidget::OpenGeneralSettings, this,
&CheatsManager::OpenGeneralSettings);
#ifdef USE_RETRO_ACHIEVEMENTS
connect(m_ar_code, &ARCodeWidget::OpenAchievementSettings, this,
&CheatsManager::OpenAchievementSettings);
connect(m_gecko_code, &GeckoCodeWidget::OpenAchievementSettings, this,
&CheatsManager::OpenAchievementSettings);
#endif // USE_RETRO_ACHIEVEMENTS
m_ar_code->ChangeGame(m_game_id, m_revision);
m_gecko_code->ChangeGame(m_game_id, m_game_tdb_id, m_revision);
}

void CheatsManager::CreateWidgets()
{
m_tab_widget = new PartiallyClosableTabWidget;
m_button_box = new QDialogButtonBox(QDialogButtonBox::Close);

int tab_index;

m_ar_code = new ARCodeWidget(m_game_id, m_revision, false);
tab_index = m_tab_widget->addTab(m_ar_code, tr("AR Code"));
m_tab_widget->setTabUnclosable(tab_index);

m_gecko_code = new GeckoCodeWidget(m_game_id, m_game_tdb_id, m_revision, false);
tab_index = m_tab_widget->addTab(m_gecko_code, tr("Gecko Codes"));
m_tab_widget->setTabUnclosable(tab_index);

m_cheat_search_new = new CheatSearchFactoryWidget();
m_tab_widget->addTab(m_cheat_search_new, tr("Start New Cheat Search"));
m_tab_widget->setTabUnclosable(0);
tab_index = m_tab_widget->addTab(m_cheat_search_new, tr("Start New Cheat Search"));
m_tab_widget->setTabUnclosable(tab_index);

auto* layout = new QVBoxLayout;
layout->addWidget(m_tab_widget);
Expand All @@ -172,14 +148,9 @@ void CheatsManager::OnNewSessionCreated(const Cheats::CheatSearchSessionBase& se
{
auto* w = new CheatSearchWidget(m_system, session.Clone());
const int tab_index = m_tab_widget->addTab(w, tr("Cheat Search"));
w->connect(w, &CheatSearchWidget::ActionReplayCodeGenerated, this,
[this](const ActionReplay::ARCode& ar_code) {
if (m_ar_code)
m_ar_code->AddCode(ar_code);
});
w->connect(w, &CheatSearchWidget::ShowMemory, [this](u32 address) { emit ShowMemory(address); });
w->connect(w, &CheatSearchWidget::RequestWatch,
[this](QString name, u32 address) { emit RequestWatch(name, address); });
connect(w, &CheatSearchWidget::ActionReplayCodeGenerated, m_ar_code, &ARCodeWidget::AddCode);
connect(w, &CheatSearchWidget::ShowMemory, this, &CheatsManager::ShowMemory);
connect(w, &CheatSearchWidget::RequestWatch, this, &CheatsManager::RequestWatch);
m_tab_widget->setCurrentIndex(tab_index);
}

Expand All @@ -196,4 +167,13 @@ void CheatsManager::ConnectWidgets()
connect(m_cheat_search_new, &CheatSearchFactoryWidget::NewSessionCreated, this,
&CheatsManager::OnNewSessionCreated);
connect(m_tab_widget, &QTabWidget::tabCloseRequested, this, &CheatsManager::OnTabCloseRequested);
connect(m_ar_code, &ARCodeWidget::OpenGeneralSettings, this, &CheatsManager::OpenGeneralSettings);
connect(m_gecko_code, &GeckoCodeWidget::OpenGeneralSettings, this,
&CheatsManager::OpenGeneralSettings);
#ifdef USE_RETRO_ACHIEVEMENTS
connect(m_ar_code, &ARCodeWidget::OpenAchievementSettings, this,
&CheatsManager::OpenAchievementSettings);
connect(m_gecko_code, &GeckoCodeWidget::OpenAchievementSettings, this,
&CheatsManager::OpenAchievementSettings);
#endif // USE_RETRO_ACHIEVEMENTS
}
2 changes: 1 addition & 1 deletion Source/Core/DolphinQt/CheatsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class CheatsManager : public QDialog
void OnNewSessionCreated(const Cheats::CheatSearchSessionBase& session);
void OnTabCloseRequested(int index);

void RefreshCodeTabs(Core::State state, bool force);
void RefreshCodeTabs(Core::State state);
void UpdateAllCheatSearchWidgetCurrentValues();

std::string m_game_id;
Expand Down
11 changes: 11 additions & 0 deletions Source/Core/DolphinQt/Config/ARCodeWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ ARCodeWidget::ARCodeWidget(std::string game_id, u16 game_revision, bool restart_

ARCodeWidget::~ARCodeWidget() = default;

void ARCodeWidget::ChangeGame(std::string game_id, const u16 game_revision)
{
m_game_id = std::move(game_id);
m_game_revision = game_revision;
m_restart_required = false;

m_ar_codes.clear();

LoadCodes();
}

void ARCodeWidget::CreateWidgets()
{
m_warning = new CheatWarningWidget(m_game_id, m_restart_required, this);
Expand Down
1 change: 1 addition & 0 deletions Source/Core/DolphinQt/Config/ARCodeWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ARCodeWidget : public QWidget
explicit ARCodeWidget(std::string game_id, u16 game_revision, bool restart_required = true);
~ARCodeWidget() override;

void ChangeGame(std::string game_id, u16 game_revision);
void AddCode(ActionReplay::ARCode code);

signals:
Expand Down
18 changes: 18 additions & 0 deletions Source/Core/DolphinQt/Config/GeckoCodeWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ GeckoCodeWidget::GeckoCodeWidget(std::string game_id, std::string gametdb_id, u1
LoadCodes();
}

void GeckoCodeWidget::ChangeGame(std::string game_id, std::string gametdb_id,
const u16 game_revision)
{
m_game_id = std::move(game_id);
m_gametdb_id = std::move(gametdb_id);
m_game_revision = game_revision;
m_restart_required = false;

m_gecko_codes.clear();
m_code_list->clear();
m_name_label->clear();
m_creator_label->clear();
m_code_description->clear();
m_code_view->clear();

LoadCodes();
}

GeckoCodeWidget::~GeckoCodeWidget() = default;

void GeckoCodeWidget::CreateWidgets()
Expand Down
2 changes: 2 additions & 0 deletions Source/Core/DolphinQt/Config/GeckoCodeWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class GeckoCodeWidget : public QWidget
bool restart_required = true);
~GeckoCodeWidget() override;

void ChangeGame(std::string game_id, std::string gametdb_id, u16 game_revision);

signals:
void OpenGeneralSettings();
#ifdef USE_RETRO_ACHIEVEMENTS
Expand Down

0 comments on commit ab304fb

Please sign in to comment.