Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow unknown game dialog to be reopened #1130

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/services/AchievementRuntimeExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,12 @@ class AchievementRuntimeExports : private AchievementRuntime
{
auto& pClient = ra::services::ServiceLocator::GetMutable<ra::services::AchievementRuntime>();
rc_client_load_unknown_game(pClient.GetClient(), hash);

auto& pGameContext = ra::services::ServiceLocator::GetMutable<ra::data::context::GameContext>();
pGameContext.SetGameHash(hash);

const auto& pConsoleContext = ra::services::ServiceLocator::Get<ra::data::context::ConsoleContext>();
pClient.GetClient()->game->public_.console_id = ra::etoi(pConsoleContext.Id());
}

static void unload_game()
Expand Down
37 changes: 37 additions & 0 deletions src/ui/viewmodels/IntegrationMenuViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "data/context/GameContext.hh"
#include "data/context/UserContext.hh"

#include "services/AchievementRuntime.hh"
#include "services/AchievementRuntimeExports.hh"
#include "services/IConfiguration.hh"
#include "services/ServiceLocator.hh"

Expand All @@ -21,6 +23,8 @@
#include "ui/viewmodels/UnknownGameViewModel.hh"
#include "ui/viewmodels/WindowManager.hh"

#include "rcheevos/src/rc_client_external.h"

namespace ra {
namespace ui {
namespace viewmodels {
Expand Down Expand Up @@ -388,6 +392,39 @@ void IntegrationMenuViewModel::ShowGameHash()
}
else
{
if (pGameContext.GameId() == 0 && !pGameContext.GameHash().empty())
{
const auto& pConsoleContext = ra::services::ServiceLocator::Get<ra::data::context::ConsoleContext>();
const auto nConsoleId = pConsoleContext.Id();
if (nConsoleId != ConsoleID::UnknownConsoleID)
{
const auto& pEmulatorContext = ra::services::ServiceLocator::Get<ra::data::context::EmulatorContext>();
auto sEstimatedGameTitle = ra::Widen(pEmulatorContext.GetGameTitle());

ra::ui::viewmodels::UnknownGameViewModel vmUnknownGame;
vmUnknownGame.InitializeGameTitles(nConsoleId);
vmUnknownGame.SetSystemName(pConsoleContext.Name());
vmUnknownGame.SetChecksum(ra::Widen(pGameContext.GameHash()));
vmUnknownGame.SetEstimatedGameName(sEstimatedGameTitle);
vmUnknownGame.SetNewGameName(sEstimatedGameTitle);

if (vmUnknownGame.ShowModal() == ra::ui::DialogResult::OK)
{
// register the hash so the dialog doesn't immediately reappear
auto* pClient = ra::services::ServiceLocator::GetMutable<ra::services::AchievementRuntime>().GetClient();
rc_client_add_game_hash(pClient, pGameContext.GameHash().c_str(), vmUnknownGame.GetSelectedGameId());

// attempt to load the newly associated game
pGameContext.LoadGame(vmUnknownGame.GetSelectedGameId(), pGameContext.GameHash(),
vmUnknownGame.GetTestMode()
? ra::data::context::GameContext::Mode::CompatibilityTest
: ra::data::context::GameContext::Mode::Normal);
}

return;
}
}

ra::ui::viewmodels::GameChecksumViewModel vmGameChecksum;
vmGameChecksum.ShowModal();
}
Expand Down
46 changes: 46 additions & 0 deletions tests/services/AchievementRuntimeExports_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "tests\mocks\MockConsoleContext.hh"
#include "tests\mocks\MockDesktop.hh"
#include "tests\mocks\MockEmulatorContext.hh"
#include "tests\mocks\MockGameContext.hh"
#include "tests\mocks\MockUserContext.hh"

#include <rcheevos\src\rc_client_external.h>
Expand Down Expand Up @@ -464,6 +465,51 @@ TEST_CLASS(AchievementRuntimeExports_Tests)
*ptr = '\0';
Assert::AreEqual("Integration/" RA_INTEGRATION_VERSION, buffer);
}

TEST_METHOD(TestLoadUnknownGame)
{
AchievementRuntimeExportsHarness runtime;
ra::data::context::mocks::MockGameContext mockGameContext;
ra::data::context::mocks::MockConsoleContext mockConsoleContext;

rc_client_external_t pClient;
memset(&pClient, 0, sizeof(pClient));
_Rcheevos_GetExternalClient(&pClient, 2);

pClient.load_unknown_game("ABCDEF0123456789");

const auto* pGame = rc_client_get_game_info(runtime.GetClient());
Assert::IsNotNull(pGame);
Ensures(pGame != nullptr);
Assert::AreEqual(0U, pGame->id);
Assert::AreEqual("Unknown Game", pGame->title);
Assert::AreEqual(0U, pGame->console_id);

Assert::AreEqual(std::string("ABCDEF0123456789"), mockGameContext.GameHash());
}

TEST_METHOD(TestLoadUnknownGameWithConsole)
{
AchievementRuntimeExportsHarness runtime;
ra::data::context::mocks::MockGameContext mockGameContext;
ra::data::context::mocks::MockConsoleContext mockConsoleContext;

rc_client_external_t pClient;
memset(&pClient, 0, sizeof(pClient));
_Rcheevos_GetExternalClient(&pClient, 2);

mockConsoleContext.SetId(ConsoleID::GBC);
pClient.load_unknown_game("ABCDEF0123456789");

const auto* pGame = rc_client_get_game_info(runtime.GetClient());
Assert::IsNotNull(pGame);
Ensures(pGame != nullptr);
Assert::AreEqual(0U, pGame->id);
Assert::AreEqual("Unknown Game", pGame->title);
Assert::AreEqual({ RC_CONSOLE_GAMEBOY_COLOR }, pGame->console_id);

Assert::AreEqual(std::string("ABCDEF0123456789"), mockGameContext.GameHash());
}
};

} // namespace tests
Expand Down
77 changes: 77 additions & 0 deletions tests/ui/viewmodels/IntegrationMenuViewModel_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,83 @@ TEST_CLASS(IntegrationMenuViewModel_Tests)
Assert::IsTrue(bDialogShown);
Assert::AreEqual(ra::data::context::GameContext::Mode::Normal, menu.mockGameContext.GetMode());
}

TEST_METHOD(TestShowGameHashUnknownHashCancel)
{
IntegrationMenuViewModelHarness menu;

menu.mockConsoleContext.SetId(ConsoleID::Arcade);
menu.mockGameContext.SetGameHash("ABCDEF0123456789");

bool bDialogShown = false;
menu.mockDesktop.ExpectWindow<ra::ui::viewmodels::UnknownGameViewModel>(
[&bDialogShown](ra::ui::viewmodels::UnknownGameViewModel& vmUnknown) {
bDialogShown = true;

Assert::IsTrue(vmUnknown.IsSelectedGameEnabled());

return DialogResult::Cancel;
});

menu.ActivateMenuItem(IDM_RA_GETROMCHECKSUM);

Assert::IsTrue(bDialogShown);
Assert::AreEqual(ra::data::context::GameContext::Mode::Normal, menu.mockGameContext.GetMode());
Assert::AreEqual({ 0U }, menu.mockGameContext.GameId());
}

TEST_METHOD(TestShowGameHashUnknownHashAssociate)
{
IntegrationMenuViewModelHarness menu;

menu.mockConsoleContext.SetId(ConsoleID::Arcade);
menu.mockGameContext.SetGameHash("ABCDEF0123456789");

bool bDialogShown = false;
menu.mockDesktop.ExpectWindow<ra::ui::viewmodels::UnknownGameViewModel>(
[&bDialogShown](ra::ui::viewmodels::UnknownGameViewModel& vmUnknown) {
bDialogShown = true;

Assert::IsTrue(vmUnknown.IsSelectedGameEnabled());
vmUnknown.SetSelectedGameId(523);

return DialogResult::OK;
});

menu.ActivateMenuItem(IDM_RA_GETROMCHECKSUM);

Assert::IsTrue(bDialogShown);
Assert::AreEqual(ra::data::context::GameContext::Mode::Normal, menu.mockGameContext.GetMode());
Assert::AreEqual({ 523U }, menu.mockGameContext.GameId());
Assert::IsTrue(menu.mockGameContext.WasLoaded());
}

TEST_METHOD(TestShowGameHashUnknownHashTestCompatibility)
{
IntegrationMenuViewModelHarness menu;

menu.mockConsoleContext.SetId(ConsoleID::Arcade);
menu.mockGameContext.SetGameHash("ABCDEF0123456789");

bool bDialogShown = false;
menu.mockDesktop.ExpectWindow<ra::ui::viewmodels::UnknownGameViewModel>(
[&bDialogShown](ra::ui::viewmodels::UnknownGameViewModel& vmUnknown) {
bDialogShown = true;

Assert::IsTrue(vmUnknown.IsSelectedGameEnabled());
vmUnknown.SetSelectedGameId(523);
vmUnknown.SetTestMode(true);

return DialogResult::OK;
});

menu.ActivateMenuItem(IDM_RA_GETROMCHECKSUM);

Assert::IsTrue(bDialogShown);
Assert::AreEqual(ra::data::context::GameContext::Mode::CompatibilityTest, menu.mockGameContext.GetMode());
Assert::AreEqual({ 523U }, menu.mockGameContext.GameId());
Assert::IsTrue(menu.mockGameContext.WasLoaded());
}
};

} // namespace tests
Expand Down
Loading