forked from HMCL-dev/HMCLauncher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava.cpp
More file actions
228 lines (187 loc) · 7.05 KB
/
java.cpp
File metadata and controls
228 lines (187 loc) · 7.05 KB
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
#include <windows.h>
#include <cwctype>
#include <format>
#include <vector>
#include "debug.h"
#include "java.h"
HLJavaVersion HLJavaVersion::INVALID = HLJavaVersion{};
HLJavaVersion HLJavaVersion::FromJavaExecutable(const HLPath &filePath) {
UINT size = 0;
VS_FIXEDFILEINFO *pFileInfo;
DWORD dwSize = GetFileVersionInfoSizeW(filePath.path.c_str(), nullptr);
if (!dwSize) return INVALID;
std::vector<BYTE> data(dwSize);
if (!GetFileVersionInfoW(filePath.path.c_str(), 0, dwSize, &data[0])) {
return INVALID;
}
if (!VerQueryValueW(&data[0], L"\\", (LPVOID *)&pFileInfo, &size)) {
return INVALID;
}
return HLJavaVersion{.major = static_cast<uint16_t>((pFileInfo->dwFileVersionMS >> 16) & 0xFFFF),
.minor = static_cast<uint16_t>((pFileInfo->dwFileVersionMS >> 0) & 0xFFFF),
.build = static_cast<uint16_t>((pFileInfo->dwFileVersionLS >> 16) & 0xFFFF),
.revision = static_cast<uint16_t>((pFileInfo->dwFileVersionLS >> 0) & 0xFFFF)};
}
HLJavaVersion HLJavaVersion::FromString(const std::wstring &versionString) {
HLJavaVersion version = {};
uint16_t ver[4] = {0, 0, 0, 0}; // major, minor, build, revision
int idx = 0;
for (auto &ch : versionString) {
if (idx >= 4) {
break;
}
if (ch == L'.' || ch == L'_') {
if (idx == 0 && ver[0] == 1) {
// For legacy Java version 1.x
ver[0] = 0;
} else {
idx++;
}
} else if (ch >= L'0' && ch <= L'9') {
ver[idx] = ver[idx] * 10 + (ch - L'0');
}
}
return HLJavaVersion{
.major = ver[0],
.minor = ver[1],
.build = ver[2],
.revision = ver[3],
};
}
bool HLLaunchJVM(const HLPath &javaExecutablePath, const HLJavaOptions &options,
const std::optional<HLJavaVersion> &version) {
std::wstring command;
command += '"';
command += javaExecutablePath.path;
command += L'"';
if (options.jvmOptions.has_value()) {
command += L' ';
command += options.jvmOptions.value();
} else {
command += L" -Xmx1G -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=15";
}
command += L" -jar \"";
command += options.jarPath;
command += L'"';
STARTUPINFOW startupInfo{.cb = sizeof(STARTUPINFOW)};
PROCESS_INFORMATION processInformation{};
BOOL result = CreateProcessW(nullptr, &command[0], nullptr, nullptr, false, NORMAL_PRIORITY_CLASS, nullptr,
options.workdir.path.c_str(), &startupInfo, &processInformation);
if (result) {
HLDebugLog(L"Successfully launched HMCL with " + javaExecutablePath.path);
} else {
HLDebugLog(L"Failed to launch HMCL with " + javaExecutablePath.path);
}
return result;
}
void HLSearchJavaInDir(HLJavaList &result, const HLPath &basedir, LPCWSTR javaExecutableName) {
HLDebugLogVerbose(std::format(L"Searching in directory: {}", basedir.path));
HLPath pattern = basedir;
pattern /= L"*";
WIN32_FIND_DATA data;
HANDLE hFind = FindFirstFileW(pattern.path.c_str(), &data); // Search all subdirectory
if (hFind != INVALID_HANDLE_VALUE) {
do {
std::wstring fileName = data.cFileName;
if (fileName != L"." && fileName != L"..") {
result.TryAdd(basedir / data.cFileName / L"bin" / javaExecutableName);
}
} while (FindNextFile(hFind, &data));
FindClose(hFind);
}
}
static const LPCWSTR VENDORS[] = {L"Java", L"Microsoft", L"BellSoft", L"Zulu", L"Eclipse Foundation",
L"AdoptOpenJDK", L"Semeru"};
void HLSearchJavaInProgramFiles(HLJavaList &result, const HLPath &programFiles, LPCWSTR javaExecutableName) {
for (LPCWSTR vendorDir : VENDORS) {
HLPath dir = programFiles / vendorDir;
HLSearchJavaInDir(result, dir, javaExecutableName);
}
}
void HLSearchJavaInRegistry(HLJavaList &result, LPCWSTR subKey, LPCWSTR javaExecutableName) {
HLDebugLogVerbose(std::format(L"Searching in registry key: HKEY_LOCAL_MACHINE\\{}", subKey));
constexpr int MAX_KEY_LENGTH = 255;
WCHAR javaVer[MAX_KEY_LENGTH]; // buffer for subkey name, special for
// JavaVersion
WCHAR javaHome[MAX_PATH]; // buffer for JavaHome value
DWORD cbName; // size of name string
DWORD cSubKeys = 0; // number of subkeys
DWORD cbMaxSubKey; // longest subkey size
DWORD cValues; // number of values for key
DWORD cchMaxValue; // longest value name
DWORD cbMaxValueData; // longest value data
HKEY hKey;
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, subKey, 0, KEY_WOW64_64KEY | KEY_READ, &hKey) != ERROR_SUCCESS) {
return;
}
RegQueryInfoKeyW(hKey, nullptr, nullptr, nullptr, &cSubKeys, &cbMaxSubKey, nullptr, &cValues, &cchMaxValue,
&cbMaxValueData, nullptr, nullptr);
if (!cSubKeys) {
RegCloseKey(hKey);
return;
}
for (DWORD i = 0; i < cSubKeys; ++i) {
cbName = MAX_KEY_LENGTH;
if (RegEnumKeyExW(hKey, i, javaVer, &cbName, nullptr, nullptr, nullptr, nullptr) != ERROR_SUCCESS) {
continue;
}
DWORD dataLength = sizeof(javaHome);
if (RegGetValueW(hKey, javaVer, L"JavaHome", RRF_RT_REG_SZ, nullptr, &javaHome[0], &dataLength) != ERROR_SUCCESS) {
continue;
}
result.TryAdd(HLPath(javaHome) / L"bin" / javaExecutableName);
}
RegCloseKey(hKey);
}
void HLSearchJavaInPath(HLJavaList &result, const std::wstring &path, LPCWSTR javaExecutableName) {
std::size_t pos = 0;
while (pos < path.size()) {
auto end = path.find(L';', pos);
if (end == std::wstring::npos) {
end = path.size();
}
// Skip leading spaces
while (pos < end && std::iswspace(path.at(pos))) {
pos++;
}
// Skip trailing spaces
auto pathCount = end - pos;
while (pathCount > 0 && std::iswspace(path.at(pos + pathCount - 1))) {
pathCount--;
}
if (pathCount > 0) { // Not empty
HLPath javaExecutable = path.substr(pos, pathCount);
javaExecutable /= javaExecutableName;
// https://github.com/HMCL-dev/HMCL/issues/4079
if (javaExecutable.path.find(L"\\Common Files\\Oracle\\Java\\") == std::wstring::npos) {
HLDebugLogVerbose(L"Checking " + javaExecutable.path);
result.TryAdd(javaExecutable);
} else {
HLDebugLogVerbose(std::format(L"Ignore Oracle Java {}", javaExecutable.path));
}
}
pos = end + 1;
}
}
bool HLJavaList::TryAdd(const HLPath &javaExecutable) {
if (!javaExecutable.IsRegularFile()) {
return false;
}
if (paths.contains(javaExecutable.path)) {
HLDebugLogVerbose(std::format(L"Ignore duplicate Java {}", javaExecutable.path));
return false;
}
auto version = HLJavaVersion::FromJavaExecutable(javaExecutable);
HLDebugLogVerbose(std::format(L"Found Java {}, Version {}", javaExecutable.path, version.ToWString(),
version.IsAcceptable() ? L"" : L", Ignored"));
if (!version.IsAcceptable()) {
return false;
}
HLJavaRuntime javaRuntime = {
.version = version,
.executablePath = javaExecutable,
};
paths.insert(javaExecutable.path);
runtimes.push_back(javaRuntime);
return true;
}