diff --git a/HMCLauncher/HMCL/main.cpp b/HMCLauncher/HMCL/main.cpp index d2a324fa6..1e6869ecd 100644 --- a/HMCLauncher/HMCL/main.cpp +++ b/HMCLauncher/HMCL/main.cpp @@ -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); } diff --git a/HMCLauncher/HMCL/os.cpp b/HMCLauncher/HMCL/os.cpp index 8718bd18e..24a619c6d 100644 --- a/HMCLauncher/HMCL/os.cpp +++ b/HMCLauncher/HMCL/os.cpp @@ -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"\\\""; diff --git a/HMCLauncher/HMCL/os.h b/HMCLauncher/HMCL/os.h index 319e2194e..71504d4ed 100644 --- a/HMCLauncher/HMCL/os.h +++ b/HMCLauncher/HMCL/os.h @@ -2,6 +2,7 @@ #include #include #include +#include #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); \ No newline at end of file