Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

重构 HMCLauncher #3007

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions HMCLauncher/HMCL/Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ class Version {
if (ver[i] != other.ver[i]) return ver[i] < other.ver[i];
return true;
}

bool operator>=(const Version &other) const {
for (int i = 0; i < 4; ++i)
if (ver[i] != other.ver[i]) return ver[i] > other.ver[i];
return true;
}
};
170 changes: 140 additions & 30 deletions HMCLauncher/HMCL/java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,94 @@

const Version JAVA_8(L"1.8"), JAVA_11(L"11");

const LPCWSTR JDK_NEW = L"SOFTWARE\\JavaSoft\\JDK";
const LPCWSTR JRE_NEW = L"SOFTWARE\\JavaSoft\\JRE";
const LPCWSTR JDK_OLD = L"SOFTWARE\\JavaSoft\\Java Development Kit";
const LPCWSTR JRE_OLD = L"SOFTWARE\\JavaSoft\\Java Runtime Environment";
const LPCWSTR JDK_HKEYS[] = {L"SOFTWARE\\JavaSoft\\JDK",
L"SOFTWARE\\JavaSoft\\JRE",
L"SOFTWARE\\JavaSoft\\Java Development Kit",
L"SOFTWARE\\JavaSoft\\Java Runtime Environment"};

bool oldJavaFound = false;
const LPCWSTR VENDOR_DIRS[] = {L"Java", L"Microsoft", L"BellSoft",
L"Zulu", L"Eclipse Foundation", L"AdoptOpenJDK",
L"Semeru"};

bool FindJavaByRegistryKey(HKEY rootKey, LPCWSTR subKey, std::wstring& path) {
const LPCWSTR PROGRAM_DIRS[] = {L"ProgramFiles", L"ProgramFiles(x86)",
L"ProgramW6432"};

/* Here we find the java from Environment Variable and Registry, which is fast,
* and store the result to 'path'. */
void ScanJava(std::wstring& result, int& status) {
status = JAVA_STATUS_NOT_FOUND;

// If 'HMCL_JAVA_HOME' is settled, this value MUST be used without any check.
if (ERROR_SUCCESS == MyGetEnvironmentVariable(L"HMCL_JAVA_HOME", result)) {
MyPathNormalize(result);
status = JAVA_STATUS_BEST;
return;
}

std::wstring currentResult;
if (ERROR_SUCCESS == MyGetEnvironmentVariable(L"JAVA_HOME", currentResult)) {
MyPathNormalize(result);
if (DeterminJavaHome(currentResult, result, status) &&
status == JAVA_STATUS_BEST) {
return;
}
}

for (LPCWSTR hkey : JDK_HKEYS) {
ScanJavaRegistry(HKEY_LOCAL_MACHINE, hkey, result, status);
if (status == JAVA_STATUS_BEST) {
return;
}
}

std::wstring envPath;
if (ERROR_SUCCESS == MyGetEnvironmentVariable(L"PATH", envPath)) {
int length = envPath.size();
int lastI = 0;
for (int i = 0; i < length; i++) {
if (envPath[i] == L';') {
int partL = i - lastI;
if (partL > 0) {
std::wstring part = envPath.substr(lastI, partL);
MyPathNormalize(part);
MyPathAddBackslash(part);
int partL2 = part.size();
if (part[partL2 - 5] == L'\\' && part[partL2 - 4] == L'b' &&
part[partL2 - 3] == L'i' && part[partL2 - 2] == L'n' &&
part[partL2 - 1] == L'\\') {
part.resize(partL2 - 5);
if (DeterminJavaHome(part, result, status) && status == JAVA_STATUS_BEST) {
return;
}
}
}

lastI = i + 1;
}
}
}

std::wstring root;
for (LPCWSTR program : PROGRAM_DIRS) {
if (ERROR_SUCCESS != MyGetEnvironmentVariable(program, root)) {
continue;
}

MyPathNormalize(root);
for (LPCWSTR vender : VENDOR_DIRS) {
std::wstring currentRoot = root + L"";
MyPathAppend(currentRoot, vender);
MyPathAddBackslash(currentRoot);
ScanJavaFolder(currentRoot, result, status);
if (status == JAVA_STATUS_BEST) {
return;
}
}
}
}

void ScanJavaRegistry(HKEY rootKey, LPCWSTR subKey, std::wstring& path,
int& status) {
WCHAR javaVer[MAX_KEY_LENGTH]; // buffer for subkey name, special for
// JavaVersion
DWORD cbName; // size of name string
Expand All @@ -25,9 +105,10 @@ bool FindJavaByRegistryKey(HKEY rootKey, LPCWSTR subKey, std::wstring& path) {

HKEY hKey;
if (ERROR_SUCCESS !=
(result =
RegOpenKeyEx(rootKey, subKey, 0, KEY_WOW64_64KEY | KEY_READ, &hKey)))
return false;
(result = RegOpenKeyEx(rootKey, subKey, 0, KEY_WOW64_64KEY | KEY_READ,
&hKey))) {
return;
}

RegQueryInfoKey(hKey, // key handle
NULL, // buffer for class name
Expand All @@ -42,9 +123,10 @@ bool FindJavaByRegistryKey(HKEY rootKey, LPCWSTR subKey, std::wstring& path) {
NULL, // security descriptor
NULL); // last write time

if (!cSubKeys) return false;
if (!cSubKeys) {
return;
}

bool flag = false;
for (DWORD i = 0; i < cSubKeys; ++i) {
cbName = MAX_KEY_LENGTH;
if (ERROR_SUCCESS != (result = RegEnumKeyEx(hKey, i, javaVer, &cbName, NULL,
Expand All @@ -55,30 +137,58 @@ bool FindJavaByRegistryKey(HKEY rootKey, LPCWSTR subKey, std::wstring& path) {
if (ERROR_SUCCESS != RegOpenKeyEx(hKey, javaVer, 0, KEY_READ, &javaKey))
continue;

if (ERROR_SUCCESS == MyRegQueryValue(javaKey, L"JavaHome", REG_SZ, path)) {
if (Version(javaVer) < JAVA_8)
oldJavaFound = true;
else
flag = true;
}
std::wstring currentPath;
if (ERROR_SUCCESS ==
MyRegQueryValue(javaKey, L"JavaHome", REG_SZ, currentPath)) {
MyPathNormalize(currentPath);
Version v = Version(javaVer);

if (flag) break;
if (status < JAVA_STATUS_BEST && v >= JAVA_11) {
path = currentPath;
status = JAVA_STATUS_BEST;
break;
} else if (status < JAVA_STATUS_USABLE && v >= JAVA_8) {
path = currentPath;
status = JAVA_STATUS_USABLE;
}
}
}

RegCloseKey(hKey);

return flag;
}

bool FindJavaInRegistry(std::wstring& path) {
return FindJavaByRegistryKey(HKEY_LOCAL_MACHINE, JDK_NEW, path) ||
FindJavaByRegistryKey(HKEY_LOCAL_MACHINE, JRE_NEW, path) ||
FindJavaByRegistryKey(HKEY_LOCAL_MACHINE, JDK_OLD, path) ||
FindJavaByRegistryKey(HKEY_LOCAL_MACHINE, JRE_OLD, path);
}
void ScanJavaFolder(std::wstring root, std::wstring& result, int& status) {
WIN32_FIND_DATA data;
HANDLE hFind =
FindFirstFile((root + L"*").c_str(), &data); // Search all subdirectory

bool FindJava(std::wstring& path) {
return ERROR_SUCCESS == MyGetEnvironmentVariable(L"HMCL_JAVA_HOME", path) ||
ERROR_SUCCESS == MyGetEnvironmentVariable(L"JAVA_HOME", path) ||
FindJavaInRegistry(path);
if (hFind != INVALID_HANDLE_VALUE) {
do {
if (DeterminJavaHome(root + data.cFileName, result, status) && status == JAVA_STATUS_BEST) {
goto done;
}
} while (FindNextFile(hFind, &data));

done:
FindClose(hFind);
}
}

boolean DeterminJavaHome(std::wstring target, std::wstring& result, int& status) {
Version version(L"");
std::wstring currentRoot = target + L"";
MyPathAppend(currentRoot, std::wstring(L"bin\\javaw.exe"));

if (!MyGetFileVersionInfo(currentRoot, version)) return false;

if (status < JAVA_STATUS_BEST && version >= JAVA_11) {
result = target;
status = JAVA_STATUS_BEST;
return true;
} else if (status < JAVA_STATUS_USABLE && version >= JAVA_8) {
result = target;
status = JAVA_STATUS_USABLE;
return true;
}
return false;
}
17 changes: 13 additions & 4 deletions HMCLauncher/HMCL/java.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@
#include <windows.h>
#include <string>

// Find Java installation in system registry
bool FindJavaInRegistry(std::wstring &path);
// These increasing values represent the priority of the Java.
// Be careful while changing their values!
const int JAVA_STATUS_NOT_FOUND = 0;
const int JAVA_STATUS_USABLE = 1; // Java 8 - 11 (Exclude)
const int JAVA_STATUS_BEST = 2; // Java 11 (Include) - ++

// Find Java Installation in registry and environment variable
bool FindJava(std::wstring &path);
void ScanJava(std::wstring& result, int& status);

void ScanJavaRegistry(HKEY rootKey, LPCWSTR subKey, std::wstring& path,
int& status);

void ScanJavaFolder(std::wstring root, std::wstring& result, int& status);

boolean DeterminJavaHome(std::wstring target, std::wstring& result, int& status);
90 changes: 17 additions & 73 deletions HMCLauncher/HMCL/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,19 @@ _declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
_declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 0x00000001;
}

LPCWSTR VENDOR_DIRS[] = {
L"Java", L"Microsoft", L"BellSoft", L"Zulu", L"Eclipse Foundation", L"AdoptOpenJDK", L"Semeru"
};

void RawLaunchJVM(const std::wstring &javaPath, const std::wstring &workdir,
void LaunchHMCLDirect(const std::wstring &javaPath, const std::wstring &workdir,
const std::wstring &jarPath, const std::wstring &jvmOptions) {
if (MyCreateProcess(L"\"" + javaPath + L"\" " + jvmOptions + L" -jar \"" + jarPath + L"\"", workdir))
exit(EXIT_SUCCESS);
}

void LaunchJVM(const std::wstring &javaPath, const std::wstring &workdir,
void LaunchHMCL(const std::wstring &javaPath, const std::wstring &workdir,
const std::wstring &jarPath, const std::wstring &jvmOptions) {
Version javaVersion(L"");
if (!MyGetFileVersionInfo(javaPath, javaVersion)) return;

if (J8 <= javaVersion) {
RawLaunchJVM(javaPath, workdir, jarPath, jvmOptions);
}
}

void FindJavaInDirAndLaunchJVM(const std::wstring &baseDir, const std::wstring &workdir,
const std::wstring &jarPath, const std::wstring &jvmOptions) {
std::wstring pattern = baseDir + L"*";

WIN32_FIND_DATA data;
HANDLE hFind = FindFirstFile(pattern.c_str(), &data); // Search all subdirectory

if (hFind != INVALID_HANDLE_VALUE) {
do {
std::wstring javaw = baseDir + data.cFileName + std::wstring(L"\\bin\\javaw.exe");
if (FindFirstFileExists(javaw.c_str(), 0)) {
LaunchJVM(javaw, workdir, jarPath, jvmOptions);
}
} while (FindNextFile(hFind, &data));
FindClose(hFind);
LaunchHMCLDirect(javaPath, workdir, jarPath, jvmOptions);
}
}

Expand All @@ -56,7 +34,7 @@ void OpenHelpPage() {

int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow) {
std::wstring path, exeName, jvmOptions;
std::wstring exeName, jvmOptions;

// Since Jar file is appended to this executable, we should first get the
// location of JAR file.
Expand All @@ -78,66 +56,32 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
SYSTEM_INFO systemInfo;
GetNativeSystemInfo(&systemInfo);
// TODO: check whether the bundled JRE is valid.
// First try the Java packaged together.

// First, try the Java packaged together.
bool isX64 = (systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64);
bool isARM64 = (systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_ARM64);

if (isARM64) {
RawLaunchJVM(L"jre-arm64\\bin\\javaw.exe", workdir, exeName, jvmOptions);
LaunchHMCLDirect(L"jre-arm64\\bin\\javaw.exe", workdir, exeName, jvmOptions);
}
if (isX64) {
RawLaunchJVM(L"jre-x64\\bin\\javaw.exe", workdir, exeName, jvmOptions);
LaunchHMCLDirect(L"jre-x64\\bin\\javaw.exe", workdir, exeName, jvmOptions);
}
RawLaunchJVM(L"jre-x86\\bin\\javaw.exe", workdir, exeName, jvmOptions);

if (FindJava(path)) LaunchJVM(path + L"\\bin\\javaw.exe", workdir, exeName, jvmOptions);
LaunchHMCLDirect(L"jre-x86\\bin\\javaw.exe", workdir, exeName, jvmOptions);

std::wstring programFiles;
// Next, try the Java installed on thi computer.
std::wstring path;
int status;

// Or we try to search Java in C:\Program Files
if (!SUCCEEDED(MySHGetFolderPath(CSIDL_PROGRAM_FILES, programFiles))) programFiles = L"C:\\Program Files\\";
for (LPCWSTR vendorDir : VENDOR_DIRS) {
std::wstring dir = programFiles;
MyPathAppend(dir, vendorDir);
MyPathAddBackslash(dir);
ScanJava(path, status);

FindJavaInDirAndLaunchJVM(dir, workdir, exeName, jvmOptions);
}

// Consider C:\Program Files (x86)
if (!SUCCEEDED(MySHGetFolderPath(CSIDL_PROGRAM_FILESX86, programFiles))) programFiles = L"C:\\Program Files (x86)\\";
for (LPCWSTR vendorDir : VENDOR_DIRS) {
std::wstring dir = programFiles;
MyPathAppend(dir, vendorDir);
MyPathAddBackslash(dir);

FindJavaInDirAndLaunchJVM(dir, workdir, exeName, jvmOptions);
if (status != JAVA_STATUS_NOT_FOUND) {
MyPathAppend(path, std::wstring(L"bin\\javaw.exe"));
LaunchHMCL(path, workdir, exeName, jvmOptions);
}

// Try java in PATH
RawLaunchJVM(L"javaw", workdir, exeName, jvmOptions);

std::wstring hmclJavaDir;
{
std::wstring buffer;
if (SUCCEEDED(MySHGetFolderPath(CSIDL_APPDATA, buffer)) || SUCCEEDED(MySHGetFolderPath(CSIDL_PROFILE, buffer))) {
MyPathAppend(buffer, L".hmcl");
MyPathAppend(buffer, L"java");
if (isARM64) {
MyPathAppend(buffer, L"windows-arm64");
} else if (isX64) {
MyPathAppend(buffer, L"windows-x86_64");
} else {
MyPathAppend(buffer, L"windows-x86");
}
MyPathAddBackslash(buffer);
hmclJavaDir = buffer;
}
}

if (!hmclJavaDir.empty()) {
FindJavaInDirAndLaunchJVM(hmclJavaDir, workdir, exeName, jvmOptions);
}
LaunchHMCLDirect(L"javaw", workdir, exeName, jvmOptions);

LPCWSTR downloadLink;

Expand Down