summaryrefslogtreecommitdiffstats
path: root/logic/DerpFTBInstance.cpp
blob: 9b687461c74fa975bd445b5f5241591566fe22a2 (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
#include "DerpFTBInstance.h"

#include "DerpVersion.h"
#include "DerpLibrary.h"
#include "tasks/SequentialTask.h"
#include "ForgeInstaller.h"
#include "lists/ForgeVersionList.h"
#include "MultiMC.h"

class DerpFTBInstanceForge : public Task
{
	Q_OBJECT
public:
	explicit DerpFTBInstanceForge(const QString &version, DerpFTBInstance *inst, QObject *parent = 0) :
		Task(parent), instance(inst), version("Forge " + version)
	{
	}

	void executeTask()
	{
		for (int i = 0; i < MMC->forgelist()->count(); ++i)
		{
			if (MMC->forgelist()->at(i)->name() == version)
			{
				forgeVersion = std::dynamic_pointer_cast<ForgeVersion>(MMC->forgelist()->at(i));
				break;
			}
		}
		if (!forgeVersion)
		{
			emitFailed(QString("Couldn't find forge version ") + version );
			return;
		}
		entry = MMC->metacache()->resolveEntry("minecraftforge", forgeVersion->filename);
		if (entry->stale)
		{
			setStatus(tr("Downloading Forge..."));
			fjob = new NetJob("Forge download");
			fjob->addNetAction(CacheDownload::make(forgeVersion->installer_url, entry));
			connect(fjob, &NetJob::failed, [this](){emitFailed(m_failReason);});
			connect(fjob, &NetJob::succeeded, this, &DerpFTBInstanceForge::installForge);
			connect(fjob, &NetJob::progress, [this](qint64 c, qint64 total){ setProgress(100 * c / total); });
			fjob->start();
		}
		else
		{
			installForge();
		}
	}

private
slots:
	void installForge()
	{
		setStatus(tr("Installing Forge..."));
		QString forgePath = entry->getFullPath();
		ForgeInstaller forge(forgePath, forgeVersion->universal_url);
		if (!instance->reloadFullVersion())
		{
			emitFailed(tr("Couldn't load the version config"));
			return;
		}
		auto version = instance->getFullVersion();
		if (!forge.add(instance))
		{
			emitFailed(tr("Couldn't install Forge"));
			return;
		}
		emitSucceeded();
	}

private:
	DerpFTBInstance *instance;
	QString version;
	ForgeVersionPtr forgeVersion;
	MetaEntryPtr entry;
	NetJob *fjob;
};

DerpFTBInstance::DerpFTBInstance(const QString &rootDir, SettingsObject *settings, QObject *parent) :
	DerpInstance(rootDir, settings, parent)
{
	QFile f(QDir(minecraftRoot()).absoluteFilePath("pack.json"));
	if (f.open(QFile::ReadOnly))
	{
		QString data = QString::fromUtf8(f.readAll());
		QRegularExpressionMatch match = QRegularExpression("net.minecraftforge:minecraftforge:[\\.\\d]*").match(data);
		m_forge.reset(new DerpLibrary(match.captured()));
		m_forge->finalize();
	}
}

QString DerpFTBInstance::id() const
{
	return "FTB/" + BaseInstance::id();
}

QString DerpFTBInstance::getStatusbarDescription()
{
	return "Derp FTB: " + intendedVersionId();
}
bool DerpFTBInstance::menuActionEnabled(QString action_name) const
{
	return false;
}

std::shared_ptr<Task> DerpFTBInstance::doUpdate(bool only_prepare)
{
	std::shared_ptr<SequentialTask> task;
	task.reset(new SequentialTask(this));
	if (!MMC->forgelist()->isLoaded())
	{
		task->addTask(std::shared_ptr<Task>(MMC->forgelist()->getLoadTask()));
	}
	task->addTask(DerpInstance::doUpdate(only_prepare));
	task->addTask(std::shared_ptr<Task>(new DerpFTBInstanceForge(m_forge->version(), this, this)));
	//FIXME: yes. this may appear dumb. but the previous step can change the list, so we do it all again.
	//TODO: Add a graph task. Construct graphs of tasks so we may capture the logic properly.
	task->addTask(DerpInstance::doUpdate(only_prepare));
	return task;
}

#include "DerpFTBInstance.moc"