Cheats: Cleanup global usage.

This commit is contained in:
Unknown W. Brackets 2020-04-11 13:52:25 -07:00
parent dbc838565c
commit 5ba7cca5f3
4 changed files with 58 additions and 40 deletions

View File

@ -22,8 +22,6 @@
#endif
static int CheatEvent = -1;
std::string gameTitle;
std::string activeCheatFile;
static CWCheatEngine *cheatEngine;
static bool cheatsEnabled;
void hleCheat(u64 userdata, int cyclesLate);
@ -43,9 +41,9 @@ class CheatFileParser {
public:
CheatFileParser(const std::string &filename, const std::string &gameID = "") {
#if defined(_WIN32) && !defined(__MINGW32__)
file_.open(ConvertUTF8ToWString(activeCheatFile));
file_.open(ConvertUTF8ToWString(filename));
#else
file_.open(activeCheatFile.c_str());
file_.open(filename.c_str());
#endif
validGameID_ = ReplaceAll(gameID, "-", "");
@ -247,13 +245,13 @@ static void __CheatStop() {
static void __CheatStart() {
__CheatStop();
gameTitle = g_paramSFO.GetValueString("DISC_ID");
if (gameTitle != "") { //this only generates ini files on boot, let's leave homebrew ini file for UI
std::string gameID = g_paramSFO.GetValueString("DISC_ID");
cheatEngine = new CWCheatEngine(gameID);
// This only generates ini files on boot, let's leave homebrew ini file for UI.
if (!gameID.empty()) {
cheatEngine->CreateCheatFile();
}
cheatEngine = new CWCheatEngine();
cheatEngine->ParseCheats();
g_Config.bReloadCheats = false;
cheatsEnabled = true;
@ -348,28 +346,32 @@ void hleCheat(u64 userdata, int cyclesLate) {
cheatEngine->Run();
}
CWCheatEngine::CWCheatEngine() {
CWCheatEngine::CWCheatEngine(const std::string &gameID) : gameID_(gameID) {
}
void CWCheatEngine::CreateCheatFile() {
activeCheatFile = GetSysDirectory(DIRECTORY_CHEATS) + gameTitle + ".ini";
filename_ = GetSysDirectory(DIRECTORY_CHEATS) + gameID_ + ".ini";
File::CreateFullPath(GetSysDirectory(DIRECTORY_CHEATS));
if (!File::Exists(activeCheatFile)) {
FILE *f = File::OpenCFile(activeCheatFile, "wb");
if (!File::Exists(filename_)) {
FILE *f = File::OpenCFile(filename_, "wb");
if (f) {
fwrite("\xEF\xBB\xBF\n", 1, 4, f);
fclose(f);
}
if (!File::Exists(activeCheatFile)) {
if (!File::Exists(filename_)) {
auto err = GetI18NCategory("Error");
host->NotifyUserMessage(err->T("Unable to create cheat file, disk may be full"));
}
}
}
std::string CWCheatEngine::CheatFilename() {
return filename_;
}
void CWCheatEngine::ParseCheats() {
CheatFileParser parser(activeCheatFile, gameTitle);
CheatFileParser parser(filename_, gameID_);
parser.Parse();
// TODO: Report errors.
@ -384,7 +386,7 @@ u32 CWCheatEngine::GetAddress(u32 value) {
}
std::vector<CheatFileInfo> CWCheatEngine::FileInfo() {
CheatFileParser parser(activeCheatFile, gameTitle);
CheatFileParser parser(filename_, gameID_);
parser.Parse();
return parser.GetFileInfo();

View File

@ -46,10 +46,11 @@ struct CheatOperation;
class CWCheatEngine {
public:
CWCheatEngine();
CWCheatEngine(const std::string &gameID);
std::vector<CheatFileInfo> FileInfo();
void ParseCheats();
void CreateCheatFile();
std::string CheatFilename();
void Run();
bool HasCheats();
@ -67,4 +68,6 @@ private:
bool TestIfAddr(const CheatOperation &op, bool(*oper)(int a, int b));
std::vector<CheatCode> cheats_;
std::string gameID_;
std::string filename_;
};

View File

@ -31,32 +31,41 @@
static const int FILE_CHECK_FRAME_INTERVAL = 53;
CwCheatScreen::CwCheatScreen(std::string gamePath)
CwCheatScreen::CwCheatScreen(const std::string &gamePath)
: UIDialogScreenWithBackground() {
gamePath_ = gamePath;
}
CwCheatScreen::~CwCheatScreen() {
delete engine_;
}
void CwCheatScreen::LoadCheatInfo() {
std::shared_ptr<GameInfo> info = g_gameInfoCache->GetInfo(nullptr, gamePath_, 0);
std::string gameID;
if (info && info->paramSFOLoaded) {
gameTitle = info->paramSFO.GetValueString("DISC_ID");
gameID = info->paramSFO.GetValueString("DISC_ID");
}
if ((info->id.empty() || !info->disc_total)
&& gamePath_.find("/PSP/GAME/") != std::string::npos) {
gameTitle = g_paramSFO.GenerateFakeID(gamePath_);
gameID = g_paramSFO.GenerateFakeID(gamePath_);
}
if (engine_ == nullptr || gameID != gameID_) {
gameID_ = gameID;
delete engine_;
engine_ = new CWCheatEngine(gameID_);
engine_->CreateCheatFile();
}
// We won't parse this, just using it to detect changes to the file.
std::string str;
if (readFileToString(true, activeCheatFile.c_str(), str)) {
if (readFileToString(true, engine_->CheatFilename().c_str(), str)) {
fileCheckHash_ = CityHash64(str.c_str(), str.size());
}
fileCheckCounter_ = 0;
CWCheatEngine *cheatEngine2 = new CWCheatEngine();
cheatEngine2->CreateCheatFile();
fileInfo_ = cheatEngine2->FileInfo();
delete cheatEngine2;
fileInfo_ = engine_->FileInfo();
// Let's also trigger a reload, in case it changed.
g_Config.bReloadCheats = true;
@ -105,10 +114,10 @@ void CwCheatScreen::CreateViews() {
}
void CwCheatScreen::update() {
if (fileCheckCounter_++ >= FILE_CHECK_FRAME_INTERVAL) {
if (fileCheckCounter_++ >= FILE_CHECK_FRAME_INTERVAL && engine_) {
// Check if the file has changed. If it has, we'll reload.
std::string str;
if (readFileToString(true, activeCheatFile.c_str(), str)) {
if (readFileToString(true, engine_->CheatFilename().c_str(), str)) {
uint64_t newHash = CityHash64(str.c_str(), str.size());
if (newHash != fileCheckHash_) {
// This will update the hash.
@ -160,17 +169,19 @@ UI::EventReturn CwCheatScreen::OnEditCheatFile(UI::EventParams &params) {
if (MIPSComp::jit) {
MIPSComp::jit->ClearCache();
}
if (engine_) {
#if PPSSPP_PLATFORM(UWP)
LaunchBrowser(activeCheatFile.c_str());
LaunchBrowser(engine_->CheatFilename().c_str());
#else
File::openIniFile(activeCheatFile);
File::openIniFile(engine_->CheatFilename());
#endif
}
return UI::EVENT_DONE;
}
UI::EventReturn CwCheatScreen::OnImportCheat(UI::EventParams &params) {
if (gameTitle.length() != 9) {
WARN_LOG(COMMON, "CWCHEAT: Incorrect ID(%s) - can't import cheats.", gameTitle.c_str());
if (gameID_.length() != 9 || !engine_) {
WARN_LOG(COMMON, "CWCHEAT: Incorrect ID(%s) - can't import cheats.", gameID_.c_str());
return UI::EVENT_DONE;
}
std::string line;
@ -179,7 +190,7 @@ UI::EventReturn CwCheatScreen::OnImportCheat(UI::EventParams &params) {
std::vector<std::string> newList;
std::string cheatFile = GetSysDirectory(DIRECTORY_CHEATS) + "cheat.db";
std::string gameID = StringFromFormat("_S %s-%s", gameTitle.substr(0, 4).c_str(), gameTitle.substr(4).c_str());
std::string gameID = StringFromFormat("_S %s-%s", gameID_.substr(0, 4).c_str(), gameID_.substr(4).c_str());
std::fstream fs;
File::OpenCPPFile(fs, cheatFile, std::ios::in);
@ -226,10 +237,10 @@ UI::EventReturn CwCheatScreen::OnImportCheat(UI::EventParams &params) {
}
fs.close();
std::string title2;
File::OpenCPPFile(fs, activeCheatFile, std::ios::in);
File::OpenCPPFile(fs, engine_->CheatFilename(), std::ios::in);
getline(fs, title2);
fs.close();
File::OpenCPPFile(fs, activeCheatFile, std::ios::out | std::ios::app);
File::OpenCPPFile(fs, engine_->CheatFilename(), std::ios::out | std::ios::app);
auto it = title.begin();
if (((title2[0] == '_' && title2[1] != 'S') || title2[0] == '/' || title2[0] == '#') && it != title.end() && (++it) != title.end()) {
@ -266,7 +277,7 @@ UI::EventReturn CwCheatScreen::OnCheckBox(int index) {
bool CwCheatScreen::RebuildCheatFile(int index) {
std::fstream fs;
if (!File::OpenCPPFile(fs, activeCheatFile, std::ios::in)) {
if (!engine_ || !File::OpenCPPFile(fs, engine_->CheatFilename(), std::ios::in)) {
return false;
}
@ -292,8 +303,9 @@ bool CwCheatScreen::RebuildCheatFile(int index) {
}
line = (info.enabled ? "_C1 " : "_C0 ") + info.name;
}
return true;
}
return false;
};
if (index == INDEX_ALL) {
@ -310,7 +322,7 @@ bool CwCheatScreen::RebuildCheatFile(int index) {
}
if (!File::OpenCPPFile(fs, activeCheatFile, std::ios::out | std::ios::trunc)) {
if (!File::OpenCPPFile(fs, engine_->CheatFilename(), std::ios::out | std::ios::trunc)) {
return false;
}

View File

@ -24,13 +24,12 @@
#include "UI/MiscScreens.h"
struct CheatFileInfo;
extern std::string activeCheatFile;
extern std::string gameTitle;
class CWCheatEngine;
class CwCheatScreen : public UIDialogScreenWithBackground {
public:
CwCheatScreen(std::string gamePath);
CwCheatScreen(const std::string &gamePath);
~CwCheatScreen();
void LoadCheatInfo();
@ -52,8 +51,10 @@ private:
bool RebuildCheatFile(int index);
UI::ScrollView *rightScroll_ = nullptr;
CWCheatEngine *engine_ = nullptr;
std::vector<CheatFileInfo> fileInfo_;
std::string gamePath_;
std::string gameID_;
int fileCheckCounter_ = 0;
uint64_t fileCheckHash_;
bool enableAllFlag_ = false;