Skip to content

Commit

Permalink
Feature: Scan PATH
Browse files Browse the repository at this point in the history
  • Loading branch information
burningtnt committed Apr 26, 2024
1 parent 27c4783 commit 3c2ed91
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 32 deletions.
83 changes: 52 additions & 31 deletions HMCLauncher/HMCL/java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
namespace JavaScanner {
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"};

const LPCWSTR VENDOR_DIRS[] = {L"Java", L"Microsoft", L"BellSoft",
L"Zulu", L"Eclipse Foundation", L"AdoptOpenJDK",
Expand All @@ -23,6 +23,7 @@ const LPCWSTR PROGRAM_DIRS[] = {L"ProgramFiles", L"ProgramFiles(x86)",
void scan(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);
Expand All @@ -34,32 +35,52 @@ void scan(std::wstring& result, int& status) {
std::wstring currentResult;
if (ERROR_SUCCESS == MyGetEnvironmentVariable(L"JAVA_HOME", currentResult)) {
MyPathNormalize(result);
if (determine(currentResult, result, status)) {
trace(result, status);
if (determine(currentResult, result, status) &&
status == JAVA_STATUS_BEST) {
return;
}
return;
}
scanRegistry(HKEY_LOCAL_MACHINE, JDK_NEW, result, status);
if (status == JAVA_STATUS_BEST) {
return;
}

scanRegistry(HKEY_LOCAL_MACHINE, JRE_NEW, result, status);
if (status == JAVA_STATUS_BEST) {
return;
}

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

scanRegistry(HKEY_LOCAL_MACHINE, JRE_OLD, 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';') {
// ";XXX": partL = 0
// ";;" : partL = 1
int partL = i - lastI;
if (partL > 1) {
std::wstring part = envPath.substr(lastI, partL);
MyPathNormalize(part);
MyPathAddBackslash(part);
MessageBox(NULL, (std::wstring(L"PATH1: ") + part).c_str(),
L"DEBUG INFO", MB_OK);
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);
MessageBox(NULL, (std::wstring(L"PATH2: ") + part).c_str(),
L"DEBUG INFO", MB_OK);
if (determine(part, result, status) && status == JAVA_STATUS_BEST) {
return;
}
}
}
}
}
}

/*
std::wstring root;
for (LPCWSTR program : PROGRAM_DIRS) {
if (ERROR_SUCCESS != MyGetEnvironmentVariable(program, root)) {
Expand All @@ -77,6 +98,7 @@ void scan(std::wstring& result, int& status) {
}
}
}
*/
}

void scanRegistry(HKEY rootKey, LPCWSTR subKey, std::wstring& path,
Expand Down Expand Up @@ -156,12 +178,8 @@ void scanFolder(std::wstring root, std::wstring& result, int& status) {
do {
std::wstring javaHome = root + data.cFileName;
trace(javaHome);
if (determine(javaHome, result, status)) {
trace(result, status);

if (status == JAVA_STATUS_BEST) {
goto done;
}
if (determine(javaHome, result, status) && status == JAVA_STATUS_BEST) {
goto done;
}
} while (FindNextFile(hFind, &data));

Expand All @@ -180,10 +198,12 @@ boolean determine(std::wstring target, std::wstring& result, int& status) {
if (status < JAVA_STATUS_BEST && version >= JAVA_11) {
result = target;
status = JAVA_STATUS_BEST;
trace(result, status);
return true;
} else if (status < JAVA_STATUS_USABLE && version >= JAVA_8) {
result = target;
status = JAVA_STATUS_USABLE;
trace(result, status);
return true;
}
return false;
Expand All @@ -199,7 +219,8 @@ inline void trace(std::wstring result, int status) {
}

inline void trace(std::wstring javaHome) {
// MessageBox(NULL, (std::wstring(L"Determining Java at ") + javaHome).c_str(),
// MessageBox(NULL, (std::wstring(L"Determining Java at ") +
// javaHome).c_str(),
// L"DEBUG INFO", MB_OK);
}
} // namespace JavaScanner
2 changes: 1 addition & 1 deletion HMCLauncher/HMCL/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ bool MyGetFileVersionInfo(const std::wstring &filePath, Version &version);

HRESULT MySHGetFolderPath(int csidl, std::wstring &out);

void MyPathNormalize(std::wstring& path)
void MyPathNormalize(std::wstring& path);

void MyPathAppend(std::wstring &filePath, const std::wstring &more);

Expand Down

0 comments on commit 3c2ed91

Please sign in to comment.