diff --git a/Source/Core/DolphinQt/Achievements/AchievementBox.cpp b/Source/Core/DolphinQt/Achievements/AchievementBox.cpp index d4d92e9965..edfcf6fdf9 100644 --- a/Source/Core/DolphinQt/Achievements/AchievementBox.cpp +++ b/Source/Core/DolphinQt/Achievements/AchievementBox.cpp @@ -62,6 +62,9 @@ AchievementBox::AchievementBox(QWidget* parent, rc_client_achievement_t* achieve void AchievementBox::UpdateData() { std::lock_guard lg{AchievementManager::GetInstance().GetLock()}; + // rc_client guarantees m_achievement will be valid as long as the game is loaded + if (!AchievementManager::GetInstance().IsGameLoaded()) + return; const auto& badge = AchievementManager::GetInstance().GetAchievementBadge( m_achievement->id, m_achievement->state != RC_CLIENT_ACHIEVEMENT_STATE_UNLOCKED); @@ -92,6 +95,11 @@ void AchievementBox::UpdateData() void AchievementBox::UpdateProgress() { + std::lock_guard lg{AchievementManager::GetInstance().GetLock()}; + // rc_client guarantees m_achievement will be valid as long as the game is loaded + if (!AchievementManager::GetInstance().IsGameLoaded()) + return; + if (m_achievement->measured_percent > 0.000) { m_progress_bar->setRange(0, 100); diff --git a/Source/Core/DolphinQt/Achievements/AchievementProgressWidget.cpp b/Source/Core/DolphinQt/Achievements/AchievementProgressWidget.cpp index 741cb61227..352645c5db 100644 --- a/Source/Core/DolphinQt/Achievements/AchievementProgressWidget.cpp +++ b/Source/Core/DolphinQt/Achievements/AchievementProgressWidget.cpp @@ -42,33 +42,47 @@ void AchievementProgressWidget::UpdateData(bool clean_all) { m_achievement_boxes.clear(); ClearLayoutRecursively(m_common_layout); - - auto& instance = AchievementManager::GetInstance(); - if (!instance.IsGameLoaded()) - return; - auto* client = instance.GetClient(); - auto* achievement_list = rc_client_create_achievement_list( - client, RC_CLIENT_ACHIEVEMENT_CATEGORY_CORE_AND_UNOFFICIAL, - RC_CLIENT_ACHIEVEMENT_LIST_GROUPING_PROGRESS); - for (u32 ix = 0; ix < achievement_list->num_buckets; ix++) - { - m_common_layout->addWidget(new QLabel(tr(achievement_list->buckets[ix].label))); - for (u32 jx = 0; jx < achievement_list->buckets[ix].num_achievements; jx++) - { - auto* achievement = achievement_list->buckets[ix].achievements[jx]; - m_achievement_boxes[achievement->id] = std::make_shared(this, achievement); - m_common_layout->addWidget(m_achievement_boxes[achievement->id].get()); - } - } - rc_client_destroy_achievement_list(achievement_list); } else { - for (auto box : m_achievement_boxes) + while (auto* item = m_common_layout->takeAt(0)) { - box.second->UpdateData(); + auto* widget = item->widget(); + m_common_layout->removeWidget(widget); + if (std::strcmp(widget->metaObject()->className(), "QLabel") == 0) + { + delete widget; + delete item; + } } } + + auto& instance = AchievementManager::GetInstance(); + if (!instance.IsGameLoaded()) + return; + auto* client = instance.GetClient(); + auto* achievement_list = + rc_client_create_achievement_list(client, RC_CLIENT_ACHIEVEMENT_CATEGORY_CORE_AND_UNOFFICIAL, + RC_CLIENT_ACHIEVEMENT_LIST_GROUPING_PROGRESS); + for (u32 ix = 0; ix < achievement_list->num_buckets; ix++) + { + m_common_layout->addWidget(new QLabel(tr(achievement_list->buckets[ix].label))); + for (u32 jx = 0; jx < achievement_list->buckets[ix].num_achievements; jx++) + { + auto* achievement = achievement_list->buckets[ix].achievements[jx]; + auto box_itr = m_achievement_boxes.find(achievement->id); + if (box_itr == m_achievement_boxes.end()) + { + m_achievement_boxes[achievement->id] = std::make_shared(this, achievement); + } + else + { + box_itr->second->UpdateProgress(); + } + m_common_layout->addWidget(m_achievement_boxes[achievement->id].get()); + } + } + rc_client_destroy_achievement_list(achievement_list); } void AchievementProgressWidget::UpdateData(