Skip to content

Commit

Permalink
Update install.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
burningtnt committed Jul 24, 2024
1 parent 8b1c3f5 commit 9e722ce
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 48 deletions.
Binary file modified HMCL/src/main/resources/assets/HMCLauncher.exe
Binary file not shown.
93 changes: 46 additions & 47 deletions HMCLauncher/HMCL/install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@
int InstallHMCLJRE(const std::wstring &home, const std::wstring &domain,
const std::wstring &file) {
WIN32_FIND_DATA ffd;
std::wstring runtime, jreDownloadedFile, jreDownloadedDirectory, command;
STARTUPINFO si;
PROCESS_INFORMATION pi;
std::wstring runtimeDirectory, jreDownloadedFile, jreDownloadedDirectory;

runtime = home + L"runtime";
runtimeDirectory = home + L"runtime";
jreDownloadedFile = home + L".hmclauncher-jre-";
jreDownloadedFile.append(std::to_wstring(rand()));
jreDownloadedDirectory = jreDownloadedFile + L"";
jreDownloadedFile.append(L".zip");

HANDLE hFind = INVALID_HANDLE_VALUE, hCleanable[8];
for (int i = 0;i < 8;i ++) {
hCleanable[i] = INVALID_HANDLE_VALUE;
}
HANDLE hFind = INVALID_HANDLE_VALUE;

int err = 0;

Expand All @@ -31,38 +26,7 @@ int InstallHMCLJRE(const std::wstring &home, const std::wstring &domain,
goto cleanup;
}

command = L"tar.exe -xf \"" + jreDownloadedFile + L"\"";
si.cb = sizeof(si);
ZeroMemory(&si, sizeof(si));
ZeroMemory(&pi, sizeof(pi));

if (!CreateProcess(NULL, &command[0], NULL, NULL, false,
NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW, NULL, jreDownloadedDirectory.c_str(), &si,
&pi)) {
err = GetLastError();
goto cleanup;
}
hCleanable[0] = si.hStdInput;
hCleanable[1] = si.hStdOutput;
hCleanable[2] = si.hStdError;
hCleanable[3] = pi.hProcess;
hCleanable[4] = pi.hThread;

while (true) {
DWORD exitCode;
if (!GetExitCodeProcess(pi.hProcess, &exitCode)) {
err = GetLastError();
goto cleanup;
}

if (exitCode != STILL_ACTIVE) {
if (exitCode != 0) {
err = exitCode;
goto cleanup;
}
break;
}
}
UncompressHMCLJRE(jreDownloadedFile, jreDownloadedDirectory);

hFind = FindFirstFile((jreDownloadedDirectory + L"\\*").c_str(), &ffd);
if (hFind == INVALID_HANDLE_VALUE) {
Expand Down Expand Up @@ -97,7 +61,7 @@ int InstallHMCLJRE(const std::wstring &home, const std::wstring &domain,
goto cleanup;
}

if (!MoveFile((jreDownloadedDirectory + L'\\' + targetFile).c_str(), runtime.c_str())) {
if (!MoveFile((jreDownloadedDirectory + L'\\' + targetFile).c_str(), runtimeDirectory.c_str())) {
err = GetLastError();
goto cleanup;
}
Expand All @@ -107,12 +71,6 @@ int InstallHMCLJRE(const std::wstring &home, const std::wstring &domain,
if (hFind != INVALID_HANDLE_VALUE) {
CloseHandle(hFind);
}
for (int i = 0;i < 8;i ++) {
HANDLE hCH = hCleanable[i];
if (hCH != INVALID_HANDLE_VALUE) {
CloseHandle(hCH);
}
}
RemoveDirectory(jreDownloadedDirectory.c_str());
DeleteFile(jreDownloadedFile.c_str());
return err;
Expand Down Expand Up @@ -186,5 +144,46 @@ int DownloadHMCLJRE(std::wstring &jreDownloadedFile, const std::wstring &domain,
if (fd != NULL) {
CloseHandle(fd);
}
return err;
}

int UncompressHMCLJRE(std::wstring jreDownloadedFile, std::wstring jreDownloadedDirectory) {
std::wstring command = L"tar.exe -xf \"" + jreDownloadedFile + L"\"";
int err = 0;

STARTUPINFO si;
PROCESS_INFORMATION pi;

si.cb = sizeof(si);
ZeroMemory(&si, sizeof(si));
ZeroMemory(&pi, sizeof(pi));
if (!CreateProcess(NULL, &command[0], NULL, NULL, false,
NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW, NULL, jreDownloadedDirectory.c_str(), &si,
&pi)) {
return GetLastError();
}

while (true) {
DWORD exitCode;
if (!GetExitCodeProcess(pi.hProcess, &exitCode)) {
err = GetLastError();
goto cleanup;
}
if (exitCode != STILL_ACTIVE) {
if (exitCode != 0) {
err = exitCode;
goto cleanup;
}
break;
}
}

cleanup:
CloseHandle(si.hStdInput);
CloseHandle(si.hStdOutput);
CloseHandle(si.hStdError);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

return err;
}
4 changes: 3 additions & 1 deletion HMCLauncher/HMCL/install.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ int InstallHMCLJRE(const std::wstring &home, const std::wstring &domain,
const std::wstring &file);

int DownloadHMCLJRE(std::wstring &target, const std::wstring &domain,
const std::wstring &file);
const std::wstring &file);

int UncompressHMCLJRE(std::wstring jreDownloadedFile, std::wstring jreDownloadedDirectory);

0 comments on commit 9e722ce

Please sign in to comment.