CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/UI/CwCheatScreen.cpp
Views: 1401
1
// Copyright (c) 2012- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include "ppsspp_config.h"
19
#include "ext/xxhash.h"
20
#include "Common/UI/UI.h"
21
22
#include "Common/Data/Text/I18n.h"
23
#include "Common/Data/Encoding/Utf8.h"
24
#include "Common/File/FileUtil.h"
25
#include "Common/StringUtils.h"
26
#include "Common/System/System.h"
27
#include "Common/System/Request.h"
28
#include "Core/Core.h"
29
#include "Core/Config.h"
30
#include "Core/CwCheat.h"
31
#include "Core/MIPS/JitCommon/JitCommon.h"
32
33
#include "UI/GameInfoCache.h"
34
#include "UI/CwCheatScreen.h"
35
36
static const int FILE_CHECK_FRAME_INTERVAL = 53;
37
38
static Path GetGlobalCheatFilePath() {
39
return GetSysDirectory(DIRECTORY_CHEATS) / "cheat.db";
40
}
41
42
CwCheatScreen::CwCheatScreen(const Path &gamePath)
43
: UIDialogScreenWithGameBackground(gamePath) {
44
}
45
46
CwCheatScreen::~CwCheatScreen() {
47
delete engine_;
48
}
49
50
bool CwCheatScreen::TryLoadCheatInfo() {
51
std::shared_ptr<GameInfo> info = g_gameInfoCache->GetInfo(nullptr, gamePath_, GameInfoFlags::PARAM_SFO);
52
std::string gameID;
53
if (!info->Ready(GameInfoFlags::PARAM_SFO)) {
54
return false;
55
}
56
gameID = info->GetParamSFO().GetValueString("DISC_ID");
57
if ((info->id.empty() || !info->disc_total)
58
&& gamePath_.FilePathContainsNoCase("PSP/GAME/")) {
59
gameID = g_paramSFO.GenerateFakeID(gamePath_);
60
}
61
62
if (!engine_ || gameID != gameID_) {
63
gameID_ = gameID;
64
delete engine_;
65
engine_ = new CWCheatEngine(gameID_);
66
engine_->CreateCheatFile();
67
}
68
69
// We won't parse this, just using it to detect changes to the file.
70
std::string str;
71
if (File::ReadTextFileToString(engine_->CheatFilename(), &str)) {
72
fileCheckHash_ = XXH3_64bits(str.c_str(), str.size());
73
}
74
fileCheckCounter_ = 0;
75
76
fileInfo_ = engine_->FileInfo();
77
78
// Let's also trigger a reload, in case it changed.
79
g_Config.bReloadCheats = true;
80
return true;
81
}
82
83
void CwCheatScreen::CreateViews() {
84
using namespace UI;
85
auto cw = GetI18NCategory(I18NCat::CWCHEATS);
86
auto di = GetI18NCategory(I18NCat::DIALOG);
87
auto mm = GetI18NCategory(I18NCat::MAINMENU);
88
89
root_ = new AnchorLayout(new LayoutParams(FILL_PARENT, FILL_PARENT));
90
91
TryLoadCheatInfo(); // in case the info is already in cache.
92
Margins actionMenuMargins(50, -15, 15, 0);
93
94
LinearLayout *leftColumn = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(400, FILL_PARENT));
95
//leftColumn->Add(new Choice(cw->T("Add Cheat")))->OnClick.Handle(this, &CwCheatScreen::OnAddCheat);
96
leftColumn->Add(new ItemHeader(cw->T("Import Cheats")));
97
98
Path cheatPath = GetGlobalCheatFilePath();
99
100
std::string root = GetSysDirectory(DIRECTORY_MEMSTICK_ROOT).ToString();
101
102
std::string title = StringFromFormat(cw->T_cstr("Import from %s"), "PSP/Cheats/cheat.db");
103
104
leftColumn->Add(new Choice(title.c_str()))->OnClick.Handle(this, &CwCheatScreen::OnImportCheat);
105
leftColumn->Add(new Choice(mm->T("Browse"), ImageID("I_FOLDER_OPEN")))->OnClick.Handle(this, &CwCheatScreen::OnImportBrowse);
106
errorMessageView_ = leftColumn->Add(new TextView(di->T("LoadingFailed")));
107
errorMessageView_->SetVisibility(V_GONE);
108
109
leftColumn->Add(new ItemHeader(di->T("Options")));
110
#if !defined(MOBILE_DEVICE)
111
leftColumn->Add(new Choice(cw->T("Edit Cheat File")))->OnClick.Handle(this, &CwCheatScreen::OnEditCheatFile);
112
#endif
113
leftColumn->Add(new Choice(di->T("Disable All")))->OnClick.Handle(this, &CwCheatScreen::OnDisableAll);
114
leftColumn->Add(new PopupSliderChoice(&g_Config.iCwCheatRefreshIntervalMs, 1, 1000, 77, cw->T("Refresh interval"), 1, screenManager()))->SetFormat(di->T("%d ms"));
115
116
117
rightScroll_ = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT, FILL_PARENT, 0.5f));
118
rightScroll_->SetTag("CwCheats");
119
rightScroll_->RememberPosition(&g_Config.fCwCheatScrollPosition);
120
LinearLayout *rightColumn = new LinearLayoutList(ORIENT_VERTICAL, new LinearLayoutParams(200, FILL_PARENT, actionMenuMargins));
121
rightScroll_->Add(rightColumn);
122
123
rightColumn->Add(new ItemHeader(cw->T("Cheats")));
124
for (size_t i = 0; i < fileInfo_.size(); ++i) {
125
rightColumn->Add(new CheckBox(&fileInfo_[i].enabled, fileInfo_[i].name))->OnClick.Add([=](UI::EventParams &) {
126
return OnCheckBox((int)i);
127
});
128
}
129
130
LinearLayout *layout = new LinearLayout(ORIENT_HORIZONTAL, new LayoutParams(FILL_PARENT, FILL_PARENT));
131
layout->Add(leftColumn);
132
layout->Add(rightScroll_);
133
root_->Add(layout);
134
135
AddStandardBack(root_);
136
}
137
138
void CwCheatScreen::update() {
139
if (gameID_.empty()) {
140
if (TryLoadCheatInfo()) {
141
RecreateViews();
142
}
143
}
144
145
if (fileCheckCounter_++ >= FILE_CHECK_FRAME_INTERVAL && engine_) {
146
// Check if the file has changed. If it has, we'll reload.
147
std::string str;
148
if (File::ReadTextFileToString(engine_->CheatFilename(), &str)) {
149
uint64_t newHash = XXH3_64bits(str.c_str(), str.size());
150
if (newHash != fileCheckHash_) {
151
// This will update the hash.
152
RecreateViews();
153
}
154
}
155
fileCheckCounter_ = 0;
156
}
157
158
UIDialogScreenWithGameBackground::update();
159
}
160
161
void CwCheatScreen::onFinish(DialogResult result) {
162
if (result != DR_BACK) // This only works for BACK here.
163
return;
164
165
std::lock_guard<std::recursive_mutex> guard(MIPSComp::jitLock);
166
if (MIPSComp::jit) {
167
MIPSComp::jit->ClearCache();
168
}
169
}
170
171
UI::EventReturn CwCheatScreen::OnDisableAll(UI::EventParams &params) {
172
// Disable all the switches.
173
for (auto &info : fileInfo_) {
174
info.enabled = false;
175
}
176
177
if (!RebuildCheatFile(INDEX_ALL)) {
178
// Probably the file was modified outside PPSSPP, refresh.
179
// TODO: Report error.
180
RecreateViews();
181
return UI::EVENT_SKIPPED;
182
}
183
184
return UI::EVENT_DONE;
185
}
186
187
UI::EventReturn CwCheatScreen::OnAddCheat(UI::EventParams &params) {
188
TriggerFinish(DR_OK);
189
g_Config.bReloadCheats = true;
190
return UI::EVENT_DONE;
191
}
192
193
UI::EventReturn CwCheatScreen::OnEditCheatFile(UI::EventParams &params) {
194
g_Config.bReloadCheats = true;
195
std::lock_guard<std::recursive_mutex> guard(MIPSComp::jitLock);
196
if (MIPSComp::jit) {
197
MIPSComp::jit->ClearCache();
198
}
199
if (engine_) {
200
File::OpenFileInEditor(engine_->CheatFilename());
201
}
202
return UI::EVENT_DONE;
203
}
204
205
static char *GetLineNoNewline(char *temp, int sz, FILE *fp) {
206
char *line = fgets(temp, sz, fp);
207
if (!line)
208
return nullptr;
209
210
// If the last character is \n, just make it the terminator.
211
char *end = line + strlen(line) - 1;
212
if (*end == '\n')
213
*end = '\0';
214
return line;
215
}
216
217
UI::EventReturn CwCheatScreen::OnImportBrowse(UI::EventParams &params) {
218
System_BrowseForFile(GetRequesterToken(), "Open cheat DB file", BrowseFileType::DB, [&](const std::string &value, int) {
219
Path path(value);
220
INFO_LOG(Log::System, "Attempting to load cheats from: '%s'", path.ToVisualString().c_str());
221
if (ImportCheats(path)) {
222
g_Config.bReloadCheats = true;
223
} else {
224
// Show an error message?
225
}
226
RecreateViews();
227
});
228
return UI::EVENT_DONE;
229
}
230
231
UI::EventReturn CwCheatScreen::OnImportCheat(UI::EventParams &params) {
232
if (!ImportCheats(GetGlobalCheatFilePath())) {
233
// Show an error message?
234
errorMessageView_->SetVisibility(UI::V_VISIBLE);
235
return UI::EVENT_DONE;
236
}
237
238
g_Config.bReloadCheats = true;
239
RecreateViews();
240
return UI::EVENT_DONE;
241
}
242
243
bool CwCheatScreen::ImportCheats(const Path & cheatFile) {
244
if (gameID_.length() != 9 || !engine_) {
245
WARN_LOG(Log::Common, "CWCHEAT: Incorrect ID(%s) - can't import cheats.", gameID_.c_str());
246
return false;
247
}
248
249
std::string gameID = StringFromFormat("_S %s-%s", gameID_.substr(0, 4).c_str(), gameID_.substr(4).c_str());
250
251
FILE *in = File::OpenCFile(cheatFile, "rt");
252
if (!in) {
253
WARN_LOG(Log::Common, "Unable to open %s\n", cheatFile.c_str());
254
return false;
255
}
256
257
std::vector<std::string> title;
258
std::vector<std::string> newList;
259
260
char linebuf[2048]{};
261
bool parseGameEntry = false;
262
bool parseCheatEntry = false;
263
264
while (in && !feof(in)) {
265
char *line = GetLineNoNewline(linebuf, sizeof(linebuf), in);
266
267
if (!line) {
268
continue;
269
}
270
271
if (line[0] == '_' && line[1] == 'S') {
272
parseGameEntry = gameID == line;
273
parseCheatEntry = false;
274
} else if (parseGameEntry && line[0] == '_' && line[1] == 'C') {
275
// Test if cheat already exists.
276
parseCheatEntry = !HasCheatWithName(std::string(line).substr(4));
277
}
278
279
if (!parseGameEntry) {
280
if (newList.size() > 0) {
281
// Only parse the first matching game entry.
282
break;
283
} else {
284
// Haven't yet found a matching game entry, continue parsing.
285
continue;
286
}
287
}
288
289
if (line[0] == '_' && (line[1] == 'S' || line[1] == 'G') && title.size() < 2) {
290
title.push_back(line);
291
} else if (parseCheatEntry && ((line[0] == '_' && (line[1] == 'C' || line[1] == 'L')) || line[0] == '/' || line[0] == '#')) {
292
newList.push_back(line);
293
}
294
}
295
fclose(in);
296
297
std::string title2;
298
// Hmm, this probably gets confused about BOMs?
299
FILE *inTitle2 = File::OpenCFile(engine_->CheatFilename(), "rt");
300
if (inTitle2) {
301
char temp[2048];
302
char *line = GetLineNoNewline(temp, sizeof(temp), inTitle2);
303
if (line)
304
title2 = line;
305
fclose(inTitle2);
306
}
307
308
FILE *append = File::OpenCFile(engine_->CheatFilename(), "at");
309
if (!append)
310
return UI::EVENT_SKIPPED;
311
312
if (title2.size() == 0 || title2[0] != '_' || title2[1] != 'S') {
313
for (int i = (int)title.size(); i > 0; i--) {
314
newList.insert(newList.begin(), title[i - 1]);
315
}
316
}
317
318
NOTICE_LOG(Log::Common, "Imported %u lines from %s.\n", (int)newList.size(), cheatFile.c_str());
319
if (newList.size() != 0) {
320
fputc('\n', append);
321
}
322
323
for (int i = 0; i < (int)newList.size(); i++) {
324
fprintf(append, "%s", newList[i].c_str());
325
if (i < (int)newList.size() - 1) {
326
fputc('\n', append);
327
}
328
}
329
fclose(append);
330
return true;
331
}
332
333
UI::EventReturn CwCheatScreen::OnCheckBox(int index) {
334
if (!RebuildCheatFile(index)) {
335
// TODO: Report error. Let's reload the file, presumably it changed.
336
RecreateViews();
337
return UI::EVENT_SKIPPED;
338
}
339
340
return UI::EVENT_DONE;
341
}
342
343
bool CwCheatScreen::HasCheatWithName(const std::string &name) {
344
for (const auto &existing : fileInfo_) {
345
if (name == existing.name) {
346
return true;
347
}
348
}
349
350
return false;
351
}
352
353
bool CwCheatScreen::RebuildCheatFile(int index) {
354
if (!engine_)
355
return false;
356
FILE *in = File::OpenCFile(engine_->CheatFilename(), "rt");
357
if (!in)
358
return false;
359
360
// In case lines were edited while we weren't looking, reload them.
361
std::vector<std::string> lines;
362
for (; !feof(in); ) {
363
char temp[2048];
364
char *line = GetLineNoNewline(temp, sizeof(temp), in);
365
if (!line)
366
break;
367
368
lines.push_back(line);
369
}
370
fclose(in);
371
372
auto updateLine = [&](const CheatFileInfo &info) {
373
// Line numbers start with one, not zero.
374
size_t lineIndex = info.lineNum - 1;
375
if (lines.size() > lineIndex) {
376
auto &line = lines[lineIndex];
377
// This is the one to change. Let's see if it matches - maybe the file changed.
378
bool isCheatDef = line.find("_C") != line.npos;
379
bool hasCheatName = !info.name.empty() && line.find(info.name) != line.npos;
380
if (!isCheatDef || !hasCheatName) {
381
return false;
382
}
383
384
line = (info.enabled ? "_C1 " : "_C0 ") + info.name;
385
return true;
386
}
387
return false;
388
};
389
390
if (index == INDEX_ALL) {
391
for (const auto &info : fileInfo_) {
392
// Bail out if any don't match with no changes.
393
if (!updateLine(info)) {
394
return false;
395
}
396
}
397
} else {
398
if (!updateLine(fileInfo_[index])) {
399
return false;
400
}
401
}
402
403
FILE *out = File::OpenCFile(engine_->CheatFilename(), "wt");
404
if (!out) {
405
return false;
406
}
407
408
for (size_t i = 0; i < lines.size(); ++i) {
409
fprintf(out, "%s", lines[i].c_str());
410
if (i != lines.size() - 1)
411
fputc('\n', out);
412
}
413
fclose(out);
414
415
// Cheats will need to be reparsed now.
416
g_Config.bReloadCheats = true;
417
return true;
418
}
419
420