1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
#include "ProfileUtils.h"
#include "minecraft/VersionFilterData.h"
#include "Json.h"
#include <QDebug>
#include <QJsonDocument>
#include <QJsonArray>
#include <QRegularExpression>
#include <QSaveFile>
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);
QSaveFile orderFile(path);
if (!orderFile.open(QFile::WriteOnly))
{
qCritical() << "Couldn't open" << orderFile.fileName()
<< "for writing:" << orderFile.errorString();
return false;
}
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();
}
return true;
}
bool readOverrideOrders(QString path, PatchOrder &order)
{
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;
}
// 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;
}
// and then read it and process it if all above is true.
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));
}
auto orderArray = Json::requireArray(obj.value("order"));
for(auto item: orderArray)
{
order.append(Json::requireString(item));
}
}
catch (JSONValidationError &err)
{
qCritical() << "Couldn't parse" << orderFile.fileName() << ": bad file format";
qWarning() << "Ignoring overriden order";
order.clear();
return false;
}
return true;
}
VersionFilePtr parseJsonFile(const QFileInfo &fileInfo, const bool requireOrder)
{
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));
}
return VersionFile::fromJson(doc, file.fileName(), requireOrder);
}
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()));
}
return VersionFile::fromJson(doc, file.fileName(), false);
}
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);
}
}
|