From 34d138a12ff9c00a3b6835d20e77824d690fcfa1 Mon Sep 17 00:00:00 2001 From: LunaMoo Date: Mon, 24 May 2021 22:30:07 +0200 Subject: [PATCH] Fix UI crash on import cheat without cheat.db present Some changes to ParseCheatDB to make it easier to merge updates in the future --- UI/CwCheatScreen.cpp | 25 ++++++++++++++----------- UI/CwCheatScreen.h | 2 +- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/UI/CwCheatScreen.cpp b/UI/CwCheatScreen.cpp index 09f0da889e..57e252483e 100644 --- a/UI/CwCheatScreen.cpp +++ b/UI/CwCheatScreen.cpp @@ -194,7 +194,7 @@ static char *GetLineNoNewline(char *temp, int sz, FILE *fp) { return line; } -void CwCheatScreen::ParseCheatDB(std::string line, std::vector title, std::vector newList, Path cheatFile) { +UI::EventReturn CwCheatScreen::ParseCheatDB(std::string line, std::vector title, std::vector newList, Path cheatFile) { bool finished = false; std::string gameID = StringFromFormat("_S %s-%s", gameID_.substr(0, 4).c_str(), gameID_.substr(4).c_str()); @@ -202,11 +202,12 @@ void CwCheatScreen::ParseCheatDB(std::string line, std::vector titl if (!in) { WARN_LOG(COMMON, "Unable to open %s\n", cheatFile.c_str()); + return UI::EVENT_SKIPPED; } char linebuf[2048]{}; while (in && !feof(in)) { - char *line = GetLineNoNewline(linebuf, sizeof(linebuf), in); + char* line = GetLineNoNewline(linebuf, sizeof(linebuf), in); if (line && gameID == line) { title.push_back(line); line = GetLineNoNewline(linebuf, sizeof(linebuf), in); @@ -218,7 +219,7 @@ void CwCheatScreen::ParseCheatDB(std::string line, std::vector titl } if (line && line[0] == '_' && line[1] == 'C') { // Test if cheat already exists. - for (const auto &existing : fileInfo_) { + for (const auto& existing : fileInfo_) { if (std::string(line).substr(4) == existing.name) { finished = false; goto loop; @@ -247,16 +248,16 @@ void CwCheatScreen::ParseCheatDB(std::string line, std::vector titl std::string title2; // Hmm, this probably gets confused about BOMs? - FILE *inTitle2 = File::OpenCFile(engine_->CheatFilename(), "rt"); + FILE* inTitle2 = File::OpenCFile(engine_->CheatFilename(), "rt"); if (inTitle2) { char temp[2048]; - char *line = GetLineNoNewline(temp, sizeof(temp), inTitle2); + char* line = GetLineNoNewline(temp, sizeof(temp), inTitle2); if (line) title2 = line; fclose(inTitle2); } - FILE *append = File::OpenCFile(engine_->CheatFilename(), "at"); + FILE* append = File::OpenCFile(engine_->CheatFilename(), "at"); if (!append) return UI::EVENT_SKIPPED; @@ -277,6 +278,10 @@ void CwCheatScreen::ParseCheatDB(std::string line, std::vector titl } } fclose(append); + + g_Config.bReloadCheats = true; + RecreateViews(); + return UI::EVENT_DONE; } UI::EventReturn CwCheatScreen::OnImportCheat(UI::EventParams ¶ms) { @@ -288,14 +293,12 @@ UI::EventReturn CwCheatScreen::OnImportCheat(UI::EventParams ¶ms) { std::vector title; std::vector newList; - Path cheatFile = GetSysDirectory(DIRECTORY_CHEATS) + "cheat.db"; + Path cheatFile = GetSysDirectory(DIRECTORY_CHEATS) / "cheat.db"; ParseCheatDB(line, title, newList, cheatFile); - const std::string path = File::GetExeDirectory(); - Path patchesFile = path + "assets/patches/cheat.db"; + Path path = File::GetExeDirectory(); + Path patchesFile = path / "assets/patches/cheat.db"; ParseCheatDB(line, title, newList, patchesFile); - g_Config.bReloadCheats = true; - RecreateViews(); return UI::EVENT_DONE; } diff --git a/UI/CwCheatScreen.h b/UI/CwCheatScreen.h index 8d847d6f3d..7b7b7de1bb 100644 --- a/UI/CwCheatScreen.h +++ b/UI/CwCheatScreen.h @@ -32,7 +32,7 @@ public: ~CwCheatScreen(); void LoadCheatInfo(); - void ParseCheatDB(std::string line, std::vector title, std::vector newList, std::string cheatFile); + UI::EventReturn ParseCheatDB(std::string line, std::vector title, std::vector newList, Path cheatFile); UI::EventReturn OnAddCheat(UI::EventParams ¶ms); UI::EventReturn OnImportCheat(UI::EventParams ¶ms); UI::EventReturn OnEditCheatFile(UI::EventParams ¶ms);