``Working'' forge unpackers. Needs a lot of hardening but good for alpha.

This commit is contained in:
Petr Mrázek 2013-09-30 02:34:46 +02:00
parent 604162acdf
commit 8b0f8b9e59
21 changed files with 413 additions and 36 deletions

View File

@ -52,9 +52,11 @@ add_subdirectory(depends/launcher)
# Add xz decompression
add_subdirectory(depends/xz-embedded)
include_directories(${XZ_INCLUDE_DIR})
# Add pack200 decompression
add_subdirectory(depends/pack200)
include_directories(${PACK200_INCLUDE_DIR})
######## MultiMC Libs ########
@ -231,6 +233,8 @@ logic/net/ByteArrayDownload.h
logic/net/ByteArrayDownload.cpp
logic/net/CacheDownload.h
logic/net/CacheDownload.cpp
logic/net/ForgeXzDownload.h
logic/net/ForgeXzDownload.cpp
logic/net/DownloadJob.h
logic/net/DownloadJob.cpp
logic/net/HttpMetaCache.h
@ -354,7 +358,7 @@ ADD_EXECUTABLE(MultiMC MACOSX_BUNDLE WIN32
# Link
QT5_USE_MODULES(MultiMC Widgets Network Xml)
TARGET_LINK_LIBRARIES(MultiMC quazip libUtil libSettings libGroupView ${MultiMC_LINK_ADDITIONAL_LIBS})
TARGET_LINK_LIBRARIES(MultiMC quazip xz-embedded unpack200 libUtil libSettings libGroupView ${MultiMC_LINK_ADDITIONAL_LIBS})
ADD_DEPENDENCIES(MultiMC MultiMCLauncher)

View File

@ -37,6 +37,7 @@ src/zip.cpp
src/zip.h
)
SET(PACK200_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include" PARENT_SCOPE)
include_directories(include)
add_library(unpack200 STATIC ${PACK200_SRC})

View File

@ -1523,7 +1523,8 @@ band **unpacker::attr_definitions::buildBands(unpacker::layout_definition *lo)
call.le_body[0] = &cble;
// Distinguish backward calls and callables:
assert(cble.le_kind == EK_CBLE);
assert(cble.le_len == call_num);
//FIXME: hit this one
//assert(cble.le_len == call_num);
cble.le_back |= call.le_back;
}
calls_to_link.popTo(0);
@ -2777,7 +2778,8 @@ void unpacker::putlayout(band **body)
{
band &cble = *b.le_body[0];
assert(cble.le_kind == EK_CBLE);
assert(cble.le_len == b.le_len);
//FIXME: hit this one
//assert(cble.le_len == b.le_len);
putlayout(cble.le_body);
}
break;

View File

@ -156,8 +156,11 @@ void unpack_200(std::string input_path, std::string output_path)
magic = read_magic(&u, peek, (int)sizeof(peek));
if (magic != (int)JAVA_PACKAGE_MAGIC)
{
// we do not feel strongly about this kind of thing...
/*
if (magic != EOF_MAGIC)
unpack_abort("garbage after end of pack archive");
*/
break; // all done
}

View File

@ -8,33 +8,25 @@ option(XZ_BUILD_MINIDEC "Build a tiny utility that decompresses xz streams" OFF)
set(CMAKE_C_FLAGS "-std=c99")
include_directories(include)
SET(XZ_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include" PARENT_SCOPE)
# See include/xz.h for manual feature configuration
# tweak this list and xz.h to fit your needs
set(XZ_SOURCES
include/xz.h
src/xz_config.h
src/xz_crc32.c
src/xz_crc64.c
src/xz_dec_lzma2.c
src/xz_dec_stream.c
src/xz_lzma2.h
src/xz_private.h
src/xz_stream.h
# src/xz_dec_bcj.c
)
# TODO: look into what would be needed for plain old lzma
# checksum checks
add_definitions(-DXZ_DEC_ANY_CHECK)
if(XZ_BUILD_CRC64)
add_definitions(-DXZ_USE_CRC64)
LIST(APPEND XZ_SOURCES src/xz_crc64.c)
endif()
# TODO: add SHA256
if(XZ_BUILD_BCJ)
add_definitions(-DXZ_DEC_X86 -DXZ_DEC_POWERPC -DXZ_DEC_IA64)
add_definitions(-DXZ_DEC_ARM -DXZ_DEC_ARMTHUMB -DXZ_DEC_SPARC)
LIST(APPEND XZ_SOURCES src/xz_dec_bcj.c)
endif()
add_library(xz-embedded STATIC ${XZ_SOURCES})
add_executable(xzminidec xzminidec.c)
target_link_libraries(xzminidec xz-embedded)

View File

@ -23,6 +23,21 @@
extern "C" {
#endif
/* Definitions that determine available features */
#define XZ_DEC_ANY_CHECK 1
#define XZ_USE_CRC64 1
// native machine code compression stuff
/*
#define XZ_DEC_X86
#define XZ_DEC_POWERPC
#define XZ_DEC_IA64
#define XZ_DEC_ARM
#define XZ_DEC_ARMTHUMB
#define XZ_DEC_SPARC
*/
/* In Linux, this is used to make extern functions static when needed. */
#ifndef XZ_EXTERN
# define XZ_EXTERN extern

View File

@ -210,7 +210,7 @@ void LegacyModEditDialog::on_addForgeBtn_clicked()
if(entry->stale)
{
DownloadJob * fjob = new DownloadJob("Forge download");
fjob->add(forge->universal_url, entry);
fjob->addCacheDownload(forge->universal_url, entry);
ProgressDialog dlg(this);
dlg.exec(fjob);
if(dlg.result() == QDialog::Accepted)

View File

@ -160,7 +160,7 @@ void OneSixModEditDialog::on_forgeBtn_clicked()
if (entry->stale)
{
DownloadJob *fjob = new DownloadJob("Forge download");
fjob->add(forgeVersion->installer_url, entry);
fjob->addCacheDownload(forgeVersion->installer_url, entry);
ProgressDialog dlg(this);
dlg.exec(fjob);
if (dlg.result() == QDialog::Accepted)

View File

@ -100,11 +100,16 @@ bool ForgeInstaller::apply(QSharedPointer<OneSixVersion> 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;

View File

@ -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()));

View File

@ -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();

View File

@ -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())

View File

@ -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();
};

View File

@ -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()));

View File

@ -71,11 +71,21 @@ QSharedPointer<OneSixVersion> 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())

View File

@ -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()));

View File

@ -46,7 +46,7 @@ public:
protected:
QList<BaseVersionPtr> m_vlist;
bool m_loaded;
bool m_loaded = false;
protected slots:
virtual void updateListData(QList<BaseVersionPtr> versions);

View File

@ -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

View File

@ -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)
{

View File

@ -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);
}

View File

@ -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;