2015-01-28 03:01:07 +05:30
|
|
|
#include "ProfileUtils.h"
|
2015-02-09 06:21:14 +05:30
|
|
|
#include "minecraft/VersionFilterData.h"
|
2017-07-24 12:31:37 +05:30
|
|
|
#include "minecraft/OneSixVersionFormat.h"
|
2015-05-28 23:08:29 +05:30
|
|
|
#include "Json.h"
|
2015-02-02 06:44:14 +05:30
|
|
|
#include <QDebug>
|
2015-01-28 03:01:07 +05:30
|
|
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QRegularExpression>
|
2015-02-28 14:50:25 +05:30
|
|
|
#include <QSaveFile>
|
2015-01-28 03:01:07 +05:30
|
|
|
|
|
|
|
namespace ProfileUtils
|
|
|
|
{
|
|
|
|
|
|
|
|
static const int currentOrderFileVersion = 1;
|
|
|
|
|
|
|
|
bool readOverrideOrders(QString path, PatchOrder &order)
|
|
|
|
{
|
|
|
|
QFile orderFile(path);
|
|
|
|
if (!orderFile.exists())
|
|
|
|
{
|
2015-02-02 06:44:14 +05:30
|
|
|
qWarning() << "Order file doesn't exist. Ignoring.";
|
2015-01-28 03:01:07 +05:30
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!orderFile.open(QFile::ReadOnly))
|
|
|
|
{
|
2015-02-02 06:44:14 +05:30
|
|
|
qCritical() << "Couldn't open" << orderFile.fileName()
|
2015-01-28 03:01:07 +05:30
|
|
|
<< " for reading:" << orderFile.errorString();
|
2015-02-02 06:44:14 +05:30
|
|
|
qWarning() << "Ignoring overriden order";
|
2015-01-28 03:01:07 +05:30
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// and it's valid JSON
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(orderFile.readAll(), &error);
|
|
|
|
if (error.error != QJsonParseError::NoError)
|
|
|
|
{
|
2015-02-02 06:44:14 +05:30
|
|
|
qCritical() << "Couldn't parse" << orderFile.fileName() << ":" << error.errorString();
|
|
|
|
qWarning() << "Ignoring overriden order";
|
2015-01-28 03:01:07 +05:30
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// and then read it and process it if all above is true.
|
|
|
|
try
|
|
|
|
{
|
2015-06-04 01:27:22 +05:30
|
|
|
auto obj = Json::requireObject(doc);
|
2015-01-28 03:01:07 +05:30
|
|
|
// check order file version.
|
2015-06-04 01:27:22 +05:30
|
|
|
auto version = Json::requireInteger(obj.value("version"));
|
2015-01-28 03:01:07 +05:30
|
|
|
if (version != currentOrderFileVersion)
|
|
|
|
{
|
|
|
|
throw JSONValidationError(QObject::tr("Invalid order file version, expected %1")
|
|
|
|
.arg(currentOrderFileVersion));
|
|
|
|
}
|
2015-06-04 01:27:22 +05:30
|
|
|
auto orderArray = Json::requireArray(obj.value("order"));
|
2015-01-28 03:01:07 +05:30
|
|
|
for(auto item: orderArray)
|
|
|
|
{
|
2015-06-04 01:27:22 +05:30
|
|
|
order.append(Json::requireString(item));
|
2015-01-28 03:01:07 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (JSONValidationError &err)
|
|
|
|
{
|
2015-02-02 06:44:14 +05:30
|
|
|
qCritical() << "Couldn't parse" << orderFile.fileName() << ": bad file format";
|
|
|
|
qWarning() << "Ignoring overriden order";
|
2015-01-28 03:01:07 +05:30
|
|
|
order.clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-18 19:32:54 +05:30
|
|
|
static VersionFilePtr createErrorVersionFile(QString fileId, QString filepath, QString error)
|
|
|
|
{
|
|
|
|
auto outError = std::make_shared<VersionFile>();
|
2017-03-20 04:28:54 +05:30
|
|
|
outError->uid = outError->name = fileId;
|
2017-03-27 07:04:39 +05:30
|
|
|
// outError->filename = filepath;
|
|
|
|
outError->addProblem(ProblemSeverity::Error, error);
|
2016-03-18 19:32:54 +05:30
|
|
|
return outError;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VersionFilePtr guardedParseJson(const QJsonDocument & doc,const QString &fileId,const QString &filepath,const bool &requireOrder)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return OneSixVersionFormat::versionFileFromJson(doc, filepath, requireOrder);
|
|
|
|
}
|
|
|
|
catch (Exception & e)
|
|
|
|
{
|
|
|
|
return createErrorVersionFile(fileId, filepath, e.cause());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-04 05:31:52 +05:30
|
|
|
VersionFilePtr parseJsonFile(const QFileInfo &fileInfo, const bool requireOrder)
|
2015-01-28 03:01:07 +05:30
|
|
|
{
|
|
|
|
QFile file(fileInfo.absoluteFilePath());
|
|
|
|
if (!file.open(QFile::ReadOnly))
|
|
|
|
{
|
2016-03-18 19:32:54 +05:30
|
|
|
auto errorStr = QObject::tr("Unable to open the version file %1: %2.").arg(fileInfo.fileName(), file.errorString());
|
|
|
|
return createErrorVersionFile(fileInfo.completeBaseName(), fileInfo.absoluteFilePath(), errorStr);
|
2015-01-28 03:01:07 +05:30
|
|
|
}
|
|
|
|
QJsonParseError error;
|
2016-02-21 06:14:27 +05:30
|
|
|
auto data = file.readAll();
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(data, &error);
|
2016-03-18 19:32:54 +05:30
|
|
|
file.close();
|
2015-01-28 03:01:07 +05:30
|
|
|
if (error.error != QJsonParseError::NoError)
|
|
|
|
{
|
2016-03-18 19:32:54 +05:30
|
|
|
int line = 1;
|
2016-02-21 06:14:27 +05:30
|
|
|
int column = 0;
|
|
|
|
for(int i = 0; i < error.offset; i++)
|
|
|
|
{
|
|
|
|
if(data[i] == '\n')
|
|
|
|
{
|
|
|
|
line++;
|
|
|
|
column = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
column++;
|
|
|
|
}
|
2016-03-18 19:32:54 +05:30
|
|
|
auto errorStr = QObject::tr("Unable to process the version file %1: %2 at line %3 column %4.")
|
2015-01-28 03:01:07 +05:30
|
|
|
.arg(fileInfo.fileName(), error.errorString())
|
2016-03-18 19:32:54 +05:30
|
|
|
.arg(line).arg(column);
|
|
|
|
return createErrorVersionFile(fileInfo.completeBaseName(), fileInfo.absoluteFilePath(), errorStr);
|
2015-01-28 03:01:07 +05:30
|
|
|
}
|
2016-03-18 19:32:54 +05:30
|
|
|
return guardedParseJson(doc, fileInfo.completeBaseName(), fileInfo.absoluteFilePath(), requireOrder);
|
2015-01-28 03:01:07 +05:30
|
|
|
}
|
|
|
|
|
2017-11-11 06:08:31 +05:30
|
|
|
bool saveJsonFile(const QJsonDocument doc, const QString & filename)
|
|
|
|
{
|
|
|
|
auto data = doc.toJson();
|
|
|
|
QSaveFile jsonFile(filename);
|
|
|
|
if(!jsonFile.open(QIODevice::WriteOnly))
|
|
|
|
{
|
|
|
|
jsonFile.cancelWriting();
|
|
|
|
qWarning() << "Couldn't open" << filename << "for writing";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
jsonFile.write(data);
|
|
|
|
if(!jsonFile.commit())
|
|
|
|
{
|
|
|
|
qWarning() << "Couldn't save" << filename;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-28 03:01:07 +05:30
|
|
|
VersionFilePtr parseBinaryJsonFile(const QFileInfo &fileInfo)
|
|
|
|
{
|
|
|
|
QFile file(fileInfo.absoluteFilePath());
|
|
|
|
if (!file.open(QFile::ReadOnly))
|
|
|
|
{
|
2016-03-18 19:32:54 +05:30
|
|
|
auto errorStr = QObject::tr("Unable to open the version file %1: %2.").arg(fileInfo.fileName(), file.errorString());
|
|
|
|
return createErrorVersionFile(fileInfo.completeBaseName(), fileInfo.absoluteFilePath(), errorStr);
|
2015-01-28 03:01:07 +05:30
|
|
|
}
|
|
|
|
QJsonDocument doc = QJsonDocument::fromBinaryData(file.readAll());
|
|
|
|
file.close();
|
|
|
|
if (doc.isNull())
|
|
|
|
{
|
|
|
|
file.remove();
|
2016-03-18 19:32:54 +05:30
|
|
|
throw JSONValidationError(QObject::tr("Unable to process the version file %1.").arg(fileInfo.fileName()));
|
2015-01-28 03:01:07 +05:30
|
|
|
}
|
2016-03-18 19:32:54 +05:30
|
|
|
return guardedParseJson(doc, fileInfo.completeBaseName(), fileInfo.absoluteFilePath(), false);
|
2015-01-28 03:01:07 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void removeLwjglFromPatch(VersionFilePtr patch)
|
|
|
|
{
|
2016-03-07 06:31:28 +05:30
|
|
|
auto filter = [](QList<LibraryPtr>& libs)
|
2015-01-28 03:01:07 +05:30
|
|
|
{
|
2016-03-07 06:31:28 +05:30
|
|
|
QList<LibraryPtr> filteredLibs;
|
2015-01-28 03:01:07 +05:30
|
|
|
for (auto lib : libs)
|
|
|
|
{
|
|
|
|
if (!g_VersionFilterData.lwjglWhitelist.contains(lib->artifactPrefix()))
|
|
|
|
{
|
|
|
|
filteredLibs.append(lib);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
libs = filteredLibs;
|
|
|
|
};
|
2016-03-13 06:58:55 +05:30
|
|
|
filter(patch->libraries);
|
2015-01-28 03:01:07 +05:30
|
|
|
}
|
|
|
|
}
|