Extract and execute the script 'java-download.ps1'

This commit is contained in:
Glavo 2022-02-07 21:22:35 +08:00 committed by Yuhui Huang
parent aafe7f44e7
commit a1f8ff7463
3 changed files with 83 additions and 9 deletions

View File

@ -120,17 +120,47 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, op);
// Try downloading Java on Windows 7 or later
if (VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION, dwlConditionMask)) {
HRSRC scriptFileResource = FindResource(NULL, MAKEINTRESOURCE(ID_SCRIPT_DOWNLOAD_JAVA), RT_RCDATA);
if (scriptFileResource) {
HGLOBAL scriptHandle = LoadResource(NULL, scriptFileResource);
DWORD dataSize = SizeofResource(NULL, scriptFileResource);
void *data = LockResource(scriptHandle);
}
}
if (!VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION, dwlConditionMask)) goto error;
HRSRC scriptFileResource = FindResource(NULL, MAKEINTRESOURCE(ID_SCRIPT_DOWNLOAD_JAVA), RT_RCDATA);
if (!scriptFileResource) goto error;
HGLOBAL scriptHandle = LoadResource(NULL, scriptFileResource);
DWORD dataSize = SizeofResource(NULL, scriptFileResource);
void *data = LockResource(scriptHandle);
std::wstring tempPath;
if (ERROR_SUCCESS != MyGetTempPath(tempPath)) goto error;
std::wstring tempScriptPath;
if (ERROR_SUCCESS != MyGetTempFileName(tempPath, L"hmcl", tempScriptPath)) goto error;
HANDLE hFile;
DWORD dwBytesWritten = 0;
BOOL bErrorFlag = FALSE;
hFile = CreateFile(tempScriptPath.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) goto error;
bErrorFlag = WriteFile(hFile, data, dataSize, &dwBytesWritten, NULL);
if (FALSE == bErrorFlag || dwBytesWritten != dataSize) goto error;
CloseHandle(hFile);
std::wstring commandLineBuffer;
commandLineBuffer += L"powershell.exe -ExecutionPolicy Bypass ";
MyAppendPathToCommandLine(commandLineBuffer, tempScriptPath);
commandLineBuffer += L" -JavaDir ";
MyAppendPathToCommandLine(commandLineBuffer, hmclJavaDir);
MyCreateProcess(commandLineBuffer, workdir);
// Try starting again after installing Java
FindJavaInDirAndLaunchJVM(hmclJavaDir, workdir, exeName);
}
}
error:
MessageBox(NULL, ERROR_PROMPT, L"Error", MB_ICONERROR | MB_OK);
ShellExecute(0, 0, L"https://www.microsoft.com/openjdk", 0, 0, SW_SHOW);
return 1;

View File

@ -123,4 +123,42 @@ void MyPathAddBackslash(std::wstring &filePath) {
if (filePath.back() != L'\\') {
filePath += L'\\';
}
}
LSTATUS MyGetTempPath(std::wstring &out) {
out = std::wstring();
out.resize(MAX_PATH);
DWORD res = GetTempPath(MAX_PATH, &out[0]);
if (res == 0) {
return GetLastError();
}
out.resize(res);
return ERROR_SUCCESS;
}
LSTATUS MyGetTempFileName(const std::wstring &pathName, const std::wstring &prefixString, std::wstring &out) {
out = std::wstring();
out.resize(MAX_PATH);
if (GetTempFileName(pathName.c_str(), prefixString.c_str(), 0, &out[0]) == 0) {
out.resize(0);
return GetLastError();
}
out.resize(wcslen(&out[0]));
return ERROR_SUCCESS;
}
void MyAppendPathToCommandLine(std::wstring &commandLine, const std::wstring &path) {
commandLine += L'"';
for (WCHAR ch : path) {
if (ch == L'\\') {
commandLine += L"\\\\";
} else if (ch == L'"') {
commandLine += L"\\\"";
} else {
commandLine += ch;
}
}
commandLine += L'"';
}

View File

@ -31,4 +31,10 @@ HRESULT MySHGetFolderPath(int csidl, std::wstring &out);
void MyPathAppend(std::wstring &filePath, const std::wstring &more);
void MyPathAddBackslash(std::wstring &filePath);
void MyPathAddBackslash(std::wstring &filePath);
LSTATUS MyGetTempPath(std::wstring &out);
LSTATUS MyGetTempFileName(const std::wstring &pathName, const std::wstring &prefixString, std::wstring &out);
void MyAppendPathToCommandLine(std::wstring &commandLine, const std::wstring &path);