summaryrefslogtreecommitdiffstats
path: root/api/logic/minecraft/ftb/FTBPlugin.cpp
blob: d14eea63a1cceda0fbfda8be984e37f43d18b8ea (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
#include "FTBPlugin.h"
#include <Env.h>
#include "FTBVersion.h"
#include "LegacyFTBInstance.h"
#include "OneSixFTBInstance.h"
#include <BaseInstance.h>
#include <InstanceList.h>
#include <minecraft/MinecraftVersionList.h>
#include <settings/INISettingsObject.h>
#include <FileSystem.h>

#include <QDebug>
#include <QRegularExpression>

#ifdef Q_OS_WIN32
#include <windows.h>
static const int APPDATA_BUFFER_SIZE = 1024;
#endif

static QString getLocalCacheStorageLocation()
{
	QString ftbDefault;
#ifdef Q_OS_WIN32
	wchar_t buf[APPDATA_BUFFER_SIZE];
	if (GetEnvironmentVariableW(L"LOCALAPPDATA", buf, APPDATA_BUFFER_SIZE)) // local
	{
		ftbDefault = QDir(QString::fromWCharArray(buf)).absoluteFilePath("ftblauncher");
	}
	else if (GetEnvironmentVariableW(L"APPDATA", buf, APPDATA_BUFFER_SIZE)) // roaming
	{
		ftbDefault = QDir(QString::fromWCharArray(buf)).absoluteFilePath("ftblauncher");
	}
	else
	{
		qCritical() << "Your LOCALAPPDATA and APPDATA folders are missing!"
			" If you are on windows, this means your system is broken.";
	}
#elif defined(Q_OS_MAC)
	ftbDefault = FS::PathCombine(QDir::homePath(), "Library/Application Support/ftblauncher");
#else
	ftbDefault = QDir::home().absoluteFilePath(".ftblauncher");
#endif
	return ftbDefault;
}


static QString getRoamingStorageLocation()
{
	QString ftbDefault;
#ifdef Q_OS_WIN32
	wchar_t buf[APPDATA_BUFFER_SIZE];
	QString cacheStorage;
	if (GetEnvironmentVariableW(L"APPDATA", buf, APPDATA_BUFFER_SIZE))
	{
		ftbDefault = QDir(QString::fromWCharArray(buf)).absoluteFilePath("ftblauncher");
	}
	else
	{
		qCritical() << "Your APPDATA folder is missing! If you are on windows, this means your system is broken.";
	}
#elif defined(Q_OS_MAC)
	ftbDefault = FS::PathCombine(QDir::homePath(), "Library/Application Support/ftblauncher");
#else
	ftbDefault = QDir::home().absoluteFilePath(".ftblauncher");
#endif
	return ftbDefault;
}

void FTBPlugin::initialize(SettingsObjectPtr globalSettings)
{
	// FTB
	globalSettings->registerSetting("TrackFTBInstances", false);
	QString ftbRoaming = getRoamingStorageLocation();
	QString ftbLocal = getLocalCacheStorageLocation();

	globalSettings->registerSetting("FTBLauncherRoaming", ftbRoaming);
	globalSettings->registerSetting("FTBLauncherLocal", ftbLocal);
	qDebug() << "FTB Launcher paths:" << globalSettings->get("FTBLauncherRoaming").toString()
			 << "and" << globalSettings->get("FTBLauncherLocal").toString();

	globalSettings->registerSetting("FTBRoot");
	if (globalSettings->get("FTBRoot").isNull())
	{
		QString ftbRoot;
		QFile f(QDir(globalSettings->get("FTBLauncherRoaming").toString()).absoluteFilePath("ftblaunch.cfg"));
		qDebug() << "Attempting to read" << f.fileName();
		if (f.open(QFile::ReadOnly))
		{
			const QString data = QString::fromLatin1(f.readAll());
			QRegularExpression exp("installPath=(.*)");
			ftbRoot = QDir::cleanPath(exp.match(data).captured(1));
#ifdef Q_OS_WIN32
			if (!ftbRoot.isEmpty())
			{
				if (ftbRoot.at(0).isLetter() && ftbRoot.size() > 1 && ftbRoot.at(1) == '/')
				{
					ftbRoot.remove(1, 1);
				}
			}
#endif
			if (ftbRoot.isEmpty())
			{
				qDebug() << "Failed to get FTB root path";
			}
			else
			{
				qDebug() << "FTB is installed at" << ftbRoot;
				globalSettings->set("FTBRoot", ftbRoot);
			}
		}
		else
		{
			qWarning() << "Couldn't open" << f.fileName() << ":" << f.errorString();
			qWarning() << "This is perfectly normal if you don't have FTB installed";
		}
	}
}