mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-01-24 14:34:15 +08:00
Automatically download Java when Java is not found
This commit is contained in:
parent
a1f8ff7463
commit
1b9b0226f2
@ -129,11 +129,8 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
|
||||
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;
|
||||
if (ERROR_SUCCESS != MyGetTempFile(L"hmcl-download-java-", L"ps1", tempScriptPath)) goto error;
|
||||
|
||||
HANDLE hFile;
|
||||
DWORD dwBytesWritten = 0;
|
||||
@ -149,12 +146,20 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
|
||||
|
||||
std::wstring commandLineBuffer;
|
||||
|
||||
commandLineBuffer += L"powershell.exe -ExecutionPolicy Bypass ";
|
||||
commandLineBuffer += L"powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -File ";
|
||||
MyAppendPathToCommandLine(commandLineBuffer, tempScriptPath);
|
||||
commandLineBuffer += L" -JavaDir ";
|
||||
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
|
||||
FindJavaInDirAndLaunchJVM(hmclJavaDir, workdir, exeName);
|
||||
}
|
||||
|
@ -125,34 +125,38 @@ void MyPathAddBackslash(std::wstring &filePath) {
|
||||
}
|
||||
}
|
||||
|
||||
LSTATUS MyGetTempPath(std::wstring &out) {
|
||||
out = std::wstring();
|
||||
LSTATUS MyGetTempFile(const std::wstring &prefixString, const std::wstring &ext, std::wstring &out) {
|
||||
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);
|
||||
GUID guid;
|
||||
CoCreateGuid(&guid);
|
||||
|
||||
if (GetTempFileName(pathName.c_str(), prefixString.c_str(), 0, &out[0]) == 0) {
|
||||
out.resize(0);
|
||||
return GetLastError();
|
||||
WCHAR buffer[MAX_PATH];
|
||||
int n = StringFromGUID2(guid, buffer, MAX_PATH);
|
||||
if (n == 0) {
|
||||
return CO_E_PATHTOOLONG;
|
||||
}
|
||||
out.resize(wcslen(&out[0]));
|
||||
|
||||
MyPathAddBackslash(out);
|
||||
out += prefixString;
|
||||
out += buffer;
|
||||
out += L'.';
|
||||
out += ext;
|
||||
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
void MyAppendPathToCommandLine(std::wstring &commandLine, const std::wstring &path) {
|
||||
commandLine += L'"';
|
||||
for (WCHAR ch : path) {
|
||||
if (ch == L'\\') {
|
||||
for (size_t i = 0; i < path.size(); i++) {
|
||||
WCHAR ch = path[i];
|
||||
if (ch == L'\\' && (i + 1 == path.size() || path[i + 1] == L'"')) {
|
||||
commandLine += L"\\\\";
|
||||
} else if (ch == L'"') {
|
||||
commandLine += L"\\\"";
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include <string>
|
||||
#include <windows.h>
|
||||
#include <shlobj.h>
|
||||
#include <Objbase.h>
|
||||
#include "Version.h"
|
||||
|
||||
const int MAX_KEY_LENGTH = 255;
|
||||
@ -33,8 +34,6 @@ void MyPathAppend(std::wstring &filePath, const std::wstring &more);
|
||||
|
||||
void MyPathAddBackslash(std::wstring &filePath);
|
||||
|
||||
LSTATUS MyGetTempPath(std::wstring &out);
|
||||
|
||||
LSTATUS MyGetTempFileName(const std::wstring &pathName, const std::wstring &prefixString, std::wstring &out);
|
||||
LSTATUS MyGetTempFile(const std::wstring &prefixString, const std::wstring &ext, std::wstring &out);
|
||||
|
||||
void MyAppendPathToCommandLine(std::wstring &commandLine, const std::wstring &path);
|
Loading…
Reference in New Issue
Block a user