From c2732e4a3fd877b2045b7d4cd74cfdc9dd8cc1c6 Mon Sep 17 00:00:00 2001 From: Steven Cherry Date: Fri, 23 Aug 2013 22:07:41 -0500 Subject: [PATCH] Add "import from cheat.db" and "enable/disable all codes" --- Core/CwCheat.cpp | 6 ++- UI/CwCheatScreen.cpp | 124 ++++++++++++++++++++++++++++++++++++++++--- UI/CwCheatScreen.h | 4 ++ 3 files changed, 125 insertions(+), 9 deletions(-) diff --git a/Core/CwCheat.cpp b/Core/CwCheat.cpp index e479d170d1..e175aadc5b 100644 --- a/Core/CwCheat.cpp +++ b/Core/CwCheat.cpp @@ -19,9 +19,12 @@ void __CheatInit() { //Moved createFullPath to CheatInit from the constructor because it spams the log and constantly checks if exists. In here, only checks once. // Cheats aren't working on Android yet - need to figure out the directory structure -#ifndef ANDROID gameTitle = g_paramSFO.GetValueString("DISC_ID"); +#ifdef ANDROID + activeCheatFile = g_Config.memCardDirectory + "PSP/Cheats/" + gameTitle + ".ini"; +#else activeCheatFile = CHEATS_DIR + "/" + gameTitle + ".ini"; +#endif File::CreateFullPath(CHEATS_DIR); if (g_Config.bEnableCheats) { @@ -36,7 +39,6 @@ void __CheatInit() { CheatEvent = CoreTiming::RegisterEvent("CheatEvent", &hleCheat); CoreTiming::ScheduleEvent(msToCycles(77), CheatEvent, 0); } -#endif } void __CheatShutdown() { diff --git a/UI/CwCheatScreen.cpp b/UI/CwCheatScreen.cpp index 331f12bfc3..4d7d99fb44 100644 --- a/UI/CwCheatScreen.cpp +++ b/UI/CwCheatScreen.cpp @@ -35,7 +35,7 @@ #include "UI/MiscScreens.h" #include "UI/CwCheatScreen.h" #include "UI/view.h" - +bool enableAll = false; static std::vector cheatList; extern void DrawBackground(float alpha); static CWCheatEngine *cheatEngine2; @@ -65,14 +65,20 @@ void CwCheatScreen::CreateViews() { I18NCategory *k = GetI18NCategory("CwCheats"); formattedList = CreateCodeList(); root_ = new LinearLayout(ORIENT_HORIZONTAL); + Margins actionMenuMargins(50, 100, 100, 50); - LinearLayout *leftColumn = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(200, FILL_PARENT)); + LinearLayout *leftColumn = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(400, FILL_PARENT)); + leftColumn->Add(new ItemHeader(k->T("Options"))); leftColumn->Add(new Choice(k->T("Back")))->OnClick.Handle(this, &CwCheatScreen::OnBack); + //leftColumn->Add(new Choice(k->T("Add Cheat")))->OnClick.Handle(this, &CwCheatScreen::OnAddCheat); + leftColumn->Add(new Choice(k->T("Import from cheat.db")))->OnClick.Handle(this, &CwCheatScreen::OnImportCheat); + leftColumn->Add(new Choice(k->T("Enable/Disable All")))->OnClick.Handle(this, &CwCheatScreen::OnEnableAll); - ScrollView *rightScroll = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(1.0f)); + ScrollView *rightScroll = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(0.5f)); rightScroll->SetScrollToTop(false); - LinearLayout *rightColumn = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(1.0f)); + LinearLayout *rightColumn = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(200, FILL_PARENT, actionMenuMargins)); + LayoutParams *layout = new LayoutParams(500, 50, LP_PLAIN); rightScroll->Add(rightColumn); root_->Add(leftColumn); @@ -80,7 +86,7 @@ void CwCheatScreen::CreateViews() { rightColumn->Add(new ItemHeader(k->T("Cheats"))); for (size_t i = 0; i < formattedList.size(); i++) { name = formattedList[i].c_str(); - rightColumn->Add(new CheatCheckBox(&enableCheat[i], k->T(name)))->OnClick.Handle(this, &CwCheatScreen::OnCheckBox); + rightColumn->Add(new CheatCheckBox(&enableCheat[i], k->T(name), "" ))->OnClick.Handle(this, &CwCheatScreen::OnCheckBox); } } @@ -90,6 +96,110 @@ UI::EventReturn CwCheatScreen::OnBack(UI::EventParams ¶ms) g_Config.bReloadCheats = true; return UI::EVENT_DONE; } +UI::EventReturn CwCheatScreen::OnEnableAll(UI::EventParams ¶ms) +{ + std::vector temp = cheatList; + enableAll = !enableAll; + os.open(activeCheatFile.c_str()); + for (int j = 0; j < temp.size(); j++) { + if (enableAll == 1 && temp[j].substr(0, 3) == "_C0"){ + temp[j].replace(0,3,"_C1"); + for (int x = 0; x < sizeof(enableCheat); x++) { + enableCheat[x] = true; + } + + } + else if (enableAll == 0 && temp[j].substr(0, 3) == "_C1") { + temp[j].replace(0, 3, "_C0"); + for (int x = 0; x < 64; x++) { + enableCheat[x] = false; + } + + } + } + for (int i = 0; i < temp.size(); i++) { + os << temp[i]; + if (i < temp.size() - 1) { + os << "\n"; + } + } + os.close(); + return UI::EVENT_DONE; +} +UI::EventReturn CwCheatScreen::OnAddCheat(UI::EventParams ¶ms) +{ + screenManager()->finishDialog(this, DR_OK); + g_Config.bReloadCheats = true; + return UI::EVENT_DONE; +} +UI::EventReturn CwCheatScreen::OnImportCheat(UI::EventParams ¶ms) +{ + std::string line; + bool finished = false, skip = false; + std::vector newList; +#ifdef ANDROID + is.open (g_Config.memCardDirectory + "PSP/Cheats/cheat.db"; +#else + is.open("cheats/cheat.db"); +#endif + os.open(activeCheatFile.c_str(),std::ios::app); + while (is.good()) + { + getline(is, line); // get line from file + if (line == "_S " + gameTitle.substr(0, 4) + "-" + gameTitle.substr(4)) + { + getline(is, line); + getline(is, line); + + + + do { + if (finished == false){ + getline(is, line); + } + if (line.substr(0, 3) == "_C0") + { + //Test if cheat already exists in cheatList + for (int j = 0; j < formattedList.size(); j++) { + if (line.substr(4) == formattedList[j]) { + finished = false; + goto loop; + } + } + + newList.push_back(line); + getline(is, line); + do { + newList.push_back(line); + getline(is, line); + } while (line.substr(0, 2) == "_L"); + finished = true; + } + else { continue; } + loop:; + } while (line.substr(0, 2) != "_S"); + finished = true; + } + if (finished == true) + break; + } + if (newList.size() != 0) + { + os << "\n"; + } + for (int i = 0; i < newList.size(); i++) { + os << newList[i]; + if (i < newList.size() - 1) { + os << "\n"; + } + } + os.close(); + is.close(); + g_Config.bReloadCheats = true; + //Need a better way to refresh the screen, rather than exiting and having to re-enter. + screenManager()->finishDialog(this, DR_OK); + return UI::EVENT_DONE; +} UI::EventReturn CwCheatScreen::OnCheckBox(UI::EventParams ¶ms) { return UI::EVENT_DONE; @@ -133,8 +243,8 @@ void CwCheatScreen::processFileOff(std::string deactivatedCheat) { void CheatCheckBox::Draw(UIContext &dc) { ClickableItem::Draw(dc); - int paddingX = 12; - int paddingY = 8; + int paddingX = 16; + int paddingY = 12; int image = *toggle_ ? dc.theme->checkOn : dc.theme->checkOff; diff --git a/UI/CwCheatScreen.h b/UI/CwCheatScreen.h index fafa97574e..85b85c9022 100644 --- a/UI/CwCheatScreen.h +++ b/UI/CwCheatScreen.h @@ -24,6 +24,7 @@ #include "UI/GameSettingsScreen.h" using namespace UI; extern std::string activeCheatFile; +extern std::string gameTitle; class CwCheatScreen : public UIDialogScreenWithBackground { public: @@ -36,6 +37,9 @@ public: const char * name; std::string activatedCheat, deactivatedCheat; UI::EventReturn OnBack(UI::EventParams ¶ms); + UI::EventReturn OnAddCheat(UI::EventParams ¶ms); + UI::EventReturn OnImportCheat(UI::EventParams ¶ms); + UI::EventReturn OnEnableAll(UI::EventParams ¶ms); protected: virtual void CreateViews();