2022-05-26 22:18:54 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 MultiMC Contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
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 08:01:37 +01:00
|
|
|
#include "minecraft/OneSixVersionFormat.h"
|
2015-05-28 18:38:29 +01: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)
|
|
|
|
{
|
|
|
|
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 20:57:22 +01:00
|
|
|
auto obj = Json::requireObject(doc);
|
2015-01-27 21:31:07 +00:00
|
|
|
// check order file version.
|
2015-06-03 20:57:22 +01: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 20:57:22 +01:00
|
|
|
auto orderArray = Json::requireArray(obj.value("order"));
|
2015-01-27 21:31:07 +00:00
|
|
|
for(auto item: orderArray)
|
|
|
|
{
|
2015-06-03 20:57:22 +01:00
|
|
|
order.append(Json::requireString(item));
|
2015-01-27 21:31:07 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-01 07:51:15 +01:00
|
|
|
catch ([[maybe_unused]] const JSONValidationError &err)
|
2015-01-27 21:31:07 +00:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2016-03-18 14:02:54 +00:00
|
|
|
static VersionFilePtr createErrorVersionFile(QString fileId, QString filepath, QString error)
|
|
|
|
{
|
|
|
|
auto outError = std::make_shared<VersionFile>();
|
2017-03-19 22:58:54 +00:00
|
|
|
outError->uid = outError->name = fileId;
|
2017-03-27 02:34:39 +01:00
|
|
|
// outError->filename = filepath;
|
|
|
|
outError->addProblem(ProblemSeverity::Error, error);
|
2016-03-18 14:02:54 +00:00
|
|
|
return outError;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VersionFilePtr guardedParseJson(const QJsonDocument & doc,const QString &fileId,const QString &filepath,const bool &requireOrder)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return OneSixVersionFormat::versionFileFromJson(doc, filepath, requireOrder);
|
|
|
|
}
|
2018-05-20 00:18:26 +01:00
|
|
|
catch (const Exception &e)
|
2016-03-18 14:02:54 +00:00
|
|
|
{
|
|
|
|
return createErrorVersionFile(fileId, filepath, e.cause());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-04 01:01:52 +01: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))
|
|
|
|
{
|
2016-03-18 14:02:54 +00:00
|
|
|
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-27 21:31:07 +00:00
|
|
|
}
|
|
|
|
QJsonParseError error;
|
2016-02-21 00:44:27 +00:00
|
|
|
auto data = file.readAll();
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(data, &error);
|
2016-03-18 14:02:54 +00:00
|
|
|
file.close();
|
2015-01-27 21:31:07 +00:00
|
|
|
if (error.error != QJsonParseError::NoError)
|
|
|
|
{
|
2016-03-18 14:02:54 +00:00
|
|
|
int line = 1;
|
2016-02-21 00:44:27 +00:00
|
|
|
int column = 0;
|
|
|
|
for(int i = 0; i < error.offset; i++)
|
|
|
|
{
|
|
|
|
if(data[i] == '\n')
|
|
|
|
{
|
|
|
|
line++;
|
|
|
|
column = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
column++;
|
|
|
|
}
|
2016-03-18 14:02:54 +00:00
|
|
|
auto errorStr = 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-03-18 14:02:54 +00:00
|
|
|
.arg(line).arg(column);
|
|
|
|
return createErrorVersionFile(fileInfo.completeBaseName(), fileInfo.absoluteFilePath(), errorStr);
|
2015-01-27 21:31:07 +00:00
|
|
|
}
|
2016-03-18 14:02:54 +00:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
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-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
|
|
|
}
|
|
|
|
}
|