2013-11-04 07:23:05 +05:30
|
|
|
/* 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 07:51:36 +05:30
|
|
|
#include "ForgeInstaller.h"
|
2014-01-24 22:42:02 +05:30
|
|
|
#include "OneSixVersion.h"
|
|
|
|
#include "OneSixLibrary.h"
|
2013-09-22 17:30:37 +05:30
|
|
|
#include "net/HttpMetaCache.h"
|
2013-09-22 07:51:36 +05:30
|
|
|
#include <quazip.h>
|
|
|
|
#include <quazipfile.h>
|
|
|
|
#include <pathutils.h>
|
|
|
|
#include <QStringList>
|
2014-01-27 23:50:07 +05:30
|
|
|
#include <QRegularExpression>
|
|
|
|
#include <QRegularExpressionMatch>
|
2013-09-22 17:30:37 +05:30
|
|
|
#include "MultiMC.h"
|
2014-01-24 22:42:02 +05:30
|
|
|
#include "OneSixInstance.h"
|
2013-09-22 07:51:36 +05:30
|
|
|
|
2014-01-22 12:03:32 +05:30
|
|
|
#include <QJsonDocument>
|
2014-01-24 02:01:41 +05:30
|
|
|
#include <QJsonArray>
|
2014-01-22 12:03:32 +05:30
|
|
|
#include <QSaveFile>
|
|
|
|
#include <QCryptographicHash>
|
|
|
|
|
2013-09-22 07:51:36 +05:30
|
|
|
ForgeInstaller::ForgeInstaller(QString filename, QString universal_url)
|
|
|
|
{
|
2014-01-24 22:42:02 +05:30
|
|
|
std::shared_ptr<OneSixVersion> newVersion;
|
2013-09-22 07:51:36 +05:30
|
|
|
m_universal_url = universal_url;
|
|
|
|
|
|
|
|
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-01-24 22:42:02 +05:30
|
|
|
newVersion = OneSixVersion::fromJson(versionInfoVal.toObject());
|
2013-09-22 07:51:36 +05:30
|
|
|
if (!newVersion)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject installObj = installVal.toObject();
|
|
|
|
QString libraryName = installObj.value("path").toString();
|
|
|
|
internalPath = installObj.value("filePath").toString();
|
2014-02-01 19:22:21 +05:30
|
|
|
m_forgeVersionString = installObj.value("version").toString().remove("Forge").trimmed();
|
2013-09-22 07:51:36 +05:30
|
|
|
|
|
|
|
// where do we put the library? decode the mojang path
|
2014-01-24 22:42:02 +05:30
|
|
|
OneSixLibrary lib(libraryName);
|
2013-09-22 07:51:36 +05:30
|
|
|
lib.finalize();
|
2013-09-22 17:30:37 +05:30
|
|
|
|
|
|
|
auto cacheentry = MMC->metacache()->resolveEntry("libraries", lib.storagePath());
|
2013-09-22 07:51:36 +05:30
|
|
|
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 17:30:37 +05:30
|
|
|
QCryptographicHash md5sum(QCryptographicHash::Md5);
|
|
|
|
md5sum.addData(data);
|
|
|
|
|
|
|
|
cacheentry->stale = false;
|
|
|
|
cacheentry->md5sum = md5sum.result().toHex().constData();
|
|
|
|
MMC->metacache()->updateEntry(cacheentry);
|
2013-09-22 07:51:36 +05:30
|
|
|
}
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
m_forge_version = newVersion;
|
|
|
|
realVersionId = m_forge_version->id = installObj.value("minecraft").toString();
|
|
|
|
}
|
|
|
|
|
2014-01-24 22:42:02 +05:30
|
|
|
bool ForgeInstaller::add(OneSixInstance *to)
|
2013-09-22 07:51:36 +05:30
|
|
|
{
|
2014-01-24 02:01:41 +05:30
|
|
|
if (!BaseInstaller::add(to))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject obj;
|
|
|
|
obj.insert("order", 5);
|
|
|
|
|
2013-09-22 07:51:36 +05:30
|
|
|
if (!m_forge_version)
|
|
|
|
return false;
|
|
|
|
int sliding_insert_window = 0;
|
|
|
|
{
|
2014-01-24 02:01:41 +05:30
|
|
|
QJsonArray librariesPlus;
|
|
|
|
|
2013-09-22 07:51:36 +05:30
|
|
|
// for each library in the version we are adding (except for the blacklisted)
|
|
|
|
QSet<QString> blacklist{"lwjgl", "lwjgl_util", "lwjgl-platform"};
|
|
|
|
for (auto lib : m_forge_version->libraries)
|
|
|
|
{
|
|
|
|
QString libName = lib->name();
|
2013-09-30 06:04:46 +05:30
|
|
|
// WARNING: This could actually break.
|
2013-09-22 07:51:36 +05:30
|
|
|
// if this is the actual forge lib, set an absolute url for the download
|
2013-09-22 17:30:37 +05:30
|
|
|
if (libName.contains("minecraftforge"))
|
2013-09-22 07:51:36 +05:30
|
|
|
{
|
|
|
|
lib->setAbsoluteUrl(m_universal_url);
|
|
|
|
}
|
2013-09-30 06:04:46 +05:30
|
|
|
else if (libName.contains("scala"))
|
|
|
|
{
|
|
|
|
lib->setHint("forge-pack-xz");
|
|
|
|
}
|
2013-09-22 07:51:36 +05:30
|
|
|
if (blacklist.contains(libName))
|
|
|
|
continue;
|
|
|
|
|
2014-01-24 02:01:41 +05:30
|
|
|
QJsonObject libObj = lib->toJson();
|
|
|
|
|
2013-09-22 07:51:36 +05:30
|
|
|
bool found = false;
|
2014-01-27 23:50:07 +05:30
|
|
|
bool equals = false;
|
2014-01-24 02:01:41 +05:30
|
|
|
// find an entry that matches this one
|
2014-01-27 23:50:07 +05:30
|
|
|
for (auto tolib : to->getNonCustomVersion()->libraries)
|
2013-09-22 07:51:36 +05:30
|
|
|
{
|
|
|
|
if (tolib->name() != libName)
|
|
|
|
continue;
|
|
|
|
found = true;
|
2014-01-27 23:50:07 +05:30
|
|
|
if (tolib->toJson() == libObj)
|
|
|
|
{
|
|
|
|
equals = true;
|
|
|
|
}
|
2013-09-22 07:51:36 +05:30
|
|
|
// replace lib
|
2014-01-28 02:53:07 +05:30
|
|
|
libObj.insert("insert", QString("replace"));
|
2013-09-22 07:51:36 +05:30
|
|
|
break;
|
|
|
|
}
|
2014-01-27 23:50:07 +05:30
|
|
|
if (equals)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2013-09-22 07:51:36 +05:30
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
// add lib
|
2014-02-02 18:35:07 +05:30
|
|
|
libObj.insert("insert", QString("prepend"));
|
|
|
|
if (lib->name() == "minecraftforge")
|
|
|
|
{
|
|
|
|
libObj.insert("MMC-depend", QString("hard"));
|
|
|
|
}
|
2013-09-22 07:51:36 +05:30
|
|
|
sliding_insert_window++;
|
|
|
|
}
|
2014-01-27 23:50:07 +05:30
|
|
|
librariesPlus.prepend(libObj);
|
2013-09-22 07:51:36 +05:30
|
|
|
}
|
2014-01-24 02:01:41 +05:30
|
|
|
obj.insert("+libraries", librariesPlus);
|
|
|
|
obj.insert("mainClass", m_forge_version->mainClass);
|
2014-01-27 23:50:07 +05:30
|
|
|
QString args = m_forge_version->minecraftArguments;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!args.isEmpty() && args != to->getNonCustomVersion()->minecraftArguments)
|
|
|
|
{
|
|
|
|
obj.insert("minecraftArguments", args);
|
|
|
|
}
|
|
|
|
if (!tweakers.isEmpty())
|
|
|
|
{
|
|
|
|
obj.insert("+tweakers", QJsonArray::fromStringList(tweakers));
|
|
|
|
}
|
|
|
|
if (!m_forge_version->processArguments.isEmpty() &&
|
|
|
|
m_forge_version->processArguments != to->getNonCustomVersion()->processArguments)
|
|
|
|
{
|
|
|
|
obj.insert("processArguments", m_forge_version->processArguments);
|
|
|
|
}
|
2014-01-24 02:01:41 +05:30
|
|
|
}
|
|
|
|
|
2014-02-01 19:22:21 +05:30
|
|
|
obj.insert("name", QString("Forge"));
|
2014-02-01 20:56:38 +05:30
|
|
|
obj.insert("fileId", id());
|
2014-02-01 19:22:21 +05:30
|
|
|
obj.insert("version", m_forgeVersionString);
|
|
|
|
obj.insert("mcVersion", to->intendedVersionId());
|
|
|
|
|
2014-01-24 02:01:41 +05:30
|
|
|
QFile file(filename(to->instanceRoot()));
|
|
|
|
if (!file.open(QFile::WriteOnly))
|
|
|
|
{
|
2014-01-27 23:50:07 +05:30
|
|
|
QLOG_ERROR() << "Error opening" << file.fileName()
|
|
|
|
<< "for reading:" << file.errorString();
|
2014-01-24 02:01:41 +05:30
|
|
|
return false;
|
2013-09-22 07:51:36 +05:30
|
|
|
}
|
2014-01-24 02:01:41 +05:30
|
|
|
file.write(QJsonDocument(obj).toJson());
|
|
|
|
file.close();
|
|
|
|
|
2014-01-22 12:03:32 +05:30
|
|
|
return true;
|
2013-09-22 07:51:36 +05:30
|
|
|
}
|