summaryrefslogtreecommitdiffstats
path: root/logic/LegacyUpdate.cpp
blob: 84d3d8305f2fda41aca8571b69335e05bc1ef696 (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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#include "LegacyUpdate.h"
#include "lists/LwjglVersionList.h"
#include "lists/MinecraftVersionList.h"
#include "BaseInstance.h"
#include "LegacyInstance.h"
#include "MultiMC.h"
#include "ModList.h"
#include <pathutils.h>
#include <quazip.h>
#include <quazipfile.h>
#include <JlCompress.h>


LegacyUpdate::LegacyUpdate ( BaseInstance* inst, QObject* parent ) : BaseUpdate ( inst, parent ) {}

void LegacyUpdate::executeTask()
{
	lwjglStart();
}

void LegacyUpdate::lwjglStart()
{
	LegacyInstance * inst = (LegacyInstance *) m_inst;

	lwjglVersion =  inst->lwjglVersion();
	lwjglTargetPath = PathCombine("lwjgl", lwjglVersion );
	lwjglNativesPath = PathCombine( lwjglTargetPath, "natives");
	
	// if the 'done' file exists, we don't have to download this again
	QFileInfo doneFile(PathCombine(lwjglTargetPath, "done"));
	if(doneFile.exists())
	{
		jarStart();
		return;
	}
	
	auto list = MMC->lwjgllist();
	if(!list->isLoaded())
	{
		emitFailed("Too soon! Let the LWJGL list load :)");
		return;
	}
	
	setStatus("Downloading new LWJGL.");
	auto version = list->getVersion(lwjglVersion);
	if(!version)
	{
		emitFailed("Game update failed: the selected LWJGL version is invalid.");
		return;
	}
	
	QString url = version->url();
	QUrl realUrl(url);
	QString hostname = realUrl.host();
	auto worker = MMC->qnam();
	QNetworkRequest req(realUrl);
	req.setRawHeader("Host", hostname.toLatin1());
	req.setHeader(QNetworkRequest::UserAgentHeader, "Wget/1.14 (linux-gnu)");
	QNetworkReply * rep = worker->get ( req );
	
	m_reply = QSharedPointer<QNetworkReply> (rep, &QObject::deleteLater);
	connect(rep, SIGNAL(downloadProgress(qint64,qint64)), SIGNAL(progress(qint64,qint64)));
	connect(worker.data(), SIGNAL(finished(QNetworkReply*)), SLOT(lwjglFinished(QNetworkReply*)));
	//connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(downloadError(QNetworkReply::NetworkError)));
}

void LegacyUpdate::lwjglFinished(QNetworkReply* reply)
{
	if(m_reply != reply)
	{
		return;
	}
	if(reply->error() != QNetworkReply::NoError)
	{
		emitFailed( "Failed to download: "+
					reply->errorString()+
					"\nSometimes you have to wait a bit if you download many LWJGL versions in a row. YMMV");
		return;
	}
	auto worker = MMC->qnam();
	//Here i check if there is a cookie for me in the reply and extract it
	QList<QNetworkCookie> cookies = qvariant_cast<QList<QNetworkCookie>>(reply->header(QNetworkRequest::SetCookieHeader));
	if(cookies.count() != 0)
	{
		//you must tell which cookie goes with which url
		worker->cookieJar()->setCookiesFromUrl(cookies, QUrl("sourceforge.net"));
	}

	//here you can check for the 302 or whatever other header i need
	QVariant newLoc = reply->header(QNetworkRequest::LocationHeader);
	if(newLoc.isValid())
	{
		QString redirectedTo = reply->header(QNetworkRequest::LocationHeader).toString();
		QUrl realUrl(redirectedTo);
		QString hostname = realUrl.host();
		QNetworkRequest req(redirectedTo);
		req.setRawHeader("Host", hostname.toLatin1());
		req.setHeader(QNetworkRequest::UserAgentHeader, "Wget/1.14 (linux-gnu)");
		QNetworkReply * rep = worker->get(req);
		connect(rep, SIGNAL(downloadProgress(qint64,qint64)), SIGNAL(progress(qint64,qint64)));
		m_reply = QSharedPointer<QNetworkReply> (rep, &QObject::deleteLater);
		return;
	}
	QFile saveMe("lwjgl.zip");
	saveMe.open(QIODevice::WriteOnly);
	saveMe.write(m_reply->readAll());
	saveMe.close();
	setStatus("Installing new LWJGL...");
	extractLwjgl();
	jarStart();
}
void LegacyUpdate::extractLwjgl()
{
	// make sure the directories are there

	bool success = ensureFolderPathExists(lwjglNativesPath);
	
	if(!success)
	{
		emitFailed("Failed to extract the lwjgl libs - error when creating required folders.");
		return;
	}
	
	QuaZip zip("lwjgl.zip");
	if(!zip.open(QuaZip::mdUnzip))
	{
		emitFailed("Failed to extract the lwjgl libs - not a valid archive.");
		return;
	}
	
	// and now we are going to access files inside it
	QuaZipFile file(&zip);
	const QString jarNames[] = { "jinput.jar", "lwjgl_util.jar", "lwjgl.jar" };
	for(bool more=zip.goToFirstFile(); more; more=zip.goToNextFile())
	{
		if(!file.open(QIODevice::ReadOnly))
		{
			zip.close();
			emitFailed("Failed to extract the lwjgl libs - error while reading archive.");
			return;
		}
		QuaZipFileInfo info;
		QString name = file.getActualFileName();
		if(name.endsWith('/'))
		{
			file.close();
			continue;
		}
		QString destFileName;
		// Look for the jars
		for (int i = 0; i < 3; i++)
		{
			if (name.endsWith(jarNames[i]))
			{
				destFileName = PathCombine(lwjglTargetPath, jarNames[i]);
			}
		}
		// Not found? look for the natives
		if(destFileName.isEmpty())
		{
#ifdef Q_OS_WIN32
			QString nativesDir = "windows";
#else
	#ifdef Q_OS_MAC
			QString nativesDir = "macosx";
	#else
			QString nativesDir = "linux";
	#endif
#endif
			if (name.contains(nativesDir))
			{
				int lastSlash = name.lastIndexOf('/');
				int lastBackSlash = name.lastIndexOf('\\');
				if(lastSlash != -1)
					name = name.mid(lastSlash+1);
				else if(lastBackSlash != -1)
					name = name.mid(lastBackSlash+1);
				destFileName = PathCombine(lwjglNativesPath, name);
			}
		}
		// Now if destFileName is still empty, go to the next file.
		if (!destFileName.isEmpty())
		{
			setStatus("Installing new LWJGL - Extracting " + name);
			QFile output(destFileName);
			output.open(QIODevice::WriteOnly);
			output.write(file.readAll()); // FIXME: wste of memory!?
			output.close();
		}
		file.close(); // do not forget to close!
	}
	zip.close();
	m_reply.clear();
	QFile doneFile(PathCombine(lwjglTargetPath, "done"));
	doneFile.open(QIODevice::WriteOnly);
	doneFile.write("done.");
	doneFile.close();
}

void LegacyUpdate::lwjglFailed()
{
	emitFailed("Bad stuff happened while trying to get the lwjgl libs...");
}

void LegacyUpdate::jarStart()
{
	LegacyInstance * inst = (LegacyInstance *) m_inst;
	if(!inst->shouldUpdate() || inst->shouldUseCustomBaseJar())
	{
		ModTheJar();
		return;
	}
	
	setStatus("Checking for jar updates...");
	// Make directories
	QDir binDir(inst->binDir());
	if (!binDir.exists() && !binDir.mkpath("."))
	{
		emitFailed("Failed to create bin folder.");
		return;
	}

	// Build a list of URLs that will need to be downloaded.
	setStatus("Downloading new minecraft.jar");

	QString urlstr("http://s3.amazonaws.com/Minecraft.Download/versions/");
	QString intended_version_id = inst->intendedVersionId();
	urlstr += intended_version_id + "/" + intended_version_id + ".jar";
	
	auto dljob = new DownloadJob("Minecraft.jar for version " + intended_version_id);
	dljob->add(QUrl(urlstr), inst->defaultBaseJar());
	legacyDownloadJob.reset(dljob);
	connect(dljob, SIGNAL(succeeded()), SLOT(jarFinished()));
	connect(dljob, SIGNAL(failed()), SLOT(jarFailed()));
	connect(dljob, SIGNAL(progress(qint64,qint64)), SIGNAL(progress(qint64,qint64)));
	legacyDownloadJob->start();
}

void LegacyUpdate::jarFinished()
{
	// process the jar
	ModTheJar();
}

void LegacyUpdate::jarFailed()
{
	// bad, bad
	emitFailed("Failed to download the minecraft jar. Try again later.");
}

bool LegacyUpdate::MergeZipFiles( QuaZip* into, QFileInfo from, QSet< QString >& contained, MetainfAction metainf )
{
	setStatus("Installing mods - Adding " + from.fileName());
	
	QuaZip modZip(from.filePath());
	modZip.open(QuaZip::mdUnzip);
	
	QuaZipFile fileInsideMod(&modZip);
	QuaZipFile zipOutFile( into );
	for(bool more=modZip.goToFirstFile(); more; more=modZip.goToNextFile())
	{
		QString filename = modZip.getCurrentFileName();
		if(filename.contains("META-INF") && metainf == LegacyUpdate::IgnoreMetainf)
		{
			qDebug() << "Skipping META-INF " << filename << " from " << from.fileName();
			continue;
		}
		if(contained.contains(filename))
		{
			qDebug() << "Skipping already contained file " << filename << " from " << from.fileName();
			continue;
		}
		contained.insert(filename);
		qDebug() << "Adding file " << filename << " from " << from.fileName();
		
		if(!fileInsideMod.open(QIODevice::ReadOnly))
		{
			qDebug() << "Failed to open " << filename << " from " << from.fileName();
			return false;
		}
		/*
		QuaZipFileInfo old_info;
		fileInsideMod.getFileInfo(&old_info);
		*/
		QuaZipNewInfo info_out(fileInsideMod.getActualFileName());
		/*
		info_out.externalAttr = old_info.externalAttr;
		*/
		if(!zipOutFile.open(QIODevice::WriteOnly, info_out))
		{
			qDebug() << "Failed to open " << filename << " in the jar";
			fileInsideMod.close();
			return false;
		}
		if(!JlCompress::copyData(fileInsideMod, zipOutFile))
		{
			zipOutFile.close();
			fileInsideMod.close();
			qDebug() << "Failed to copy data of " << filename << " into the jar";
			return false;
		}
		zipOutFile.close();
		fileInsideMod.close();
	}
	return true;
}

void LegacyUpdate::ModTheJar()
{
	LegacyInstance * inst = (LegacyInstance *) m_inst;
	
	if(!inst->shouldRebuild())
	{
		emitSucceeded();
		return;
	}
	
	// Get the mod list
	auto modList = inst->jarModList();
	
	QFileInfo runnableJar (inst->runnableJar());
	QFileInfo baseJar (inst->baseJar());
	bool base_is_custom = inst->shouldUseCustomBaseJar();
	
	// Nothing to do if there are no jar mods to install, no backup and just the mc jar
	if(base_is_custom)
	{
		// yes, this can happen if the instance only has the runnable jar and not the base jar
		// it *could* be assumed that such an instance is vanilla, but that wouldn't be safe
		// because that's not something mmc4 guarantees
		if(runnableJar.isFile() && !baseJar.exists() && modList->empty())
		{
			inst->setShouldRebuild(false);
			emitSucceeded();
			return;
		}
		
		setStatus("Installing mods - backing up minecraft.jar...");
		if (!baseJar.exists() && !QFile::copy(runnableJar.filePath(), baseJar.filePath()))
		{
			emitFailed("Failed to back up minecraft.jar");
			return;
		}
	}
	
	if (!baseJar.exists())
	{
		emitFailed("The base jar " + baseJar.filePath() + " does not exist");
		return;
	}
	
	if (runnableJar.exists() && !QFile::remove(runnableJar.filePath()))
	{
		emitFailed("Failed to delete old minecraft.jar");
		return;
	}
	
	//TaskStep(); // STEP 1
	setStatus("Installing mods - Opening minecraft.jar");

	QuaZip zipOut(runnableJar.filePath());
	if(!zipOut.open(QuaZip::mdCreate))
	{
		QFile::remove(runnableJar.filePath());
		emitFailed("Failed to open the minecraft.jar for modding");
		return;
	}
	// Files already added to the jar.
	// These files will be skipped.
	QSet<QString> addedFiles;

	// Modify the jar
	setStatus("Installing mods - Adding mod files...");
	for (int i = modList->size() - 1; i >= 0; i--)
	{
		auto &mod = modList->operator[](i);
		if (mod.type() == Mod::MOD_ZIPFILE)
		{
			if(!MergeZipFiles(&zipOut, mod.filename(), addedFiles, LegacyUpdate::KeepMetainf))
			{
				zipOut.close();
				QFile::remove(runnableJar.filePath());
				emitFailed("Failed to add " + mod.filename().fileName() + " to the jar.");
				return;
			}
		}
		else if (mod.type() == Mod::MOD_SINGLEFILE)
		{
			auto filename = mod.filename();
			if(!JlCompress::compressFile(&zipOut, filename.absoluteFilePath(), filename.fileName()))
			{
				zipOut.close();
				QFile::remove(runnableJar.filePath());
				emitFailed("Failed to add " + filename.fileName() + " to the jar");
				return;
			}
			addedFiles.insert(filename.fileName());
			qDebug() << "Adding file " << filename.fileName() << " from " << filename.absoluteFilePath();
		}
		else if (mod.type() == Mod::MOD_FOLDER)
		{
			auto filename = mod.filename();
			QString what_to_zip = filename.absoluteFilePath();
			QDir dir(what_to_zip);
			dir.cdUp();
			QString parent_dir = dir.absolutePath();
			if(!JlCompress::compressSubDir(&zipOut, what_to_zip, parent_dir, true, addedFiles))
			{
				zipOut.close();
				QFile::remove(runnableJar.filePath());
				emitFailed("Failed to add " + filename.fileName() + " to the jar");
				return;
			}
			qDebug() << "Adding folder " << filename.fileName() << " from " << filename.absoluteFilePath();
		}
	}
	
	if(!MergeZipFiles(&zipOut, baseJar, addedFiles, LegacyUpdate::IgnoreMetainf))
	{
		zipOut.close();
		QFile::remove(runnableJar.filePath());
		emitFailed("Failed to insert minecraft.jar contents.");
		return;
	}
	
	// Recompress the jar
	zipOut.close();
    if(zipOut.getZipError()!=0)
	{
		QFile::remove(runnableJar.filePath());
		emitFailed("Failed to finalize minecraft.jar!");
		return;
    }
	inst->setShouldRebuild(false);
	//inst->UpdateVersion(true);
	emitSucceeded();
	return;
}