2015-01-31 15:59:03 +00:00
|
|
|
#include "logic/Env.h"
|
2013-11-17 10:44:18 +00:00
|
|
|
#include "ForgeMirrors.h"
|
2015-02-02 01:14:14 +00:00
|
|
|
#include <QDebug>
|
2013-11-17 10:44:18 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <random>
|
|
|
|
|
|
|
|
ForgeMirrors::ForgeMirrors(QList<ForgeXzDownloadPtr> &libs, NetJobPtr parent_job,
|
|
|
|
QString mirrorlist)
|
|
|
|
{
|
|
|
|
m_libs = libs;
|
|
|
|
m_parent_job = parent_job;
|
|
|
|
m_url = QUrl(mirrorlist);
|
|
|
|
m_status = Job_NotStarted;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ForgeMirrors::start()
|
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qDebug() << "Downloading " << m_url.toString();
|
2013-11-17 10:44:18 +00:00
|
|
|
QNetworkRequest request(m_url);
|
|
|
|
request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)");
|
2015-01-31 15:59:03 +00:00
|
|
|
auto worker = ENV.qnam();
|
2013-11-17 10:44:18 +00:00
|
|
|
QNetworkReply *rep = worker->get(request);
|
|
|
|
|
|
|
|
m_reply = std::shared_ptr<QNetworkReply>(rep);
|
|
|
|
connect(rep, SIGNAL(downloadProgress(qint64, qint64)),
|
|
|
|
SLOT(downloadProgress(qint64, qint64)));
|
|
|
|
connect(rep, SIGNAL(finished()), SLOT(downloadFinished()));
|
|
|
|
connect(rep, SIGNAL(error(QNetworkReply::NetworkError)),
|
|
|
|
SLOT(downloadError(QNetworkReply::NetworkError)));
|
|
|
|
connect(rep, SIGNAL(readyRead()), SLOT(downloadReadyRead()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ForgeMirrors::downloadError(QNetworkReply::NetworkError error)
|
|
|
|
{
|
|
|
|
// error happened during download.
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "Error getting URL:" << m_url.toString().toLocal8Bit()
|
2013-11-17 10:44:18 +00:00
|
|
|
<< "Network error: " << error;
|
|
|
|
m_status = Job_Failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ForgeMirrors::downloadFinished()
|
|
|
|
{
|
|
|
|
// if the download succeeded
|
|
|
|
if (m_status != Job_Failed)
|
|
|
|
{
|
|
|
|
// nothing went wrong... ?
|
|
|
|
parseMirrorList();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// else the download failed, we use a fixed list
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_status = Job_Finished;
|
|
|
|
m_reply.reset();
|
|
|
|
deferToFixedList();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ForgeMirrors::deferToFixedList()
|
|
|
|
{
|
|
|
|
m_mirrors.clear();
|
|
|
|
m_mirrors.append(
|
|
|
|
{"Minecraft Forge", "http://files.minecraftforge.net/forge_logo.png",
|
|
|
|
"http://files.minecraftforge.net/", "http://files.minecraftforge.net/maven/"});
|
|
|
|
m_mirrors.append({"Creeper Host",
|
|
|
|
"http://files.minecraftforge.net/forge_logo.png",
|
|
|
|
"https://www.creeperhost.net/link.php?id=1",
|
|
|
|
"http://new.creeperrepo.net/forge/maven/"});
|
|
|
|
injectDownloads();
|
2013-12-15 14:00:09 +00:00
|
|
|
emit succeeded(m_index_within_job);
|
2013-11-17 10:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ForgeMirrors::parseMirrorList()
|
|
|
|
{
|
|
|
|
m_status = Job_Finished;
|
|
|
|
auto data = m_reply->readAll();
|
|
|
|
m_reply.reset();
|
|
|
|
auto dataLines = data.split('\n');
|
|
|
|
for(auto line: dataLines)
|
|
|
|
{
|
|
|
|
auto elements = line.split('!');
|
|
|
|
if (elements.size() == 4)
|
|
|
|
{
|
|
|
|
m_mirrors.append({elements[0],elements[1],elements[2],elements[3]});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!m_mirrors.size())
|
|
|
|
deferToFixedList();
|
|
|
|
injectDownloads();
|
2013-12-15 14:00:09 +00:00
|
|
|
emit succeeded(m_index_within_job);
|
2013-11-17 10:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ForgeMirrors::injectDownloads()
|
|
|
|
{
|
|
|
|
// shuffle the mirrors randomly
|
|
|
|
std::random_device rd;
|
|
|
|
std::mt19937 rng(rd());
|
|
|
|
std::shuffle(m_mirrors.begin(), m_mirrors.end(), rng);
|
|
|
|
|
|
|
|
// tell parent to download the libs
|
|
|
|
for(auto lib: m_libs)
|
|
|
|
{
|
|
|
|
lib->setMirrors(m_mirrors);
|
|
|
|
m_parent_job->addNetAction(lib);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ForgeMirrors::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
|
|
|
|
{
|
2013-12-15 14:00:09 +00:00
|
|
|
m_total_progress = bytesTotal;
|
|
|
|
m_progress = bytesReceived;
|
|
|
|
emit progress(m_index_within_job, bytesReceived, bytesTotal);
|
2013-11-17 10:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ForgeMirrors::downloadReadyRead()
|
|
|
|
{
|
|
|
|
}
|