summaryrefslogtreecommitdiffstats
path: root/api/logic/java/JavaUtils.cpp
blob: 798c80e8c6d363f8580b1f35ab812e5ac43c006d (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
/* Copyright 2013-2018 MultiMC Contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <QStringList>
#include <QString>
#include <QDir>
#include <QStringList>

#include <settings/Setting.h>

#include <QDebug>
#include "java/JavaUtils.h"
#include "java/JavaInstallList.h"
#include "FileSystem.h"

#define IBUS "@im=ibus"

JavaUtils::JavaUtils()
{
}

#ifdef Q_OS_LINUX
static QString processLD_LIBRARY_PATH(const QString & LD_LIBRARY_PATH)
{
	QDir mmcBin(QCoreApplication::applicationDirPath());
	auto items = LD_LIBRARY_PATH.split(':');
	QStringList final;
	for(auto & item: items)
	{
		QDir test(item);
		if(test == mmcBin)
		{
			qDebug() << "Env:LD_LIBRARY_PATH ignoring path" << item;
			continue;
		}
		final.append(item);
	}
	return final.join(':');
}
#endif

QProcessEnvironment CleanEnviroment()
{
	// prepare the process environment
	QProcessEnvironment rawenv = QProcessEnvironment::systemEnvironment();
	QProcessEnvironment env;

	QStringList ignored =
	{
		"JAVA_ARGS",
		"CLASSPATH",
		"CONFIGPATH",
		"JAVA_HOME",
		"JRE_HOME",
		"_JAVA_OPTIONS",
		"JAVA_OPTIONS",
		"JAVA_TOOL_OPTIONS"
	};
	for(auto key: rawenv.keys())
	{
		auto value = rawenv.value(key);
		// filter out dangerous java crap
		if(ignored.contains(key))
		{
			qDebug() << "Env: ignoring" << key << value;
			continue;
		}
		// filter MultiMC-related things
		if(key.startsWith("QT_"))
		{
			qDebug() << "Env: ignoring" << key << value;
			continue;
		}
#ifdef Q_OS_LINUX
		// Do not pass LD_* variables to java. They were intended for MultiMC
		if(key.startsWith("LD_"))
		{
			qDebug() << "Env: ignoring" << key << value;
			continue;
		}
		// Strip IBus
		// IBus is a Linux IME framework. For some reason, it breaks MC?
		if (key == "XMODIFIERS" && value.contains(IBUS))
		{
			QString save = value;
			value.replace(IBUS, "");
			qDebug() << "Env: stripped" << IBUS << "from" << save << ":" << value;
		}
		if(key == "GAME_PRELOAD")
		{
			env.insert("LD_PRELOAD", value);
			continue;
		}
		if(key == "GAME_LIBRARY_PATH")
		{
			env.insert("LD_LIBRARY_PATH", processLD_LIBRARY_PATH(value));
			continue;
		}
#endif
		// qDebug() << "Env: " << key << value;
		env.insert(key, value);
	}
#ifdef Q_OS_LINUX
	// HACK: Workaround for QTBUG42500
	if(!env.contains("LD_LIBRARY_PATH"))
	{
		env.insert("LD_LIBRARY_PATH", "");
	}
#endif

	return env;
}

JavaInstallPtr JavaUtils::MakeJavaPtr(QString path, QString id, QString arch)
{
	JavaInstallPtr javaVersion(new JavaInstall());

	javaVersion->id = id;
	javaVersion->arch = arch;
	javaVersion->path = path;

	return javaVersion;
}

JavaInstallPtr JavaUtils::GetDefaultJava()
{
	JavaInstallPtr javaVersion(new JavaInstall());

	javaVersion->id = "java";
	javaVersion->arch = "unknown";
#if defined(Q_OS_WIN32)
	javaVersion->path = "javaw";
#else
	javaVersion->path = "java";
#endif

	return javaVersion;
}

#if defined(Q_OS_WIN32)
QList<JavaInstallPtr> JavaUtils::FindJavaFromRegistryKey(DWORD keyType, QString keyName)
{
	QList<JavaInstallPtr> javas;

	QString archType = "unknown";
	if (keyType == KEY_WOW64_64KEY)
		archType = "64";
	else if (keyType == KEY_WOW64_32KEY)
		archType = "32";

	HKEY jreKey;
	if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, keyName.toStdString().c_str(), 0,
					  KEY_READ | keyType | KEY_ENUMERATE_SUB_KEYS, &jreKey) == ERROR_SUCCESS)
	{
		// Read the current type version from the registry.
		// This will be used to find any key that contains the JavaHome value.
		char *value = new char[0];
		DWORD valueSz = 0;
		if (RegQueryValueExA(jreKey, "CurrentVersion", NULL, NULL, (BYTE *)value, &valueSz) ==
			ERROR_MORE_DATA)
		{
			value = new char[valueSz];
			RegQueryValueExA(jreKey, "CurrentVersion", NULL, NULL, (BYTE *)value, &valueSz);
		}

		QString recommended = value;

		TCHAR subKeyName[255];
		DWORD subKeyNameSize, numSubKeys, retCode;

		// Get the number of subkeys
		RegQueryInfoKey(jreKey, NULL, NULL, NULL, &numSubKeys, NULL, NULL, NULL, NULL, NULL,
						NULL, NULL);

		// Iterate until RegEnumKeyEx fails
		if (numSubKeys > 0)
		{
			for (DWORD i = 0; i < numSubKeys; i++)
			{
				subKeyNameSize = 255;
				retCode = RegEnumKeyEx(jreKey, i, subKeyName, &subKeyNameSize, NULL, NULL, NULL,
									   NULL);
				if (retCode == ERROR_SUCCESS)
				{
					// Now open the registry key for the version that we just got.
					QString newKeyName = keyName + "\\" + subKeyName;

					HKEY newKey;
					if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, newKeyName.toStdString().c_str(), 0,
									 KEY_READ | KEY_WOW64_64KEY, &newKey) == ERROR_SUCCESS)
					{
						// Read the JavaHome value to find where Java is installed.
						value = new char[0];
						valueSz = 0;
						if (RegQueryValueEx(newKey, "JavaHome", NULL, NULL, (BYTE *)value,
											&valueSz) == ERROR_MORE_DATA)
						{
							value = new char[valueSz];
							RegQueryValueEx(newKey, "JavaHome", NULL, NULL, (BYTE *)value,
											&valueSz);

							// Now, we construct the version object and add it to the list.
							JavaInstallPtr javaVersion(new JavaInstall());

							javaVersion->id = subKeyName;
							javaVersion->arch = archType;
							javaVersion->path =
								QDir(FS::PathCombine(value, "bin")).absoluteFilePath("javaw.exe");
							javas.append(javaVersion);
						}

						RegCloseKey(newKey);
					}
				}
			}
		}

		RegCloseKey(jreKey);
	}

	return javas;
}

QList<QString> JavaUtils::FindJavaPaths()
{
	QList<JavaInstallPtr> java_candidates;

	QList<JavaInstallPtr> JRE64s = this->FindJavaFromRegistryKey(
		KEY_WOW64_64KEY, "SOFTWARE\\JavaSoft\\Java Runtime Environment");
	QList<JavaInstallPtr> JDK64s = this->FindJavaFromRegistryKey(
		KEY_WOW64_64KEY, "SOFTWARE\\JavaSoft\\Java Development Kit");
	QList<JavaInstallPtr> JRE32s = this->FindJavaFromRegistryKey(
		KEY_WOW64_32KEY, "SOFTWARE\\JavaSoft\\Java Runtime Environment");
	QList<JavaInstallPtr> JDK32s = this->FindJavaFromRegistryKey(
		KEY_WOW64_32KEY, "SOFTWARE\\JavaSoft\\Java Development Kit");

	java_candidates.append(JRE64s);
	java_candidates.append(MakeJavaPtr("C:/Program Files/Java/jre8/bin/javaw.exe"));
	java_candidates.append(MakeJavaPtr("C:/Program Files/Java/jre7/bin/javaw.exe"));
	java_candidates.append(MakeJavaPtr("C:/Program Files/Java/jre6/bin/javaw.exe"));
	java_candidates.append(JDK64s);
	java_candidates.append(JRE32s);
	java_candidates.append(MakeJavaPtr("C:/Program Files (x86)/Java/jre8/bin/javaw.exe"));
	java_candidates.append(MakeJavaPtr("C:/Program Files (x86)/Java/jre7/bin/javaw.exe"));
	java_candidates.append(MakeJavaPtr("C:/Program Files (x86)/Java/jre6/bin/javaw.exe"));
	java_candidates.append(JDK32s);
	java_candidates.append(MakeJavaPtr(this->GetDefaultJava()->path));

	QList<QString> candidates;
	for(JavaInstallPtr java_candidate : java_candidates)
	{
		if(!candidates.contains(java_candidate->path))
		{
			candidates.append(java_candidate->path);
		}
	}

	return candidates;
}

#elif defined(Q_OS_MAC)
QList<QString> JavaUtils::FindJavaPaths()
{
	QList<QString> javas;
	javas.append(this->GetDefaultJava()->path);
	javas.append("/Applications/Xcode.app/Contents/Applications/Application Loader.app/Contents/MacOS/itms/java/bin/java");
	javas.append("/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java");
	javas.append("/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java");
	QDir libraryJVMDir("/Library/Java/JavaVirtualMachines/");
	QStringList libraryJVMJavas = libraryJVMDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
	foreach (const QString &java, libraryJVMJavas) {
		javas.append(libraryJVMDir.absolutePath() + "/" + java + "/Contents/Home/bin/java");
		javas.append(libraryJVMDir.absolutePath() + "/" + java + "/Contents/Home/jre/bin/java");
	}
	QDir systemLibraryJVMDir("/System/Library/Java/JavaVirtualMachines/");
	QStringList systemLibraryJVMJavas = systemLibraryJVMDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
	foreach (const QString &java, systemLibraryJVMJavas) {
		javas.append(systemLibraryJVMDir.absolutePath() + "/" + java + "/Contents/Home/bin/java");
		javas.append(systemLibraryJVMDir.absolutePath() + "/" + java + "/Contents/Commands/java");
	}
	return javas;
}

#elif defined(Q_OS_LINUX)
QList<QString> JavaUtils::FindJavaPaths()
{
	qDebug() << "Linux Java detection incomplete - defaulting to \"java\"";

	QList<QString> javas;
	javas.append(this->GetDefaultJava()->path);
	auto scanJavaDir = [&](const QString & dirPath)
	{
		QDir dir(dirPath);
		if(!dir.exists())
			return;
		auto entries = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
		for(auto & entry: entries)
		{

			QString prefix;
			if(entry.isAbsolute())
			{
				prefix = entry.absoluteFilePath();
			}
			else
			{
				prefix = entry.filePath();
			}

			javas.append(FS::PathCombine(prefix, "jre/bin/java"));
			javas.append(FS::PathCombine(prefix, "bin/java"));
		}
	};
	// oracle RPMs
	scanJavaDir("/usr/java");
	// general locations used by distro packaging
	scanJavaDir("/usr/lib/jvm");
	scanJavaDir("/usr/lib32/jvm");
	// javas stored in MultiMC's folder
	scanJavaDir("java");
	return javas;
}
#else
QList<QString> JavaUtils::FindJavaPaths()
{
	qDebug() << "Unknown operating system build - defaulting to \"java\"";

	QList<QString> javas;
	javas.append(this->GetDefaultJava()->path);

	return javas;
}
#endif