Skip to content

Commit

Permalink
Merge pull request #17164 from hrydgard/cheat-import-fix
Browse files Browse the repository at this point in the history
Cheat import: Allow picking a file to import from
  • Loading branch information
hrydgard authored Mar 23, 2023
2 parents 8804c3c + 84f33d9 commit c791d64
Show file tree
Hide file tree
Showing 52 changed files with 145 additions and 123 deletions.
26 changes: 24 additions & 2 deletions Common/File/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,34 @@ std::wstring Path::ToWString() const {
}
#endif

std::string Path::ToVisualString() const {
std::string Path::ToVisualString(const char *relativeRoot) const {
if (type_ == PathType::CONTENT_URI) {
return AndroidContentURI(path_).ToVisualString();
#if PPSSPP_PLATFORM(WINDOWS)
} else if (type_ == PathType::NATIVE) {
return ReplaceAll(path_, "/", "\\");
// It can be useful to show the path as relative to the memstick
if (relativeRoot) {
std::string root = ReplaceAll(relativeRoot, "/", "\\");
std::string path = ReplaceAll(path_, "/", "\\");
if (startsWithNoCase(path, root)) {
return path.substr(root.size());
} else {
return path;
}
} else {
return ReplaceAll(path_, "/", "\\");
}
#else
if (relativeRoot) {
std::string root = relativeRoot;
if (startsWithNoCase(path_, root)) {
return path_.substr(root.size());
} else {
return path_;
}
} else {
return path_;
}
#endif
} else {
return path_;
Expand Down
3 changes: 2 additions & 1 deletion Common/File/Path.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ class Path {
std::wstring ToWString() const;
#endif

std::string ToVisualString() const;
// Pass in a relative root to turn the path into a relative path - if it is one!
std::string ToVisualString(const char *relativeRoot = nullptr) const;

bool CanNavigateUp() const;
Path NavigateUp() const;
Expand Down
1 change: 1 addition & 0 deletions Common/System/Request.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ enum class BrowseFileType {
BOOTABLE,
IMAGE,
INI,
DB,
ANY,
};

Expand Down
8 changes: 4 additions & 4 deletions Common/UI/View.h
Original file line number Diff line number Diff line change
Expand Up @@ -741,10 +741,10 @@ class Choice : public ClickableItem {
std::string smallText_;
ImageID image_; // Centered if no text, on the left if text.
ImageID rightIconImage_ = ImageID::invalid(); // Shows in the right.
float rightIconScale_;
float rightIconRot_;
bool rightIconFlipH_;
bool rightIconKeepColor_;
float rightIconScale_ = 0.0f;
float rightIconRot_ = 0.0f;
bool rightIconFlipH_ = false;
bool rightIconKeepColor_ = false;
Padding textPadding_;
bool centered_ = false;
float imgScale_ = 1.0f;
Expand Down
3 changes: 3 additions & 0 deletions Qt/QtMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@ bool MainUI::HandleCustomEvent(QEvent *e) {
case BrowseFileType::INI:
filter = "INI files (*.ini)";
break;
case BrowseFileType::DB:
filter = "DB files (*.db)";
break;
case BrowseFileType::ANY:
break;
}
Expand Down
76 changes: 57 additions & 19 deletions UI/CwCheatScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "Common/File/FileUtil.h"
#include "Common/StringUtils.h"
#include "Common/System/System.h"
#include "Common/System/Request.h"
#include "Core/Core.h"
#include "Core/Config.h"
#include "Core/CwCheat.h"
Expand All @@ -34,6 +35,10 @@

static const int FILE_CHECK_FRAME_INTERVAL = 53;

static Path GetGlobalCheatFilePath() {
return GetSysDirectory(DIRECTORY_CHEATS) / "cheat.db";
}

CwCheatScreen::CwCheatScreen(const Path &gamePath)
: UIDialogScreenWithGameBackground(gamePath) {
}
Expand All @@ -53,7 +58,7 @@ void CwCheatScreen::LoadCheatInfo() {
gameID = g_paramSFO.GenerateFakeID(gamePath_.ToString());
}

if (engine_ == nullptr || gameID != gameID_) {
if (!engine_ || gameID != gameID_) {
gameID_ = gameID;
delete engine_;
engine_ = new CWCheatEngine(gameID_);
Expand All @@ -77,20 +82,33 @@ void CwCheatScreen::CreateViews() {
using namespace UI;
auto cw = GetI18NCategory("CwCheats");
auto di = GetI18NCategory("Dialog");
auto mm = GetI18NCategory("MainMenu");

root_ = new AnchorLayout(new LayoutParams(FILL_PARENT, FILL_PARENT));

LoadCheatInfo();
Margins actionMenuMargins(50, -15, 15, 0);

LinearLayout *leftColumn = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(400, FILL_PARENT));
leftColumn->Add(new ItemHeader(cw->T("Options")));
//leftColumn->Add(new Choice(cw->T("Add Cheat")))->OnClick.Handle(this, &CwCheatScreen::OnAddCheat);
leftColumn->Add(new Choice(cw->T("Import Cheats")))->OnClick.Handle(this, &CwCheatScreen::OnImportCheat);
leftColumn->Add(new ItemHeader(cw->T("Import Cheats")));

Path cheatPath = GetGlobalCheatFilePath();

std::string root = GetSysDirectory(DIRECTORY_MEMSTICK_ROOT).ToString();

std::string title = StringFromFormat(cw->T("Import from %s"), "PSP/Cheats/cheat.db");

leftColumn->Add(new Choice(title.c_str()))->OnClick.Handle(this, &CwCheatScreen::OnImportCheat);
leftColumn->Add(new Choice(mm->T("Browse"), ImageID("I_FOLDER_OPEN")))->OnClick.Handle(this, &CwCheatScreen::OnImportBrowse);
errorMessageView_ = leftColumn->Add(new TextView(di->T("LoadingFailed")));
errorMessageView_->SetVisibility(V_GONE);

leftColumn->Add(new ItemHeader(di->T("Options")));
#if !defined(MOBILE_DEVICE)
leftColumn->Add(new Choice(cw->T("Edit Cheat File")))->OnClick.Handle(this, &CwCheatScreen::OnEditCheatFile);
#endif
leftColumn->Add(new Choice(cw->T("Enable/Disable All")))->OnClick.Handle(this, &CwCheatScreen::OnEnableAll);
leftColumn->Add(new Choice(di->T("Disable All")))->OnClick.Handle(this, &CwCheatScreen::OnDisableAll);
leftColumn->Add(new PopupSliderChoice(&g_Config.iCwCheatRefreshRate, 1, 1000, cw->T("Refresh Rate"), 1, screenManager()));

rightScroll_ = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT, FILL_PARENT, 0.5f));
Expand Down Expand Up @@ -141,12 +159,10 @@ void CwCheatScreen::onFinish(DialogResult result) {
}
}

UI::EventReturn CwCheatScreen::OnEnableAll(UI::EventParams &params) {
enableAllFlag_ = !enableAllFlag_;

// Flip all the switches.
UI::EventReturn CwCheatScreen::OnDisableAll(UI::EventParams &params) {
// Disable all the switches.
for (auto &info : fileInfo_) {
info.enabled = enableAllFlag_;
info.enabled = false;
}

if (!RebuildCheatFile(INDEX_ALL)) {
Expand Down Expand Up @@ -193,24 +209,49 @@ static char *GetLineNoNewline(char *temp, int sz, FILE *fp) {
return line;
}

UI::EventReturn CwCheatScreen::OnImportBrowse(UI::EventParams &params) {
System_BrowseForFile("Open cheat DB file", BrowseFileType::DB, [&](const std::string &value, int) {
Path path(value);
INFO_LOG(SYSTEM, "Attempting to load cheats from: '%s'", path.ToVisualString().c_str());
if (ImportCheats(path)) {
g_Config.bReloadCheats = true;
} else {
// Show an error message?
}
RecreateViews();
});
return UI::EVENT_DONE;
}

UI::EventReturn CwCheatScreen::OnImportCheat(UI::EventParams &params) {
if (!ImportCheats(GetGlobalCheatFilePath())) {
// Show an error message?
errorMessageView_->SetVisibility(UI::V_VISIBLE);
return UI::EVENT_DONE;
}

g_Config.bReloadCheats = true;
RecreateViews();
return UI::EVENT_DONE;
}

bool CwCheatScreen::ImportCheats(const Path & cheatFile) {
if (gameID_.length() != 9 || !engine_) {
WARN_LOG(COMMON, "CWCHEAT: Incorrect ID(%s) - can't import cheats.", gameID_.c_str());
return UI::EVENT_DONE;
return false;
}
std::vector<std::string> title;
std::vector<std::string> newList;

Path cheatFile = GetSysDirectory(DIRECTORY_CHEATS) / "cheat.db";
std::string gameID = StringFromFormat("_S %s-%s", gameID_.substr(0, 4).c_str(), gameID_.substr(4).c_str());

FILE *in = File::OpenCFile(cheatFile, "rt");

if (!in) {
WARN_LOG(COMMON, "Unable to open %s\n", cheatFile.c_str());
return UI::EVENT_SKIPPED;
return false;
}

std::vector<std::string> title;
std::vector<std::string> newList;

char linebuf[2048]{};
bool parseGameEntry = false;
bool parseCheatEntry = false;
Expand Down Expand Up @@ -281,10 +322,7 @@ UI::EventReturn CwCheatScreen::OnImportCheat(UI::EventParams &params) {
}
}
fclose(append);

g_Config.bReloadCheats = true;
RecreateViews();
return UI::EVENT_DONE;
return true;
}

UI::EventReturn CwCheatScreen::OnCheckBox(int index) {
Expand Down
8 changes: 6 additions & 2 deletions UI/CwCheatScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ class CwCheatScreen : public UIDialogScreenWithGameBackground {

UI::EventReturn OnAddCheat(UI::EventParams &params);
UI::EventReturn OnImportCheat(UI::EventParams &params);
UI::EventReturn OnImportBrowse(UI::EventParams &params);
UI::EventReturn OnEditCheatFile(UI::EventParams &params);
UI::EventReturn OnEnableAll(UI::EventParams &params);
UI::EventReturn OnDisableAll(UI::EventParams &params);

void update() override;
void onFinish(DialogResult result) override;
Expand All @@ -48,16 +49,19 @@ class CwCheatScreen : public UIDialogScreenWithGameBackground {

private:
UI::EventReturn OnCheckBox(int index);
bool ImportCheats(const Path &cheatFile);

enum { INDEX_ALL = -1 };
bool HasCheatWithName(const std::string &name);
bool RebuildCheatFile(int index);

UI::ScrollView *rightScroll_ = nullptr;
UI::TextView *errorMessageView_ = nullptr;

CWCheatEngine *engine_ = nullptr;
std::vector<CheatFileInfo> fileInfo_;
std::string gameID_;
int fileCheckCounter_ = 0;
uint64_t fileCheckHash_;
uint64_t fileCheckHash_ = 0;
bool enableAllFlag_ = false;
};
3 changes: 3 additions & 0 deletions UWP/PPSSPP_UWPMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,9 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string
case BrowseFileType::INI:
picker->FileTypeFilter->Append(".ini");
break;
case BrowseFileType::DB:
picker->FileTypeFilter->Append(".db");
break;
case BrowseFileType::ANY:
picker->FileTypeFilter->Append("*");
break;
Expand Down
3 changes: 3 additions & 0 deletions Windows/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,9 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string
case BrowseFileType::INI:
filter = MakeFilter(L"Ini files (*.ini)|*.ini|All files (*.*)|*.*||");
break;
case BrowseFileType::DB:
filter = MakeFilter(L"Cheat db files (*.db)|*.db|All files (*.*)|*.*||");
break;
case BrowseFileType::ANY:
filter = MakeFilter(L"All files (*.*)|*.*||");
break;
Expand Down
3 changes: 1 addition & 2 deletions assets/lang/ar_AE.ini
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@ Y = Y
[CwCheats]
Cheats = ‎الشفرات
Edit Cheat File = ‎عدل ملف الشفرة
Enable/Disable All = ‎تمكين/تعطيل كل الشفرات
Import Cheats = ‎إستورد من cheat.db
Options = ‎إعدادات
Import from %s = ‎إستورد من %s
Refresh Rate = ‎معدل التحديث

[DesktopUI]
Expand Down
3 changes: 1 addition & 2 deletions assets/lang/az_AZ.ini
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,8 @@ Y = Y
[CwCheats]
Cheats = Cheats
Edit Cheat File = Edit cheat file
Enable/Disable All = Enable/Disable all Cheats
Import Cheats = Import from cheat.db
Options = Options
Import from %s = Import from %s
Refresh Rate = Refresh rate

[DesktopUI]
Expand Down
3 changes: 1 addition & 2 deletions assets/lang/bg_BG.ini
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,8 @@ Y = Y
[CwCheats]
Cheats = Чийтове
Edit Cheat File = Edit cheat file
Enable/Disable All = Включи/Изключи всички чийтове
Import Cheats = Внеси от cheat.db
Options = Опции
Import from %s = Внеси от %s
Refresh Rate = Refresh rate

[DesktopUI]
Expand Down
3 changes: 1 addition & 2 deletions assets/lang/ca_ES.ini
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,8 @@ Y = Y
[CwCheats]
Cheats = Trucs
Edit Cheat File = Editar el fitxer de trucs
Enable/Disable All = Activar/desactivar tots els trucs
Import Cheats = Importar «cheat.db»
Options = Opcions
Import from %s = Importar %s
Refresh Rate = Freqüència de refrescament

[DesktopUI]
Expand Down
3 changes: 1 addition & 2 deletions assets/lang/cz_CZ.ini
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,8 @@ Y = Y
[CwCheats]
Cheats = Cheaty
Edit Cheat File = Upravit soubor s cheaty
Enable/Disable All = Povolit/Zakázat všechny cheaty
Import Cheats = Importovat z cheat.db
Options = Nastavení
Import from %s = Importovat z %s
Refresh Rate = Frekvence obnoveni

[DesktopUI]
Expand Down
4 changes: 1 addition & 3 deletions assets/lang/da_DK.ini
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ Y = Y
[CwCheats]
Cheats = Snyd
Edit Cheat File = Editer snydefil
Enable/Disable All = Aktiver/Deaktiver alle snyd
Import Cheats = Importer fra cheat.db
Options = Optioner
Import from %s = Importer fra %s
Refresh Rate = Opdateringsrate

[DesktopUI]
Expand Down
2 changes: 0 additions & 2 deletions assets/lang/de_DE.ini
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ Y = Y
[CwCheats]
Cheats = Cheats
Edit Cheat File = Cheatdatei ändern
Enable/Disable All = Alle de- oder aktivieren
Import Cheats = Cheats importieren (aus cheat.db)
Options = Optionen
Refresh Rate = Wiederholfrequenz

[DesktopUI]
Expand Down
3 changes: 1 addition & 2 deletions assets/lang/dr_ID.ini
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,8 @@ Y = Y
[CwCheats]
Cheats = Cheat
Edit Cheat File = Edit cheat file
Enable/Disable All = Pajalan/Bosian nasan Cheat
Import Cheats = Patamanni cheat.db
Options = Pangpillean
Import from %s = Patamanni %s
Refresh Rate = Refresh rate
[DesktopUI]
Expand Down
3 changes: 1 addition & 2 deletions assets/lang/en_US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,8 @@ Y = Y
[CwCheats]
Cheats = Cheats
Edit Cheat File = Edit cheat file
Enable/Disable All = Enable/Disable all Cheats
Import Cheats = Import from cheat.db
Options = Options
Import from %s = Import from %s
Refresh Rate = Refresh rate

[DesktopUI]
Expand Down
2 changes: 0 additions & 2 deletions assets/lang/es_ES.ini
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ Y = Y
[CwCheats]
Cheats = Trucos
Edit Cheat File = Editar archivo de trucos
Enable/Disable All = Activar/desactivar todos los trucos
Import Cheats = Importar archivo cheat.db
Options = Opciones
Refresh Rate = Frecuencia de actualización

[DesktopUI]
Expand Down
2 changes: 0 additions & 2 deletions assets/lang/es_LA.ini
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ Y = Y
[CwCheats]
Cheats = trucos
Edit Cheat File = Editar archivo de trucos
Enable/Disable All = Activar/desactivar todos los trucos
Import Cheats = Importar archivo cheat.db
Options = Opciones
Refresh Rate = Frecuencia de actualización

[DesktopUI]
Expand Down
Loading

0 comments on commit c791d64

Please sign in to comment.