1
Fork 0
mirror of https://github.com/redstrate/Astra.git synced 2025-04-20 19:57:45 +00:00

Don't keep patches downloaded in memory

This now writes them directly to a temporary file, then renames them
when finished. This reduces the amount of memory Astra uses while
updating immensely and just makes the experience all-around much nicer.

Closes #10
This commit is contained in:
Joshua Goins 2024-06-26 23:43:06 -04:00
parent d9b44e230c
commit f543dd433e

View file

@ -71,10 +71,13 @@ QCoro::Task<bool> Patcher::patch(const PatchList &patchList)
const int ourIndex = patchIndex++; const int ourIndex = patchIndex++;
const QString filename = QStringLiteral("%1.patch").arg(patch.name); const QString filename = QStringLiteral("%1.patch").arg(patch.name);
const QString tempFilename = QStringLiteral("%1.patch~").arg(patch.name); // tilde afterwards to hide it easily
const QDir repositoryDir = m_patchesDir.absoluteFilePath(patch.repository); const QDir repositoryDir = m_patchesDir.absoluteFilePath(patch.repository);
Utility::createPathIfNeeded(repositoryDir); Utility::createPathIfNeeded(repositoryDir);
const QString patchPath = repositoryDir.absoluteFilePath(filename); const QString patchPath = repositoryDir.absoluteFilePath(filename);
const QString tempPatchPath = repositoryDir.absoluteFilePath(tempFilename);
const QueuedPatch queuedPatch{.name = patch.name, const QueuedPatch queuedPatch{.name = patch.name,
.repository = patch.repository, .repository = patch.repository,
@ -102,18 +105,23 @@ QCoro::Task<bool> Patcher::patch(const PatchList &patchList)
auto patchReply = m_launcher.mgr()->get(patchRequest); auto patchReply = m_launcher.mgr()->get(patchRequest);
connect(patchReply, &QNetworkReply::downloadProgress, this, [this, ourIndex, queuedPatch](int received, int total) { connect(patchReply, &QNetworkReply::downloadProgress, this, [this, ourIndex](int received, int total) {
Q_UNUSED(total) Q_UNUSED(total)
updateDownloadProgress(ourIndex, received); updateDownloadProgress(ourIndex, received);
}); });
synchronizer.addFuture(QtFuture::connect(patchReply, &QNetworkReply::finished).then([this, ourIndex, patchPath, patchReply] { connect(patchReply, &QNetworkReply::readyRead, this, [this, tempPatchPath, patchReply] {
qDebug(ASTRA_PATCHER) << "Downloaded to" << patchPath; // TODO: don't open the file each time we recieve data
QFile file(tempPatchPath);
QFile file(patchPath); file.open(QIODevice::WriteOnly | QIODevice::Append);
file.open(QIODevice::WriteOnly);
file.write(patchReply->readAll()); file.write(patchReply->readAll());
file.close(); file.close();
});
synchronizer.addFuture(QtFuture::connect(patchReply, &QNetworkReply::finished).then([this, ourIndex, patchPath, tempPatchPath] {
qDebug(ASTRA_PATCHER) << "Downloaded to" << patchPath;
QDir().rename(tempPatchPath, patchPath);
QMutexLocker locker(&m_finishedPatchesMutex); QMutexLocker locker(&m_finishedPatchesMutex);
m_finishedPatches++; m_finishedPatches++;