summaryrefslogtreecommitdiffstats
path: root/logic/forge/ForgeMirrors.cpp
blob: b224306f84d72dd9022d458736f89c476ead7363 (plain)
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
#include "MultiMC.h"
#include "ForgeMirrors.h"
#include "logger/QsLog.h"
#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()
{
	QLOG_INFO() << "Downloading " << m_url.toString();
	QNetworkRequest request(m_url);
	request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)");
	auto worker = MMC->qnam();
	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.
	QLOG_ERROR() << "Error getting URL:" << m_url.toString().toLocal8Bit()
				 << "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();
	emit succeeded(m_index_within_job);
}

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();
	emit succeeded(m_index_within_job);
}

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)
{
	m_total_progress = bytesTotal;
	m_progress = bytesReceived;
	emit progress(m_index_within_job, bytesReceived, bytesTotal);
}

void ForgeMirrors::downloadReadyRead()
{
}