Cheats: Move file clearing into core

Needed for Android.
This commit is contained in:
Stenzek 2024-12-06 22:05:19 +10:00
parent d5432da082
commit 3a661a1c3d
No known key found for this signature in database
6 changed files with 49 additions and 36 deletions

View File

@ -107,11 +107,17 @@ public:
ALWAYS_INLINE bool IsOpen() const { return static_cast<bool>(m_zip); } ALWAYS_INLINE bool IsOpen() const { return static_cast<bool>(m_zip); }
bool Open(const char* name) bool Open(bool cheats)
{ {
if (m_zip) if (m_zip)
return true; return true;
#ifndef __ANDROID__
const char* name = cheats ? "cheats.zip" : "patches.zip";
#else
const char* name = cheats ? "patchcodes.zip" : "patches.zip";
#endif
Error error; Error error;
std::optional<DynamicHeapArray<u8>> data = Host::ReadResourceFile(name, false, &error); std::optional<DynamicHeapArray<u8>> data = Host::ReadResourceFile(name, false, &error);
if (!data.has_value()) if (!data.has_value())
@ -398,10 +404,7 @@ void Cheats::EnumerateChtFiles(const std::string_view serial, std::optional<Game
const std::unique_lock lock(s_zip_mutex); const std::unique_lock lock(s_zip_mutex);
CheatArchive& archive = cheats ? s_cheats_zip : s_patches_zip; CheatArchive& archive = cheats ? s_cheats_zip : s_patches_zip;
if (!archive.IsOpen()) if (!archive.IsOpen())
{ archive.Open(cheats);
const char* archive_name = cheats ? "cheats.zip" : "patches.zip";
archive.Open(archive_name);
}
if (archive.IsOpen()) if (archive.IsOpen())
{ {
@ -701,6 +704,36 @@ bool Cheats::SaveCodesToFile(const char* path, const CodeInfoList& codes, Error*
return true; return true;
} }
void Cheats::RemoveAllCodes(const std::string_view serial, const std::string_view title, std::optional<GameHash> hash)
{
Error error;
std::string path = GetChtFilename(serial, hash, true);
if (FileSystem::FileExists(path.c_str()))
{
if (!FileSystem::DeleteFile(path.c_str(), &error))
ERROR_LOG("Failed to remove cht file '{}': {}", Path::GetFileName(path), error.GetDescription());
}
// check for a non-hashed path and remove that too
path = GetChtFilename(serial, std::nullopt, true);
if (FileSystem::FileExists(path.c_str()))
{
if (!FileSystem::DeleteFile(path.c_str(), &error))
ERROR_LOG("Failed to remove cht file '{}': {}", Path::GetFileName(path), error.GetDescription());
}
// and a legacy cht file with the game title
if (!title.empty())
{
path = fmt::format("{}" FS_OSPATH_SEPARATOR_STR "{}.cht", EmuFolders::Cheats, Path::SanitizeFileName(title));
if (FileSystem::FileExists(path.c_str()))
{
if (!FileSystem::DeleteFile(path.c_str(), &error))
ERROR_LOG("Failed to remove cht file '{}': {}", Path::GetFileName(path), error.GetDescription());
}
}
}
std::string Cheats::GetChtFilename(const std::string_view serial, std::optional<GameHash> hash, bool cheats) std::string Cheats::GetChtFilename(const std::string_view serial, std::optional<GameHash> hash, bool cheats)
{ {
return Path::Combine(cheats ? EmuFolders::Cheats : EmuFolders::Patches, GetChtTemplate(serial, hash, false)); return Path::Combine(cheats ? EmuFolders::Cheats : EmuFolders::Patches, GetChtTemplate(serial, hash, false));

View File

@ -117,6 +117,9 @@ extern bool UpdateCodeInFile(const char* path, const std::string_view name, cons
/// Updates or adds multiple codes to the file, rewriting it. /// Updates or adds multiple codes to the file, rewriting it.
extern bool SaveCodesToFile(const char* path, const CodeInfoList& codes, Error* error); extern bool SaveCodesToFile(const char* path, const CodeInfoList& codes, Error* error);
/// Removes any .cht files for the specified game.
extern void RemoveAllCodes(const std::string_view serial, const std::string_view title, std::optional<GameHash> hash);
/// Returns the path to a new cheat/patch cht for the specified serial and hash. /// Returns the path to a new cheat/patch cht for the specified serial and hash.
extern std::string GetChtFilename(const std::string_view serial, std::optional<GameHash> hash, bool cheats); extern std::string GetChtFilename(const std::string_view serial, std::optional<GameHash> hash, bool cheats);

View File

@ -170,7 +170,7 @@ struct Settings
bool display_stretch_vertically : 1 = false; bool display_stretch_vertically : 1 = false;
bool display_auto_resize_window : 1 = false; bool display_auto_resize_window : 1 = false;
float display_pre_frame_sleep_buffer = DEFAULT_DISPLAY_PRE_FRAME_SLEEP_BUFFER; float display_pre_frame_sleep_buffer = DEFAULT_DISPLAY_PRE_FRAME_SLEEP_BUFFER;
float display_osd_scale = 100.0f; float display_osd_scale = DEFAULT_OSD_SCALE;
float display_osd_margin = 0.0f; float display_osd_margin = 0.0f;
float gpu_pgxp_tolerance = -1.0f; float gpu_pgxp_tolerance = -1.0f;
float gpu_pgxp_depth_clear_threshold = DEFAULT_GPU_PGXP_DEPTH_THRESHOLD / GPU_PGXP_DEPTH_THRESHOLD_SCALE; float gpu_pgxp_depth_clear_threshold = DEFAULT_GPU_PGXP_DEPTH_THRESHOLD / GPU_PGXP_DEPTH_THRESHOLD_SCALE;

View File

@ -626,34 +626,7 @@ void GameCheatSettingsWidget::onClearClicked()
} }
disableAllCheats(); disableAllCheats();
Cheats::RemoveAllCodes(m_dialog->getGameSerial(), m_dialog->getGameTitle(), m_dialog->getGameHash());
Error error;
std::string path = Cheats::GetChtFilename(m_dialog->getGameSerial(), m_dialog->getGameHash(), true);
if (FileSystem::FileExists(path.c_str()))
{
if (!FileSystem::DeleteFile(path.c_str(), &error))
ERROR_LOG("Failed to remove cht file '{}': {}", Path::GetFileName(path), error.GetDescription());
}
// check for a non-hashed path and remove that too
path = Cheats::GetChtFilename(m_dialog->getGameSerial(), std::nullopt, true);
if (FileSystem::FileExists(path.c_str()))
{
if (!FileSystem::DeleteFile(path.c_str(), &error))
ERROR_LOG("Failed to remove cht file '{}': {}", Path::GetFileName(path), error.GetDescription());
}
// and a legacy cht file with the game title
if (const std::string& title = m_dialog->getGameTitle(); !title.empty())
{
path = fmt::format("{}" FS_OSPATH_SEPARATOR_STR "{}.cht", EmuFolders::Cheats, Path::SanitizeFileName(title));
if (FileSystem::FileExists(path.c_str()))
{
if (!FileSystem::DeleteFile(path.c_str(), &error))
ERROR_LOG("Failed to remove cht file '{}': {}", Path::GetFileName(path), error.GetDescription());
}
}
reloadList(); reloadList();
} }

View File

@ -54,7 +54,11 @@ namespace ImGuiManager {
using WCharType = u32; using WCharType = u32;
/// Default size for screen margins. /// Default size for screen margins.
#ifndef __ANDROID__
static constexpr float DEFAULT_SCREEN_MARGIN = 10.0f; static constexpr float DEFAULT_SCREEN_MARGIN = 10.0f;
#else
static constexpr float DEFAULT_SCREEN_MARGIN = 16.0f;
#endif
/// Sets the path to the font to use. Empty string means to use the default. /// Sets the path to the font to use. Empty string means to use the default.
void SetFontPathAndRange(std::string path, std::vector<WCharType> range); void SetFontPathAndRange(std::string path, std::vector<WCharType> range);

View File

@ -15,7 +15,7 @@
#if defined(_WIN32) #if defined(_WIN32)
#include "common/windows_headers.h" #include "common/windows_headers.h"
#elif defined(__linux__) #elif defined(__linux__) && !defined(__ANDROID__)
#include <signal.h> #include <signal.h>
#include <ucontext.h> #include <ucontext.h>
#include <unistd.h> #include <unistd.h>
@ -136,7 +136,7 @@ bool PageFaultHandler::Install(Error* error)
return true; return true;
} }
#else #elif !defined(__ANDROID__)
namespace PageFaultHandler { namespace PageFaultHandler {
static void SignalHandler(int sig, siginfo_t* info, void* ctx); static void SignalHandler(int sig, siginfo_t* info, void* ctx);