2013-08-24 03:28:21 +00:00
|
|
|
// Copyright (c) 2012- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
2021-03-03 04:42:55 +00:00
|
|
|
#include "ppsspp_config.h"
|
2020-08-28 03:40:55 +00:00
|
|
|
#include "ext/xxhash.h"
|
2020-10-04 18:48:47 +00:00
|
|
|
#include "Common/UI/UI.h"
|
2013-08-24 03:28:21 +00:00
|
|
|
|
2020-10-01 11:05:04 +00:00
|
|
|
#include "Common/Data/Text/I18n.h"
|
|
|
|
#include "Common/Data/Encoding/Utf8.h"
|
2020-10-04 18:48:47 +00:00
|
|
|
#include "Common/File/FileUtil.h"
|
2020-09-29 10:19:22 +00:00
|
|
|
#include "Common/StringUtils.h"
|
2020-10-04 08:30:18 +00:00
|
|
|
#include "Common/System/System.h"
|
2023-03-23 00:16:12 +00:00
|
|
|
#include "Common/System/Request.h"
|
2013-08-24 03:28:21 +00:00
|
|
|
#include "Core/Core.h"
|
2013-09-15 03:28:41 +00:00
|
|
|
#include "Core/Config.h"
|
2016-03-06 21:16:50 +00:00
|
|
|
#include "Core/CwCheat.h"
|
2016-05-01 08:35:37 +00:00
|
|
|
#include "Core/MIPS/JitCommon/JitCommon.h"
|
2013-08-24 03:28:21 +00:00
|
|
|
|
|
|
|
#include "UI/GameInfoCache.h"
|
|
|
|
#include "UI/CwCheatScreen.h"
|
2013-08-24 13:54:15 +00:00
|
|
|
|
2020-04-11 19:57:35 +00:00
|
|
|
static const int FILE_CHECK_FRAME_INTERVAL = 53;
|
|
|
|
|
2023-02-16 18:01:13 +00:00
|
|
|
static Path GetGlobalCheatFile() {
|
|
|
|
return GetSysDirectory(DIRECTORY_CHEATS) / "cheat.db";
|
|
|
|
}
|
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
CwCheatScreen::CwCheatScreen(const Path &gamePath)
|
2022-11-22 21:53:54 +00:00
|
|
|
: UIDialogScreenWithGameBackground(gamePath) {
|
2016-06-19 22:18:35 +00:00
|
|
|
}
|
2013-10-23 14:10:11 +00:00
|
|
|
|
2020-04-11 20:52:25 +00:00
|
|
|
CwCheatScreen::~CwCheatScreen() {
|
|
|
|
delete engine_;
|
|
|
|
}
|
|
|
|
|
2020-04-11 19:43:55 +00:00
|
|
|
void CwCheatScreen::LoadCheatInfo() {
|
|
|
|
std::shared_ptr<GameInfo> info = g_gameInfoCache->GetInfo(nullptr, gamePath_, 0);
|
2020-04-11 20:52:25 +00:00
|
|
|
std::string gameID;
|
2016-06-19 22:18:35 +00:00
|
|
|
if (info && info->paramSFOLoaded) {
|
2020-04-11 20:52:25 +00:00
|
|
|
gameID = info->paramSFO.GetValueString("DISC_ID");
|
2016-06-19 22:18:35 +00:00
|
|
|
}
|
2017-05-29 01:57:39 +00:00
|
|
|
if ((info->id.empty() || !info->disc_total)
|
2021-12-16 19:37:05 +00:00
|
|
|
&& gamePath_.FilePathContainsNoCase("PSP/GAME/")) {
|
2021-05-05 23:31:38 +00:00
|
|
|
gameID = g_paramSFO.GenerateFakeID(gamePath_.ToString());
|
2020-04-11 20:52:25 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 18:01:13 +00:00
|
|
|
if (!engine_ || gameID != gameID_) {
|
2020-04-11 20:52:25 +00:00
|
|
|
gameID_ = gameID;
|
|
|
|
delete engine_;
|
|
|
|
engine_ = new CWCheatEngine(gameID_);
|
|
|
|
engine_->CreateCheatFile();
|
2016-06-20 22:15:29 +00:00
|
|
|
}
|
2017-05-29 01:57:39 +00:00
|
|
|
|
2020-04-11 19:57:35 +00:00
|
|
|
// We won't parse this, just using it to detect changes to the file.
|
|
|
|
std::string str;
|
2021-05-09 13:02:46 +00:00
|
|
|
if (File::ReadFileToString(true, engine_->CheatFilename(), str)) {
|
2020-08-28 03:40:55 +00:00
|
|
|
fileCheckHash_ = XXH3_64bits(str.c_str(), str.size());
|
2020-04-11 19:57:35 +00:00
|
|
|
}
|
|
|
|
fileCheckCounter_ = 0;
|
|
|
|
|
2020-04-11 20:52:25 +00:00
|
|
|
fileInfo_ = engine_->FileInfo();
|
2020-04-11 19:57:35 +00:00
|
|
|
|
|
|
|
// Let's also trigger a reload, in case it changed.
|
|
|
|
g_Config.bReloadCheats = true;
|
2013-08-24 03:28:21 +00:00
|
|
|
}
|
2013-08-30 18:13:59 +00:00
|
|
|
|
2013-08-24 03:28:21 +00:00
|
|
|
void CwCheatScreen::CreateViews() {
|
|
|
|
using namespace UI;
|
2020-01-26 18:43:18 +00:00
|
|
|
auto cw = GetI18NCategory("CwCheats");
|
|
|
|
auto di = GetI18NCategory("Dialog");
|
2023-02-16 18:01:13 +00:00
|
|
|
auto mm = GetI18NCategory("MainMenu");
|
2020-04-11 18:22:11 +00:00
|
|
|
|
|
|
|
root_ = new AnchorLayout(new LayoutParams(FILL_PARENT, FILL_PARENT));
|
|
|
|
|
2020-04-11 19:43:55 +00:00
|
|
|
LoadCheatInfo();
|
2013-09-03 15:50:34 +00:00
|
|
|
Margins actionMenuMargins(50, -15, 15, 0);
|
2013-08-24 03:28:21 +00:00
|
|
|
|
2013-08-24 03:07:41 +00:00
|
|
|
LinearLayout *leftColumn = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(400, FILL_PARENT));
|
2015-07-01 20:40:11 +00:00
|
|
|
//leftColumn->Add(new Choice(cw->T("Add Cheat")))->OnClick.Handle(this, &CwCheatScreen::OnAddCheat);
|
2023-02-16 18:01:13 +00:00
|
|
|
leftColumn->Add(new ItemHeader(cw->T("Import Cheats")));
|
|
|
|
|
|
|
|
Path cheatPath = GetGlobalCheatFile();
|
|
|
|
|
|
|
|
leftColumn->Add(new Choice(cheatPath.ToVisualString()))->OnClick.Handle(this, &CwCheatScreen::OnImportCheat);
|
|
|
|
leftColumn->Add(new Choice(mm->T("Browse"), ImageID("I_FOLDER_OPEN")))->OnClick.Handle(this, &CwCheatScreen::OnImportBrowse);
|
|
|
|
|
|
|
|
leftColumn->Add(new ItemHeader(cw->T("Options")));
|
2015-05-26 11:59:00 +00:00
|
|
|
#if !defined(MOBILE_DEVICE)
|
2015-07-01 20:40:11 +00:00
|
|
|
leftColumn->Add(new Choice(cw->T("Edit Cheat File")))->OnClick.Handle(this, &CwCheatScreen::OnEditCheatFile);
|
2014-06-08 22:13:26 +00:00
|
|
|
#endif
|
2015-07-01 20:40:11 +00:00
|
|
|
leftColumn->Add(new Choice(cw->T("Enable/Disable All")))->OnClick.Handle(this, &CwCheatScreen::OnEnableAll);
|
|
|
|
leftColumn->Add(new PopupSliderChoice(&g_Config.iCwCheatRefreshRate, 1, 1000, cw->T("Refresh Rate"), 1, screenManager()));
|
2013-08-24 03:28:21 +00:00
|
|
|
|
2020-04-11 18:22:11 +00:00
|
|
|
rightScroll_ = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT, FILL_PARENT, 0.5f));
|
2020-03-26 09:59:08 +00:00
|
|
|
rightScroll_->SetTag("CwCheats");
|
2021-09-28 06:25:41 +00:00
|
|
|
rightScroll_->RememberPosition(&g_Config.fCwCheatScrollPosition);
|
2021-02-22 02:41:08 +00:00
|
|
|
LinearLayout *rightColumn = new LinearLayoutList(ORIENT_VERTICAL, new LinearLayoutParams(200, FILL_PARENT, actionMenuMargins));
|
2020-03-26 09:59:08 +00:00
|
|
|
rightScroll_->Add(rightColumn);
|
2013-10-23 14:10:11 +00:00
|
|
|
|
2015-07-01 20:40:11 +00:00
|
|
|
rightColumn->Add(new ItemHeader(cw->T("Cheats")));
|
2020-04-11 19:43:55 +00:00
|
|
|
for (size_t i = 0; i < fileInfo_.size(); ++i) {
|
2020-04-11 19:59:06 +00:00
|
|
|
rightColumn->Add(new CheckBox(&fileInfo_[i].enabled, fileInfo_[i].name))->OnClick.Add([=](UI::EventParams &) {
|
2020-04-11 19:57:35 +00:00
|
|
|
return OnCheckBox((int)i);
|
2020-04-11 19:43:55 +00:00
|
|
|
});
|
2013-08-30 18:13:59 +00:00
|
|
|
}
|
2020-04-11 18:22:11 +00:00
|
|
|
|
|
|
|
LinearLayout *layout = new LinearLayout(ORIENT_HORIZONTAL, new LayoutParams(FILL_PARENT, FILL_PARENT));
|
|
|
|
layout->Add(leftColumn);
|
|
|
|
layout->Add(rightScroll_);
|
|
|
|
root_->Add(layout);
|
|
|
|
|
|
|
|
AddStandardBack(root_);
|
2013-08-24 03:28:21 +00:00
|
|
|
}
|
2013-08-30 18:13:59 +00:00
|
|
|
|
2020-04-11 19:57:35 +00:00
|
|
|
void CwCheatScreen::update() {
|
2020-04-11 20:52:25 +00:00
|
|
|
if (fileCheckCounter_++ >= FILE_CHECK_FRAME_INTERVAL && engine_) {
|
2020-04-11 19:57:35 +00:00
|
|
|
// Check if the file has changed. If it has, we'll reload.
|
|
|
|
std::string str;
|
2021-05-09 13:02:46 +00:00
|
|
|
if (File::ReadFileToString(true, engine_->CheatFilename(), str)) {
|
2020-08-28 03:40:55 +00:00
|
|
|
uint64_t newHash = XXH3_64bits(str.c_str(), str.size());
|
2020-04-11 19:57:35 +00:00
|
|
|
if (newHash != fileCheckHash_) {
|
|
|
|
// This will update the hash.
|
|
|
|
RecreateViews();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fileCheckCounter_ = 0;
|
|
|
|
}
|
|
|
|
|
2022-11-22 21:53:54 +00:00
|
|
|
UIDialogScreenWithGameBackground::update();
|
2020-04-11 19:57:35 +00:00
|
|
|
}
|
|
|
|
|
2013-10-25 11:19:08 +00:00
|
|
|
void CwCheatScreen::onFinish(DialogResult result) {
|
2013-11-05 12:38:51 +00:00
|
|
|
if (result != DR_BACK) // This only works for BACK here.
|
|
|
|
return;
|
2020-04-11 19:43:55 +00:00
|
|
|
|
2021-11-28 00:11:51 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> guard(MIPSComp::jitLock);
|
2013-10-23 14:10:11 +00:00
|
|
|
if (MIPSComp::jit) {
|
2013-10-23 15:23:05 +00:00
|
|
|
MIPSComp::jit->ClearCache();
|
2013-10-23 14:10:11 +00:00
|
|
|
}
|
2013-08-23 02:58:37 +00:00
|
|
|
}
|
2013-08-30 18:13:59 +00:00
|
|
|
|
2013-10-23 14:10:11 +00:00
|
|
|
UI::EventReturn CwCheatScreen::OnEnableAll(UI::EventParams ¶ms) {
|
2020-04-11 19:43:55 +00:00
|
|
|
enableAllFlag_ = !enableAllFlag_;
|
|
|
|
|
|
|
|
// Flip all the switches.
|
|
|
|
for (auto &info : fileInfo_) {
|
|
|
|
info.enabled = enableAllFlag_;
|
2013-09-04 08:51:14 +00:00
|
|
|
}
|
2020-04-11 19:43:55 +00:00
|
|
|
|
|
|
|
if (!RebuildCheatFile(INDEX_ALL)) {
|
|
|
|
// Probably the file was modified outside PPSSPP, refresh.
|
|
|
|
// TODO: Report error.
|
|
|
|
RecreateViews();
|
|
|
|
return UI::EVENT_SKIPPED;
|
2013-08-24 03:07:41 +00:00
|
|
|
}
|
2013-10-23 14:10:11 +00:00
|
|
|
|
2013-08-24 03:07:41 +00:00
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
2013-09-04 08:51:14 +00:00
|
|
|
|
2013-10-23 14:10:11 +00:00
|
|
|
UI::EventReturn CwCheatScreen::OnAddCheat(UI::EventParams ¶ms) {
|
2017-03-20 00:43:03 +00:00
|
|
|
TriggerFinish(DR_OK);
|
2013-08-24 03:07:41 +00:00
|
|
|
g_Config.bReloadCheats = true;
|
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
2013-09-04 08:51:14 +00:00
|
|
|
|
2014-06-08 22:13:26 +00:00
|
|
|
UI::EventReturn CwCheatScreen::OnEditCheatFile(UI::EventParams ¶ms) {
|
2015-01-28 08:06:00 +00:00
|
|
|
g_Config.bReloadCheats = true;
|
2021-11-28 00:11:51 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> guard(MIPSComp::jitLock);
|
2015-01-28 08:06:00 +00:00
|
|
|
if (MIPSComp::jit) {
|
|
|
|
MIPSComp::jit->ClearCache();
|
|
|
|
}
|
2020-04-11 20:52:25 +00:00
|
|
|
if (engine_) {
|
2017-02-28 00:47:13 +00:00
|
|
|
#if PPSSPP_PLATFORM(UWP)
|
2023-03-21 09:42:23 +00:00
|
|
|
System_LaunchUrl(LaunchUrlType::BROWSER_URL, engine_->CheatFilename().c_str());
|
2017-02-28 00:47:13 +00:00
|
|
|
#else
|
2021-05-09 13:02:46 +00:00
|
|
|
File::OpenFileInEditor(engine_->CheatFilename());
|
2017-02-28 00:47:13 +00:00
|
|
|
#endif
|
2020-04-11 20:52:25 +00:00
|
|
|
}
|
2014-06-08 22:13:26 +00:00
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
2021-05-09 23:48:51 +00:00
|
|
|
static char *GetLineNoNewline(char *temp, int sz, FILE *fp) {
|
|
|
|
char *line = fgets(temp, sz, fp);
|
|
|
|
if (!line)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// If the last character is \n, just make it the terminator.
|
|
|
|
char *end = line + strlen(line) - 1;
|
|
|
|
if (*end == '\n')
|
|
|
|
*end = '\0';
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2023-02-16 18:01:13 +00:00
|
|
|
UI::EventReturn CwCheatScreen::OnImportBrowse(UI::EventParams ¶ms) {
|
2023-03-23 00:16:12 +00:00
|
|
|
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();
|
|
|
|
// Chose a cheat file.
|
|
|
|
});
|
2023-02-16 18:01:13 +00:00
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
2013-10-23 14:10:11 +00:00
|
|
|
UI::EventReturn CwCheatScreen::OnImportCheat(UI::EventParams ¶ms) {
|
2023-02-16 18:01:13 +00:00
|
|
|
if (!ImportCheats(GetGlobalCheatFile())) {
|
|
|
|
// Show an error message?
|
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_Config.bReloadCheats = true;
|
|
|
|
RecreateViews();
|
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CwCheatScreen::ImportCheats(const Path & cheatFile) {
|
2020-04-11 20:52:25 +00:00
|
|
|
if (gameID_.length() != 9 || !engine_) {
|
|
|
|
WARN_LOG(COMMON, "CWCHEAT: Incorrect ID(%s) - can't import cheats.", gameID_.c_str());
|
2023-02-16 18:01:13 +00:00
|
|
|
return false;
|
2016-06-20 09:32:31 +00:00
|
|
|
}
|
2013-10-15 07:04:12 +00:00
|
|
|
|
2020-04-11 20:52:25 +00:00
|
|
|
std::string gameID = StringFromFormat("_S %s-%s", gameID_.substr(0, 4).c_str(), gameID_.substr(4).c_str());
|
2014-06-05 02:41:42 +00:00
|
|
|
|
2021-05-09 23:48:51 +00:00
|
|
|
FILE *in = File::OpenCFile(cheatFile, "rt");
|
|
|
|
if (!in) {
|
2014-07-08 04:56:20 +00:00
|
|
|
WARN_LOG(COMMON, "Unable to open %s\n", cheatFile.c_str());
|
2023-02-16 18:01:13 +00:00
|
|
|
return false;
|
2014-07-08 04:56:20 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 18:01:13 +00:00
|
|
|
std::vector<std::string> title;
|
|
|
|
std::vector<std::string> newList;
|
|
|
|
|
2021-05-09 23:48:51 +00:00
|
|
|
char linebuf[2048]{};
|
2022-08-07 02:48:00 +00:00
|
|
|
bool parseGameEntry = false;
|
|
|
|
bool parseCheatEntry = false;
|
|
|
|
|
2021-05-09 23:48:51 +00:00
|
|
|
while (in && !feof(in)) {
|
|
|
|
char *line = GetLineNoNewline(linebuf, sizeof(linebuf), in);
|
2022-08-07 02:48:00 +00:00
|
|
|
|
|
|
|
if (!line) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line[0] == '_' && line[1] == 'S') {
|
|
|
|
parseGameEntry = gameID == line;
|
|
|
|
parseCheatEntry = false;
|
|
|
|
} else if (parseGameEntry && line[0] == '_' && line[1] == 'C') {
|
|
|
|
// Test if cheat already exists.
|
|
|
|
parseCheatEntry = !HasCheatWithName(std::string(line).substr(4));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!parseGameEntry) {
|
|
|
|
if (newList.size() > 0) {
|
|
|
|
// Only parse the first matching game entry.
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// Haven't yet found a matching game entry, continue parsing.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line[0] == '_' && (line[1] == 'S' || line[1] == 'G') && title.size() < 2) {
|
2013-09-01 02:15:50 +00:00
|
|
|
title.push_back(line);
|
2022-08-08 14:42:29 +00:00
|
|
|
} else if (parseCheatEntry && ((line[0] == '_' && (line[1] == 'C' || line[1] == 'L')) || line[0] == '/' || line[0] == '#')) {
|
2022-08-07 02:48:00 +00:00
|
|
|
newList.push_back(line);
|
2013-09-01 02:15:50 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-09 23:48:51 +00:00
|
|
|
fclose(in);
|
|
|
|
|
2013-09-01 02:15:50 +00:00
|
|
|
std::string title2;
|
2021-05-09 23:48:51 +00:00
|
|
|
// Hmm, this probably gets confused about BOMs?
|
|
|
|
FILE *inTitle2 = File::OpenCFile(engine_->CheatFilename(), "rt");
|
|
|
|
if (inTitle2) {
|
|
|
|
char temp[2048];
|
|
|
|
char *line = GetLineNoNewline(temp, sizeof(temp), inTitle2);
|
|
|
|
if (line)
|
|
|
|
title2 = line;
|
|
|
|
fclose(inTitle2);
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *append = File::OpenCFile(engine_->CheatFilename(), "at");
|
|
|
|
if (!append)
|
|
|
|
return UI::EVENT_SKIPPED;
|
2014-06-05 14:22:43 +00:00
|
|
|
|
2022-08-07 02:48:00 +00:00
|
|
|
if (title2.size() == 0 || title2[0] != '_' || title2[1] != 'S') {
|
|
|
|
for (int i = (int)title.size(); i > 0; i--) {
|
|
|
|
newList.insert(newList.begin(), title[i - 1]);
|
|
|
|
}
|
2013-09-01 02:15:50 +00:00
|
|
|
}
|
2014-07-08 04:56:20 +00:00
|
|
|
|
2022-08-07 02:48:00 +00:00
|
|
|
NOTICE_LOG(COMMON, "Imported %u lines from %s.\n", (int)newList.size(), cheatFile.c_str());
|
2013-10-23 14:10:11 +00:00
|
|
|
if (newList.size() != 0) {
|
2021-05-09 23:48:51 +00:00
|
|
|
fputc('\n', append);
|
2013-08-24 03:07:41 +00:00
|
|
|
}
|
2014-07-08 04:56:20 +00:00
|
|
|
|
2013-08-30 18:13:59 +00:00
|
|
|
for (int i = 0; i < (int)newList.size(); i++) {
|
2021-05-09 23:48:51 +00:00
|
|
|
fprintf(append, "%s", newList[i].c_str());
|
2013-09-04 08:51:14 +00:00
|
|
|
if (i < (int)newList.size() - 1) {
|
2021-05-09 23:48:51 +00:00
|
|
|
fputc('\n', append);
|
2013-08-24 03:07:41 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-09 23:48:51 +00:00
|
|
|
fclose(append);
|
2023-02-16 18:01:13 +00:00
|
|
|
return true;
|
2013-08-24 03:07:41 +00:00
|
|
|
}
|
2013-08-23 02:58:37 +00:00
|
|
|
|
2020-04-11 19:43:55 +00:00
|
|
|
UI::EventReturn CwCheatScreen::OnCheckBox(int index) {
|
|
|
|
if (!RebuildCheatFile(index)) {
|
|
|
|
// TODO: Report error. Let's reload the file, presumably it changed.
|
|
|
|
RecreateViews();
|
|
|
|
return UI::EVENT_SKIPPED;
|
2020-04-11 18:22:11 +00:00
|
|
|
}
|
2020-04-11 19:43:55 +00:00
|
|
|
|
2013-08-24 03:28:21 +00:00
|
|
|
return UI::EVENT_DONE;
|
2013-08-23 00:46:30 +00:00
|
|
|
}
|
2013-08-30 12:47:28 +00:00
|
|
|
|
2022-08-07 02:48:00 +00:00
|
|
|
bool CwCheatScreen::HasCheatWithName(const std::string &name) {
|
|
|
|
for (const auto &existing : fileInfo_) {
|
|
|
|
if (name == existing.name) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-11 19:43:55 +00:00
|
|
|
bool CwCheatScreen::RebuildCheatFile(int index) {
|
2021-05-09 23:48:51 +00:00
|
|
|
if (!engine_)
|
|
|
|
return false;
|
|
|
|
FILE *in = File::OpenCFile(engine_->CheatFilename(), "rt");
|
|
|
|
if (!in)
|
2020-04-11 19:43:55 +00:00
|
|
|
return false;
|
2013-10-23 14:10:11 +00:00
|
|
|
|
2020-04-11 19:43:55 +00:00
|
|
|
// In case lines were edited while we weren't looking, reload them.
|
|
|
|
std::vector<std::string> lines;
|
2021-05-09 23:48:51 +00:00
|
|
|
for (; !feof(in); ) {
|
|
|
|
char temp[2048];
|
|
|
|
char *line = GetLineNoNewline(temp, sizeof(temp), in);
|
|
|
|
if (!line)
|
|
|
|
break;
|
|
|
|
|
2020-04-11 19:43:55 +00:00
|
|
|
lines.push_back(line);
|
2013-08-23 00:46:30 +00:00
|
|
|
}
|
2021-05-09 23:48:51 +00:00
|
|
|
fclose(in);
|
2013-08-30 12:47:28 +00:00
|
|
|
|
2020-04-11 19:43:55 +00:00
|
|
|
auto updateLine = [&](const CheatFileInfo &info) {
|
|
|
|
// Line numbers start with one, not zero.
|
|
|
|
size_t lineIndex = info.lineNum - 1;
|
|
|
|
if (lines.size() > lineIndex) {
|
|
|
|
auto &line = lines[lineIndex];
|
|
|
|
// This is the one to change. Let's see if it matches - maybe the file changed.
|
|
|
|
bool isCheatDef = line.find("_C") != line.npos;
|
|
|
|
bool hasCheatName = !info.name.empty() && line.find(info.name) != line.npos;
|
|
|
|
if (!isCheatDef || !hasCheatName) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
line = (info.enabled ? "_C1 " : "_C0 ") + info.name;
|
2020-04-11 20:52:25 +00:00
|
|
|
return true;
|
2020-04-11 19:43:55 +00:00
|
|
|
}
|
2020-04-11 20:52:25 +00:00
|
|
|
return false;
|
2020-04-11 19:43:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (index == INDEX_ALL) {
|
|
|
|
for (const auto &info : fileInfo_) {
|
|
|
|
// Bail out if any don't match with no changes.
|
|
|
|
if (!updateLine(info)) {
|
|
|
|
return false;
|
2016-10-24 02:37:49 +00:00
|
|
|
}
|
2013-08-23 02:58:37 +00:00
|
|
|
}
|
2020-04-11 19:43:55 +00:00
|
|
|
} else {
|
|
|
|
if (!updateLine(fileInfo_[index])) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-23 02:58:37 +00:00
|
|
|
}
|
2013-08-23 00:46:30 +00:00
|
|
|
|
2021-05-09 23:48:51 +00:00
|
|
|
FILE *out = File::OpenCFile(engine_->CheatFilename(), "wt");
|
|
|
|
if (!out) {
|
2020-04-11 19:43:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-07 04:40:29 +00:00
|
|
|
for (size_t i = 0; i < lines.size(); ++i) {
|
2021-05-09 23:48:51 +00:00
|
|
|
fprintf(out, "%s", lines[i].c_str());
|
|
|
|
if (i != lines.size() - 1)
|
|
|
|
fputc('\n', out);
|
2013-08-23 02:58:37 +00:00
|
|
|
}
|
2021-05-09 23:48:51 +00:00
|
|
|
fclose(out);
|
2013-08-23 00:46:30 +00:00
|
|
|
|
2020-04-11 19:43:55 +00:00
|
|
|
// Cheats will need to be reparsed now.
|
|
|
|
g_Config.bReloadCheats = true;
|
|
|
|
return true;
|
|
|
|
}
|