Achievements: Misc fixes

This commit is contained in:
Connor McLaughlin 2022-08-22 20:01:04 +10:00
parent ccaad874a9
commit c320e8cf57
5 changed files with 51 additions and 63 deletions

View file

@ -17,7 +17,7 @@ AchievementSettingsWidget::AchievementSettingsWidget(SettingsDialog* dialog, QWi
m_ui.setupUi(this); m_ui.setupUi(this);
SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.enable, "Cheevos", "Enabled", true); SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.enable, "Cheevos", "Enabled", false);
SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.richPresence, "Cheevos", "RichPresence", true); SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.richPresence, "Cheevos", "RichPresence", true);
SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.challengeMode, "Cheevos", "ChallengeMode", false); SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.challengeMode, "Cheevos", "ChallengeMode", false);
SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.testMode, "Cheevos", "TestMode", false); SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.testMode, "Cheevos", "TestMode", false);

View file

@ -1502,7 +1502,7 @@ void MainWindow::setupAdditionalUi()
raMenu->clear(); raMenu->clear();
const auto items = Achievements::RAIntegration::GetMenuItems(); const auto items = Achievements::RAIntegration::GetMenuItems();
for (const auto& [id, title] : items) for (const auto& [id, title, checked] : items)
{ {
if (id == 0) if (id == 0)
{ {
@ -1511,6 +1511,12 @@ void MainWindow::setupAdditionalUi()
} }
QAction* raAction = raMenu->addAction(QString::fromUtf8(title)); QAction* raAction = raMenu->addAction(QString::fromUtf8(title));
if (checked)
{
raAction->setCheckable(true);
raAction->setChecked(checked);
}
connect(raAction, &QAction::triggered, this, connect(raAction, &QAction::triggered, this,
[id = id]() { Host::RunOnCPUThread([id]() { Achievements::RAIntegration::ActivateMenuItem(id); }); }); [id = id]() { Host::RunOnCPUThread([id]() { Achievements::RAIntegration::ActivateMenuItem(id); }); });
} }

View file

@ -558,13 +558,12 @@ void Achievements::SetChallengeMode(bool enabled)
if (HasActiveGame()) if (HasActiveGame())
{ {
Host::AddKeyedOSDMessage("achievements_set_challenge_mode", Host::AddKeyedOSDMessage("achievements_set_challenge_mode",
enabled ? enabled ? Host::TranslateStdString("Achievements", "Hardcore mode is now enabled.") :
Host::TranslateStdString("Achievements", "Hardcore mode is now enabled.") : Host::TranslateStdString("Achievements", "Hardcore mode is now disabled."),
Host::TranslateStdString("Achievements", "Hardcore mode is now disabled."),
10.0f); 10.0f);
} }
if (HasActiveGame() && !g_settings.achievements_test_mode) if (HasActiveGame() && !IsTestModeActive())
{ {
// deactivate, but don't clear all achievements (getting unlocks will reactivate them) // deactivate, but don't clear all achievements (getting unlocks will reactivate them)
std::unique_lock lock(s_achievements_mutex); std::unique_lock lock(s_achievements_mutex);
@ -668,7 +667,7 @@ void Achievements::FrameUpdate()
rc_runtime_do_frame(&s_rcheevos_runtime, &CheevosEventHandler, &PeekMemory, nullptr, nullptr); rc_runtime_do_frame(&s_rcheevos_runtime, &CheevosEventHandler, &PeekMemory, nullptr, nullptr);
UpdateRichPresence(); UpdateRichPresence();
if (!g_settings.achievements_test_mode) if (!IsTestModeActive())
{ {
const s32 ping_frequency = const s32 ping_frequency =
g_settings.achievements_rich_presence ? RICH_PRESENCE_PING_FREQUENCY : NO_RICH_PRESENCE_PING_FREQUENCY; g_settings.achievements_rich_presence ? RICH_PRESENCE_PING_FREQUENCY : NO_RICH_PRESENCE_PING_FREQUENCY;
@ -1091,7 +1090,7 @@ void Achievements::GetPatchesCallback(s32 status_code, Common::HTTPDownloader::R
// Skip local and unofficial achievements for now, unless "Test Unofficial Achievements" is enabled // Skip local and unofficial achievements for now, unless "Test Unofficial Achievements" is enabled
if (defn.category == RC_ACHIEVEMENT_CATEGORY_UNOFFICIAL) if (defn.category == RC_ACHIEVEMENT_CATEGORY_UNOFFICIAL)
{ {
if (!g_settings.achievements_unofficial_test_mode) if (!IsUnofficialTestModeActive())
{ {
Log_WarningPrintf("Skipping unofficial achievement %u (%s)", defn.id, defn.title); Log_WarningPrintf("Skipping unofficial achievement %u (%s)", defn.id, defn.title);
continue; continue;
@ -1147,7 +1146,7 @@ void Achievements::GetPatchesCallback(s32 status_code, Common::HTTPDownloader::R
// parse rich presence // parse rich presence
if (std::strlen(response.rich_presence_script) > 0) if (std::strlen(response.rich_presence_script) > 0)
{ {
int res = rc_runtime_activate_richpresence(&s_rcheevos_runtime, response.rich_presence_script, nullptr, 0); const int res = rc_runtime_activate_richpresence(&s_rcheevos_runtime, response.rich_presence_script, nullptr, 0);
if (res == RC_OK) if (res == RC_OK)
s_has_rich_presence = true; s_has_rich_presence = true;
else else
@ -1164,7 +1163,7 @@ void Achievements::GetPatchesCallback(s32 status_code, Common::HTTPDownloader::R
if (!s_achievements.empty() || s_has_rich_presence) if (!s_achievements.empty() || s_has_rich_presence)
{ {
if (!g_settings.achievements_test_mode) if (!IsTestModeActive())
{ {
GetUserUnlocks(); GetUserUnlocks();
} }
@ -1313,6 +1312,7 @@ void Achievements::GameChanged(const std::string& path, CDImage* image)
if (!temp_image) if (!temp_image)
{ {
Log_ErrorPrintf("Failed to open temporary CD image '%s'", path.c_str()); Log_ErrorPrintf("Failed to open temporary CD image '%s'", path.c_str());
std::unique_lock lock(s_achievements_mutex);
DisableChallengeMode(); DisableChallengeMode();
ClearGameInfo(); ClearGameInfo();
return; return;
@ -1424,7 +1424,8 @@ void Achievements::UpdateRichPresence()
return; return;
char buffer[512]; char buffer[512];
int res = rc_runtime_get_richpresence(&s_rcheevos_runtime, buffer, sizeof(buffer), PeekMemory, nullptr, nullptr); const int res =
rc_runtime_get_richpresence(&s_rcheevos_runtime, buffer, sizeof(buffer), PeekMemory, nullptr, nullptr);
if (res <= 0) if (res <= 0)
{ {
const bool had_rich_presence = !s_rich_presence_string.empty(); const bool had_rich_presence = !s_rich_presence_string.empty();
@ -1634,6 +1635,9 @@ void Achievements::DeactivateAchievement(Achievement* achievement)
void Achievements::UnlockAchievementCallback(s32 status_code, Common::HTTPDownloader::Request::Data data) void Achievements::UnlockAchievementCallback(s32 status_code, Common::HTTPDownloader::Request::Data data)
{ {
if (!System::IsValid())
return;
RAPIResponse<rc_api_award_achievement_response_t, rc_api_process_award_achievement_response, RAPIResponse<rc_api_award_achievement_response_t, rc_api_process_award_achievement_response,
rc_api_destroy_award_achievement_response> rc_api_destroy_award_achievement_response>
response(status_code, data); response(status_code, data);
@ -1646,6 +1650,9 @@ void Achievements::UnlockAchievementCallback(s32 status_code, Common::HTTPDownlo
void Achievements::SubmitLeaderboardCallback(s32 status_code, Common::HTTPDownloader::Request::Data data) void Achievements::SubmitLeaderboardCallback(s32 status_code, Common::HTTPDownloader::Request::Data data)
{ {
if (!System::IsValid())
return;
RAPIResponse<rc_api_submit_lboard_entry_response_t, rc_api_process_submit_lboard_entry_response, RAPIResponse<rc_api_submit_lboard_entry_response_t, rc_api_process_submit_lboard_entry_response,
rc_api_destroy_submit_lboard_entry_response> rc_api_destroy_submit_lboard_entry_response>
response(status_code, data); response(status_code, data);
@ -1712,7 +1719,7 @@ void Achievements::UnlockAchievement(u32 achievement_id, bool add_notification /
ImGuiFullscreen::AddNotification(15.0f, std::move(title), achievement->description, ImGuiFullscreen::AddNotification(15.0f, std::move(title), achievement->description,
GetAchievementBadgePath(*achievement)); GetAchievementBadgePath(*achievement));
if (g_settings.achievements_test_mode) if (IsTestModeActive())
{ {
Log_WarningPrintf("Skipping sending achievement %u unlock to server because of test mode.", achievement_id); Log_WarningPrintf("Skipping sending achievement %u unlock to server because of test mode.", achievement_id);
return; return;
@ -1736,7 +1743,7 @@ void Achievements::UnlockAchievement(u32 achievement_id, bool add_notification /
void Achievements::SubmitLeaderboard(u32 leaderboard_id, int value) void Achievements::SubmitLeaderboard(u32 leaderboard_id, int value)
{ {
if (g_settings.achievements_test_mode) if (IsTestModeActive())
{ {
Log_WarningPrintf("Skipping sending leaderboard %u result to server because of test mode.", leaderboard_id); Log_WarningPrintf("Skipping sending leaderboard %u result to server because of test mode.", leaderboard_id);
return; return;
@ -1870,6 +1877,7 @@ static void RACallbackEstimateTitle(char* buf);
static void RACallbackResetEmulator(); static void RACallbackResetEmulator();
static void RACallbackLoadROM(const char* unused); static void RACallbackLoadROM(const char* unused);
static unsigned char RACallbackReadMemory(unsigned int address); static unsigned char RACallbackReadMemory(unsigned int address);
static unsigned int RACallbackReadMemoryBlock(unsigned int nAddress, unsigned char* pBuffer, unsigned int nBytes);
static void RACallbackWriteMemory(unsigned int address, unsigned char value); static void RACallbackWriteMemory(unsigned int address, unsigned char value);
static bool s_raintegration_initialized = false; static bool s_raintegration_initialized = false;
@ -1896,6 +1904,7 @@ void Achievements::RAIntegration::InitializeRAIntegration(void* main_window_hand
// Apparently this has to be done early, or the memory inspector doesn't work. // Apparently this has to be done early, or the memory inspector doesn't work.
// That's a bit unfortunate, because the RAM size can vary between games, and depending on the option. // That's a bit unfortunate, because the RAM size can vary between games, and depending on the option.
RA_InstallMemoryBank(0, RACallbackReadMemory, RACallbackWriteMemory, Bus::RAM_2MB_SIZE); RA_InstallMemoryBank(0, RACallbackReadMemory, RACallbackWriteMemory, Bus::RAM_2MB_SIZE);
RA_InstallMemoryBankBlockReader(0, RACallbackReadMemoryBlock);
// Fire off a login anyway. Saves going into the menu and doing it. // Fire off a login anyway. Saves going into the menu and doing it.
RA_AttemptLogin(0); RA_AttemptLogin(0);
@ -1923,54 +1932,21 @@ void Achievements::RAIntegration::GameChanged()
RA_ActivateGame(s_game_id); RA_ActivateGame(s_game_id);
} }
std::vector<std::pair<int, const char*>> Achievements::RAIntegration::GetMenuItems() std::vector<std::tuple<int, std::string, bool>> Achievements::RAIntegration::GetMenuItems()
{ {
// NOTE: I *really* don't like doing this. But sadly it's the only way we can integrate with Qt. std::array<RA_MenuItem, 64> items;
static constexpr int IDM_RA_RETROACHIEVEMENTS = 1700; const int num_items = RA_GetPopupMenuItems(items.data());
static constexpr int IDM_RA_OVERLAYSETTINGS = 1701;
static constexpr int IDM_RA_FILES_MEMORYBOOKMARKS = 1703;
static constexpr int IDM_RA_FILES_ACHIEVEMENTS = 1704;
static constexpr int IDM_RA_FILES_MEMORYFINDER = 1705;
static constexpr int IDM_RA_FILES_LOGIN = 1706;
static constexpr int IDM_RA_FILES_LOGOUT = 1707;
static constexpr int IDM_RA_FILES_ACHIEVEMENTEDITOR = 1708;
static constexpr int IDM_RA_HARDCORE_MODE = 1710;
static constexpr int IDM_RA_REPORTBROKENACHIEVEMENTS = 1711;
static constexpr int IDM_RA_GETROMCHECKSUM = 1712;
static constexpr int IDM_RA_OPENUSERPAGE = 1713;
static constexpr int IDM_RA_OPENGAMEPAGE = 1714;
static constexpr int IDM_RA_PARSERICHPRESENCE = 1716;
static constexpr int IDM_RA_TOGGLELEADERBOARDS = 1717;
static constexpr int IDM_RA_NON_HARDCORE_WARNING = 1718;
std::vector<std::pair<int, const char*>> ret; std::vector<std::tuple<int, std::string, bool>> ret;
ret.reserve(static_cast<u32>(num_items));
const char* username = RA_UserName(); for (int i = 0; i < num_items; i++)
if (!username || std::strlen(username) == 0)
{ {
ret.emplace_back(IDM_RA_FILES_LOGIN, "&Login"); const RA_MenuItem& it = items[i];
} if (!it.sLabel)
else ret.emplace_back(0, std::string(), false);
{ else
ret.emplace_back(IDM_RA_FILES_LOGOUT, "Log&out"); ret.emplace_back(static_cast<int>(it.nID), StringUtil::WideStringToUTF8String(it.sLabel), it.bChecked);
ret.emplace_back(0, nullptr);
ret.emplace_back(IDM_RA_OPENUSERPAGE, "Open my &User Page");
ret.emplace_back(IDM_RA_OPENGAMEPAGE, "Open this &Game's Page");
ret.emplace_back(0, nullptr);
ret.emplace_back(IDM_RA_HARDCORE_MODE, "&Hardcore Mode");
ret.emplace_back(IDM_RA_NON_HARDCORE_WARNING, "Non-Hardcore &Warning");
ret.emplace_back(0, nullptr);
ret.emplace_back(IDM_RA_TOGGLELEADERBOARDS, "Enable &Leaderboards");
ret.emplace_back(IDM_RA_OVERLAYSETTINGS, "O&verlay Settings");
ret.emplace_back(0, nullptr);
ret.emplace_back(IDM_RA_FILES_ACHIEVEMENTS, "Assets Li&st");
ret.emplace_back(IDM_RA_FILES_ACHIEVEMENTEDITOR, "Assets &Editor");
ret.emplace_back(IDM_RA_FILES_MEMORYFINDER, "&Memory Inspector");
ret.emplace_back(IDM_RA_FILES_MEMORYBOOKMARKS, "Memory &Bookmarks");
ret.emplace_back(IDM_RA_PARSERICHPRESENCE, "Rich &Presence Monitor");
ret.emplace_back(0, nullptr);
ret.emplace_back(IDM_RA_REPORTBROKENACHIEVEMENTS, "&Report Achievement Problem");
ret.emplace_back(IDM_RA_GETROMCHECKSUM, "View Game H&ash");
} }
return ret; return ret;
@ -2033,4 +2009,15 @@ void Achievements::RAIntegration::RACallbackWriteMemory(unsigned int address, un
CPU::SafeWriteMemoryByte(address, value); CPU::SafeWriteMemoryByte(address, value);
} }
unsigned int Achievements::RAIntegration::RACallbackReadMemoryBlock(unsigned int nAddress, unsigned char* pBuffer,
unsigned int nBytes)
{
if (nAddress >= Bus::g_ram_size)
return 0;
const u32 copy_size = std::min<u32>(Bus::g_ram_size - nAddress, nBytes);
std::memcpy(pBuffer, Bus::g_ram + nAddress, copy_size);
return copy_size;
}
#endif #endif

View file

@ -113,7 +113,6 @@ bool LoginAsync(const char* username, const char* password);
bool Login(const char* username, const char* password); bool Login(const char* username, const char* password);
void Logout(); void Logout();
bool HasActiveGame();
void GameChanged(const std::string& path, CDImage* image); void GameChanged(const std::string& path, CDImage* image);
/// Re-enables hardcode mode if it is enabled in the settings. /// Re-enables hardcode mode if it is enabled in the settings.
@ -158,7 +157,7 @@ void SwitchToRAIntegration();
namespace RAIntegration { namespace RAIntegration {
void MainWindowChanged(void* new_handle); void MainWindowChanged(void* new_handle);
void GameChanged(); void GameChanged();
std::vector<std::pair<int, const char*>> GetMenuItems(); std::vector<std::tuple<int, std::string, bool>> GetMenuItems();
void ActivateMenuItem(int item); void ActivateMenuItem(int item);
} // namespace RAIntegration } // namespace RAIntegration
#endif #endif

View file

@ -4694,7 +4694,6 @@ void FullscreenUI::DrawAchievementsWindow()
bool FullscreenUI::OpenLeaderboardsWindow() bool FullscreenUI::OpenLeaderboardsWindow()
{ {
#ifdef WITH_CHEEVOS
if (!System::IsValid() || !Achievements::HasActiveGame() || Achievements::GetLeaderboardCount() == 0 || !Initialize()) if (!System::IsValid() || !Achievements::HasActiveGame() || Achievements::GetLeaderboardCount() == 0 || !Initialize())
return false; return false;
@ -4705,9 +4704,6 @@ bool FullscreenUI::OpenLeaderboardsWindow()
s_open_leaderboard_id.reset(); s_open_leaderboard_id.reset();
QueueResetFocus(); QueueResetFocus();
return true; return true;
#else
return false;
#endif
} }
void FullscreenUI::DrawLeaderboardListEntry(const Achievements::Leaderboard& lboard) void FullscreenUI::DrawLeaderboardListEntry(const Achievements::Leaderboard& lboard)