FrontendCommon: Add auto cheat loading option

This commit is contained in:
Connor McLaughlin
2020-09-09 23:44:02 +10:00
parent bf6b4514a0
commit 03f052e12e
8 changed files with 190 additions and 18 deletions

View File

@ -7,6 +7,7 @@
#include "common/string_util.h"
#include "controller_interface.h"
#include "core/cdrom.h"
#include "core/cheats.h"
#include "core/cpu_code_cache.h"
#include "core/dma.h"
#include "core/gpu.h"
@ -116,6 +117,7 @@ void CommonHostInterface::InitializeUserDirectory()
result &= FileSystem::CreateDirectory(GetUserDirectoryRelativePath("bios").c_str(), false);
result &= FileSystem::CreateDirectory(GetUserDirectoryRelativePath("cache").c_str(), false);
result &= FileSystem::CreateDirectory(GetUserDirectoryRelativePath("cheats").c_str(), false);
result &= FileSystem::CreateDirectory(GetUserDirectoryRelativePath("dump").c_str(), false);
result &= FileSystem::CreateDirectory(GetUserDirectoryRelativePath("dump/audio").c_str(), false);
result &= FileSystem::CreateDirectory(GetUserDirectoryRelativePath("inputprofiles").c_str(), false);
@ -710,6 +712,13 @@ void CommonHostInterface::OnRunningGameChanged()
{
HostInterface::OnRunningGameChanged();
if (!System::IsShutdown())
{
System::SetCheatList(nullptr);
if (g_settings.auto_load_cheats)
LoadCheatListFromGameTitle();
}
#ifdef WITH_DISCORD_PRESENCE
UpdateDiscordPresence();
#endif
@ -2140,6 +2149,111 @@ void CommonHostInterface::ApplyGameSettings(bool display_osd_messages)
gs->ApplySettings(display_osd_messages);
}
std::string CommonHostInterface::GetCheatFileName() const
{
const std::string& title = System::GetRunningTitle();
if (title.empty())
return {};
return GetUserDirectoryRelativePath("cheats/%s.cht", title.c_str());
}
bool CommonHostInterface::LoadCheatList(const char* filename)
{
if (System::IsShutdown())
return false;
std::unique_ptr<CheatList> cl = std::make_unique<CheatList>();
if (!cl->LoadFromPCSXRFile(filename))
{
AddFormattedOSDMessage(15.0f, TranslateString("OSDMessage", "Failed to load cheats from '%s'."), filename);
return false;
}
AddFormattedOSDMessage(10.0f, TranslateString("OSDMessage", "Loaded %u cheats from list. %u cheats are enabled."),
cl->GetCodeCount(), cl->GetEnabledCodeCount());
System::SetCheatList(std::move(cl));
return true;
}
bool CommonHostInterface::LoadCheatListFromGameTitle()
{
const std::string filename(GetCheatFileName());
if (filename.empty() || !FileSystem::FileExists(filename.c_str()))
return false;
return LoadCheatList(filename.c_str());
}
bool CommonHostInterface::SaveCheatList(const char* filename)
{
if (!System::IsValid() || !System::HasCheatList())
return false;
if (!System::GetCheatList()->SaveToPCSXRFile(filename))
return false;
AddFormattedOSDMessage(5.0f, TranslateString("OSDMessage", "Saved %u cheats to '%s'."),
System::GetCheatList()->GetCodeCount(), filename);
return true;
}
void CommonHostInterface::SetCheatCodeState(u32 index, bool enabled, bool save_to_file)
{
if (!System::IsValid() || !System::HasCheatList())
return;
CheatList* cl = System::GetCheatList();
if (index >= cl->GetCodeCount())
return;
CheatCode& cc = cl->GetCode(index);
if (cc.enabled == enabled)
return;
cc.enabled = enabled;
if (enabled)
{
AddFormattedOSDMessage(5.0f, TranslateString("OSDMessage", "Cheat '%s' enabled."), cc.description.c_str());
}
else
{
AddFormattedOSDMessage(5.0f, TranslateString("OSDMessage", "Cheat '%s' disabled."), cc.description.c_str());
}
if (save_to_file)
{
const std::string filename(GetCheatFileName());
if (!filename.empty())
{
if (!cl->SaveToPCSXRFile(filename.c_str()))
{
AddFormattedOSDMessage(15.0f, TranslateString("OSDMessage", "Failed to save cheat list to '%s'"),
filename.c_str());
}
}
}
}
void CommonHostInterface::ApplyCheatCode(u32 index)
{
if (!System::HasCheatList() || index >= System::GetCheatList()->GetCodeCount())
return;
const CheatCode& cc = System::GetCheatList()->GetCode(index);
if (!cc.enabled)
{
cc.Apply();
AddFormattedOSDMessage(5.0f, TranslateString("OSDMessage", "Applied cheat '%s'."), cc.description.c_str());
}
else
{
AddFormattedOSDMessage(5.0f, TranslateString("OSDMessage", "Cheat '%s' is already enabled."),
cc.description.c_str());
}
}
#ifdef WITH_DISCORD_PRESENCE
void CommonHostInterface::SetDiscordPresenceEnabled(bool enabled)

View File

@ -151,6 +151,21 @@ public:
/// Saves a screenshot to the specified file. IF no file name is provided, one will be generated automatically.
bool SaveScreenshot(const char* filename = nullptr, bool full_resolution = true, bool apply_aspect_ratio = true);
/// Loads the cheat list from the specified file.
bool LoadCheatList(const char* filename);
/// Loads the cheat list for the current game title from the user directory.
bool LoadCheatListFromGameTitle();
/// Saves the current cheat list to the specified file.
bool SaveCheatList(const char* filename);
/// Enables/disabled the specified cheat code.
void SetCheatCodeState(u32 index, bool enabled, bool save_to_file);
/// Immediately applies the specified cheat code.
void ApplyCheatCode(u32 index);
protected:
enum : u32
{
@ -248,6 +263,9 @@ protected:
/// Returns the most recent resume save state.
std::string GetMostRecentResumeSaveStatePath() const;
/// Returns the path to the cheat file for the specified game title.
std::string GetCheatFileName() const;
/// Ensures the settings is valid and the correct version. If not, resets to defaults.
void CheckSettings(SettingsInterface& si);