2015-01-28 03:01:07 +05:30
|
|
|
#include "ProfileUtils.h"
|
2015-02-09 06:21:14 +05:30
|
|
|
#include "minecraft/VersionFilterData.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 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 14:50:25 +05:30
|
|
|
QSaveFile orderFile(path);
|
2015-01-28 03:01:07 +05:30
|
|
|
if (!orderFile.open(QFile::WriteOnly))
|
|
|
|
{
|
2015-02-02 06:44:14 +05:30
|
|
|
qCritical() << "Couldn't open" << orderFile.fileName()
|
2015-01-28 03:01:07 +05:30
|
|
|
<< "for writing:" << orderFile.errorString();
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-28 14:50:25 +05:30
|
|
|
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-28 03:01:07 +05:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-28 23:08:29 +05:30
|
|
|
auto obj = Json::ensureObject(doc);
|
2015-01-28 03:01:07 +05:30
|
|
|
// check order file version.
|
2015-05-28 23:08:29 +05:30
|
|
|
auto version = Json::ensureInteger(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-05-28 23:08:29 +05:30
|
|
|
auto orderArray = Json::ensureArray(obj.value("order"));
|
2015-01-28 03:01:07 +05:30
|
|
|
for(auto item: orderArray)
|
|
|
|
{
|
2015-05-28 23:08:29 +05:30
|
|
|
order.append(Json::ensureString(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;
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
throw JSONValidationError(QObject::tr("Unable to open the version file %1: %2.")
|
|
|
|
.arg(fileInfo.fileName(), file.errorString()));
|
|
|
|
}
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(file.readAll(), &error);
|
|
|
|
if (error.error != QJsonParseError::NoError)
|
|
|
|
{
|
|
|
|
throw JSONValidationError(
|
|
|
|
QObject::tr("Unable to process the version file %1: %2 at %3.")
|
|
|
|
.arg(fileInfo.fileName(), error.errorString())
|
|
|
|
.arg(error.offset));
|
|
|
|
}
|
2015-04-04 05:31:52 +05:30
|
|
|
return VersionFile::fromJson(doc, file.fileName(), requireOrder);
|
2015-01-28 03:01:07 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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()));
|
|
|
|
}
|
2015-04-04 05:31:52 +05:30
|
|
|
return VersionFile::fromJson(doc, file.fileName(), false);
|
2015-01-28 03:01:07 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void removeLwjglFromPatch(VersionFilePtr patch)
|
|
|
|
{
|
|
|
|
auto filter = [](QList<RawLibraryPtr>& libs)
|
|
|
|
{
|
|
|
|
QList<RawLibraryPtr> filteredLibs;
|
|
|
|
for (auto lib : libs)
|
|
|
|
{
|
|
|
|
if (!g_VersionFilterData.lwjglWhitelist.contains(lib->artifactPrefix()))
|
|
|
|
{
|
|
|
|
filteredLibs.append(lib);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
libs = filteredLibs;
|
|
|
|
};
|
|
|
|
filter(patch->addLibs);
|
|
|
|
filter(patch->overwriteLibs);
|
|
|
|
}
|
|
|
|
}
|