From 8e286c2b5c432e972df2e94b01481bbe094e464c Mon Sep 17 00:00:00 2001 From: Petr Mrázek Date: Fri, 10 Jan 2014 22:08:00 +0100 Subject: Make CacheDownload use QSaveFile --- logic/net/CacheDownload.cpp | 48 ++++++++++++++++++++------------------------- logic/net/CacheDownload.h | 6 +++--- 2 files changed, 24 insertions(+), 30 deletions(-) (limited to 'logic/net') diff --git a/logic/net/CacheDownload.cpp b/logic/net/CacheDownload.cpp index 6eadae39..17fd7f2a 100644 --- a/logic/net/CacheDownload.cpp +++ b/logic/net/CacheDownload.cpp @@ -42,6 +42,13 @@ void CacheDownload::start() // if there already is a file and md5 checking is in effect and it can be opened if (!ensureFilePathExists(m_target_path)) { + QLOG_ERROR() << "Could not create folder for " + m_target_path; + emit failed(m_index_within_job); + return; + } + if(!m_output_file.open(QIODevice::WriteOnly)) + { + QLOG_ERROR() << "Could not open " + m_target_path + " for writing"; emit failed(m_index_within_job); return; } @@ -85,26 +92,21 @@ void CacheDownload::downloadFinished() // if the download succeeded if (m_status != Job_Failed) { - // nothing went wrong... m_status = Job_Finished; - if (m_output_file.isOpen()) + if (m_output_file.commit()) { - // save the data to the downloadable if we aren't saving to file - m_output_file.close(); m_entry->md5sum = md5sum.result().toHex().constData(); } else { - if (m_output_file.open(QIODevice::ReadOnly)) - { - m_entry->md5sum = - QCryptographicHash::hash(m_output_file.readAll(), QCryptographicHash::Md5) - .toHex() - .constData(); - m_output_file.close(); - } + QLOG_ERROR() << "Failed to commit changes to " << m_target_path; + m_output_file.cancelWriting(); + m_reply.reset(); + emit failed(m_index_within_job); + return; } + QFileInfo output_file_info(m_target_path); m_entry->etag = m_reply->rawHeader("ETag").constData(); @@ -124,8 +126,7 @@ void CacheDownload::downloadFinished() // else the download failed else { - m_output_file.close(); - m_output_file.remove(); + m_output_file.cancelWriting(); m_reply.reset(); emit failed(m_index_within_job); return; @@ -134,19 +135,12 @@ void CacheDownload::downloadFinished() void CacheDownload::downloadReadyRead() { - if (!m_output_file.isOpen()) - { - if (!m_output_file.open(QIODevice::WriteOnly)) - { - /* - * Can't open the file... the job failed - */ - m_reply->abort(); - emit failed(m_index_within_job); - return; - } - } QByteArray ba = m_reply->readAll(); md5sum.addData(ba); - m_output_file.write(ba); + if(m_output_file.write(ba) != ba.size()) + { + QLOG_ERROR() << "Failed writing into " + m_target_path; + m_reply->abort(); + emit failed(m_index_within_job); + } } diff --git a/logic/net/CacheDownload.h b/logic/net/CacheDownload.h index e25aecd2..921e231b 100644 --- a/logic/net/CacheDownload.h +++ b/logic/net/CacheDownload.h @@ -17,8 +17,8 @@ #include "NetAction.h" #include "HttpMetaCache.h" -#include -#include +#include +#include typedef std::shared_ptr CacheDownloadPtr; class CacheDownload : public NetAction @@ -29,7 +29,7 @@ public: /// if saving to file, use the one specified in this string QString m_target_path; /// this is the output file, if any - QFile m_output_file; + QSaveFile m_output_file; /// the hash-as-you-download QCryptographicHash md5sum; -- cgit From 43a39a3bfbffa2997cf8ca5126ee3ac5157f62c8 Mon Sep 17 00:00:00 2001 From: Petr Mrázek Date: Sat, 11 Jan 2014 02:06:22 +0100 Subject: Harden CacheDownload. It's now super hard. SRSLY. --- logic/net/CacheDownload.cpp | 62 +++++++++++++++++++++++++++------------------ logic/net/CacheDownload.h | 2 ++ 2 files changed, 39 insertions(+), 25 deletions(-) (limited to 'logic/net') diff --git a/logic/net/CacheDownload.cpp b/logic/net/CacheDownload.cpp index 17fd7f2a..8a8d00f0 100644 --- a/logic/net/CacheDownload.cpp +++ b/logic/net/CacheDownload.cpp @@ -33,8 +33,10 @@ CacheDownload::CacheDownload(QUrl url, MetaEntryPtr entry) void CacheDownload::start() { + m_status = Job_InProgress; if (!m_entry->stale) { + m_status = Job_Finished; emit succeeded(m_index_within_job); return; } @@ -43,12 +45,14 @@ void CacheDownload::start() if (!ensureFilePathExists(m_target_path)) { QLOG_ERROR() << "Could not create folder for " + m_target_path; + m_status = Job_Failed; emit failed(m_index_within_job); return; } - if(!m_output_file.open(QIODevice::WriteOnly)) + if (!m_output_file.open(QIODevice::WriteOnly)) { QLOG_ERROR() << "Could not open " + m_target_path + " for writing"; + m_status = Job_Failed; emit failed(m_index_within_job); return; } @@ -90,12 +94,21 @@ void CacheDownload::downloadError(QNetworkReply::NetworkError error) void CacheDownload::downloadFinished() { // if the download succeeded - if (m_status != Job_Failed) + if (m_status == Job_Failed) + { + m_output_file.cancelWriting(); + m_reply.reset(); + m_status = Job_Failed; + emit failed(m_index_within_job); + return; + } + + if (wroteAnyData) { // nothing went wrong... - m_status = Job_Finished; if (m_output_file.commit()) { + m_status = Job_Finished; m_entry->md5sum = md5sum.result().toHex().constData(); } else @@ -103,44 +116,43 @@ void CacheDownload::downloadFinished() QLOG_ERROR() << "Failed to commit changes to " << m_target_path; m_output_file.cancelWriting(); m_reply.reset(); + m_status = Job_Failed; emit failed(m_index_within_job); return; } - - QFileInfo output_file_info(m_target_path); - - m_entry->etag = m_reply->rawHeader("ETag").constData(); - if (m_reply->hasRawHeader("Last-Modified")) - { - m_entry->remote_changed_timestamp = m_reply->rawHeader("Last-Modified").constData(); - } - m_entry->local_changed_timestamp = - output_file_info.lastModified().toUTC().toMSecsSinceEpoch(); - m_entry->stale = false; - MMC->metacache()->updateEntry(m_entry); - - m_reply.reset(); - emit succeeded(m_index_within_job); - return; } - // else the download failed else { - m_output_file.cancelWriting(); - m_reply.reset(); - emit failed(m_index_within_job); - return; + m_status = Job_Finished; + } + + QFileInfo output_file_info(m_target_path); + + m_entry->etag = m_reply->rawHeader("ETag").constData(); + if (m_reply->hasRawHeader("Last-Modified")) + { + m_entry->remote_changed_timestamp = m_reply->rawHeader("Last-Modified").constData(); } + m_entry->local_changed_timestamp = + output_file_info.lastModified().toUTC().toMSecsSinceEpoch(); + m_entry->stale = false; + MMC->metacache()->updateEntry(m_entry); + + m_reply.reset(); + emit succeeded(m_index_within_job); + return; } void CacheDownload::downloadReadyRead() { QByteArray ba = m_reply->readAll(); md5sum.addData(ba); - if(m_output_file.write(ba) != ba.size()) + if (m_output_file.write(ba) != ba.size()) { QLOG_ERROR() << "Failed writing into " + m_target_path; + m_status = Job_Failed; m_reply->abort(); emit failed(m_index_within_job); } + wroteAnyData = true; } diff --git a/logic/net/CacheDownload.h b/logic/net/CacheDownload.h index 921e231b..48be1dae 100644 --- a/logic/net/CacheDownload.h +++ b/logic/net/CacheDownload.h @@ -33,6 +33,8 @@ public: /// the hash-as-you-download QCryptographicHash md5sum; + bool wroteAnyData = false; + public: explicit CacheDownload(QUrl url, MetaEntryPtr entry); static CacheDownloadPtr make(QUrl url, MetaEntryPtr entry) -- cgit From a774b3d24816aa0010e1d5f67b20acb99e7181ac Mon Sep 17 00:00:00 2001 From: Sky Date: Sun, 12 Jan 2014 18:28:42 +0000 Subject: Show Mojang service statuses in status bar --- logic/net/URLConstants.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'logic/net') diff --git a/logic/net/URLConstants.h b/logic/net/URLConstants.h index 9579198d..8cb1f3fd 100644 --- a/logic/net/URLConstants.h +++ b/logic/net/URLConstants.h @@ -31,4 +31,6 @@ const QString SKINS_BASE("skins.minecraft.net/MinecraftSkins/"); const QString AUTH_BASE("authserver.mojang.com/"); const QString FORGE_LEGACY_URL("http://files.minecraftforge.net/minecraftforge/json"); const QString FORGE_GRADLE_URL("http://files.minecraftforge.net/maven/net/minecraftforge/forge/json"); +const QString MOJANG_STATUS_URL("http://status.mojang.com/check"); +const QString MOJANG_STATUS_NEWS_URL("http://status.mojang.com/news"); } -- cgit From 4744ea07a892bc7066fbb21d394a2ce5efb0bdd9 Mon Sep 17 00:00:00 2001 From: Petr Mrázek Date: Mon, 13 Jan 2014 02:19:20 +0100 Subject: Small fix for stale files getting stuck in the cache --- logic/net/CacheDownload.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'logic/net') diff --git a/logic/net/CacheDownload.cpp b/logic/net/CacheDownload.cpp index 8a8d00f0..0022c361 100644 --- a/logic/net/CacheDownload.cpp +++ b/logic/net/CacheDownload.cpp @@ -58,11 +58,17 @@ void CacheDownload::start() } QLOG_INFO() << "Downloading " << m_url.toString(); QNetworkRequest request(m_url); - if (m_entry->remote_changed_timestamp.size()) - request.setRawHeader(QString("If-Modified-Since").toLatin1(), - m_entry->remote_changed_timestamp.toLatin1()); - if (m_entry->etag.size()) - request.setRawHeader(QString("If-None-Match").toLatin1(), m_entry->etag.toLatin1()); + + // check file consistency first. + QFile current(m_target_path); + if(current.exists() && current.size() != 0) + { + if (m_entry->remote_changed_timestamp.size()) + request.setRawHeader(QString("If-Modified-Since").toLatin1(), + m_entry->remote_changed_timestamp.toLatin1()); + if (m_entry->etag.size()) + request.setRawHeader(QString("If-None-Match").toLatin1(), m_entry->etag.toLatin1()); + } request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Cached)"); -- cgit From 3fabb11f4c59baffb14db00d338d9efe342e277e Mon Sep 17 00:00:00 2001 From: Petr Mrázek Date: Sat, 18 Jan 2014 22:11:33 +0100 Subject: Marginally improve OneSix offline mode launch While reconstructing assets, skip files that don't exist. Report missing OneSix native libraries. --- logic/net/CacheDownload.cpp | 21 +++++++++++++-------- logic/net/CacheDownload.h | 9 ++++++--- 2 files changed, 19 insertions(+), 11 deletions(-) (limited to 'logic/net') diff --git a/logic/net/CacheDownload.cpp b/logic/net/CacheDownload.cpp index 0022c361..d2a9bdee 100644 --- a/logic/net/CacheDownload.cpp +++ b/logic/net/CacheDownload.cpp @@ -40,7 +40,9 @@ void CacheDownload::start() emit succeeded(m_index_within_job); return; } - m_output_file.setFileName(m_target_path); + // create a new save file + m_output_file.reset(new QSaveFile(m_target_path)); + // if there already is a file and md5 checking is in effect and it can be opened if (!ensureFilePathExists(m_target_path)) { @@ -49,7 +51,7 @@ void CacheDownload::start() emit failed(m_index_within_job); return; } - if (!m_output_file.open(QIODevice::WriteOnly)) + if (!m_output_file->open(QIODevice::WriteOnly)) { QLOG_ERROR() << "Could not open " + m_target_path + " for writing"; m_status = Job_Failed; @@ -94,7 +96,7 @@ void CacheDownload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) void CacheDownload::downloadError(QNetworkReply::NetworkError error) { // error happened during download. - QLOG_ERROR() << "Failed" << m_url.toString() << "with reason" << error; + QLOG_ERROR() << "Failed " << m_url.toString() << " with reason " << error; m_status = Job_Failed; } void CacheDownload::downloadFinished() @@ -102,17 +104,17 @@ void CacheDownload::downloadFinished() // if the download succeeded if (m_status == Job_Failed) { - m_output_file.cancelWriting(); + m_output_file->cancelWriting(); m_reply.reset(); - m_status = Job_Failed; emit failed(m_index_within_job); return; } + // if we wrote any data to the save file, we try to commit the data to the real file. if (wroteAnyData) { // nothing went wrong... - if (m_output_file.commit()) + if (m_output_file->commit()) { m_status = Job_Finished; m_entry->md5sum = md5sum.result().toHex().constData(); @@ -120,7 +122,7 @@ void CacheDownload::downloadFinished() else { QLOG_ERROR() << "Failed to commit changes to " << m_target_path; - m_output_file.cancelWriting(); + m_output_file->cancelWriting(); m_reply.reset(); m_status = Job_Failed; emit failed(m_index_within_job); @@ -132,6 +134,9 @@ void CacheDownload::downloadFinished() m_status = Job_Finished; } + // then get rid of the save file + m_output_file.reset(); + QFileInfo output_file_info(m_target_path); m_entry->etag = m_reply->rawHeader("ETag").constData(); @@ -153,7 +158,7 @@ void CacheDownload::downloadReadyRead() { QByteArray ba = m_reply->readAll(); md5sum.addData(ba); - if (m_output_file.write(ba) != ba.size()) + if (m_output_file->write(ba) != ba.size()) { QLOG_ERROR() << "Failed writing into " + m_target_path; m_status = Job_Failed; diff --git a/logic/net/CacheDownload.h b/logic/net/CacheDownload.h index 48be1dae..154f5988 100644 --- a/logic/net/CacheDownload.h +++ b/logic/net/CacheDownload.h @@ -24,12 +24,12 @@ typedef std::shared_ptr CacheDownloadPtr; class CacheDownload : public NetAction { Q_OBJECT -public: +private: MetaEntryPtr m_entry; /// if saving to file, use the one specified in this string QString m_target_path; /// this is the output file, if any - QSaveFile m_output_file; + std::shared_ptr m_output_file; /// the hash-as-you-download QCryptographicHash md5sum; @@ -41,7 +41,10 @@ public: { return CacheDownloadPtr(new CacheDownload(url, entry)); } - + QString getTargetFilepath() + { + return m_target_path; + } protected slots: virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal); -- cgit