summaryrefslogtreecommitdiffstats
path: root/api/logic/modplatform/FtbPackInstallTask.cpp
blob: bedf39420cf4602dd7ab6607821f32f49814ceae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "FtbPackInstallTask.h"
#include "Env.h"
#include "MMCZip.h"
#include "QtConcurrent"

FtbPackInstallTask::FtbPackInstallTask(FtbPackDownloader *downloader, SettingsObjectPtr settings,
						   const QString &stagingPath, const QString &instName, const QString &instIcon, const QString &instGroup) :
	m_globalSettings(settings), m_stagingPath(stagingPath), m_instName(instName), m_instIcon(instIcon), m_instGroup(instGroup)
{
	m_downloader = downloader;
}

void FtbPackInstallTask::executeTask() {
	downloadPack();
}

void FtbPackInstallTask::downloadPack(){
	FtbModpack toInstall = m_downloader->getSelectedPack();
	setStatus(tr("Installing new FTB Pack %1").arg(toInstall.name));

	auto entry = ENV.metacache()->resolveEntry("general", "FTBPack/" + toInstall.name);
	m_downloader->downloadSelected(entry);

	connect(m_downloader, &FtbPackDownloader::downloadSucceded, this, &FtbPackInstallTask::onDownloadSucceeded);
	connect(m_downloader, &FtbPackDownloader::downloadProgress, this, &FtbPackInstallTask::onDownloadProgress);
	connect(m_downloader, &FtbPackDownloader::downloadFailed, this,&FtbPackInstallTask::onDownloadFailed);
}

void FtbPackInstallTask::onDownloadSucceeded(QString archivePath){
	qDebug() << "Download succeeded!";
	unzip(archivePath);
}

void FtbPackInstallTask::onDownloadFailed(QString reason) {
	emitFailed(reason);
}

void FtbPackInstallTask::onDownloadProgress(qint64 current, qint64 total){
	progress(current, total);
}

void FtbPackInstallTask::unzip(QString archivePath) {
	setStatus(QString("Extracting modpack from %1").arg(archivePath));
	QDir extractDir(m_stagingPath);

	m_packZip.reset(new QuaZip(archivePath));
	if(!m_packZip->open(QuaZip::mdUnzip)) {
		emitFailed(tr("Failed to open modpack file %1!").arg(archivePath));
		return;
	}

	m_extractFuture = QtConcurrent::run(QThreadPool::globalInstance(), MMCZip::extractSubDir, m_packZip.get(), QString("/"), extractDir.absolutePath());
	connect(&m_extractFutureWatcher, &QFutureWatcher<QStringList>::finished, this, &FtbPackInstallTask::onUnzipFinished);
	connect(&m_extractFutureWatcher, &QFutureWatcher<QStringList>::canceled, this, &FtbPackInstallTask::onUnzipCanceled);
	m_extractFutureWatcher.setFuture(m_extractFuture);
}

void FtbPackInstallTask::onUnzipFinished() {
	qDebug() << "Unzipped:" << m_stagingPath;
	emitSucceeded();
}

void FtbPackInstallTask::onUnzipCanceled() {
	emitAborted();
}