2015-01-27 21:31:07 +00:00
|
|
|
#include "ProfileUtils.h"
|
2015-02-09 00:51:14 +00:00
|
|
|
#include "minecraft/VersionFilterData.h"
|
2016-02-27 21:02:56 +00:00
|
|
|
#include "minecraft/onesix/OneSixVersionFormat.h"
|
2015-05-28 17:38:29 +00:00
|
|
|
#include "Json.h"
|
2015-02-02 01:14:14 +00:00
|
|
|
#include <QDebug>
|
2015-01-27 21:31:07 +00:00
|
|
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QRegularExpression>
|
2015-02-28 09:20:25 +00:00
|
|
|
#include <QSaveFile>
|
2015-01-27 21:31:07 +00:00
|
|
|
|
|
|
|
namespace ProfileUtils
|
|
|
|
{
|
|
|
|
|
|
|
|
static const int currentOrderFileVersion = 1;
|
|
|
|
|
|
|
|
bool writeOverrideOrders(QString path, const PatchOrder &order)
|
|
|
|
{
|
|
|
|
QJsonObject obj;
|
|
|
|
obj.insert("version", currentOrderFileVersion);
|
|
|
|
QJsonArray orderArray;
|
|
|
|
for(auto str: order)
|
|
|
|
{
|
|
|
|
orderArray.append(str);
|
|
|
|
}
|
|
|
|
obj.insert("order", orderArray);
|
2015-02-28 09:20:25 +00:00
|
|
|
QSaveFile orderFile(path);
|
2015-01-27 21:31:07 +00:00
|
|
|
if (!orderFile.open(QFile::WriteOnly))
|
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "Couldn't open" << orderFile.fileName()
|
2015-01-27 21:31:07 +00:00
|
|
|
<< "for writing:" << orderFile.errorString();
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-28 09:20:25 +00:00
|
|
|
auto data = QJsonDocument(obj).toJson(QJsonDocument::Indented);
|
|
|
|
if(orderFile.write(data) != data.size())
|
|
|
|
{
|
|
|
|
qCritical() << "Couldn't write all the data into" << orderFile.fileName()
|
|
|
|
<< "because:" << orderFile.errorString();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!orderFile.commit())
|
|
|
|
{
|
|
|
|
qCritical() << "Couldn't save" << orderFile.fileName()
|
|
|
|
<< "because:" << orderFile.errorString();
|
|
|
|
}
|
2015-01-27 21:31:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool readOverrideOrders(QString path, PatchOrder &order)
|
|
|
|
{
|
|
|
|
QFile orderFile(path);
|
|
|
|
if (!orderFile.exists())
|
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qWarning() << "Order file doesn't exist. Ignoring.";
|
2015-01-27 21:31:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!orderFile.open(QFile::ReadOnly))
|
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "Couldn't open" << orderFile.fileName()
|
2015-01-27 21:31:07 +00:00
|
|
|
<< " for reading:" << orderFile.errorString();
|
2015-02-02 01:14:14 +00:00
|
|
|
qWarning() << "Ignoring overriden order";
|
2015-01-27 21:31:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// and it's valid JSON
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(orderFile.readAll(), &error);
|
|
|
|
if (error.error != QJsonParseError::NoError)
|
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "Couldn't parse" << orderFile.fileName() << ":" << error.errorString();
|
|
|
|
qWarning() << "Ignoring overriden order";
|
2015-01-27 21:31:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// and then read it and process it if all above is true.
|
|
|
|
try
|
|
|
|
{
|
2015-06-03 19:57:22 +00:00
|
|
|
auto obj = Json::requireObject(doc);
|
2015-01-27 21:31:07 +00:00
|
|
|
// check order file version.
|
2015-06-03 19:57:22 +00:00
|
|
|
auto version = Json::requireInteger(obj.value("version"));
|
2015-01-27 21:31:07 +00:00
|
|
|
if (version != currentOrderFileVersion)
|
|
|
|
{
|
|
|
|
throw JSONValidationError(QObject::tr("Invalid order file version, expected %1")
|
|
|
|
.arg(currentOrderFileVersion));
|
|
|
|
}
|
2015-06-03 19:57:22 +00:00
|
|
|
auto orderArray = Json::requireArray(obj.value("order"));
|
2015-01-27 21:31:07 +00:00
|
|
|
for(auto item: orderArray)
|
|
|
|
{
|
2015-06-03 19:57:22 +00:00
|
|
|
order.append(Json::requireString(item));
|
2015-01-27 21:31:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (JSONValidationError &err)
|
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "Couldn't parse" << orderFile.fileName() << ": bad file format";
|
|
|
|
qWarning() << "Ignoring overriden order";
|
2015-01-27 21:31:07 +00:00
|
|
|
order.clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-04 00:01:52 +00:00
|
|
|
VersionFilePtr parseJsonFile(const QFileInfo &fileInfo, const bool requireOrder)
|
2015-01-27 21:31:07 +00:00
|
|
|
{
|
|
|
|
QFile file(fileInfo.absoluteFilePath());
|
|
|
|
if (!file.open(QFile::ReadOnly))
|
|
|
|
{
|
|
|
|
throw JSONValidationError(QObject::tr("Unable to open the version file %1: %2.")
|
|
|
|
.arg(fileInfo.fileName(), file.errorString()));
|
|
|
|
}
|
|
|
|
QJsonParseError error;
|
2016-02-21 00:44:27 +00:00
|
|
|
auto data = file.readAll();
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(data, &error);
|
2015-01-27 21:31:07 +00:00
|
|
|
if (error.error != QJsonParseError::NoError)
|
|
|
|
{
|
2016-02-21 00:44:27 +00:00
|
|
|
int line = 0;
|
|
|
|
int column = 0;
|
|
|
|
for(int i = 0; i < error.offset; i++)
|
|
|
|
{
|
|
|
|
if(data[i] == '\n')
|
|
|
|
{
|
|
|
|
line++;
|
|
|
|
column = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
column++;
|
|
|
|
}
|
2015-01-27 21:31:07 +00:00
|
|
|
throw JSONValidationError(
|
2016-02-21 00:44:27 +00:00
|
|
|
QObject::tr("Unable to process the version file %1: %2 at line %3 column %4.")
|
2015-01-27 21:31:07 +00:00
|
|
|
.arg(fileInfo.fileName(), error.errorString())
|
2016-02-21 00:44:27 +00:00
|
|
|
.arg(line).arg(column));
|
2015-01-27 21:31:07 +00:00
|
|
|
}
|
2016-02-28 18:01:54 +00:00
|
|
|
return OneSixVersionFormat::versionFileFromJson(doc, file.fileName(), requireOrder);
|
2015-01-27 21:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VersionFilePtr parseBinaryJsonFile(const QFileInfo &fileInfo)
|
|
|
|
{
|
|
|
|
QFile file(fileInfo.absoluteFilePath());
|
|
|
|
if (!file.open(QFile::ReadOnly))
|
|
|
|
{
|
|
|
|
throw JSONValidationError(QObject::tr("Unable to open the version file %1: %2.")
|
|
|
|
.arg(fileInfo.fileName(), file.errorString()));
|
|
|
|
}
|
|
|
|
QJsonDocument doc = QJsonDocument::fromBinaryData(file.readAll());
|
|
|
|
file.close();
|
|
|
|
if (doc.isNull())
|
|
|
|
{
|
|
|
|
file.remove();
|
|
|
|
throw JSONValidationError(
|
|
|
|
QObject::tr("Unable to process the version file %1.").arg(fileInfo.fileName()));
|
|
|
|
}
|
2016-02-28 18:01:54 +00:00
|
|
|
return OneSixVersionFormat::versionFileFromJson(doc, file.fileName(), false);
|
2015-01-27 21:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void removeLwjglFromPatch(VersionFilePtr patch)
|
|
|
|
{
|
2016-03-07 01:01:28 +00:00
|
|
|
auto filter = [](QList<LibraryPtr>& libs)
|
2015-01-27 21:31:07 +00:00
|
|
|
{
|
2016-03-07 01:01:28 +00:00
|
|
|
QList<LibraryPtr> filteredLibs;
|
2015-01-27 21:31:07 +00:00
|
|
|
for (auto lib : libs)
|
|
|
|
{
|
|
|
|
if (!g_VersionFilterData.lwjglWhitelist.contains(lib->artifactPrefix()))
|
|
|
|
{
|
|
|
|
filteredLibs.append(lib);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
libs = filteredLibs;
|
|
|
|
};
|
2016-03-13 01:28:55 +00:00
|
|
|
filter(patch->libraries);
|
2015-01-27 21:31:07 +00:00
|
|
|
}
|
|
|
|
}
|