Automatically download Java when Java is not found

This commit is contained in:
Glavo
2022-02-07 23:10:02 +08:00
committed by Yuhui Huang
parent a1f8ff7463
commit 1b9b0226f2
3 changed files with 31 additions and 23 deletions

View File

@@ -129,11 +129,8 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
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; std::wstring tempScriptPath;
if (ERROR_SUCCESS != MyGetTempFileName(tempPath, L"hmcl", tempScriptPath)) goto error; if (ERROR_SUCCESS != MyGetTempFile(L"hmcl-download-java-", L"ps1", tempScriptPath)) goto error;
HANDLE hFile; HANDLE hFile;
DWORD dwBytesWritten = 0; DWORD dwBytesWritten = 0;
@@ -149,12 +146,20 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
std::wstring commandLineBuffer; std::wstring commandLineBuffer;
commandLineBuffer += L"powershell.exe -ExecutionPolicy Bypass "; commandLineBuffer += L"powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -File ";
MyAppendPathToCommandLine(commandLineBuffer, tempScriptPath); MyAppendPathToCommandLine(commandLineBuffer, tempScriptPath);
commandLineBuffer += L" -JavaDir "; commandLineBuffer += L" -JavaDir ";
MyAppendPathToCommandLine(commandLineBuffer, hmclJavaDir); MyAppendPathToCommandLine(commandLineBuffer, hmclJavaDir);
MyCreateProcess(commandLineBuffer, workdir); STARTUPINFO si;
PROCESS_INFORMATION pi;
si.cb = sizeof(si);
ZeroMemory(&si, sizeof(si));
ZeroMemory(&pi, sizeof(pi));
if (!CreateProcess(NULL, &commandLineBuffer[0], NULL, NULL, false, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi)) goto error;
WaitForSingleObject(pi.hProcess, INFINITE);
DeleteFile(tempScriptPath.c_str());
// Try starting again after installing Java // Try starting again after installing Java
FindJavaInDirAndLaunchJVM(hmclJavaDir, workdir, exeName); FindJavaInDirAndLaunchJVM(hmclJavaDir, workdir, exeName);
} }

View File

@@ -125,34 +125,38 @@ void MyPathAddBackslash(std::wstring &filePath) {
} }
} }
LSTATUS MyGetTempPath(std::wstring &out) { LSTATUS MyGetTempFile(const std::wstring &prefixString, const std::wstring &ext, std::wstring &out) {
out = std::wstring();
out.resize(MAX_PATH); out.resize(MAX_PATH);
DWORD res = GetTempPath(MAX_PATH, &out[0]); DWORD res = GetTempPath(MAX_PATH, &out[0]);
if (res == 0) { if (res == 0) {
return GetLastError(); return GetLastError();
} }
out.resize(res); out.resize(res);
return ERROR_SUCCESS;
GUID guid;
CoCreateGuid(&guid);
WCHAR buffer[MAX_PATH];
int n = StringFromGUID2(guid, buffer, MAX_PATH);
if (n == 0) {
return CO_E_PATHTOOLONG;
} }
LSTATUS MyGetTempFileName(const std::wstring &pathName, const std::wstring &prefixString, std::wstring &out) { MyPathAddBackslash(out);
out = std::wstring(); out += prefixString;
out.resize(MAX_PATH); out += buffer;
out += L'.';
out += ext;
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; return ERROR_SUCCESS;
} }
void MyAppendPathToCommandLine(std::wstring &commandLine, const std::wstring &path) { void MyAppendPathToCommandLine(std::wstring &commandLine, const std::wstring &path) {
commandLine += L'"'; commandLine += L'"';
for (WCHAR ch : path) { for (size_t i = 0; i < path.size(); i++) {
if (ch == L'\\') { WCHAR ch = path[i];
if (ch == L'\\' && (i + 1 == path.size() || path[i + 1] == L'"')) {
commandLine += L"\\\\"; commandLine += L"\\\\";
} else if (ch == L'"') { } else if (ch == L'"') {
commandLine += L"\\\""; commandLine += L"\\\"";

View File

@@ -2,6 +2,7 @@
#include <string> #include <string>
#include <windows.h> #include <windows.h>
#include <shlobj.h> #include <shlobj.h>
#include <Objbase.h>
#include "Version.h" #include "Version.h"
const int MAX_KEY_LENGTH = 255; const int MAX_KEY_LENGTH = 255;
@@ -33,8 +34,6 @@ 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 MyGetTempFile(const std::wstring &prefixString, const std::wstring &ext, 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); void MyAppendPathToCommandLine(std::wstring &commandLine, const std::wstring &path);