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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
/* Copyright 2013-2015 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.
*/
#include "LiteLoaderVersionList.h"
#include "logic/Env.h"
#include "logic/net/URLConstants.h"
#include <MMCError.h>
#include <QtXml>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include <QJsonParseError>
#include <QtAlgorithms>
#include <QtNetwork>
LiteLoaderVersionList::LiteLoaderVersionList(QObject *parent) : BaseVersionList(parent)
{
}
Task *LiteLoaderVersionList::getLoadTask()
{
return new LLListLoadTask(this);
}
bool LiteLoaderVersionList::isLoaded()
{
return m_loaded;
}
const BaseVersionPtr LiteLoaderVersionList::at(int i) const
{
return m_vlist.at(i);
}
int LiteLoaderVersionList::count() const
{
return m_vlist.count();
}
static bool cmpVersions(BaseVersionPtr first, BaseVersionPtr second)
{
auto left = std::dynamic_pointer_cast<LiteLoaderVersion>(first);
auto right = std::dynamic_pointer_cast<LiteLoaderVersion>(second);
return left->timestamp > right->timestamp;
}
void LiteLoaderVersionList::sort()
{
beginResetModel();
std::sort(m_vlist.begin(), m_vlist.end(), cmpVersions);
endResetModel();
}
BaseVersionPtr LiteLoaderVersionList::getLatestStable() const
{
for (int i = 0; i < m_vlist.length(); i++)
{
auto ver = std::dynamic_pointer_cast<LiteLoaderVersion>(m_vlist.at(i));
if (ver->isLatest)
{
return m_vlist.at(i);
}
}
return BaseVersionPtr();
}
void LiteLoaderVersionList::updateListData(QList<BaseVersionPtr> versions)
{
beginResetModel();
m_vlist = versions;
m_loaded = true;
std::sort(m_vlist.begin(), m_vlist.end(), cmpVersions);
endResetModel();
}
LLListLoadTask::LLListLoadTask(LiteLoaderVersionList *vlist)
{
m_list = vlist;
}
LLListLoadTask::~LLListLoadTask()
{
}
void LLListLoadTask::executeTask()
{
setStatus(tr("Loading LiteLoader version list..."));
auto job = new NetJob("Version index");
// we do not care if the version is stale or not.
auto liteloaderEntry = ENV.metacache()->resolveEntry("liteloader", "versions.json");
// verify by poking the server.
liteloaderEntry->stale = true;
job->addNetAction(listDownload = CacheDownload::make(QUrl(URLConstants::LITELOADER_URL),
liteloaderEntry));
connect(listDownload.get(), SIGNAL(failed(int)), SLOT(listFailed()));
listJob.reset(job);
connect(listJob.get(), SIGNAL(succeeded()), SLOT(listDownloaded()));
connect(listJob.get(), SIGNAL(progress(qint64, qint64)), SIGNAL(progress(qint64, qint64)));
listJob->start();
}
void LLListLoadTask::listFailed()
{
emitFailed("Failed to load LiteLoader version list.");
return;
}
void LLListLoadTask::listDownloaded()
{
QByteArray data;
{
auto dlJob = listDownload;
auto filename = std::dynamic_pointer_cast<CacheDownload>(dlJob)->getTargetFilepath();
QFile listFile(filename);
if (!listFile.open(QIODevice::ReadOnly))
{
emitFailed("Failed to open the LiteLoader version list.");
return;
}
data = listFile.readAll();
listFile.close();
dlJob.reset();
}
QJsonParseError jsonError;
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
if (jsonError.error != QJsonParseError::NoError)
{
emitFailed("Error parsing version list JSON:" + jsonError.errorString());
return;
}
if (!jsonDoc.isObject())
{
emitFailed("Error parsing version list JSON: jsonDoc is not an object");
return;
}
const QJsonObject root = jsonDoc.object();
// Now, get the array of versions.
if (!root.value("versions").isObject())
{
emitFailed("Error parsing version list JSON: missing 'versions' object");
return;
}
auto meta = root.value("meta").toObject();
QString description = meta.value("description").toString(tr("This is a lightweight loader for mods that don't change game mechanics."));
QString defaultUrl = meta.value("url").toString("http://dl.liteloader.com");
QString authors = meta.value("authors").toString("Mumfrey");
auto versions = root.value("versions").toObject();
QList<BaseVersionPtr> tempList;
for (auto vIt = versions.begin(); vIt != versions.end(); ++vIt)
{
const QString mcVersion = vIt.key();
QString latest;
const QJsonObject artefacts = vIt.value()
.toObject()
.value("artefacts")
.toObject()
.value("com.mumfrey:liteloader")
.toObject();
QList<BaseVersionPtr> perMcVersionList;
for (auto aIt = artefacts.begin(); aIt != artefacts.end(); ++aIt)
{
const QString identifier = aIt.key();
const QJsonObject artefact = aIt.value().toObject();
if (identifier == "latest")
{
latest = artefact.value("version").toString();
continue;
}
LiteLoaderVersionPtr version(new LiteLoaderVersion());
version->version = artefact.value("version").toString();
version->file = artefact.value("file").toString();
version->mcVersion = mcVersion;
version->md5 = artefact.value("md5").toString();
version->timestamp = artefact.value("timestamp").toString().toInt();
version->tweakClass = artefact.value("tweakClass").toString();
version->authors = authors;
version->description = description;
version->defaultUrl = defaultUrl;
const QJsonArray libs = artefact.value("libraries").toArray();
for (auto lIt = libs.begin(); lIt != libs.end(); ++lIt)
{
auto libobject = (*lIt).toObject();
try
{
auto lib = RawLibrary::fromJson(libobject, "versions.json");
// hack to make liteloader 1.7.10_00 work
if(lib->rawName() == GradleSpecifier("org.ow2.asm:asm-all:5.0.3"))
{
lib->m_base_url = "http://repo.maven.apache.org/maven2/";
}
version->libraries.append(lib);
}
catch (MMCError &e)
{
QLOG_ERROR() << "Couldn't read JSON object:";
continue;
}
}
perMcVersionList.append(version);
}
for (auto version : perMcVersionList)
{
auto v = std::dynamic_pointer_cast<LiteLoaderVersion>(version);
v->isLatest = v->version == latest;
}
tempList.append(perMcVersionList);
}
m_list->updateListData(tempList);
emitSucceeded();
}
|