From 8b0f8b9e597eb50ff9323037fd5fa1b9e330c467 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Mon, 30 Sep 2013 02:34:46 +0200 Subject: ``Working'' forge unpackers. Needs a lot of hardening but good for alpha. --- logic/ForgeInstaller.cpp | 5 + logic/LegacyUpdate.cpp | 2 +- logic/OneSixAssets.cpp | 4 +- logic/OneSixLibrary.cpp | 14 +- logic/OneSixLibrary.h | 8 +- logic/OneSixUpdate.cpp | 9 +- logic/OneSixVersion.cpp | 12 +- logic/lists/ForgeVersionList.cpp | 2 +- logic/lists/MinecraftVersionList.h | 2 +- logic/net/DownloadJob.cpp | 16 ++- logic/net/DownloadJob.h | 8 +- logic/net/ForgeXzDownload.cpp | 277 +++++++++++++++++++++++++++++++++++++ logic/net/ForgeXzDownload.h | 35 +++++ 13 files changed, 377 insertions(+), 17 deletions(-) create mode 100644 logic/net/ForgeXzDownload.cpp create mode 100644 logic/net/ForgeXzDownload.h (limited to 'logic') diff --git a/logic/ForgeInstaller.cpp b/logic/ForgeInstaller.cpp index bcba00e9..9ae3f1e1 100644 --- a/logic/ForgeInstaller.cpp +++ b/logic/ForgeInstaller.cpp @@ -100,11 +100,16 @@ bool ForgeInstaller::apply(QSharedPointer to) for (auto lib : m_forge_version->libraries) { QString libName = lib->name(); + // WARNING: This could actually break. // if this is the actual forge lib, set an absolute url for the download if (libName.contains("minecraftforge")) { lib->setAbsoluteUrl(m_universal_url); } + else if (libName.contains("scala")) + { + lib->setHint("forge-pack-xz"); + } if (blacklist.contains(libName)) continue; diff --git a/logic/LegacyUpdate.cpp b/logic/LegacyUpdate.cpp index 84d3d830..d8e622dd 100644 --- a/logic/LegacyUpdate.cpp +++ b/logic/LegacyUpdate.cpp @@ -228,7 +228,7 @@ void LegacyUpdate::jarStart() urlstr += intended_version_id + "/" + intended_version_id + ".jar"; auto dljob = new DownloadJob("Minecraft.jar for version " + intended_version_id); - dljob->add(QUrl(urlstr), inst->defaultBaseJar()); + dljob->addFileDownload(QUrl(urlstr), inst->defaultBaseJar()); legacyDownloadJob.reset(dljob); connect(dljob, SIGNAL(succeeded()), SLOT(jarFinished())); connect(dljob, SIGNAL(failed()), SLOT(jarFailed())); diff --git a/logic/OneSixAssets.cpp b/logic/OneSixAssets.cpp index 5bdd29d7..ca7a5534 100644 --- a/logic/OneSixAssets.cpp +++ b/logic/OneSixAssets.cpp @@ -113,7 +113,7 @@ void OneSixAssets::fetchXMLFinished() auto entry = metacache->resolveEntry("assets", keyStr, etagStr); if(entry->stale) { - job->add(QUrl(prefix + keyStr), entry); + job->addCacheDownload(QUrl(prefix + keyStr), entry); } } if(job->size()) @@ -130,7 +130,7 @@ void OneSixAssets::fetchXMLFinished() void OneSixAssets::start() { auto job = new DownloadJob("Assets index"); - job->add(QUrl ( "http://s3.amazonaws.com/Minecraft.Resources/" )); + job->addByteArrayDownload(QUrl ( "http://s3.amazonaws.com/Minecraft.Resources/" )); connect ( job, SIGNAL(succeeded()), SLOT ( fetchXMLFinished() ) ); index_job.reset ( job ); job->start(); diff --git a/logic/OneSixLibrary.cpp b/logic/OneSixLibrary.cpp index 8da1fde7..63d42646 100644 --- a/logic/OneSixLibrary.cpp +++ b/logic/OneSixLibrary.cpp @@ -105,12 +105,24 @@ QString OneSixLibrary::absoluteUrl() return m_absolute_url; } +void OneSixLibrary::setHint(QString hint) +{ + m_hint = hint; +} + +QString OneSixLibrary::hint() +{ + return m_hint; +} + QJsonObject OneSixLibrary::toJson() { QJsonObject libRoot; libRoot.insert("name", m_name); if(m_absolute_url.size()) - libRoot.insert("MMC-absulute_url", m_absolute_url); + libRoot.insert("MMC-absoluteUrl", m_absolute_url); + if(m_hint.size()) + libRoot.insert("MMC-hint", m_hint); if(m_base_url != "https://s3.amazonaws.com/Minecraft.Download/libraries/") libRoot.insert("url", m_base_url); if (isNative() && m_native_suffixes.size()) diff --git a/logic/OneSixLibrary.h b/logic/OneSixLibrary.h index f3106483..2a16d8e1 100644 --- a/logic/OneSixLibrary.h +++ b/logic/OneSixLibrary.h @@ -19,6 +19,8 @@ private: // custom values /// absolute URL. takes precedence over m_download_path, if defined QString m_absolute_url; + /// download hint - how to actually get the library + QString m_hint; // derived values used for real things /// a decent name fit for display @@ -91,8 +93,12 @@ public: QString downloadUrl(); /// Get the relative path where the library should be saved QString storagePath(); - + /// set an absolute URL for the library. This is an MMC extension. void setAbsoluteUrl(QString absolute_url); QString absoluteUrl(); + + /// set a hint about how to treat the library. This is an MMC extension. + void setHint(QString hint); + QString hint(); }; diff --git a/logic/OneSixUpdate.cpp b/logic/OneSixUpdate.cpp index 73bd9403..41d8f599 100644 --- a/logic/OneSixUpdate.cpp +++ b/logic/OneSixUpdate.cpp @@ -75,7 +75,7 @@ void OneSixUpdate::versionFileStart() QString urlstr("http://s3.amazonaws.com/Minecraft.Download/versions/"); urlstr += targetVersion->descriptor() + "/" + targetVersion->descriptor() + ".json"; auto job = new DownloadJob("Version index"); - job->add(QUrl(urlstr)); + job->addByteArrayDownload(QUrl(urlstr)); specificVersionDownloadJob.reset(job); connect(specificVersionDownloadJob.data(), SIGNAL(succeeded()), SLOT(versionFileFinished())); @@ -158,7 +158,7 @@ void OneSixUpdate::jarlibStart() targetstr += version->id + "/" + version->id + ".jar"; auto job = new DownloadJob("Libraries for instance " + inst->name()); - job->add(QUrl(urlstr), targetstr); + job->addFileDownload(QUrl(urlstr), targetstr); jarlibDownloadJob.reset(job); auto libs = version->getActiveNativeLibs(); @@ -171,7 +171,10 @@ void OneSixUpdate::jarlibStart() auto entry = metacache->resolveEntry("libraries", lib->storagePath()); if (entry->stale) { - jarlibDownloadJob->add(download_path, entry); + if(lib->hint() == "forge-pack-xz") + jarlibDownloadJob->addForgeXzDownload(download_path, entry); + else + jarlibDownloadJob->addCacheDownload(download_path, entry); } } connect(jarlibDownloadJob.data(), SIGNAL(succeeded()), SLOT(jarlibFinished())); diff --git a/logic/OneSixVersion.cpp b/logic/OneSixVersion.cpp index 663d903a..64a47562 100644 --- a/logic/OneSixVersion.cpp +++ b/logic/OneSixVersion.cpp @@ -71,11 +71,21 @@ QSharedPointer fromJsonV4(QJsonObject root, { library->setBaseUrl(urlVal.toString()); } - auto urlAbsVal = libObj.value("MMC-absulute_url"); + auto hintVal = libObj.value("MMC-hint"); + if (hintVal.isString()) + { + library->setHint(hintVal.toString()); + } + auto urlAbsVal = libObj.value("MMC-absoluteUrl"); + auto urlAbsuVal = libObj.value("MMC-absulute_url"); // compatibility if (urlAbsVal.isString()) { library->setAbsoluteUrl(urlAbsVal.toString()); } + else if(urlAbsuVal.isString()) + { + library->setAbsoluteUrl(urlAbsuVal.toString()); + } // Extract excludes (if any) auto extractVal = libObj.value("extract"); if (extractVal.isObject()) diff --git a/logic/lists/ForgeVersionList.cpp b/logic/lists/ForgeVersionList.cpp index 721f2c0a..e2adbf3b 100644 --- a/logic/lists/ForgeVersionList.cpp +++ b/logic/lists/ForgeVersionList.cpp @@ -162,7 +162,7 @@ void ForgeListLoadTask::executeTask() auto job = new DownloadJob("Version index"); // we do not care if the version is stale or not. auto forgeListEntry = MMC->metacache()->resolveEntry("minecraftforge", "list.json"); - job->add(QUrl(JSON_URL), forgeListEntry); + job->addCacheDownload(QUrl(JSON_URL), forgeListEntry); listJob.reset(job); connect(listJob.data(), SIGNAL(succeeded()), SLOT(list_downloaded())); connect(listJob.data(), SIGNAL(failed()), SLOT(versionFileFailed())); diff --git a/logic/lists/MinecraftVersionList.h b/logic/lists/MinecraftVersionList.h index fb28ddfe..ed68efbb 100644 --- a/logic/lists/MinecraftVersionList.h +++ b/logic/lists/MinecraftVersionList.h @@ -46,7 +46,7 @@ public: protected: QList m_vlist; - bool m_loaded; + bool m_loaded = false; protected slots: virtual void updateListData(QList versions); 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 -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 + +#include +#include +#include +#include + +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(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 +#include + +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 ForgeXzDownloadPtr; -- cgit v1.2.3