use github api to download patches

removing this workaround makes the code cleaner
This commit is contained in:
DanielSvoboda 2024-09-10 11:30:35 -03:00
parent 3a65052b8e
commit 14e7cd587d

View file

@ -669,34 +669,31 @@ void CheatsPatches::populateFileListPatches() {
void CheatsPatches::downloadPatches(const QString repository, const bool showMessageBox) {
QString url;
if (repository == "GoldHEN") {
url = "https://github.com/illusion0001/PS4-PS5-Game-Patch/tree/main/"
"patches/xml";
url = "https://api.github.com/repos/illusion0001/PS4-PS5-Game-Patch/contents/patches/xml";
}
if (repository == "shadPS4") {
url = "https://github.com/shadps4-emu/ps4_cheats/tree/main/"
"PATCHES";
url = "https://api.github.com/repos/shadps4-emu/ps4_cheats/contents/PATCHES";
}
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
QNetworkRequest request(url);
request.setRawHeader("Accept", "application/vnd.github.v3+json");
QNetworkReply* reply = manager->get(request);
connect(reply, &QNetworkReply::finished, [=, this]() {
connect(reply, &QNetworkReply::finished, [=]() {
if (reply->error() == QNetworkReply::NoError) {
QByteArray htmlData = reply->readAll();
QByteArray jsonData = reply->readAll();
reply->deleteLater();
// Parsear HTML e extrair JSON usando QRegularExpression
QString htmlString = QString::fromUtf8(htmlData);
QRegularExpression jsonRegex(
R"(<script type="application/json" data-target="react-app.embeddedData">(.+?)</script>)");
QRegularExpressionMatch match = jsonRegex.match(htmlString);
if (match.hasMatch()) {
QByteArray jsonData = match.captured(1).toUtf8();
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
QJsonObject jsonObj = jsonDoc.object();
QJsonArray itemsArray =
jsonObj["payload"].toObject()["tree"].toObject()["items"].toArray();
QJsonArray itemsArray = jsonDoc.array();
if (itemsArray.isEmpty()) {
if (showMessageBox) {
QMessageBox::warning(this, tr("Error"),
tr("Failed to parse JSON data from HTML."));
}
return;
}
QDir dir(Common::FS::GetUserPath(Common::FS::PathType::PatchesDir));
QString fullPath = dir.filePath(repository);
@ -709,23 +706,13 @@ void CheatsPatches::downloadPatches(const QString repository, const bool showMes
QJsonObject fileObj = value.toObject();
QString fileName = fileObj["name"].toString();
QString filePath = fileObj["path"].toString();
QString downloadUrl = fileObj["download_url"].toString();
if (fileName.endsWith(".xml")) {
QString fileUrl;
if (repository == "GoldHEN") {
fileUrl = QString("https://raw.githubusercontent.com/illusion0001/"
"PS4-PS5-Game-Patch/main/%1")
.arg(filePath);
}
if (repository == "shadPS4") {
fileUrl = QString("https://raw.githubusercontent.com/shadps4-emu/"
"ps4_cheats/main/%1")
.arg(filePath);
}
QNetworkRequest fileRequest(fileUrl);
QNetworkRequest fileRequest(downloadUrl);
QNetworkReply* fileReply = manager->get(fileRequest);
connect(fileReply, &QNetworkReply::finished, [=, this]() {
connect(fileReply, &QNetworkReply::finished, [=]() {
if (fileReply->error() == QNetworkReply::NoError) {
QByteArray fileData = fileReply->readAll();
QFile localFile(dir.filePath(fileName));
@ -743,7 +730,7 @@ void CheatsPatches::downloadPatches(const QString repository, const bool showMes
if (showMessageBox) {
QMessageBox::warning(
this, tr("Error"),
QString(tr("Failed to download:") + "\n%1").arg(fileUrl));
QString(tr("Failed to download:") + "\n%1").arg(downloadUrl));
}
}
fileReply->deleteLater();
@ -754,20 +741,14 @@ void CheatsPatches::downloadPatches(const QString repository, const bool showMes
QMessageBox::information(this, tr("Download Complete"),
QString(tr("DownloadComplete_MSG")));
}
// Create the files.json file with the identification of which file to open
createFilesJson(repository);
populateFileListPatches();
} else {
if (showMessageBox) {
QMessageBox::warning(this, tr("Error"),
tr("Failed to parse JSON data from HTML."));
}
}
} else {
if (showMessageBox) {
QMessageBox::warning(this, tr("Error"), tr("Failed to retrieve HTML page."));
QString(tr("Failed to retrieve HTML page.") + "\n%1")
.arg(reply->errorString()));
}
}
emit downloadFinished();