Skip to content

Commit

Permalink
UI: Monitor cheat file changes on cheat editor.
Browse files Browse the repository at this point in the history
This way if they switch apps or whatever, it'll update when they come
back.  This also means constant disk access, but it should be cached
anyway.
  • Loading branch information
unknownbrackets committed Apr 11, 2020
1 parent 29808ae commit 698dfa7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
35 changes: 32 additions & 3 deletions UI/CwCheatScreen.cpp
Expand Up @@ -15,9 +15,10 @@
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.

#include "ext/cityhash/city.h"
#include "i18n/i18n.h"
#include "ui/ui.h"
#include "util/text/utf8.h"
#include "i18n/i18n.h"

#include "Common/FileUtil.h"
#include "Core/Core.h"
Expand All @@ -28,6 +29,8 @@
#include "UI/GameInfoCache.h"
#include "UI/CwCheatScreen.h"

static const int FILE_CHECK_FRAME_INTERVAL = 53;

CwCheatScreen::CwCheatScreen(std::string gamePath)
: UIDialogScreenWithBackground() {
gamePath_ = gamePath;
Expand All @@ -43,10 +46,20 @@ void CwCheatScreen::LoadCheatInfo() {
gameTitle = g_paramSFO.GenerateFakeID(gamePath_);
}

// We won't parse this, just using it to detect changes to the file.
std::string str;
if (readFileToString(true, activeCheatFile.c_str(), str)) {
fileCheckHash_ = CityHash64(str.c_str(), str.size());
}
fileCheckCounter_ = 0;

CWCheatEngine *cheatEngine2 = new CWCheatEngine();
cheatEngine2->CreateCheatFile();
fileInfo_ = cheatEngine2->FileInfo();
delete cheatEngine2;

// Let's also trigger a reload, in case it changed.
g_Config.bReloadCheats = true;
}

void CwCheatScreen::CreateViews() {
Expand Down Expand Up @@ -79,7 +92,7 @@ void CwCheatScreen::CreateViews() {
rightColumn->Add(new ItemHeader(cw->T("Cheats")));
for (size_t i = 0; i < fileInfo_.size(); ++i) {
rightColumn->Add(new CheatCheckBox(&fileInfo_[i].enabled, fileInfo_[i].name))->OnClick.Add([=](UI::EventParams &) {
return OnCheckBox(i);
return OnCheckBox((int)i);
});
}

Expand All @@ -91,6 +104,23 @@ void CwCheatScreen::CreateViews() {
AddStandardBack(root_);
}

void CwCheatScreen::update() {
if (fileCheckCounter_++ >= FILE_CHECK_FRAME_INTERVAL) {
// Check if the file has changed. If it has, we'll reload.
std::string str;
if (readFileToString(true, activeCheatFile.c_str(), str)) {
uint64_t newHash = CityHash64(str.c_str(), str.size());
if (newHash != fileCheckHash_) {
// This will update the hash.
RecreateViews();
}
}
fileCheckCounter_ = 0;
}

UIDialogScreenWithBackground::update();
}

void CwCheatScreen::onFinish(DialogResult result) {
if (result != DR_BACK) // This only works for BACK here.
return;
Expand Down Expand Up @@ -130,7 +160,6 @@ UI::EventReturn CwCheatScreen::OnEditCheatFile(UI::EventParams &params) {
if (MIPSComp::jit) {
MIPSComp::jit->ClearCache();
}
TriggerFinish(DR_OK);
#if PPSSPP_PLATFORM(UWP)
LaunchBrowser(activeCheatFile.c_str());
#else
Expand Down
4 changes: 4 additions & 0 deletions UI/CwCheatScreen.h
Expand Up @@ -15,6 +15,7 @@
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.

#include <cstdint>
#include <functional>

#include "ui/view.h"
Expand All @@ -38,6 +39,7 @@ class CwCheatScreen : public UIDialogScreenWithBackground {
UI::EventReturn OnEditCheatFile(UI::EventParams &params);
UI::EventReturn OnEnableAll(UI::EventParams &params);

void update() override;
void onFinish(DialogResult result) override;

protected:
Expand All @@ -52,6 +54,8 @@ class CwCheatScreen : public UIDialogScreenWithBackground {
UI::ScrollView *rightScroll_ = nullptr;
std::vector<CheatFileInfo> fileInfo_;
std::string gamePath_;
int fileCheckCounter_ = 0;
uint64_t fileCheckHash_;
bool enableAllFlag_ = false;
};

Expand Down

0 comments on commit 698dfa7

Please sign in to comment.