summaryrefslogtreecommitdiffstats
path: root/logic/net
diff options
context:
space:
mode:
authorPetr Mrázek <peterix@gmail.com>2013-09-30 02:34:46 +0200
committerPetr Mrázek <peterix@gmail.com>2013-09-30 02:34:46 +0200
commit8b0f8b9e597eb50ff9323037fd5fa1b9e330c467 (patch)
tree3b6613d6c7672803c371ef7043cf35b713b975b1 /logic/net
parent604162acdf5283a9759c1b3ce9e90887a6599ce7 (diff)
downloadMultiMC-8b0f8b9e597eb50ff9323037fd5fa1b9e330c467.tar
MultiMC-8b0f8b9e597eb50ff9323037fd5fa1b9e330c467.tar.gz
MultiMC-8b0f8b9e597eb50ff9323037fd5fa1b9e330c467.tar.lz
MultiMC-8b0f8b9e597eb50ff9323037fd5fa1b9e330c467.tar.xz
MultiMC-8b0f8b9e597eb50ff9323037fd5fa1b9e330c467.zip
``Working'' forge unpackers. Needs a lot of hardening but good for alpha.
Diffstat (limited to 'logic/net')
-rw-r--r--logic/net/DownloadJob.cpp16
-rw-r--r--logic/net/DownloadJob.h8
-rw-r--r--logic/net/ForgeXzDownload.cpp277
-rw-r--r--logic/net/ForgeXzDownload.h35
4 files changed, 330 insertions, 6 deletions
diff --git a/logic/net/DownloadJob.cpp b/logic/net/DownloadJob.cpp
index 8da1f39b..03a69555 100644
--- a/logic/net/DownloadJob.cpp
+++ b/logic/net/DownloadJob.cpp
@@ -7,7 +7,7 @@
#include <QDebug>
-ByteArrayDownloadPtr DownloadJob::add(QUrl url)
+ByteArrayDownloadPtr DownloadJob::addByteArrayDownload(QUrl url)
{
ByteArrayDownloadPtr ptr(new ByteArrayDownload(url));
ptr->index_within_job = downloads.size();
@@ -17,7 +17,7 @@ ByteArrayDownloadPtr DownloadJob::add(QUrl url)
return ptr;
}
-FileDownloadPtr DownloadJob::add(QUrl url, QString rel_target_path)
+FileDownloadPtr DownloadJob::addFileDownload(QUrl url, QString rel_target_path)
{
FileDownloadPtr ptr(new FileDownload(url, rel_target_path));
ptr->index_within_job = downloads.size();
@@ -27,7 +27,7 @@ FileDownloadPtr DownloadJob::add(QUrl url, QString rel_target_path)
return ptr;
}
-CacheDownloadPtr DownloadJob::add(QUrl url, MetaEntryPtr entry)
+CacheDownloadPtr DownloadJob::addCacheDownload(QUrl url, MetaEntryPtr entry)
{
CacheDownloadPtr ptr(new CacheDownload(url, entry));
ptr->index_within_job = downloads.size();
@@ -37,6 +37,16 @@ CacheDownloadPtr DownloadJob::add(QUrl url, MetaEntryPtr entry)
return ptr;
}
+ForgeXzDownloadPtr DownloadJob::addForgeXzDownload(QUrl url, MetaEntryPtr entry)
+{
+ ForgeXzDownloadPtr ptr(new ForgeXzDownload(url, entry));
+ ptr->index_within_job = downloads.size();
+ downloads.append(ptr);
+ parts_progress.append(part_info());
+ total_progress++;
+ return ptr;
+}
+
void DownloadJob::partSucceeded(int index)
{
// do progress. all slots are 1 in size at least
diff --git a/logic/net/DownloadJob.h b/logic/net/DownloadJob.h
index 5d5ba01a..8c32950a 100644
--- a/logic/net/DownloadJob.h
+++ b/logic/net/DownloadJob.h
@@ -5,6 +5,7 @@
#include "FileDownload.h"
#include "CacheDownload.h"
#include "HttpMetaCache.h"
+#include "ForgeXzDownload.h"
#include "logic/tasks/ProgressProvider.h"
class DownloadJob;
@@ -20,9 +21,10 @@ public:
explicit DownloadJob(QString job_name)
:ProgressProvider(), m_job_name(job_name){};
- ByteArrayDownloadPtr add(QUrl url);
- FileDownloadPtr add(QUrl url, QString rel_target_path);
- CacheDownloadPtr add(QUrl url, MetaEntryPtr entry);
+ ByteArrayDownloadPtr addByteArrayDownload(QUrl url);
+ FileDownloadPtr addFileDownload(QUrl url, QString rel_target_path);
+ CacheDownloadPtr addCacheDownload(QUrl url, MetaEntryPtr entry);
+ ForgeXzDownloadPtr addForgeXzDownload(QUrl url, MetaEntryPtr entry);
DownloadPtr operator[](int index)
{
diff --git a/logic/net/ForgeXzDownload.cpp b/logic/net/ForgeXzDownload.cpp
new file mode 100644
index 00000000..b7e7eedf
--- /dev/null
+++ b/logic/net/ForgeXzDownload.cpp
@@ -0,0 +1,277 @@
+#include "MultiMC.h"
+#include "ForgeXzDownload.h"
+#include <pathutils.h>
+
+#include <QCryptographicHash>
+#include <QFileInfo>
+#include <QDateTime>
+#include <QDebug>
+
+ForgeXzDownload::ForgeXzDownload(QUrl url, MetaEntryPtr entry)
+ : Download()
+{
+ QString urlstr = url.toString();
+ urlstr.append(".pack.xz");
+ m_url = QUrl(urlstr);
+ m_entry = entry;
+ m_target_path = entry->getFullPath();
+ m_status = Job_NotStarted;
+ m_opened_for_saving = false;
+}
+
+void ForgeXzDownload::start()
+{
+ if (!m_entry->stale)
+ {
+ emit succeeded(index_within_job);
+ return;
+ }
+ // can we actually create the real, final file?
+ if (!ensureFilePathExists(m_target_path))
+ {
+ emit failed(index_within_job);
+ return;
+ }
+ qDebug() << "Downloading " << m_url.toString();
+ QNetworkRequest request(m_url);
+ request.setRawHeader(QString("If-None-Match").toLatin1(), m_entry->etag.toLatin1());
+
+ auto worker = MMC->qnam();
+ QNetworkReply *rep = worker->get(request);
+
+ m_reply = QSharedPointer<QNetworkReply>(rep, &QObject::deleteLater);
+ connect(rep, SIGNAL(downloadProgress(qint64, qint64)),
+ SLOT(downloadProgress(qint64, qint64)));
+ connect(rep, SIGNAL(finished()), SLOT(downloadFinished()));
+ connect(rep, SIGNAL(error(QNetworkReply::NetworkError)),
+ SLOT(downloadError(QNetworkReply::NetworkError)));
+ connect(rep, SIGNAL(readyRead()), SLOT(downloadReadyRead()));
+}
+
+void ForgeXzDownload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
+{
+ emit progress(index_within_job, bytesReceived, bytesTotal);
+}
+
+void ForgeXzDownload::downloadError(QNetworkReply::NetworkError error)
+{
+ // error happened during download.
+ // TODO: log the reason why
+ m_status = Job_Failed;
+}
+
+void ForgeXzDownload::downloadFinished()
+{
+ // if the download succeeded
+ if (m_status != Job_Failed)
+ {
+ // nothing went wrong...
+ m_status = Job_Finished;
+ if (m_opened_for_saving)
+ {
+ // we actually downloaded something! process and isntall it
+ decompressAndInstall();
+ return;
+ }
+ else
+ {
+ // something bad happened
+ m_pack200_xz_file.remove();
+ m_reply.clear();
+ emit failed(index_within_job);
+ return;
+ }
+ }
+ // else the download failed
+ else
+ {
+ m_pack200_xz_file.close();
+ m_pack200_xz_file.remove();
+ m_reply.clear();
+ emit failed(index_within_job);
+ return;
+ }
+}
+
+void ForgeXzDownload::downloadReadyRead()
+{
+
+ if (!m_opened_for_saving)
+ {
+ if (!m_pack200_xz_file.open())
+ {
+ /*
+ * Can't open the file... the job failed
+ */
+ m_reply->abort();
+ emit failed(index_within_job);
+ return;
+ }
+ m_opened_for_saving = true;
+ }
+ m_pack200_xz_file.write(m_reply->readAll());
+}
+
+#include "xz.h"
+#include "unpack200.h"
+
+const size_t buffer_size = 8196;
+
+void ForgeXzDownload::decompressAndInstall()
+{
+ // rewind the downloaded temp file
+ m_pack200_xz_file.seek(0);
+ // de-xz'd file
+ QTemporaryFile pack200_file;
+ pack200_file.open();
+
+ bool xz_success = false;
+ // first, de-xz
+ {
+ uint8_t in[buffer_size];
+ uint8_t out[buffer_size];
+ struct xz_buf b;
+ struct xz_dec *s;
+ enum xz_ret ret;
+ xz_crc32_init();
+ xz_crc64_init();
+ s = xz_dec_init(XZ_DYNALLOC, 1 << 26);
+ if (s == nullptr)
+ {
+ xz_dec_end(s);
+ emit failed(index_within_job);
+ return;
+ }
+ b.in = in;
+ b.in_pos = 0;
+ b.in_size = 0;
+ b.out = out;
+ b.out_pos = 0;
+ b.out_size = buffer_size;
+ while (!xz_success)
+ {
+ if (b.in_pos == b.in_size)
+ {
+ b.in_size = m_pack200_xz_file.read((char*)in, sizeof(in));
+ b.in_pos = 0;
+ }
+
+ ret = xz_dec_run(s, &b);
+
+ if (b.out_pos == sizeof(out))
+ {
+ if (pack200_file.write((char*)out, b.out_pos) != b.out_pos)
+ {
+ // msg = "Write error\n";
+ xz_dec_end(s);
+ emit failed(index_within_job);
+ return;
+ }
+
+ b.out_pos = 0;
+ }
+
+ if (ret == XZ_OK)
+ continue;
+
+ if (ret == XZ_UNSUPPORTED_CHECK)
+ {
+ // unsupported check. this is OK, but we should log this
+ continue;
+ }
+
+ if (pack200_file.write((char*)out, b.out_pos) != b.out_pos )
+ {
+ // write error
+ pack200_file.close();
+ xz_dec_end(s);
+ return;
+ }
+
+ switch (ret)
+ {
+ case XZ_STREAM_END:
+ xz_dec_end(s);
+ xz_success = true;
+ break;
+
+ case XZ_MEM_ERROR:
+ qDebug() << "Memory allocation failed\n";
+ xz_dec_end(s);
+ emit failed(index_within_job);
+ return;
+
+ case XZ_MEMLIMIT_ERROR:
+ qDebug() << "Memory usage limit reached\n";
+ xz_dec_end(s);
+ emit failed(index_within_job);
+ return;
+
+ case XZ_FORMAT_ERROR:
+ qDebug() << "Not a .xz file\n";
+ xz_dec_end(s);
+ emit failed(index_within_job);
+ return;
+
+ case XZ_OPTIONS_ERROR:
+ qDebug() << "Unsupported options in the .xz headers\n";
+ xz_dec_end(s);
+ emit failed(index_within_job);
+ return;
+
+ case XZ_DATA_ERROR:
+ case XZ_BUF_ERROR:
+ qDebug() << "File is corrupt\n";
+ xz_dec_end(s);
+ emit failed(index_within_job);
+ return;
+
+ default:
+ qDebug() << "Bug!\n";
+ xz_dec_end(s);
+ emit failed(index_within_job);
+ return;
+ }
+ }
+ }
+
+ // revert pack200
+ pack200_file.close();
+ QString pack_name = pack200_file.fileName();
+ try
+ {
+ unpack_200(pack_name.toStdString(), m_target_path.toStdString());
+ }
+ catch(std::runtime_error & err)
+ {
+ qDebug() << "Error unpacking " << pack_name.toUtf8() << " : " << err.what();
+ QFile f(m_target_path);
+ if(f.exists())
+ f.remove();
+ emit failed(index_within_job);
+ return;
+ }
+
+ QFile jar_file(m_target_path);
+
+ if (!jar_file.open(QIODevice::ReadOnly))
+ {
+ jar_file.remove();
+ emit failed(index_within_job);
+ return;
+ }
+ m_entry->md5sum = QCryptographicHash::hash(jar_file.readAll(), QCryptographicHash::Md5)
+ .toHex()
+ .constData();
+ jar_file.close();
+
+ QFileInfo output_file_info(m_target_path);
+ m_entry->etag = m_reply->rawHeader("ETag").constData();
+ m_entry->last_changed_timestamp =
+ output_file_info.lastModified().toUTC().toMSecsSinceEpoch();
+ m_entry->stale = false;
+ MMC->metacache()->updateEntry(m_entry);
+
+ m_reply.clear();
+ emit succeeded(index_within_job);
+}
diff --git a/logic/net/ForgeXzDownload.h b/logic/net/ForgeXzDownload.h
new file mode 100644
index 00000000..8cb47783
--- /dev/null
+++ b/logic/net/ForgeXzDownload.h
@@ -0,0 +1,35 @@
+#pragma once
+
+#include "Download.h"
+#include "HttpMetaCache.h"
+#include <QFile>
+#include <QTemporaryFile>
+
+class ForgeXzDownload : public Download
+{
+ Q_OBJECT
+public:
+ MetaEntryPtr m_entry;
+ /// is the saving file already open?
+ bool m_opened_for_saving;
+ /// if saving to file, use the one specified in this string
+ QString m_target_path;
+ /// this is the output file, if any
+ QTemporaryFile m_pack200_xz_file;
+
+public:
+ explicit ForgeXzDownload(QUrl url, MetaEntryPtr entry);
+
+protected slots:
+ virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
+ virtual void downloadError(QNetworkReply::NetworkError error);
+ virtual void downloadFinished();
+ virtual void downloadReadyRead();
+
+public slots:
+ virtual void start();
+private:
+ void decompressAndInstall();
+};
+
+typedef QSharedPointer<ForgeXzDownload> ForgeXzDownloadPtr;