diff --git a/HMCL/src/main/resources/assets/HMCLauncher.exe b/HMCL/src/main/resources/assets/HMCLauncher.exe index 11eab6b26..a233150dd 100644 Binary files a/HMCL/src/main/resources/assets/HMCLauncher.exe and b/HMCL/src/main/resources/assets/HMCLauncher.exe differ diff --git a/HMCLauncher/.clang-format b/HMCLauncher/.clang-format new file mode 100644 index 000000000..7a5cb8897 --- /dev/null +++ b/HMCLauncher/.clang-format @@ -0,0 +1,2 @@ +# Use the Google style in this project. +BasedOnStyle: Google \ No newline at end of file diff --git a/HMCLauncher/HMCL/HMCL.vcxproj b/HMCLauncher/HMCL/HMCL.vcxproj index c1bb2d2cd..7c1a5bdea 100644 --- a/HMCLauncher/HMCL/HMCL.vcxproj +++ b/HMCLauncher/HMCL/HMCL.vcxproj @@ -120,7 +120,7 @@ true false true - WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions);_CRT_SECURE_NO_WARNINGS false MultiThreaded Disabled diff --git a/HMCLauncher/HMCL/main.cpp b/HMCLauncher/HMCL/main.cpp index c51747685..47b29e104 100644 --- a/HMCLauncher/HMCL/main.cpp +++ b/HMCLauncher/HMCL/main.cpp @@ -8,13 +8,13 @@ using namespace std; Version J8(TEXT("8")); -void RawLaunchJVM(const wstring &javaPath, const wstring &jarPath) +void RawLaunchJVM(const wstring &javaPath, const wstring& workdir, const wstring &jarPath) { - if (MyCreateProcess(L"\"" + javaPath + L"\" -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=15 -jar \"" + jarPath + L"\"")) + if (MyCreateProcess(L"\"" + javaPath + L"\" -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=15 -jar \"" + jarPath + L"\"", workdir)) exit(EXIT_SUCCESS); } -void LaunchJVM(const wstring &javaPath, const wstring &jarPath) +void LaunchJVM(const wstring &javaPath, const wstring& workdir, const wstring &jarPath) { Version javaVersion(L""); if (!MyGetFileVersionInfo(javaPath, javaVersion)) @@ -22,7 +22,7 @@ void LaunchJVM(const wstring &javaPath, const wstring &jarPath) if (J8 <= javaVersion) { - RawLaunchJVM(javaPath, jarPath); + RawLaunchJVM(javaPath, workdir, jarPath); } } @@ -34,6 +34,13 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd if (ERROR_SUCCESS != MyGetModuleFileName(NULL, exeName)) return 1; + wstring workdir; + size_t last_slash = exeName.find_last_of(L"/\\"); + if (last_slash != wstring::npos && last_slash + 1 < exeName.size()) { + workdir = exeName.substr(0, last_slash); + exeName = exeName.substr(last_slash + 1); + } + // TODO: check whether the bundled JRE is valid. // First try the Java packaged together. bool is64Bit = false; @@ -41,15 +48,15 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd if (is64Bit) { - RawLaunchJVM(L"jre-x64\\bin\\javaw.exe", exeName); + RawLaunchJVM(L"jre-x64\\bin\\javaw.exe", workdir, exeName); } else { - RawLaunchJVM(L"jre-x86\\bin\\javaw.exe", exeName); + RawLaunchJVM(L"jre-x86\\bin\\javaw.exe", workdir, exeName); } if (FindJava(path)) - LaunchJVM(path + L"\\bin\\javaw.exe", exeName); + LaunchJVM(path + L"\\bin\\javaw.exe", workdir, exeName); // Or we try to search Java in C:\Program Files. { @@ -60,7 +67,7 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd do { wstring javaw = wstring(L"C:\\Program Files\\Java\\") + data.cFileName + wstring(L"\\bin\\javaw.exe"); if (FindFirstFileExists(javaw.c_str(), 0)) { - LaunchJVM(javaw, exeName); + LaunchJVM(javaw, workdir, exeName); } } while (FindNextFile(hFind, &data)); FindClose(hFind); @@ -76,7 +83,7 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd do { wstring javaw = wstring(L"C:\\Program Files (x86)\\Java\\") + data.cFileName + L"\\bin\\javaw.exe"; if (FindFirstFileExists(javaw.c_str(), 0)) { - LaunchJVM(javaw, exeName); + LaunchJVM(javaw, workdir, exeName); } } while (FindNextFile(hFind, &data)); FindClose(hFind); @@ -84,7 +91,7 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd } // Try java in PATH - RawLaunchJVM(L"javaw", exeName); + RawLaunchJVM(L"javaw", workdir, exeName); MessageBox(NULL, ERROR_PROMPT, L"Error", MB_ICONERROR | MB_OK); ShellExecute(0, 0, L"https://java.com/", 0, 0, SW_SHOW); diff --git a/HMCLauncher/HMCL/os.cpp b/HMCLauncher/HMCL/os.cpp index 17c0c3bc9..9b640e653 100644 --- a/HMCLauncher/HMCL/os.cpp +++ b/HMCLauncher/HMCL/os.cpp @@ -52,7 +52,7 @@ LSTATUS MyGetEnvironmentVariable(LPCWSTR name, std::wstring & out) } } -bool MyCreateProcess(const std::wstring &command) +bool MyCreateProcess(const std::wstring &command, const std::wstring &workdir) { wstring writable_command = command; STARTUPINFO si; @@ -61,7 +61,12 @@ bool MyCreateProcess(const std::wstring &command) ZeroMemory(&si, sizeof(si)); ZeroMemory(&pi, sizeof(pi)); - return (CreateProcess(NULL, &writable_command[0], NULL, NULL, false, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi)); + if (workdir.empty()) { + return CreateProcess(NULL, &writable_command[0], NULL, NULL, false, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi); + } + else { + return CreateProcess(NULL, &writable_command[0], NULL, NULL, false, NORMAL_PRIORITY_CLASS, NULL, workdir.c_str(), &si, &pi); + } } bool FindFirstFileExists(LPCWSTR lpPath, DWORD dwFilter) diff --git a/HMCLauncher/HMCL/os.h b/HMCLauncher/HMCL/os.h index e3207899d..ab2f69ff8 100644 --- a/HMCLauncher/HMCL/os.h +++ b/HMCLauncher/HMCL/os.h @@ -16,7 +16,7 @@ LSTATUS MyGetModuleFileName(HMODULE hModule, std::wstring &out); LSTATUS MyGetEnvironmentVariable(LPCWSTR name, std::wstring &out); // Create process by invoking CreateProcess, only pass command. -bool MyCreateProcess(const std::wstring &command); +bool MyCreateProcess(const std::wstring &command, const std::wstring& workdir); // Check if file lpPath exists. bool FindFirstFileExists(LPCWSTR lpPath, DWORD dwFilter);