diff --git a/HMCLauncher/HMCL/main.cpp b/HMCLauncher/HMCL/main.cpp index 9f8368cb6..d2a324fa6 100644 --- a/HMCLauncher/HMCL/main.cpp +++ b/HMCLauncher/HMCL/main.cpp @@ -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; diff --git a/HMCLauncher/HMCL/os.cpp b/HMCLauncher/HMCL/os.cpp index 4dc3c21cd..8718bd18e 100644 --- a/HMCLauncher/HMCL/os.cpp +++ b/HMCLauncher/HMCL/os.cpp @@ -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'"'; } \ No newline at end of file diff --git a/HMCLauncher/HMCL/os.h b/HMCLauncher/HMCL/os.h index 9af58f528..319e2194e 100644 --- a/HMCLauncher/HMCL/os.h +++ b/HMCLauncher/HMCL/os.h @@ -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); \ No newline at end of file +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); \ No newline at end of file