mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-11 12:56:53 -04:00
Extract and execute the script 'java-download.ps1'
This commit is contained in:
parent
aafe7f44e7
commit
a1f8ff7463
@ -120,17 +120,47 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
|
|||||||
VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, op);
|
VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, op);
|
||||||
|
|
||||||
// Try downloading Java on Windows 7 or later
|
// Try downloading Java on Windows 7 or later
|
||||||
if (VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION, dwlConditionMask)) {
|
if (!VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION, dwlConditionMask)) goto error;
|
||||||
|
|
||||||
HRSRC scriptFileResource = FindResource(NULL, MAKEINTRESOURCE(ID_SCRIPT_DOWNLOAD_JAVA), RT_RCDATA);
|
HRSRC scriptFileResource = FindResource(NULL, MAKEINTRESOURCE(ID_SCRIPT_DOWNLOAD_JAVA), RT_RCDATA);
|
||||||
if (scriptFileResource) {
|
if (!scriptFileResource) goto error;
|
||||||
|
|
||||||
HGLOBAL scriptHandle = LoadResource(NULL, scriptFileResource);
|
HGLOBAL scriptHandle = LoadResource(NULL, scriptFileResource);
|
||||||
DWORD dataSize = SizeofResource(NULL, scriptFileResource);
|
DWORD dataSize = SizeofResource(NULL, scriptFileResource);
|
||||||
void *data = LockResource(scriptHandle);
|
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);
|
MessageBox(NULL, ERROR_PROMPT, L"Error", MB_ICONERROR | MB_OK);
|
||||||
ShellExecute(0, 0, L"https://www.microsoft.com/openjdk", 0, 0, SW_SHOW);
|
ShellExecute(0, 0, L"https://www.microsoft.com/openjdk", 0, 0, SW_SHOW);
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -124,3 +124,41 @@ void MyPathAddBackslash(std::wstring &filePath) {
|
|||||||
filePath += 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'"';
|
||||||
|
}
|
@ -32,3 +32,9 @@ HRESULT MySHGetFolderPath(int csidl, std::wstring &out);
|
|||||||
void MyPathAppend(std::wstring &filePath, const std::wstring &more);
|
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);
|
Loading…
x
Reference in New Issue
Block a user