2015-01-27 21:31:07 +00:00
|
|
|
#include "ProfileUtils.h"
|
2015-02-09 00:51:14 +00:00
|
|
|
#include "minecraft/VersionFilterData.h"
|
2017-07-24 07:01:37 +00:00
|
|
|
#include "minecraft/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 readOverrideOrders(QString path, PatchOrder &order)
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
QFile orderFile(path);
|
|
|
|
if (!orderFile.exists())
|
|
|
|
{
|
|
|
|
qWarning() << "Order file doesn't exist. Ignoring.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!orderFile.open(QFile::ReadOnly))
|
|
|
|
{
|
|
|
|
qCritical() << "Couldn't open" << orderFile.fileName()
|
|
|
|
<< " for reading:" << orderFile.errorString();
|
|
|
|
qWarning() << "Ignoring overriden order";
|
|
|
|
return false;
|
|
|
|
}
|
2015-01-27 21:31:07 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
// and it's valid JSON
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(orderFile.readAll(), &error);
|
|
|
|
if (error.error != QJsonParseError::NoError)
|
|
|
|
{
|
|
|
|
qCritical() << "Couldn't parse" << orderFile.fileName() << ":" << error.errorString();
|
|
|
|
qWarning() << "Ignoring overriden order";
|
|
|
|
return false;
|
|
|
|
}
|
2015-01-27 21:31:07 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
// and then read it and process it if all above is true.
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto obj = Json::requireObject(doc);
|
|
|
|
// check order file version.
|
2022-05-29 02:58:42 +00:00
|
|
|
auto version = Json::requireValueInteger(obj.value("version"));
|
2018-07-15 12:51:05 +00:00
|
|
|
if (version != currentOrderFileVersion)
|
|
|
|
{
|
|
|
|
throw JSONValidationError(QObject::tr("Invalid order file version, expected %1")
|
|
|
|
.arg(currentOrderFileVersion));
|
|
|
|
}
|
2022-05-29 02:58:42 +00:00
|
|
|
auto orderArray = Json::requireValueArray(obj.value("order"));
|
2018-07-15 12:51:05 +00:00
|
|
|
for(auto item: orderArray)
|
|
|
|
{
|
2022-05-29 02:58:42 +00:00
|
|
|
order.append(Json::requireValueString(item));
|
2018-07-15 12:51:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const JSONValidationError &err)
|
|
|
|
{
|
|
|
|
qCritical() << "Couldn't parse" << orderFile.fileName() << ": bad file format";
|
|
|
|
qWarning() << "Ignoring overriden order";
|
|
|
|
order.clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2015-01-27 21:31:07 +00:00
|
|
|
}
|
|
|
|
|
2016-03-18 14:02:54 +00:00
|
|
|
static VersionFilePtr createErrorVersionFile(QString fileId, QString filepath, QString error)
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
auto outError = std::make_shared<VersionFile>();
|
|
|
|
outError->uid = outError->name = fileId;
|
|
|
|
// outError->filename = filepath;
|
|
|
|
outError->addProblem(ProblemSeverity::Error, error);
|
|
|
|
return outError;
|
2016-03-18 14:02:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static VersionFilePtr guardedParseJson(const QJsonDocument & doc,const QString &fileId,const QString &filepath,const bool &requireOrder)
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return OneSixVersionFormat::versionFileFromJson(doc, filepath, requireOrder);
|
|
|
|
}
|
|
|
|
catch (const Exception &e)
|
|
|
|
{
|
|
|
|
return createErrorVersionFile(fileId, filepath, e.cause());
|
|
|
|
}
|
2016-03-18 14:02:54 +00:00
|
|
|
}
|
|
|
|
|
2015-04-04 00:01:52 +00:00
|
|
|
VersionFilePtr parseJsonFile(const QFileInfo &fileInfo, const bool requireOrder)
|
2015-01-27 21:31:07 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
QFile file(fileInfo.absoluteFilePath());
|
|
|
|
if (!file.open(QFile::ReadOnly))
|
|
|
|
{
|
|
|
|
auto errorStr = QObject::tr("Unable to open the version file %1: %2.").arg(fileInfo.fileName(), file.errorString());
|
|
|
|
return createErrorVersionFile(fileInfo.completeBaseName(), fileInfo.absoluteFilePath(), errorStr);
|
|
|
|
}
|
|
|
|
QJsonParseError error;
|
|
|
|
auto data = file.readAll();
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(data, &error);
|
|
|
|
file.close();
|
|
|
|
if (error.error != QJsonParseError::NoError)
|
|
|
|
{
|
|
|
|
int line = 1;
|
|
|
|
int column = 0;
|
|
|
|
for(int i = 0; i < error.offset; i++)
|
|
|
|
{
|
|
|
|
if(data[i] == '\n')
|
|
|
|
{
|
|
|
|
line++;
|
|
|
|
column = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
column++;
|
|
|
|
}
|
|
|
|
auto errorStr = QObject::tr("Unable to process the version file %1: %2 at line %3 column %4.")
|
|
|
|
.arg(fileInfo.fileName(), error.errorString())
|
|
|
|
.arg(line).arg(column);
|
|
|
|
return createErrorVersionFile(fileInfo.completeBaseName(), fileInfo.absoluteFilePath(), errorStr);
|
|
|
|
}
|
|
|
|
return guardedParseJson(doc, fileInfo.completeBaseName(), fileInfo.absoluteFilePath(), requireOrder);
|
2015-01-27 21:31:07 +00:00
|
|
|
}
|
|
|
|
|
2017-11-11 00:38:31 +00:00
|
|
|
bool saveJsonFile(const QJsonDocument doc, const QString & filename)
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
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;
|
2017-11-11 00:38:31 +00:00
|
|
|
}
|
|
|
|
|
2015-01-27 21:31:07 +00:00
|
|
|
VersionFilePtr parseBinaryJsonFile(const QFileInfo &fileInfo)
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
QFile file(fileInfo.absoluteFilePath());
|
|
|
|
if (!file.open(QFile::ReadOnly))
|
|
|
|
{
|
|
|
|
auto errorStr = QObject::tr("Unable to open the version file %1: %2.").arg(fileInfo.fileName(), file.errorString());
|
|
|
|
return createErrorVersionFile(fileInfo.completeBaseName(), fileInfo.absoluteFilePath(), errorStr);
|
|
|
|
}
|
|
|
|
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()));
|
|
|
|
}
|
|
|
|
return guardedParseJson(doc, fileInfo.completeBaseName(), fileInfo.absoluteFilePath(), false);
|
2015-01-27 21:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void removeLwjglFromPatch(VersionFilePtr patch)
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
auto filter = [](QList<LibraryPtr>& libs)
|
|
|
|
{
|
|
|
|
QList<LibraryPtr> filteredLibs;
|
|
|
|
for (auto lib : libs)
|
|
|
|
{
|
|
|
|
if (!g_VersionFilterData.lwjglWhitelist.contains(lib->artifactPrefix()))
|
|
|
|
{
|
|
|
|
filteredLibs.append(lib);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
libs = filteredLibs;
|
|
|
|
};
|
|
|
|
filter(patch->libraries);
|
2015-01-27 21:31:07 +00:00
|
|
|
}
|
|
|
|
}
|