updater works

This commit is contained in:
Nayla Hanegan 2025-01-07 17:41:42 -05:00
parent e37df05d75
commit 5259ec5fc9
13 changed files with 288 additions and 205 deletions

View file

@ -112,9 +112,13 @@ HttpRequest::Response HttpRequest::Post(const std::string& url, const std::strin
int HttpRequest::Impl::CurlProgressCallback(Impl* impl, curl_off_t dltotal, curl_off_t dlnow,
curl_off_t ultotal, curl_off_t ulnow)
{
// Abort if callback isn't true
return !impl->m_callback(static_cast<s64>(dltotal), static_cast<s64>(dlnow),
static_cast<s64>(ultotal), static_cast<s64>(ulnow));
// Call the progress callback with the current download progress
if (impl->m_callback)
{
return !impl->m_callback(static_cast<s64>(dltotal), static_cast<s64>(dlnow),
static_cast<s64>(ultotal), static_cast<s64>(ulnow));
}
return 0; // If no callback, continue
}
HttpRequest::Impl::Impl(std::chrono::milliseconds timeout_ms, ProgressCallback callback)

View file

@ -28,6 +28,11 @@ public:
// Return false to abort the request
using ProgressCallback = std::function<bool(s64 dltotal, s64 dlnow, s64 ultotal, s64 ulnow)>;
void SetProgressCallback(ProgressCallback callback) {
m_callback = std::move(callback);
}
explicit HttpRequest(std::chrono::milliseconds timeout_ms = std::chrono::milliseconds{3000},
ProgressCallback callback = nullptr);
~HttpRequest();
@ -62,5 +67,6 @@ public:
private:
class Impl;
std::unique_ptr<Impl> m_impl;
ProgressCallback m_callback;
};
} // namespace Common

View file

@ -276,6 +276,8 @@ add_executable(dolphin-mpn
MainWindow.h
MarioPartyNetplay/DownloadUpdateDialog.cpp
MarioPartyNetplay/DownloadUpdateDialog.h
MarioPartyNetplay/DownloadWorker.cpp
MarioPartyNetplay/Downloadworker.h
MarioPartyNetplay/InstallUpdateDialog.cpp
MarioPartyNetplay/InstallUpdateDialog.h
MarioPartyNetplay/UpdateDialog.cpp

View file

@ -187,6 +187,7 @@
<ClCompile Include="Main.cpp" />
<ClCompile Include="MainWindow.cpp" />
<ClCompile Include="MarioPartyNetplay/DownloadUpdateDialog.cpp" />
<ClCompile Include="MarioPartyNetplay/DownloadWorker.cpp" />
<ClCompile Include="MarioPartyNetplay/InstallUpdateDialog.cpp" />
<ClCompile Include="MarioPartyNetplay/UpdateDialog.cpp" />
<ClCompile Include="MenuBar.cpp" />
@ -405,6 +406,7 @@
<QtMoc Include="InfinityBase/InfinityBaseWindow.h" />
<QtMoc Include="MainWindow.h" />
<QtMoc Include="MarioPartyNetplay/DownloadUpdateDialog.h" />
<QtMoc Include="MarioPartyNetplay/DownloadWorker.h" />
<QtMoc Include="MarioPartyNetplay/InstallUpdateDialog.h" />
<QtMoc Include="MarioPartyNetplay/UpdateDialog.h" />
<QtMoc Include="MenuBar.h" />

View file

@ -4,38 +4,60 @@
*/
#include "DownloadUpdateDialog.h"
#include "InstallUpdateDialog.h"
#include <QMessageBox>
#include <QFile>
#include <QVBoxLayout>
#include <QCoreApplication>
#include <QDir>
#include <QString>
#include <QLabel>
#include <curl/curl.h>
#include <iostream>
// Callback function for libcurl to write data
size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
#include <QThread>
#include "DownloadWorker.h"
// Constructor implementation
DownloadUpdateDialog::DownloadUpdateDialog(QWidget* parent, const QUrl& url, const QString& filename)
DownloadUpdateDialog::DownloadUpdateDialog(QWidget* parent, const QString& url, const QString& filename)
: QDialog(parent), filename(filename)
{
// Create UI components
setWindowTitle(QStringLiteral("Downloading %1").arg(this->filename));
QVBoxLayout* layout = new QVBoxLayout(this);
// Create and add label
QLabel* label = new QLabel(QStringLiteral("Downloading %1...").arg(this->filename), this);
progressBar = new QProgressBar(this);
// Set up the layout
layout->addWidget(label);
// Create and add progress bar
progressBar = new QProgressBar(this);
layout->addWidget(progressBar);
// Set the layout for the dialog
setLayout(layout);
// Set a minimum size for the dialog
setMinimumSize(300, 100); // Adjust size as needed
// Start the download
startDownload(url);
// Create the worker and thread
DownloadWorker* worker = new DownloadWorker(url, filename);
QThread* thread = new QThread;
// Move the worker to the thread
worker->moveToThread(thread);
// Connect signals and slots
connect(thread, &QThread::started, worker, &DownloadWorker::startDownload, Qt::UniqueConnection);
connect(worker, &DownloadWorker::progressUpdated, this, &DownloadUpdateDialog::updateProgress, Qt::UniqueConnection);
connect(worker, &DownloadWorker::finished, thread, &QThread::quit, Qt::UniqueConnection);
connect(worker, &DownloadWorker::finished, worker, &DownloadWorker::deleteLater, Qt::UniqueConnection);
connect(worker, &DownloadWorker::finished, this, &DownloadUpdateDialog::onDownloadFinished, Qt::UniqueConnection);
connect(worker, &DownloadWorker::finished, this, &DownloadUpdateDialog::accept, Qt::UniqueConnection);
connect(worker, &DownloadWorker::errorOccurred, this, &DownloadUpdateDialog::handleError, Qt::UniqueConnection);
connect(thread, &QThread::finished, thread, &QThread::deleteLater, Qt::UniqueConnection);
// Start the thread
thread->start();
this->exec(); // Show the dialog
}
// Destructor implementation
@ -43,74 +65,36 @@ DownloadUpdateDialog::~DownloadUpdateDialog()
{
}
// Returns the temporary directory
QString DownloadUpdateDialog::GetTempDirectory() const
// Handle errors
void DownloadUpdateDialog::handleError(const QString& errorMsg)
{
return temporaryDirectory;
QMessageBox::critical(this, tr("Error"), errorMsg);
reject();
}
// Returns the filename
QString DownloadUpdateDialog::GetFileName() const
void DownloadUpdateDialog::onDownloadFinished()
{
return filename;
// Use QCoreApplication to get the application's directory path
#ifdef _WIN32
installationDirectory = QCoreApplication::applicationDirPath(); // Set the installation directory
#endif
#ifdef __APPLE__
installationDirectory = QCoreApplication::applicationDirPath() + "/../../"
#endif
// Use QStandardPaths to get the system's temporary directory
temporaryDirectory = QDir::tempPath();
InstallUpdateDialog* installDialog =
new InstallUpdateDialog(this, installationDirectory, temporaryDirectory, this->filename);
installDialog->exec(); // Show the installation dialog modally
accept();
}
// Update the progress bar
void DownloadUpdateDialog::updateProgress(qint64 size, qint64 total)
{
progressBar->setMaximum(total);
progressBar->setMinimum(0);
progressBar->setValue(size);
}
// Starts the download process
void DownloadUpdateDialog::startDownload(const QUrl& url)
{
CURL* curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if (curl)
{
// Convert QUrl to std::string properly
std::string urlStr = url.toString().toStdString();
curl_easy_setopt(curl, CURLOPT_URL, urlStr.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
// Set the progress function
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, [](void* clientp, double totalToDownload, double nowDownloaded, double totalToUpload, double nowUploaded) {
DownloadUpdateDialog* dialog = static_cast<DownloadUpdateDialog*>(clientp);
dialog->updateProgress(static_cast<qint64>(nowDownloaded), static_cast<qint64>(totalToDownload));
return 0; // Return 0 to continue the download
});
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, this);
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
// Use QString::fromStdString() for proper string conversion
QString errorMsg = QString::fromStdString(std::string(curl_easy_strerror(res)));
QMessageBox::critical(this, tr("Error"), tr("Failed to download update file: %1").arg(errorMsg));
reject();
}
else
{
QFile file(filename);
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
QMessageBox::critical(this, tr("Error"), tr("Failed to open file for writing."));
reject();
}
else
{
file.write(readBuffer.c_str(), readBuffer.size());
file.close();
accept();
}
}
curl_easy_cleanup(curl);
}
}

View file

@ -6,7 +6,6 @@
#pragma once
#include <QDialog>
#include <QUrl>
#include <QString>
#include <QProgressBar>
@ -15,17 +14,17 @@ class DownloadUpdateDialog : public QDialog
Q_OBJECT
public:
DownloadUpdateDialog(QWidget* parent, const QUrl& url, const QString& filename);
explicit DownloadUpdateDialog(QWidget* parent, const QString& url, const QString& filename);
~DownloadUpdateDialog();
QString GetTempDirectory() const;
QString GetFileName() const;
public slots:
void updateProgress(qint64 size, qint64 total);
void handleError(const QString& errorMsg); // Ensure this is declared
void onDownloadFinished();
private:
QString filename; // Member variable for filename
QString temporaryDirectory; // Member variable for temporary directory
QProgressBar* progressBar; // Declare progressBar as a member variable
void startDownload(const QUrl& url);
void updateProgress(qint64 size, qint64 total); // Method to update progress
QString filename; // Ensure this is declared as a private member
QProgressBar* progressBar;
QString installationDirectory;
QString temporaryDirectory;
};

View file

@ -0,0 +1,50 @@
/*
* Dolphin for Mario Party Netplay
* Copyright (C) 2025 Tabitha Hanegan <tabithahanegan.com>
*/
#include "DownloadWorker.h"
#include "Common/HttpRequest.h"
#include <QFile>
#include <QMessageBox>
DownloadWorker::DownloadWorker(const QString& url, const QString& filename)
: url(url), filename(filename) {}
void DownloadWorker::startDownload()
{
Common::HttpRequest httpRequest;
// Set the progress callback
httpRequest.SetProgressCallback([this](s64 dltotal, s64 dlnow, s64 ultotal, s64 ulnow) {
emit progressUpdated(dlnow, dltotal); // Emit progress signal
return true; // Continue the download
});
httpRequest.FollowRedirects();
Common::HttpRequest::Headers headers;
headers["User-Agent"] = "Dolphin-MPN/1.0";
// Perform the GET request
auto response = httpRequest.Get(url.toStdString(), headers);
// Check the response
if (response)
{
QFile file(filename);
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
emit errorOccurred(QStringLiteral("Failed to open file for writing."));
return;
}
QByteArray byteArray(reinterpret_cast<const char*>(response->data()), response->size());
file.write(byteArray);
file.close();
emit finished(); // Emit finished signal
}
else
{
emit errorOccurred(QStringLiteral("Failed to download update file.")); // This should also work
}
}

View file

@ -0,0 +1,27 @@
/*
* Dolphin for Mario Party Netplay
* Copyright (C) 2025 Tabitha Hanegan <tabithahanegan.com>
*/
#include <QObject>
#include <QString>
class DownloadWorker : public QObject
{
Q_OBJECT
public:
DownloadWorker(const QString& url, const QString& filename);
public slots:
void startDownload();
signals:
void progressUpdated(qint64 size, qint64 total);
void finished();
void errorOccurred(const QString& errorMsg);
private:
QString url;
QString filename;
};

View file

@ -9,21 +9,26 @@
#include <QDir>
#include <QTextStream>
#include <QVBoxLayout>
#include <QDialog>
#include <QLabel>
#include <QProgressBar>
#include <QMessageBox>
#include <mz_compat.h>
#include "Common/MinizipUtil.h"
#include <Common/Logging/Log.h>
// Constructor implementation
InstallUpdateDialog::InstallUpdateDialog(QWidget *parent, QString installationDirectory, QString temporaryDirectory, QString filename)
: DownloadUpdateDialog(parent, QUrl(), filename),
: QDialog(parent), // Only pass the parent
installationDirectory(installationDirectory),
temporaryDirectory(temporaryDirectory)
temporaryDirectory(temporaryDirectory),
filename(filename) // Initialize member variables
{
setWindowTitle(QStringLiteral("Installing %1...").arg(this->filename));
// Create UI components
QVBoxLayout* layout = new QVBoxLayout(this);
QLabel* label = new QLabel(QStringLiteral("Installing %1...").arg(this->filename), this);
QProgressBar* progressBar = new QProgressBar(this);
label = new QLabel(QStringLiteral("Installing %1...").arg(this->filename), this);
progressBar = new QProgressBar(this);
// Set up the layout
layout->addWidget(label);
@ -41,7 +46,7 @@ InstallUpdateDialog::~InstallUpdateDialog(void)
void InstallUpdateDialog::install(void)
{
QString fullFilePath = this->temporaryDirectory + QStringLiteral("/") + this->filename;
QString fullFilePath = QCoreApplication::applicationDirPath() + QStringLiteral("/") + this->filename;
QString appPath = QCoreApplication::applicationDirPath();
QString appPid = QString::number(QCoreApplication::applicationPid());
@ -76,21 +81,28 @@ void InstallUpdateDialog::install(void)
this->label->setText(QStringLiteral("Extracting %1...").arg(this->filename));
this->progressBar->setValue(50);
QString extractDirectory = this->temporaryDirectory + QDir::separator() + QStringLiteral("Dolphin-MPN");
// Ensure the extract directory exists before attempting to unzip
QDir dir(this->temporaryDirectory);
if (!dir.mkdir(QStringLiteral("extract")))
if (!QDir(extractDirectory).exists())
{
QMessageBox::critical(this, QStringLiteral("Error"), QStringLiteral("Failed to create extract directory."));
if (!dir.mkdir(QStringLiteral("Dolphin-MPN")))
{
QMessageBox::critical(this, QStringLiteral("Error"),
QStringLiteral("Failed to create extract directory."));
this->reject();
return;
}
}
QString extractDirectory = this->temporaryDirectory + QStringLiteral("/extract");
// Attempt to unzip files into the extract directory
if (!unzipFile(fullFilePath.toStdString(), extractDirectory.toStdString()))
{
QMessageBox::critical(this, QStringLiteral("Error"), QStringLiteral("Unzip failed: Unable to extract files."));
this->reject();
return;
QMessageBox::critical(this, QStringLiteral("Error"),
QStringLiteral("Unzip failed: Unable to extract files."));
this->reject();
return;
}
this->label->setText(QStringLiteral("Executing update script..."));
@ -122,64 +134,65 @@ void InstallUpdateDialog::install(void)
bool InstallUpdateDialog::unzipFile(const std::string& zipFilePath, const std::string& destDir)
{
unzFile zipFile = unzOpen(zipFilePath.c_str());
if (!zipFile)
{
return false; // Failed to open zip file
}
if (unzGoToFirstFile(zipFile) != UNZ_OK)
{
unzClose(zipFile);
return false; // No files in zip
}
do
{
char filename[256];
unz_file_info fileInfo;
if (unzGetCurrentFileInfo(zipFile, &fileInfo, filename, sizeof(filename), nullptr, 0, nullptr, 0) != UNZ_OK)
{
unzClose(zipFile);
return false; // Failed to get file info
}
// Create full path for the extracted file using std::string concatenation
std::string fullPath = destDir + "/" + std::string(filename);
// Create directories if needed
QString qFullPath = QString::fromStdString(fullPath);
QDir().mkpath(QFileInfo(qFullPath).path());
// Open the file in the zip
if (unzOpenCurrentFile(zipFile) != UNZ_OK)
{
unzClose(zipFile);
return false; // Failed to open file in zip
}
// Write the file to disk
QFile outFile(qFullPath);
if (!outFile.open(QIODevice::WriteOnly))
{
unzCloseCurrentFile(zipFile);
unzClose(zipFile);
return false; // Failed to create output file
}
char buffer[8192];
int bytesRead;
while ((bytesRead = unzReadCurrentFile(zipFile, buffer, sizeof(buffer))) > 0)
{
outFile.write(buffer, bytesRead);
}
outFile.close();
unzCloseCurrentFile(zipFile);
} while (unzGoToNextFile(zipFile) == UNZ_OK);
unzFile zipFile = unzOpen(zipFilePath.c_str());
if (!zipFile)
{
return false; // Failed to open zip file
}
if (unzGoToFirstFile(zipFile) != UNZ_OK)
{
unzClose(zipFile);
return true; // Successfully unzipped all files
return false; // No files in zip
}
do
{
char filename[256];
unz_file_info fileInfo;
if (unzGetCurrentFileInfo(zipFile, &fileInfo, filename, sizeof(filename), nullptr, 0, nullptr,
0) != UNZ_OK)
{
unzClose(zipFile);
return false; // Failed to get file info
}
// Create full path for the extracted file
std::string fullPath = destDir + "/" + std::string(filename);
QString qFullPath = QString::fromStdString(fullPath);
// Handle directories
if (filename[std::strlen(filename) - 1] == '/')
{
QDir().mkpath(qFullPath);
continue;
}
// Ensure the directory structure exists
QDir().mkpath(QFileInfo(qFullPath).path());
// Prepare a buffer to store file data
std::vector<u8> fileData(fileInfo.uncompressed_size);
if (!Common::ReadFileFromZip(zipFile, &fileData))
{
unzClose(zipFile);
return false; // Failed to read file from zip
}
// Write the file data to disk
QFile outFile(qFullPath);
if (!outFile.open(QIODevice::WriteOnly))
{
unzClose(zipFile);
return false; // Failed to create output file
}
outFile.write(reinterpret_cast<const char*>(fileData.data()), fileData.size());
outFile.close();
} while (unzGoToNextFile(zipFile) == UNZ_OK);
unzClose(zipFile);
return true; // Successfully unzipped all files
}
void InstallUpdateDialog::writeAndRunScript(QStringList stringList)

View file

@ -3,7 +3,6 @@
* Copyright (C) 2025 Tabitha Hanegan <tabithahanegan.com>
*/
#include "DownloadUpdateDialog.h"
#include <QDialog>
#include <QString>
#include <QLabel>
@ -16,7 +15,7 @@ class QLabel;
class QProgressBar;
QT_END_NAMESPACE
class InstallUpdateDialog : public DownloadUpdateDialog
class InstallUpdateDialog : public QDialog
{
Q_OBJECT

View file

@ -9,6 +9,7 @@
#include <QFileInfo>
#include <QPushButton>
#include <QJsonArray>
#include <QJsonObject>
#include <QDesktopServices>
#include <QTemporaryDir>
#include <QFile>
@ -16,9 +17,11 @@
#include <QProcess>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QWidget>
#include <QLabel>
#include <QTextEdit>
#include <QDialogButtonBox>
#include "../../Common/Logging/Log.h"
using namespace UserInterface::Dialog;
@ -62,51 +65,43 @@ UpdateDialog::~UpdateDialog()
{
}
QString UpdateDialog::GetFileName()
{
return this->filename;
}
QUrl UpdateDialog::GetUrl()
{
return this->url;
}
void UpdateDialog::accept()
{
QJsonArray jsonArray = jsonObject[QStringLiteral("assets")].toArray();
QString filenameToDownload;
QUrl urlToDownload;
QString urlToDownload;
for (const QJsonValue& value : jsonArray)
{
QJsonObject object = value.toObject();
QString filename = object.value(QStringLiteral("name")).toString();
QUrl url(object.value(QStringLiteral("browser_download_url")).toString());
QString filenameBlob = object.value(QStringLiteral("name")).toString();
QString downloadUrl(object.value(QStringLiteral("browser_download_url")).toString());
if (QSysInfo::productType() == QStringLiteral("windows")) {
if (filename.contains(QStringLiteral("win32")) ||
filename.contains(QStringLiteral("windows")) ||
filename.contains(QStringLiteral("win64")))
{
filenameToDownload = filename;
urlToDownload = url;
break;
}
#ifdef _WIN32
if (filenameBlob.contains(QStringLiteral("win32")) ||
filenameBlob.contains(QStringLiteral("windows")) ||
filenameBlob.contains(QStringLiteral("win64")))
{
filenameToDownload = filenameBlob;
urlToDownload = downloadUrl;
break;
}
else if (QSysInfo::productType() == QStringLiteral("macos")) {
if (filename.contains(QStringLiteral("darwin")) ||
filename.contains(QStringLiteral("macOS")))
{
filenameToDownload = filename;
urlToDownload = url;
break;
}
#endif
#ifdef __APPLE__
if (filenameBlob.contains(QStringLiteral("darwin")) ||
filenameBlob.contains(QStringLiteral("macOS")))
{
filenameToDownload = filenameBlob;
urlToDownload = downloadUrl;
break;
}
#endif
}
this->url = urlToDownload;
this->filename = filenameToDownload;
QDialog::accept();
DownloadUpdateDialog downloadDialog(this, urlToDownload, filenameToDownload);
}

View file

@ -6,7 +6,7 @@
#include <QDialog>
#include <QJsonObject>
#include <QUrl>
#include <QString>
class QLabel;
class QTextEdit;
@ -24,7 +24,7 @@ class UpdateDialog : public QDialog
private:
QJsonObject jsonObject;
QString filename;
QUrl url;
QString url;
// UI elements
QLabel* label;
@ -38,12 +38,9 @@ private:
public:
explicit UpdateDialog(QWidget *parent, QJsonObject jsonObject, bool forced);
~UpdateDialog();
QString GetFileName();
QUrl GetUrl();
private slots:
void accept() override;
void accept(void) Q_DECL_OVERRIDE;
};
} // namespace Dialog

View file

@ -620,26 +620,31 @@ void MenuBar::AddHelpMenu()
{
QMenu* help_menu = addMenu(tr("&Help"));
QAction* website = help_menu->addAction(tr("&Website"));
QAction* updaterCheck = help_menu->addAction(tr("Check For &Updates"));
connect(updaterCheck, &QAction::triggered, this, &MenuBar::ShowUpdateDialog);
QAction* website = help_menu->addAction(tr("&Dolphin Website"));
connect(website, &QAction::triggered, this,
[]() { QDesktopServices::openUrl(QUrl(QStringLiteral("https://dolphin-emu.org/"))); });
QAction* documentation = help_menu->addAction(tr("Online &Documentation"));
connect(documentation, &QAction::triggered, this, []() {
QDesktopServices::openUrl(QUrl(QStringLiteral("https://dolphin-emu.org/docs/guides")));
});
help_menu->addSeparator();
QAction* discord = help_menu->addAction(tr("&MPN Discord"));
connect(discord, &QAction::triggered, this,
[]() { QDesktopServices::openUrl(QUrl(QStringLiteral("https://discord.gg/marioparty/"))); });
QAction* github = help_menu->addAction(tr("&GitHub Repository"));
connect(github, &QAction::triggered, this, []() {
QDesktopServices::openUrl(QUrl(QStringLiteral("https://github.com/MarioPartyNetplay/Dolphin-MPN")));
});
QAction* documentation = help_menu->addAction(tr("&How-to Netplay"));
connect(documentation, &QAction::triggered, this, []() {
QDesktopServices::openUrl(
QUrl(QStringLiteral("https://rentry.org/mario-party-netplay-gcn-wii")));
});
#ifndef __APPLE__
help_menu->addSeparator();
#endif
QAction* updaterCheck = help_menu->addAction(tr("Check For &Updates"));
connect(updaterCheck, &QAction::triggered, this, &MenuBar::ShowUpdateDialog);
help_menu->addAction(tr("&About"), this, &MenuBar::ShowAboutDialog);
}