2013-11-04 01:53:05 +00:00
|
|
|
/* Copyright 2013 MultiMC Contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2013-09-22 03:21:36 +01:00
|
|
|
#include "ForgeInstaller.h"
|
2014-05-19 01:22:09 +01:00
|
|
|
#include "logic/minecraft/InstanceVersion.h"
|
2014-05-08 20:20:10 +01:00
|
|
|
#include "logic/minecraft/OneSixLibrary.h"
|
2014-04-23 01:27:40 +01:00
|
|
|
#include "logic/net/HttpMetaCache.h"
|
|
|
|
#include "logic/tasks/Task.h"
|
|
|
|
#include "logic/OneSixInstance.h"
|
|
|
|
#include "logic/forge/ForgeVersionList.h"
|
2014-05-04 23:10:59 +01:00
|
|
|
#include "logic/VersionFilterData.h"
|
2014-04-23 01:27:40 +01:00
|
|
|
#include "gui/dialogs/ProgressDialog.h"
|
|
|
|
|
2013-09-22 03:21:36 +01:00
|
|
|
#include <quazip.h>
|
|
|
|
#include <quazipfile.h>
|
|
|
|
#include <pathutils.h>
|
|
|
|
#include <QStringList>
|
2014-01-27 18:20:07 +00:00
|
|
|
#include <QRegularExpression>
|
|
|
|
#include <QRegularExpressionMatch>
|
2013-09-22 13:00:37 +01:00
|
|
|
#include "MultiMC.h"
|
2014-01-22 06:33:32 +00:00
|
|
|
#include <QJsonDocument>
|
2014-01-23 20:31:41 +00:00
|
|
|
#include <QJsonArray>
|
2014-01-22 06:33:32 +00:00
|
|
|
#include <QSaveFile>
|
|
|
|
#include <QCryptographicHash>
|
|
|
|
|
2014-04-23 01:27:40 +01:00
|
|
|
ForgeInstaller::ForgeInstaller() : BaseInstaller()
|
2014-03-14 19:48:57 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
void ForgeInstaller::prepare(const QString &filename, const QString &universalUrl)
|
2013-09-22 03:21:36 +01:00
|
|
|
{
|
2014-05-19 01:22:09 +01:00
|
|
|
std::shared_ptr<InstanceVersion> newVersion;
|
2014-03-14 19:48:57 +00:00
|
|
|
m_universal_url = universalUrl;
|
2013-09-22 03:21:36 +01:00
|
|
|
|
|
|
|
QuaZip zip(filename);
|
|
|
|
if (!zip.open(QuaZip::mdUnzip))
|
|
|
|
return;
|
|
|
|
|
|
|
|
QuaZipFile file(&zip);
|
|
|
|
|
|
|
|
// read the install profile
|
|
|
|
if (!zip.setCurrentFile("install_profile.json"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
QJsonParseError jsonError;
|
|
|
|
if (!file.open(QIODevice::ReadOnly))
|
|
|
|
return;
|
|
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(file.readAll(), &jsonError);
|
|
|
|
file.close();
|
|
|
|
if (jsonError.error != QJsonParseError::NoError)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!jsonDoc.isObject())
|
|
|
|
return;
|
|
|
|
|
|
|
|
QJsonObject root = jsonDoc.object();
|
|
|
|
|
|
|
|
auto installVal = root.value("install");
|
|
|
|
auto versionInfoVal = root.value("versionInfo");
|
|
|
|
if (!installVal.isObject() || !versionInfoVal.isObject())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// read the forge version info
|
|
|
|
{
|
2014-05-19 01:22:09 +01:00
|
|
|
newVersion = InstanceVersion::fromJson(versionInfoVal.toObject());
|
2013-09-22 03:21:36 +01:00
|
|
|
if (!newVersion)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject installObj = installVal.toObject();
|
|
|
|
QString libraryName = installObj.value("path").toString();
|
|
|
|
internalPath = installObj.value("filePath").toString();
|
2014-02-01 13:52:21 +00:00
|
|
|
m_forgeVersionString = installObj.value("version").toString().remove("Forge").trimmed();
|
2013-09-22 03:21:36 +01:00
|
|
|
|
|
|
|
// where do we put the library? decode the mojang path
|
2014-01-24 17:12:02 +00:00
|
|
|
OneSixLibrary lib(libraryName);
|
2013-09-22 03:21:36 +01:00
|
|
|
lib.finalize();
|
2013-09-22 13:00:37 +01:00
|
|
|
|
|
|
|
auto cacheentry = MMC->metacache()->resolveEntry("libraries", lib.storagePath());
|
2013-09-22 03:21:36 +01:00
|
|
|
finalPath = "libraries/" + lib.storagePath();
|
|
|
|
if (!ensureFilePathExists(finalPath))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!zip.setCurrentFile(internalPath))
|
|
|
|
return;
|
|
|
|
if (!file.open(QIODevice::ReadOnly))
|
|
|
|
return;
|
|
|
|
{
|
|
|
|
QByteArray data = file.readAll();
|
|
|
|
// extract file
|
|
|
|
QSaveFile extraction(finalPath);
|
|
|
|
if (!extraction.open(QIODevice::WriteOnly))
|
|
|
|
return;
|
|
|
|
if (extraction.write(data) != data.size())
|
|
|
|
return;
|
|
|
|
if (!extraction.commit())
|
|
|
|
return;
|
2013-09-22 13:00:37 +01:00
|
|
|
QCryptographicHash md5sum(QCryptographicHash::Md5);
|
|
|
|
md5sum.addData(data);
|
|
|
|
|
|
|
|
cacheentry->stale = false;
|
|
|
|
cacheentry->md5sum = md5sum.result().toHex().constData();
|
|
|
|
MMC->metacache()->updateEntry(cacheentry);
|
2013-09-22 03:21:36 +01:00
|
|
|
}
|
|
|
|
file.close();
|
|
|
|
|
2014-04-23 01:27:40 +01:00
|
|
|
m_forge_json = newVersion;
|
|
|
|
realVersionId = m_forge_json->id = installObj.value("minecraft").toString();
|
2013-09-22 03:21:36 +01:00
|
|
|
}
|
2014-01-24 17:12:02 +00:00
|
|
|
bool ForgeInstaller::add(OneSixInstance *to)
|
2013-09-22 03:21:36 +01:00
|
|
|
{
|
2014-01-23 20:31:41 +00:00
|
|
|
if (!BaseInstaller::add(to))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject obj;
|
|
|
|
obj.insert("order", 5);
|
|
|
|
|
2014-04-23 01:27:40 +01:00
|
|
|
if (!m_forge_json)
|
2013-09-22 03:21:36 +01:00
|
|
|
return false;
|
|
|
|
int sliding_insert_window = 0;
|
|
|
|
{
|
2014-01-23 20:31:41 +00:00
|
|
|
QJsonArray librariesPlus;
|
|
|
|
|
2013-09-22 03:21:36 +01:00
|
|
|
// for each library in the version we are adding (except for the blacklisted)
|
|
|
|
QSet<QString> blacklist{"lwjgl", "lwjgl_util", "lwjgl-platform"};
|
2014-04-23 01:27:40 +01:00
|
|
|
for (auto lib : m_forge_json->libraries)
|
2013-09-22 03:21:36 +01:00
|
|
|
{
|
|
|
|
QString libName = lib->name();
|
2013-09-30 01:34:46 +01:00
|
|
|
// WARNING: This could actually break.
|
2013-09-22 03:21:36 +01:00
|
|
|
// if this is the actual forge lib, set an absolute url for the download
|
2013-09-22 13:00:37 +01:00
|
|
|
if (libName.contains("minecraftforge"))
|
2013-09-22 03:21:36 +01:00
|
|
|
{
|
|
|
|
lib->setAbsoluteUrl(m_universal_url);
|
|
|
|
}
|
2013-09-30 01:34:46 +01:00
|
|
|
else if (libName.contains("scala"))
|
|
|
|
{
|
|
|
|
lib->setHint("forge-pack-xz");
|
|
|
|
}
|
2013-09-22 03:21:36 +01:00
|
|
|
if (blacklist.contains(libName))
|
|
|
|
continue;
|
|
|
|
|
2014-01-23 20:31:41 +00:00
|
|
|
QJsonObject libObj = lib->toJson();
|
|
|
|
|
2013-09-22 03:21:36 +01:00
|
|
|
bool found = false;
|
2014-01-27 18:20:07 +00:00
|
|
|
bool equals = false;
|
2014-01-23 20:31:41 +00:00
|
|
|
// find an entry that matches this one
|
2014-04-23 01:27:40 +01:00
|
|
|
for (auto tolib : to->getFullVersion()->vanillaLibraries)
|
2013-09-22 03:21:36 +01:00
|
|
|
{
|
|
|
|
if (tolib->name() != libName)
|
|
|
|
continue;
|
|
|
|
found = true;
|
2014-01-27 18:20:07 +00:00
|
|
|
if (tolib->toJson() == libObj)
|
|
|
|
{
|
|
|
|
equals = true;
|
|
|
|
}
|
2013-09-22 03:21:36 +01:00
|
|
|
// replace lib
|
2014-01-27 21:23:07 +00:00
|
|
|
libObj.insert("insert", QString("replace"));
|
2013-09-22 03:21:36 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-01-27 18:20:07 +00:00
|
|
|
if (equals)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2013-09-22 03:21:36 +01:00
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
// add lib
|
2014-02-02 13:05:07 +00:00
|
|
|
libObj.insert("insert", QString("prepend"));
|
|
|
|
if (lib->name() == "minecraftforge")
|
|
|
|
{
|
|
|
|
libObj.insert("MMC-depend", QString("hard"));
|
|
|
|
}
|
2013-09-22 03:21:36 +01:00
|
|
|
sliding_insert_window++;
|
|
|
|
}
|
2014-01-27 18:20:07 +00:00
|
|
|
librariesPlus.prepend(libObj);
|
2013-09-22 03:21:36 +01:00
|
|
|
}
|
2014-01-23 20:31:41 +00:00
|
|
|
obj.insert("+libraries", librariesPlus);
|
2014-04-23 01:27:40 +01:00
|
|
|
obj.insert("mainClass", m_forge_json->mainClass);
|
|
|
|
QString args = m_forge_json->minecraftArguments;
|
2014-01-27 18:20:07 +00:00
|
|
|
QStringList tweakers;
|
|
|
|
{
|
|
|
|
QRegularExpression expression("--tweakClass ([a-zA-Z0-9\\.]*)");
|
|
|
|
QRegularExpressionMatch match = expression.match(args);
|
|
|
|
while (match.hasMatch())
|
|
|
|
{
|
|
|
|
tweakers.append(match.captured(1));
|
|
|
|
args.remove(match.capturedStart(), match.capturedLength());
|
|
|
|
match = expression.match(args);
|
|
|
|
}
|
|
|
|
}
|
2014-04-23 01:27:40 +01:00
|
|
|
if (!args.isEmpty() && args != to->getFullVersion()->vanillaMinecraftArguments)
|
2014-01-27 18:20:07 +00:00
|
|
|
{
|
|
|
|
obj.insert("minecraftArguments", args);
|
|
|
|
}
|
|
|
|
if (!tweakers.isEmpty())
|
|
|
|
{
|
|
|
|
obj.insert("+tweakers", QJsonArray::fromStringList(tweakers));
|
|
|
|
}
|
2014-04-23 01:27:40 +01:00
|
|
|
if (!m_forge_json->processArguments.isEmpty() &&
|
|
|
|
m_forge_json->processArguments != to->getFullVersion()->vanillaProcessArguments)
|
2014-01-27 18:20:07 +00:00
|
|
|
{
|
2014-04-23 01:27:40 +01:00
|
|
|
obj.insert("processArguments", m_forge_json->processArguments);
|
2014-01-27 18:20:07 +00:00
|
|
|
}
|
2014-01-23 20:31:41 +00:00
|
|
|
}
|
|
|
|
|
2014-02-01 13:52:21 +00:00
|
|
|
obj.insert("name", QString("Forge"));
|
2014-02-01 15:26:38 +00:00
|
|
|
obj.insert("fileId", id());
|
2014-02-01 13:52:21 +00:00
|
|
|
obj.insert("version", m_forgeVersionString);
|
|
|
|
obj.insert("mcVersion", to->intendedVersionId());
|
|
|
|
|
2014-01-23 20:31:41 +00:00
|
|
|
QFile file(filename(to->instanceRoot()));
|
|
|
|
if (!file.open(QFile::WriteOnly))
|
|
|
|
{
|
2014-01-27 18:20:07 +00:00
|
|
|
QLOG_ERROR() << "Error opening" << file.fileName()
|
|
|
|
<< "for reading:" << file.errorString();
|
2014-01-23 20:31:41 +00:00
|
|
|
return false;
|
2013-09-22 03:21:36 +01:00
|
|
|
}
|
2014-01-23 20:31:41 +00:00
|
|
|
file.write(QJsonDocument(obj).toJson());
|
|
|
|
file.close();
|
|
|
|
|
2014-01-22 06:33:32 +00:00
|
|
|
return true;
|
2013-09-22 03:21:36 +01:00
|
|
|
}
|
2014-03-14 19:48:57 +00:00
|
|
|
|
2014-04-23 01:27:40 +01:00
|
|
|
bool ForgeInstaller::addLegacy(OneSixInstance *to)
|
|
|
|
{
|
|
|
|
if (!BaseInstaller::add(to))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-04 23:10:59 +01:00
|
|
|
auto entry = MMC->metacache()->resolveEntry("minecraftforge", m_forge_version->filename());
|
|
|
|
finalPath = PathCombine(to->jarModsDir(), m_forge_version->filename());
|
|
|
|
if (!ensureFilePathExists(finalPath))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!QFile::copy(entry->getFullPath(),finalPath))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-04-23 01:27:40 +01:00
|
|
|
QJsonObject obj;
|
|
|
|
obj.insert("order", 5);
|
|
|
|
{
|
|
|
|
QJsonArray jarmodsPlus;
|
|
|
|
{
|
|
|
|
QJsonObject libObj;
|
|
|
|
libObj.insert("name", m_forge_version->universal_filename);
|
|
|
|
jarmodsPlus.append(libObj);
|
|
|
|
}
|
|
|
|
obj.insert("+jarMods", jarmodsPlus);
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.insert("name", QString("Forge"));
|
|
|
|
obj.insert("fileId", id());
|
|
|
|
obj.insert("version", m_forge_version->jobbuildver);
|
|
|
|
obj.insert("mcVersion", to->intendedVersionId());
|
2014-05-04 23:10:59 +01:00
|
|
|
if (g_VersionFilterData.fmlLibsMapping.contains(m_forge_version->mcver))
|
2014-04-23 01:27:40 +01:00
|
|
|
{
|
|
|
|
QJsonArray traitsPlus;
|
|
|
|
traitsPlus.append(QString("legacyFML"));
|
|
|
|
obj.insert("+traits", traitsPlus);
|
|
|
|
}
|
2014-05-04 23:10:59 +01:00
|
|
|
auto fullversion = to->getFullVersion();
|
|
|
|
fullversion->remove("net.minecraftforge");
|
|
|
|
|
2014-04-23 01:27:40 +01:00
|
|
|
QFile file(filename(to->instanceRoot()));
|
|
|
|
if (!file.open(QFile::WriteOnly))
|
|
|
|
{
|
|
|
|
QLOG_ERROR() << "Error opening" << file.fileName()
|
|
|
|
<< "for reading:" << file.errorString();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
file.write(QJsonDocument(obj).toJson());
|
|
|
|
file.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-14 19:48:57 +00:00
|
|
|
class ForgeInstallTask : public Task
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2014-04-23 01:27:40 +01:00
|
|
|
ForgeInstallTask(ForgeInstaller *installer, OneSixInstance *instance,
|
|
|
|
BaseVersionPtr version, QObject *parent = 0)
|
2014-03-14 19:48:57 +00:00
|
|
|
: Task(parent), m_installer(installer), m_instance(instance), m_version(version)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void executeTask() override
|
|
|
|
{
|
2014-04-23 01:27:40 +01:00
|
|
|
setStatus(tr("Installing forge..."));
|
|
|
|
ForgeVersionPtr forgeVersion = std::dynamic_pointer_cast<ForgeVersion>(m_version);
|
|
|
|
if (!forgeVersion)
|
2014-03-14 19:48:57 +00:00
|
|
|
{
|
2014-04-23 01:27:40 +01:00
|
|
|
emitFailed(tr("Unknown error occured"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
prepare(forgeVersion);
|
|
|
|
}
|
|
|
|
void prepare(ForgeVersionPtr forgeVersion)
|
|
|
|
{
|
2014-05-04 23:10:59 +01:00
|
|
|
auto entry = MMC->metacache()->resolveEntry("minecraftforge", forgeVersion->filename());
|
2014-04-23 01:27:40 +01:00
|
|
|
auto installFunction = [this, entry, forgeVersion]()
|
|
|
|
{
|
|
|
|
if (!install(entry, forgeVersion))
|
2014-03-14 19:48:57 +00:00
|
|
|
{
|
2014-04-23 01:27:40 +01:00
|
|
|
QLOG_ERROR() << "Failure installing forge";
|
|
|
|
emitFailed(tr("Failure to install forge"));
|
2014-03-14 19:48:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-23 01:27:40 +01:00
|
|
|
reload();
|
2014-03-14 19:48:57 +00:00
|
|
|
}
|
2014-04-23 01:27:40 +01:00
|
|
|
};
|
2014-05-04 23:10:59 +01:00
|
|
|
|
2014-04-23 01:27:40 +01:00
|
|
|
if (entry->stale)
|
|
|
|
{
|
|
|
|
NetJob *fjob = new NetJob("Forge download");
|
|
|
|
fjob->addNetAction(CacheDownload::make(forgeVersion->url(), entry));
|
|
|
|
connect(fjob, &NetJob::progress, [this](qint64 current, qint64 total)
|
|
|
|
{ setProgress(100 * current / qMax((qint64)1, total)); });
|
|
|
|
connect(fjob, &NetJob::status, [this](const QString & msg)
|
|
|
|
{ setStatus(msg); });
|
|
|
|
connect(fjob, &NetJob::failed, [this]()
|
|
|
|
{ emitFailed(tr("Failure to download forge")); });
|
|
|
|
connect(fjob, &NetJob::succeeded, installFunction);
|
|
|
|
fjob->start();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
installFunction();
|
2014-03-14 19:48:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
bool install(const std::shared_ptr<MetaEntry> &entry, const ForgeVersionPtr &forgeVersion)
|
|
|
|
{
|
2014-04-23 01:27:40 +01:00
|
|
|
if (forgeVersion->usesInstaller())
|
|
|
|
{
|
|
|
|
QString forgePath = entry->getFullPath();
|
|
|
|
m_installer->prepare(forgePath, forgeVersion->universal_url);
|
|
|
|
return m_installer->add(m_instance);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return m_installer->addLegacy(m_instance);
|
2014-03-14 19:48:57 +00:00
|
|
|
}
|
|
|
|
void reload()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_instance->reloadVersion();
|
|
|
|
emitSucceeded();
|
|
|
|
}
|
|
|
|
catch (MMCError &e)
|
|
|
|
{
|
|
|
|
emitFailed(e.cause());
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
emitFailed(tr("Failed to load the version description file for reasons unknown."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ForgeInstaller *m_installer;
|
|
|
|
OneSixInstance *m_instance;
|
|
|
|
BaseVersionPtr m_version;
|
|
|
|
};
|
|
|
|
|
2014-04-23 01:27:40 +01:00
|
|
|
ProgressProvider *ForgeInstaller::createInstallTask(OneSixInstance *instance,
|
|
|
|
BaseVersionPtr version, QObject *parent)
|
2014-03-14 19:48:57 +00:00
|
|
|
{
|
2014-04-23 01:27:40 +01:00
|
|
|
if (!version)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
m_forge_version = std::dynamic_pointer_cast<ForgeVersion>(version);
|
2014-03-14 19:48:57 +00:00
|
|
|
return new ForgeInstallTask(this, instance, version, parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "ForgeInstaller.moc"
|