@ -34,33 +34,29 @@
|
||||
*/
|
||||
|
||||
#include "ProfileUtils.h"
|
||||
#include "minecraft/VersionFilterData.h"
|
||||
#include "minecraft/OneSixVersionFormat.h"
|
||||
#include "Json.h"
|
||||
#include <QDebug>
|
||||
#include "Json.h"
|
||||
#include "minecraft/OneSixVersionFormat.h"
|
||||
#include "minecraft/VersionFilterData.h"
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QRegularExpression>
|
||||
#include <QSaveFile>
|
||||
|
||||
namespace ProfileUtils
|
||||
{
|
||||
namespace ProfileUtils {
|
||||
|
||||
static const int currentOrderFileVersion = 1;
|
||||
|
||||
bool readOverrideOrders(QString path, PatchOrder &order)
|
||||
bool readOverrideOrders(QString path, PatchOrder& order)
|
||||
{
|
||||
QFile orderFile(path);
|
||||
if (!orderFile.exists())
|
||||
{
|
||||
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();
|
||||
if (!orderFile.open(QFile::ReadOnly)) {
|
||||
qCritical() << "Couldn't open" << orderFile.fileName() << " for reading:" << orderFile.errorString();
|
||||
qWarning() << "Ignoring overriden order";
|
||||
return false;
|
||||
}
|
||||
@ -68,32 +64,25 @@ bool readOverrideOrders(QString path, PatchOrder &order)
|
||||
// and it's valid JSON
|
||||
QJsonParseError error;
|
||||
QJsonDocument doc = QJsonDocument::fromJson(orderFile.readAll(), &error);
|
||||
if (error.error != QJsonParseError::NoError)
|
||||
{
|
||||
if (error.error != QJsonParseError::NoError) {
|
||||
qCritical() << "Couldn't parse" << orderFile.fileName() << ":" << error.errorString();
|
||||
qWarning() << "Ignoring overriden order";
|
||||
return false;
|
||||
}
|
||||
|
||||
// and then read it and process it if all above is true.
|
||||
try
|
||||
{
|
||||
try {
|
||||
auto obj = Json::requireObject(doc);
|
||||
// check order file version.
|
||||
auto version = Json::requireInteger(obj.value("version"));
|
||||
if (version != currentOrderFileVersion)
|
||||
{
|
||||
throw JSONValidationError(QObject::tr("Invalid order file version, expected %1")
|
||||
.arg(currentOrderFileVersion));
|
||||
if (version != currentOrderFileVersion) {
|
||||
throw JSONValidationError(QObject::tr("Invalid order file version, expected %1").arg(currentOrderFileVersion));
|
||||
}
|
||||
auto orderArray = Json::requireArray(obj.value("order"));
|
||||
for(auto item: orderArray)
|
||||
{
|
||||
for (auto item : orderArray) {
|
||||
order.append(Json::requireString(item));
|
||||
}
|
||||
}
|
||||
catch ([[maybe_unused]] const JSONValidationError &err)
|
||||
{
|
||||
} catch ([[maybe_unused]] const JSONValidationError& err) {
|
||||
qCritical() << "Couldn't parse" << orderFile.fileName() << ": bad file format";
|
||||
qWarning() << "Ignoring overriden order";
|
||||
order.clear();
|
||||
@ -111,23 +100,19 @@ static VersionFilePtr createErrorVersionFile(QString fileId, QString filepath, Q
|
||||
return outError;
|
||||
}
|
||||
|
||||
static VersionFilePtr guardedParseJson(const QJsonDocument & doc,const QString &fileId,const QString &filepath,const bool &requireOrder)
|
||||
static VersionFilePtr guardedParseJson(const QJsonDocument& doc, const QString& fileId, const QString& filepath, const bool& requireOrder)
|
||||
{
|
||||
try
|
||||
{
|
||||
try {
|
||||
return OneSixVersionFormat::versionFileFromJson(doc, filepath, requireOrder);
|
||||
}
|
||||
catch (const Exception &e)
|
||||
{
|
||||
} catch (const Exception& e) {
|
||||
return createErrorVersionFile(fileId, filepath, e.cause());
|
||||
}
|
||||
}
|
||||
|
||||
VersionFilePtr parseJsonFile(const QFileInfo &fileInfo, const bool requireOrder)
|
||||
VersionFilePtr parseJsonFile(const QFileInfo& fileInfo, const bool requireOrder)
|
||||
{
|
||||
QFile file(fileInfo.absoluteFilePath());
|
||||
if (!file.open(QFile::ReadOnly))
|
||||
{
|
||||
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);
|
||||
}
|
||||
@ -135,14 +120,11 @@ VersionFilePtr parseJsonFile(const QFileInfo &fileInfo, const bool requireOrder)
|
||||
auto data = file.readAll();
|
||||
QJsonDocument doc = QJsonDocument::fromJson(data, &error);
|
||||
file.close();
|
||||
if (error.error != QJsonParseError::NoError)
|
||||
{
|
||||
if (error.error != QJsonParseError::NoError) {
|
||||
int line = 1;
|
||||
int column = 0;
|
||||
for(int i = 0; i < error.offset; i++)
|
||||
{
|
||||
if(data[i] == '\n')
|
||||
{
|
||||
for (int i = 0; i < error.offset; i++) {
|
||||
if (data[i] == '\n') {
|
||||
line++;
|
||||
column = 0;
|
||||
continue;
|
||||
@ -150,26 +132,25 @@ VersionFilePtr parseJsonFile(const QFileInfo &fileInfo, const bool requireOrder)
|
||||
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);
|
||||
.arg(fileInfo.fileName(), error.errorString())
|
||||
.arg(line)
|
||||
.arg(column);
|
||||
return createErrorVersionFile(fileInfo.completeBaseName(), fileInfo.absoluteFilePath(), errorStr);
|
||||
}
|
||||
return guardedParseJson(doc, fileInfo.completeBaseName(), fileInfo.absoluteFilePath(), requireOrder);
|
||||
}
|
||||
|
||||
bool saveJsonFile(const QJsonDocument doc, const QString & filename)
|
||||
bool saveJsonFile(const QJsonDocument doc, const QString& filename)
|
||||
{
|
||||
auto data = doc.toJson();
|
||||
QSaveFile jsonFile(filename);
|
||||
if(!jsonFile.open(QIODevice::WriteOnly))
|
||||
{
|
||||
if (!jsonFile.open(QIODevice::WriteOnly)) {
|
||||
jsonFile.cancelWriting();
|
||||
qWarning() << "Couldn't open" << filename << "for writing";
|
||||
return false;
|
||||
}
|
||||
jsonFile.write(data);
|
||||
if(!jsonFile.commit())
|
||||
{
|
||||
if (!jsonFile.commit()) {
|
||||
qWarning() << "Couldn't save" << filename;
|
||||
return false;
|
||||
}
|
||||
@ -178,13 +159,10 @@ bool saveJsonFile(const QJsonDocument doc, const QString & filename)
|
||||
|
||||
void removeLwjglFromPatch(VersionFilePtr patch)
|
||||
{
|
||||
auto filter = [](QList<LibraryPtr>& libs)
|
||||
{
|
||||
auto filter = [](QList<LibraryPtr>& libs) {
|
||||
QList<LibraryPtr> filteredLibs;
|
||||
for (auto lib : libs)
|
||||
{
|
||||
if (!g_VersionFilterData.lwjglWhitelist.contains(lib->artifactPrefix()))
|
||||
{
|
||||
for (auto lib : libs) {
|
||||
if (!g_VersionFilterData.lwjglWhitelist.contains(lib->artifactPrefix())) {
|
||||
filteredLibs.append(lib);
|
||||
}
|
||||
}
|
||||
@ -192,4 +170,4 @@ void removeLwjglFromPatch(VersionFilePtr patch)
|
||||
};
|
||||
filter(patch->libraries);
|
||||
}
|
||||
}
|
||||
} // namespace ProfileUtils
|
||||
|
Reference in New Issue
Block a user