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