summaryrefslogtreecommitdiffstats
path: root/api/logic/minecraft/update/FMLLibrariesTask.cpp
blob: e64b7a824d92109c67474dc0509adc8f5b974313 (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
#include "Env.h"
#include <FileSystem.h>
#include <minecraft/VersionFilterData.h>
#include "FMLLibrariesTask.h"
#include "minecraft/MinecraftInstance.h"
#include "minecraft/ComponentList.h"

FMLLibrariesTask::FMLLibrariesTask(MinecraftInstance * inst)
{
	m_inst = inst;
}
void FMLLibrariesTask::executeTask()
{
	// Get the mod list
	MinecraftInstance *inst = (MinecraftInstance *)m_inst;
	std::shared_ptr<ComponentList> profile = inst->getComponentList();
	bool forge_present = false;

	if (!profile->hasTrait("legacyFML"))
	{
		emitSucceeded();
		return;
	}

	QString version = inst->getComponentVersion("net.minecraft");
	auto &fmlLibsMapping = g_VersionFilterData.fmlLibsMapping;
	if (!fmlLibsMapping.contains(version))
	{
		emitSucceeded();
		return;
	}

	auto &libList = fmlLibsMapping[version];

	// determine if we need some libs for FML or forge
	setStatus(tr("Checking for FML libraries..."));
	forge_present = (profile->versionPatch("net.minecraftforge") != nullptr);
	// we don't...
	if (!forge_present)
	{
		emitSucceeded();
		return;
	}

	// now check the lib folder inside the instance for files.
	for (auto &lib : libList)
	{
		QFileInfo libInfo(FS::PathCombine(inst->libDir(), lib.filename));
		if (libInfo.exists())
			continue;
		fmlLibsToProcess.append(lib);
	}

	// if everything is in place, there's nothing to do here...
	if (fmlLibsToProcess.isEmpty())
	{
		emitSucceeded();
		return;
	}

	// download missing libs to our place
	setStatus(tr("Dowloading FML libraries..."));
	auto dljob = new NetJob("FML libraries");
	auto metacache = ENV.metacache();
	for (auto &lib : fmlLibsToProcess)
	{
		auto entry = metacache->resolveEntry("fmllibs", lib.filename);
		QString urlString = lib.ours ? URLConstants::FMLLIBS_OUR_BASE_URL + lib.filename
									: URLConstants::FMLLIBS_FORGE_BASE_URL + lib.filename;
		dljob->addNetAction(Net::Download::makeCached(QUrl(urlString), entry));
	}

	connect(dljob, &NetJob::succeeded, this, &FMLLibrariesTask::fmllibsFinished);
	connect(dljob, &NetJob::failed, this, &FMLLibrariesTask::fmllibsFailed);
	connect(dljob, &NetJob::progress, this, &FMLLibrariesTask::progress);
	downloadJob.reset(dljob);
	downloadJob->start();
}

bool FMLLibrariesTask::canAbort() const
{
	return true;
}

void FMLLibrariesTask::fmllibsFinished()
{
	downloadJob.reset();
	if (!fmlLibsToProcess.isEmpty())
	{
		setStatus(tr("Copying FML libraries into the instance..."));
		MinecraftInstance *inst = (MinecraftInstance *)m_inst;
		auto metacache = ENV.metacache();
		int index = 0;
		for (auto &lib : fmlLibsToProcess)
		{
			progress(index, fmlLibsToProcess.size());
			auto entry = metacache->resolveEntry("fmllibs", lib.filename);
			auto path = FS::PathCombine(inst->libDir(), lib.filename);
			if (!FS::ensureFilePathExists(path))
			{
				emitFailed(tr("Failed creating FML library folder inside the instance."));
				return;
			}
			if (!QFile::copy(entry->getFullPath(), FS::PathCombine(inst->libDir(), lib.filename)))
			{
				emitFailed(tr("Failed copying Forge/FML library: %1.").arg(lib.filename));
				return;
			}
			index++;
		}
		progress(index, fmlLibsToProcess.size());
	}
	emitSucceeded();
}
void FMLLibrariesTask::fmllibsFailed(QString reason)
{
	QStringList failed = downloadJob->getFailedFiles();
	QString failed_all = failed.join("\n");
	emitFailed(tr("Failed to download the following files:\n%1\n\nReason:%2\nPlease try again.").arg(failed_all, reason));
}

bool FMLLibrariesTask::abort()
{
	if(downloadJob)
	{
		return downloadJob->abort();
	}
	else
	{
		qWarning() << "Prematurely aborted FMLLibrariesTask";
	}
	return true;
}