-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonParser.cpp
114 lines (103 loc) · 5.54 KB
/
jsonParser.cpp
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
#include "jsonParser.h"
using namespace Sparrow;
using namespace Sparrow::Init;
using namespace Sparrow::Init::JsonParser;
const QMap<QString, versionType> jsonParser::strToVersionType = {
{"RELEASE", Sparrow::versionType::RELEASE},
{"SNAPSHOT", Sparrow::versionType::SNAPSHOT},
{"OLD_ALPHA", Sparrow::versionType::OLD_ALPHA},
{"OLD_BETA", Sparrow::versionType::OLD_BETA}
};
libraryFile jsonParser::parseLibraryFile(const QJsonObject &libraryItem) {
QString temp = ".mincraft/libraries/" + libraryItem["path"].toString();
return libraryFile(libraryItem["url"].toString(), temp.left(temp.lastIndexOf('/')), libraryItem["sha1"].toString(),
libraryItem["size"].toInt());
}
nativesLibrary jsonParser::parseNativesLibrary(const QJsonObject &nativesLibraryItem) {
nativesLibrary result;
result.nativesLibrary_Windows = parseLibraryFile(nativesLibraryItem["natives-windows"].toObject());
result.nativesLibrary_Linux = parseLibraryFile(nativesLibraryItem["natives-linux"].toObject());
result.nativesLibrary_macOS = parseLibraryFile(nativesLibraryItem["natives-macos"].toObject());
return result;
}
QHash<QString, ParserResult_minecraftVersion> jsonParser::parseMinecraftVersions(const QJsonDocument &versionManifest) {
QHash<QString, ParserResult_minecraftVersion> result;
for (QJsonValueRef i: versionManifest["versions"].toArray()) {
QJsonObject obj = i.toObject();
QString Version = obj["id"].toString();
versionType versionType = strToVersionType[obj["type"].toString()];
QUrl url = QUrl(obj["url"].toString());
QString time = obj["time"].toString(), releaseTime = obj["releaseTime"].toString();
result.insert(Version, ParserResult_minecraftVersion(Version, versionType, url, time, releaseTime));
}
return result;
}
MinecraftVersion jsonParser::parseMinecraftVersion(const QJsonDocument &versionInfo) {
QString version = versionInfo["id"].toString();
QString assetVersion = versionInfo["assets"].toString();
versionType versionType = strToVersionType[versionInfo["type"].toString()];
QString mainClass = versionInfo["mainClass"].toString();
fileInfo assetIndex(versionInfo["assetIndex"]["url"].toString(), ".minecraft/assets/indexes/",
versionInfo["assetIndex"]["sha1"].toString(), versionInfo["assetIndex"]["size"].toInt());
fileInfo clientJar(versionInfo["downloads"]["client"]["url"].toString(),
".minecraft/versions/" + versionInfo["id"].toString() + "/",
versionInfo["downloads"]["client"]["sha1"].toString(),
versionInfo["downloads"]["client"]["size"].toInt());
QVector<fileInfo> libs;
for (auto i: versionInfo["libraries"].toArray()) {
if (i.toObject()["classifiers"] == NULL) {
libs.push_back(parseLibraryFile(i.toObject()["artifact"].toObject()));
} else {
libs.push_back(nativesLibraryFile(parseLibraryFile(i.toObject()["artifact"].toObject()),
parseNativesLibrary(i.toObject()["classifiers"].toObject())));
}
}
return MinecraftVersion(version, versionType, assetIndex, assetVersion, clientJar, libs, mainClass);
}
Sparrow::Minecraft Sparrow::Init::JsonParser::jsonParser::parseMinecraft(const QDir &dir) {
for (auto i: dir.entryList()) {
if (i.endsWith(".json")) {
MinecraftVersion version = parseMinecraftVersion(
QJsonDocument::fromJson(QFile(dir.dirName() + i).readAll()));
MinecraftDirectory root = MinecraftDirectory(
dir.dirName().left(dir.dirName().lastIndexOf("/versions") - 1));
QString path = dir.dirName();
for (auto k: dir.entryList()) {
if (k.endsWith(".jar")) {
return Minecraft(version, root, path, dir.dirName() + k, dir.dirName() + i);
}
}
}
}
}
Sparrow::Minecraft::Minecraft(const QDir &dir) {
for (auto i: dir.entryList()) {
if (i.endsWith(".json")) {
this->version = jsonParser::parseMinecraftVersion(
QJsonDocument::fromJson(QFile(dir.dirName() + i).readAll()));
this->root = MinecraftDirectory(dir.dirName().left(dir.dirName().lastIndexOf("/versions") - 1));
this->path = dir.dirName();
this->jsonFile = dir.dirName() + i;
for (auto k: dir.entryList()) {
if (k.endsWith(".jar")) {
this->clientJarFile = dir.dirName() + k;
}
}
}
}
}
QHash<QString, QString>
Sparrow::Init::JsonParser::jsonParser::parseLatestVersion(const QJsonDocument &versionManifest) {
QJsonObject i = versionManifest["latest"].toObject();
QHash<QString, QString> result;
result.insert("release", i["release"].toString());
result.insert("snapshot", i["snapshot"].toString());
return result;
}
Sparrow::Init::JsonParser::ParserResult_minecraftVersion::ParserResult_minecraftVersion(const QString &id,
const versionType &type,
const QUrl &url,
const QString &time,
const QString &releaseTime)
: id(id), type(type), url(url), time(time), releaseTime(releaseTime) {
}