summaryrefslogtreecommitdiffstats
path: root/api/logic/modplatform/ftb/FtbPackInstallTask.cpp
blob: 4962bcac92a41cc86af179ff579123cb76f34c2a (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "FtbPackInstallTask.h"
#include "Env.h"
#include "MMCZip.h"
#include "QtConcurrent"
#include "BaseInstance.h"
#include "FileSystem.h"
#include "settings/INISettingsObject.h"
#include "minecraft/MinecraftInstance.h"
#include "minecraft/ComponentList.h"
#include "minecraft/GradleSpecifier.h"

#include "net/URLConstants.h"

FtbPackInstallTask::FtbPackInstallTask(FtbModpack pack, QString version)
{
    m_pack = pack;
    m_version = version;
}

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

void FtbPackInstallTask::downloadPack()
{
    setStatus(tr("Downloading zip for %1").arg(m_pack.name));

    auto packoffset = QString("%1/%2/%3").arg(m_pack.dir, m_version.replace(".", "_"), m_pack.file);
    auto entry = ENV.metacache()->resolveEntry("FTBPacks", packoffset);
    NetJob *job = new NetJob("Download FTB Pack");

    entry->setStale(true);
    QString url;
    if(m_pack.type == FtbPackType::Private)
    {
        url = QString(URLConstants::FTB_CDN_BASE_URL + "privatepacks/%1").arg(packoffset);
    }
    else
    {
        url = QString(URLConstants::FTB_CDN_BASE_URL + "modpacks/%1").arg(packoffset);
    }
    job->addNetAction(Net::Download::makeCached(url, entry));
    archivePath = entry->getFullPath();

    netJobContainer.reset(job);
    connect(job, &NetJob::succeeded, this, &FtbPackInstallTask::onDownloadSucceeded);
    connect(job, &NetJob::failed, this, &FtbPackInstallTask::onDownloadFailed);
    connect(job, &NetJob::progress, this, &FtbPackInstallTask::onDownloadProgress);
    job->start();

    progress(1, 4);
}

void FtbPackInstallTask::onDownloadSucceeded()
{
    abortable = false;
    unzip();
}

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

void FtbPackInstallTask::onDownloadProgress(qint64 current, qint64 total)
{
    abortable = true;
    progress(current, total * 4);
    setStatus(tr("Downloading zip for %1 (%2%)").arg(m_pack.name).arg(current / 10));
}

void FtbPackInstallTask::unzip()
{
    progress(2, 4);
    setStatus(tr("Extracting modpack"));
    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::extractDir, archivePath, extractDir.absolutePath() + "/unzip");
    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()
{
    install();
}

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

void FtbPackInstallTask::install()
{
    progress(3, 4);
    setStatus(tr("Installing modpack"));
    QDir unzipMcDir(m_stagingPath + "/unzip/minecraft");
    if(unzipMcDir.exists())
    {
        //ok, found minecraft dir, move contents to instance dir
        if(!QDir().rename(m_stagingPath + "/unzip/minecraft", m_stagingPath + "/.minecraft"))
        {
            emitFailed(tr("Failed to move unzipped minecraft!"));
            return;
        }
    }

    QString instanceConfigPath = FS::PathCombine(m_stagingPath, "instance.cfg");
    auto instanceSettings = std::make_shared<INISettingsObject>(instanceConfigPath);
    instanceSettings->registerSetting("InstanceType", "Legacy");
    instanceSettings->set("InstanceType", "OneSix");

    MinecraftInstance instance(m_globalSettings, instanceSettings, m_stagingPath);
    auto components = instance.getComponentList();
    components->buildingFromScratch();
    components->setComponentVersion("net.minecraft", m_pack.mcVersion, true);

    bool fallback = true;

    //handle different versions
    QFile packJson(m_stagingPath + "/.minecraft/pack.json");
    QDir jarmodDir = QDir(m_stagingPath + "/unzip/instMods");
    if(packJson.exists())
    {
        packJson.open(QIODevice::ReadOnly | QIODevice::Text);
        QJsonDocument doc = QJsonDocument::fromJson(packJson.readAll());
        packJson.close();

        //we only care about the libs
        QJsonArray libs = doc.object().value("libraries").toArray();

        foreach (const QJsonValue &value, libs)
        {
            QString nameValue = value.toObject().value("name").toString();
            if(!nameValue.startsWith("net.minecraftforge"))
            {
                continue;
            }

            GradleSpecifier forgeVersion(nameValue);

            components->setComponentVersion("net.minecraftforge", forgeVersion.version().replace(m_pack.mcVersion, "").replace("-", ""));
            packJson.remove();
            fallback = false;
            break;
        }

    }

    if(jarmodDir.exists())
    {
        qDebug() << "Found jarmods, installing...";

        QStringList jarmods;
        for (auto info: jarmodDir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files))
        {
            qDebug() << "Jarmod:" << info.fileName();
            jarmods.push_back(info.absoluteFilePath());
        }

        components->installJarMods(jarmods);
        fallback = false;
    }

    //just nuke unzip directory, it s not needed anymore
    FS::deletePath(m_stagingPath + "/unzip");

    if(fallback)
    {
        //TODO: Some fallback mechanism... or just keep failing!
        emitFailed(tr("No installation method found!"));
        return;
    }

    components->saveNow();

    progress(4, 4);

    instance.setName(m_instName);
    if(m_instIcon == "default")
    {
        m_instIcon = "ftb_logo";
    }
    instance.setIconKey(m_instIcon);
    instanceSettings->resumeSave();

    emitSucceeded();
}

bool FtbPackInstallTask::abort()
{
    if(abortable)
    {
        return netJobContainer->abort();
    }
    return false;
}