2015-02-02 22:25:30 +00:00
|
|
|
/* Copyright 2013-2015 MultiMC Contributors
|
2013-11-04 01:53:05 +00:00
|
|
|
*
|
|
|
|
* 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-08-14 07:13:41 +01:00
|
|
|
|
2013-08-27 21:32:41 +01:00
|
|
|
#include <QDir>
|
|
|
|
#include <QString>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonValue>
|
|
|
|
#include <quazip.h>
|
|
|
|
#include <quazipfile.h>
|
|
|
|
|
2013-08-14 07:13:41 +01:00
|
|
|
#include "Mod.h"
|
|
|
|
#include <pathutils.h>
|
2014-07-01 00:48:09 +01:00
|
|
|
#include "logic/settings/INIFile.h"
|
2013-11-04 01:53:05 +00:00
|
|
|
#include "logger/QsLog.h"
|
2013-08-14 07:13:41 +01:00
|
|
|
|
2013-10-08 00:36:11 +01:00
|
|
|
Mod::Mod(const QFileInfo &file)
|
2013-08-14 07:13:41 +01:00
|
|
|
{
|
|
|
|
repath(file);
|
|
|
|
}
|
|
|
|
|
2013-10-08 00:36:11 +01:00
|
|
|
void Mod::repath(const QFileInfo &file)
|
2013-08-14 07:13:41 +01:00
|
|
|
{
|
|
|
|
m_file = file;
|
2013-12-26 04:14:32 +00:00
|
|
|
QString name_base = file.fileName();
|
2013-10-08 00:36:11 +01:00
|
|
|
|
2013-08-14 07:13:41 +01:00
|
|
|
m_type = Mod::MOD_UNKNOWN;
|
2013-12-26 04:14:32 +00:00
|
|
|
|
2013-08-14 07:13:41 +01:00
|
|
|
if (m_file.isDir())
|
2013-12-26 04:14:32 +00:00
|
|
|
{
|
2013-08-14 07:13:41 +01:00
|
|
|
m_type = MOD_FOLDER;
|
2013-12-26 04:14:32 +00:00
|
|
|
m_name = name_base;
|
|
|
|
m_mmc_id = name_base;
|
|
|
|
}
|
2013-08-14 07:13:41 +01:00
|
|
|
else if (m_file.isFile())
|
|
|
|
{
|
2013-12-29 01:28:24 +00:00
|
|
|
if (name_base.endsWith(".disabled"))
|
2013-12-26 04:14:32 +00:00
|
|
|
{
|
|
|
|
m_enabled = false;
|
|
|
|
name_base.chop(9);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_enabled = true;
|
|
|
|
}
|
|
|
|
m_mmc_id = name_base;
|
|
|
|
if (name_base.endsWith(".zip") || name_base.endsWith(".jar"))
|
|
|
|
{
|
2013-08-14 07:13:41 +01:00
|
|
|
m_type = MOD_ZIPFILE;
|
2013-12-26 04:14:32 +00:00
|
|
|
name_base.chop(4);
|
|
|
|
}
|
2013-12-29 01:28:24 +00:00
|
|
|
else if (name_base.endsWith(".litemod"))
|
|
|
|
{
|
|
|
|
m_type = MOD_LITEMOD;
|
|
|
|
name_base.chop(8);
|
|
|
|
}
|
2013-08-14 07:13:41 +01:00
|
|
|
else
|
2013-12-26 04:14:32 +00:00
|
|
|
{
|
2013-08-14 07:13:41 +01:00
|
|
|
m_type = MOD_SINGLEFILE;
|
2013-12-26 04:14:32 +00:00
|
|
|
}
|
|
|
|
m_name = name_base;
|
2013-08-14 07:13:41 +01:00
|
|
|
}
|
2013-12-26 04:14:32 +00:00
|
|
|
|
2013-10-08 00:36:11 +01:00
|
|
|
if (m_type == MOD_ZIPFILE)
|
2013-08-14 07:13:41 +01:00
|
|
|
{
|
2013-08-27 21:32:41 +01:00
|
|
|
QuaZip zip(m_file.filePath());
|
2013-10-08 00:36:11 +01:00
|
|
|
if (!zip.open(QuaZip::mdUnzip))
|
2013-08-27 21:32:41 +01:00
|
|
|
return;
|
2013-10-08 00:36:11 +01:00
|
|
|
|
2013-08-27 21:32:41 +01:00
|
|
|
QuaZipFile file(&zip);
|
2013-11-05 23:46:06 +00:00
|
|
|
|
|
|
|
if (zip.setCurrentFile("mcmod.info"))
|
2013-08-14 07:13:41 +01:00
|
|
|
{
|
2013-12-29 01:28:24 +00:00
|
|
|
if (!file.open(QIODevice::ReadOnly))
|
2013-08-14 07:13:41 +01:00
|
|
|
{
|
2013-08-27 21:32:41 +01:00
|
|
|
zip.close();
|
|
|
|
return;
|
2013-08-14 07:13:41 +01:00
|
|
|
}
|
2013-11-05 23:46:06 +00:00
|
|
|
|
|
|
|
ReadMCModInfo(file.readAll());
|
|
|
|
file.close();
|
|
|
|
zip.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (zip.setCurrentFile("forgeversion.properties"))
|
|
|
|
{
|
|
|
|
if (!file.open(QIODevice::ReadOnly))
|
2013-08-14 07:13:41 +01:00
|
|
|
{
|
2013-08-27 21:32:41 +01:00
|
|
|
zip.close();
|
|
|
|
return;
|
2013-08-14 07:13:41 +01:00
|
|
|
}
|
2013-11-05 23:46:06 +00:00
|
|
|
|
|
|
|
ReadForgeInfo(file.readAll());
|
|
|
|
file.close();
|
|
|
|
zip.close();
|
|
|
|
return;
|
2013-08-14 07:13:41 +01:00
|
|
|
}
|
2013-11-05 23:46:06 +00:00
|
|
|
|
2013-08-27 21:32:41 +01:00
|
|
|
zip.close();
|
|
|
|
}
|
2013-10-08 00:36:11 +01:00
|
|
|
else if (m_type == MOD_FOLDER)
|
2013-08-27 21:32:41 +01:00
|
|
|
{
|
|
|
|
QFileInfo mcmod_info(PathCombine(m_file.filePath(), "mcmod.info"));
|
|
|
|
if (mcmod_info.isFile())
|
|
|
|
{
|
|
|
|
QFile mcmod(mcmod_info.filePath());
|
2013-10-08 00:36:11 +01:00
|
|
|
if (!mcmod.open(QIODevice::ReadOnly))
|
2013-08-27 21:32:41 +01:00
|
|
|
return;
|
|
|
|
auto data = mcmod.readAll();
|
2013-10-08 00:36:11 +01:00
|
|
|
if (data.isEmpty() || data.isNull())
|
2013-08-27 21:32:41 +01:00
|
|
|
return;
|
|
|
|
ReadMCModInfo(data);
|
|
|
|
}
|
2013-08-14 07:13:41 +01:00
|
|
|
}
|
2013-12-29 01:28:24 +00:00
|
|
|
else if (m_type == MOD_LITEMOD)
|
|
|
|
{
|
|
|
|
QuaZip zip(m_file.filePath());
|
|
|
|
if (!zip.open(QuaZip::mdUnzip))
|
|
|
|
return;
|
|
|
|
|
|
|
|
QuaZipFile file(&zip);
|
|
|
|
|
|
|
|
if (zip.setCurrentFile("litemod.json"))
|
|
|
|
{
|
|
|
|
if (!file.open(QIODevice::ReadOnly))
|
|
|
|
{
|
|
|
|
zip.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadLiteModInfo(file.readAll());
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
zip.close();
|
|
|
|
}
|
2013-08-14 07:13:41 +01:00
|
|
|
}
|
|
|
|
|
2013-08-27 21:32:41 +01:00
|
|
|
// NEW format
|
|
|
|
// https://github.com/MinecraftForge/FML/wiki/FML-mod-information-file/6f62b37cea040daf350dc253eae6326dd9c822c3
|
2013-08-14 07:13:41 +01:00
|
|
|
|
2013-08-27 21:32:41 +01:00
|
|
|
// OLD format:
|
|
|
|
// https://github.com/MinecraftForge/FML/wiki/FML-mod-information-file/5bf6a2d05145ec79387acc0d45c958642fb049fc
|
|
|
|
void Mod::ReadMCModInfo(QByteArray contents)
|
2013-08-14 07:13:41 +01:00
|
|
|
{
|
2013-10-08 00:36:11 +01:00
|
|
|
auto getInfoFromArray = [&](QJsonArray arr)->void
|
2013-08-14 07:13:41 +01:00
|
|
|
{
|
2013-10-08 00:36:11 +01:00
|
|
|
if (!arr.at(0).isObject())
|
2013-08-27 21:32:41 +01:00
|
|
|
return;
|
|
|
|
auto firstObj = arr.at(0).toObject();
|
2013-12-26 04:14:32 +00:00
|
|
|
m_mod_id = firstObj.value("modid").toString();
|
2013-08-27 21:32:41 +01:00
|
|
|
m_name = firstObj.value("name").toString();
|
|
|
|
m_version = firstObj.value("version").toString();
|
2013-10-08 00:36:11 +01:00
|
|
|
m_homeurl = firstObj.value("url").toString();
|
2014-05-08 18:00:48 +01:00
|
|
|
m_updateurl = firstObj.value("updateUrl").toString();
|
2014-02-06 08:32:44 +00:00
|
|
|
m_homeurl = m_homeurl.trimmed();
|
|
|
|
if(!m_homeurl.isEmpty())
|
|
|
|
{
|
|
|
|
// fix up url.
|
|
|
|
if (!m_homeurl.startsWith("http://") && !m_homeurl.startsWith("https://") &&
|
|
|
|
!m_homeurl.startsWith("ftp://"))
|
|
|
|
{
|
|
|
|
m_homeurl.prepend("http://");
|
|
|
|
}
|
|
|
|
}
|
2013-10-08 17:07:54 +01:00
|
|
|
m_description = firstObj.value("description").toString();
|
2014-03-30 05:16:31 +01:00
|
|
|
QJsonArray authors = firstObj.value("authorList").toArray();
|
|
|
|
if (authors.size() == 0)
|
|
|
|
authors = firstObj.value("authors").toArray();
|
|
|
|
|
2013-11-04 01:53:05 +00:00
|
|
|
if (authors.size() == 0)
|
|
|
|
m_authors = "";
|
|
|
|
else if (authors.size() >= 1)
|
2013-10-08 17:07:54 +01:00
|
|
|
{
|
|
|
|
m_authors = authors.at(0).toString();
|
2013-11-04 01:53:05 +00:00
|
|
|
for (int i = 1; i < authors.size(); i++)
|
2013-10-08 17:07:54 +01:00
|
|
|
{
|
|
|
|
m_authors += ", " + authors.at(i).toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_credits = firstObj.value("credits").toString();
|
2013-08-27 21:32:41 +01:00
|
|
|
return;
|
2014-02-06 08:32:44 +00:00
|
|
|
}
|
|
|
|
;
|
2013-08-27 21:32:41 +01:00
|
|
|
QJsonParseError jsonError;
|
|
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(contents, &jsonError);
|
|
|
|
// this is the very old format that had just the array
|
2013-10-08 00:36:11 +01:00
|
|
|
if (jsonDoc.isArray())
|
2013-08-14 07:13:41 +01:00
|
|
|
{
|
2013-08-27 21:32:41 +01:00
|
|
|
getInfoFromArray(jsonDoc.array());
|
2013-08-14 07:13:41 +01:00
|
|
|
}
|
2013-10-08 00:36:11 +01:00
|
|
|
else if (jsonDoc.isObject())
|
2013-08-14 07:13:41 +01:00
|
|
|
{
|
2013-08-27 21:32:41 +01:00
|
|
|
auto val = jsonDoc.object().value("modinfoversion");
|
2014-05-08 18:00:48 +01:00
|
|
|
if(val.isUndefined())
|
|
|
|
val = jsonDoc.object().value("modListVersion");
|
2013-08-27 21:32:41 +01:00
|
|
|
int version = val.toDouble();
|
2013-10-08 00:36:11 +01:00
|
|
|
if (version != 2)
|
2013-08-27 21:32:41 +01:00
|
|
|
{
|
2013-10-06 00:13:40 +01:00
|
|
|
QLOG_ERROR() << "BAD stuff happened to mod json:";
|
|
|
|
QLOG_ERROR() << contents;
|
2013-08-27 21:32:41 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto arrVal = jsonDoc.object().value("modlist");
|
2014-05-08 18:00:48 +01:00
|
|
|
if(arrVal.isUndefined())
|
|
|
|
arrVal = jsonDoc.object().value("modList");
|
2013-10-08 00:36:11 +01:00
|
|
|
if (arrVal.isArray())
|
2013-08-27 21:32:41 +01:00
|
|
|
{
|
|
|
|
getInfoFromArray(arrVal.toArray());
|
|
|
|
}
|
2013-08-14 07:13:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-27 21:32:41 +01:00
|
|
|
void Mod::ReadForgeInfo(QByteArray contents)
|
2013-08-14 07:13:41 +01:00
|
|
|
{
|
|
|
|
// Read the data
|
2013-08-27 21:32:41 +01:00
|
|
|
m_name = "Minecraft Forge";
|
2013-12-26 04:14:32 +00:00
|
|
|
m_mod_id = "Forge";
|
2013-10-08 00:36:11 +01:00
|
|
|
m_homeurl = "http://www.minecraftforge.net/forum/";
|
2013-08-27 21:32:41 +01:00
|
|
|
INIFile ini;
|
2013-10-08 00:36:11 +01:00
|
|
|
if (!ini.loadFile(contents))
|
2013-08-27 21:32:41 +01:00
|
|
|
return;
|
2013-10-08 00:36:11 +01:00
|
|
|
|
|
|
|
QString major = ini.get("forge.major.number", "0").toString();
|
|
|
|
QString minor = ini.get("forge.minor.number", "0").toString();
|
|
|
|
QString revision = ini.get("forge.revision.number", "0").toString();
|
|
|
|
QString build = ini.get("forge.build.number", "0").toString();
|
|
|
|
|
2013-08-27 21:32:41 +01:00
|
|
|
m_version = major + "." + minor + "." + revision + "." + build;
|
2013-08-14 07:13:41 +01:00
|
|
|
}
|
|
|
|
|
2013-12-29 01:28:24 +00:00
|
|
|
void Mod::ReadLiteModInfo(QByteArray contents)
|
|
|
|
{
|
|
|
|
QJsonParseError jsonError;
|
|
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(contents, &jsonError);
|
|
|
|
auto object = jsonDoc.object();
|
2014-02-06 08:32:44 +00:00
|
|
|
if (object.contains("name"))
|
2013-12-29 03:17:52 +00:00
|
|
|
{
|
|
|
|
m_mod_id = m_name = object.value("name").toString();
|
|
|
|
}
|
2014-02-06 08:32:44 +00:00
|
|
|
if (object.contains("version"))
|
2013-12-29 03:17:52 +00:00
|
|
|
{
|
2014-02-06 08:32:44 +00:00
|
|
|
m_version = object.value("version").toString("");
|
2013-12-29 03:17:52 +00:00
|
|
|
}
|
2013-12-29 01:28:24 +00:00
|
|
|
else
|
2013-12-29 03:17:52 +00:00
|
|
|
{
|
2014-02-06 08:32:44 +00:00
|
|
|
m_version = object.value("revision").toString("");
|
2013-12-29 03:17:52 +00:00
|
|
|
}
|
2013-12-29 01:28:24 +00:00
|
|
|
m_mcversion = object.value("mcversion").toString();
|
|
|
|
m_authors = object.value("author").toString();
|
|
|
|
m_description = object.value("description").toString();
|
|
|
|
m_homeurl = object.value("url").toString();
|
|
|
|
}
|
|
|
|
|
2013-10-08 00:36:11 +01:00
|
|
|
bool Mod::replace(Mod &with)
|
2013-08-14 07:13:41 +01:00
|
|
|
{
|
2013-10-08 00:36:11 +01:00
|
|
|
if (!destroy())
|
2013-08-14 07:13:41 +01:00
|
|
|
return false;
|
|
|
|
bool success = false;
|
|
|
|
auto t = with.type();
|
2013-12-29 01:28:24 +00:00
|
|
|
|
2014-07-13 14:26:26 +01:00
|
|
|
if (t == MOD_ZIPFILE || t == MOD_SINGLEFILE || t == MOD_LITEMOD)
|
2013-08-14 07:13:41 +01:00
|
|
|
{
|
2013-12-26 04:14:32 +00:00
|
|
|
QLOG_DEBUG() << "Copy: " << with.m_file.filePath() << " to " << m_file.filePath();
|
|
|
|
success = QFile::copy(with.m_file.filePath(), m_file.filePath());
|
2013-08-14 07:13:41 +01:00
|
|
|
}
|
2013-10-08 00:36:11 +01:00
|
|
|
if (t == MOD_FOLDER)
|
2013-08-14 07:13:41 +01:00
|
|
|
{
|
|
|
|
success = copyPath(with.m_file.filePath(), m_file.path());
|
|
|
|
}
|
2013-10-08 00:36:11 +01:00
|
|
|
if (success)
|
2013-08-14 07:13:41 +01:00
|
|
|
{
|
|
|
|
m_name = with.m_name;
|
2013-12-26 04:14:32 +00:00
|
|
|
m_mmc_id = with.m_mmc_id;
|
|
|
|
m_mod_id = with.m_mod_id;
|
2013-08-14 07:13:41 +01:00
|
|
|
m_version = with.m_version;
|
2013-12-26 04:14:32 +00:00
|
|
|
m_mcversion = with.m_mcversion;
|
|
|
|
m_description = with.m_description;
|
|
|
|
m_authors = with.m_authors;
|
|
|
|
m_credits = with.m_credits;
|
|
|
|
m_homeurl = with.m_homeurl;
|
|
|
|
m_type = with.m_type;
|
|
|
|
m_file.refresh();
|
2013-08-14 07:13:41 +01:00
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Mod::destroy()
|
|
|
|
{
|
2013-10-08 00:36:11 +01:00
|
|
|
if (m_type == MOD_FOLDER)
|
2013-08-14 07:13:41 +01:00
|
|
|
{
|
|
|
|
QDir d(m_file.filePath());
|
2013-10-08 00:36:11 +01:00
|
|
|
if (d.removeRecursively())
|
2013-08-14 07:13:41 +01:00
|
|
|
{
|
|
|
|
m_type = MOD_UNKNOWN;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-07-13 14:26:26 +01:00
|
|
|
else if (m_type == MOD_SINGLEFILE || m_type == MOD_ZIPFILE || m_type == MOD_LITEMOD)
|
2013-08-14 07:13:41 +01:00
|
|
|
{
|
|
|
|
QFile f(m_file.filePath());
|
2013-10-08 00:36:11 +01:00
|
|
|
if (f.remove())
|
2013-08-14 07:13:41 +01:00
|
|
|
{
|
|
|
|
m_type = MOD_UNKNOWN;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Mod::version() const
|
|
|
|
{
|
2013-10-08 00:36:11 +01:00
|
|
|
switch (type())
|
2013-08-14 07:13:41 +01:00
|
|
|
{
|
2013-10-08 00:36:11 +01:00
|
|
|
case MOD_ZIPFILE:
|
2013-12-29 01:28:24 +00:00
|
|
|
case MOD_LITEMOD:
|
2013-10-08 00:36:11 +01:00
|
|
|
return m_version;
|
|
|
|
case MOD_FOLDER:
|
|
|
|
return "Folder";
|
|
|
|
case MOD_SINGLEFILE:
|
|
|
|
return "File";
|
|
|
|
default:
|
|
|
|
return "VOID";
|
2013-08-14 07:13:41 +01:00
|
|
|
}
|
|
|
|
}
|
2013-12-26 04:14:32 +00:00
|
|
|
|
|
|
|
bool Mod::enable(bool value)
|
|
|
|
{
|
2013-12-29 01:28:24 +00:00
|
|
|
if (m_type == Mod::MOD_UNKNOWN || m_type == Mod::MOD_FOLDER)
|
2013-12-26 04:14:32 +00:00
|
|
|
return false;
|
|
|
|
|
2013-12-29 01:28:24 +00:00
|
|
|
if (m_enabled == value)
|
2013-12-26 04:14:32 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
QString path = m_file.absoluteFilePath();
|
2013-12-29 01:28:24 +00:00
|
|
|
if (value)
|
2013-12-26 04:14:32 +00:00
|
|
|
{
|
|
|
|
QFile foo(path);
|
2013-12-29 01:28:24 +00:00
|
|
|
if (!path.endsWith(".disabled"))
|
2013-12-26 04:14:32 +00:00
|
|
|
return false;
|
|
|
|
path.chop(9);
|
2013-12-29 01:28:24 +00:00
|
|
|
if (!foo.rename(path))
|
2013-12-26 04:14:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QFile foo(path);
|
|
|
|
path += ".disabled";
|
2013-12-29 01:28:24 +00:00
|
|
|
if (!foo.rename(path))
|
2013-12-26 04:14:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_file = QFileInfo(path);
|
|
|
|
m_enabled = value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool Mod::operator==(const Mod &other) const
|
|
|
|
{
|
|
|
|
return mmc_id() == other.mmc_id();
|
|
|
|
}
|
|
|
|
bool Mod::strongCompare(const Mod &other) const
|
|
|
|
{
|
2013-12-29 01:28:24 +00:00
|
|
|
return mmc_id() == other.mmc_id() && version() == other.version() && type() == other.type();
|
2013-12-26 04:14:32 +00:00
|
|
|
}
|