forked from HMCL-dev/HMCL
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/refactor-hmclauncher' into prs
- Loading branch information
Showing
11 changed files
with
282 additions
and
40 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
#include "stdafx.h | ||
#include "install.h" | ||
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; | ||
runtime = 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; | ||
} | ||
int err = 0; | ||
err = DownloadHMCLJRE(jreDownloadedFile, domain, file); | ||
if (err != 0) { | ||
goto cleanup; | ||
} | ||
if (!CreateDirectory(jreDownloadedDirectory.c_str(), NULL)) { | ||
err = GetLastError(); | ||
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; | ||
} | ||
} | ||
hFind = FindFirstFile((jreDownloadedDirectory + L"\\*").c_str(), &ffd); | ||
if (hFind == INVALID_HANDLE_VALUE) { | ||
err = GetLastError(); | ||
goto cleanup; | ||
} | ||
{ | ||
std::wstring targetFile = std::wstring(); | ||
do { | ||
std::wstring fileName = std::wstring(ffd.cFileName); | ||
if (fileName.size() <= 2 && fileName[0] == L'.') { | ||
continue; | ||
} | ||
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { | ||
if (targetFile.size() != 0) { | ||
err = HMCL_ERR_INVALID_JRE_PACK; | ||
goto cleanup; | ||
} | ||
targetFile.append(fileName); | ||
} | ||
} while (FindNextFile(hFind, &ffd) != 0); | ||
err = GetLastError(); | ||
if (err != ERROR_NO_MORE_FILES) { | ||
goto cleanup; | ||
} | ||
err = 0; | ||
if (targetFile.size() == 0) { | ||
err = HMCL_ERR_INVALID_JRE_PACK; | ||
goto cleanup; | ||
} | ||
if (!MoveFile((jreDownloadedDirectory + L'\\' + targetFile).c_str(), runtime.c_str())) { | ||
err = GetLastError(); | ||
goto cleanup; | ||
} | ||
} | ||
cleanup: | ||
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; | ||
} | ||
int DownloadHMCLJRE(std::wstring &jreDownloadedFile, const std::wstring &domain, | ||
const std::wstring &file) { | ||
int err = 0; | ||
HINTERNET hInternet = NULL, hConnection = NULL, hRequest = NULL; | ||
byte buffer[4096]; | ||
HANDLE fd = NULL; | ||
{ | ||
hInternet = InternetOpen(L"HMCLauncher", INTERNET_OPEN_TYPE_PRECONFIG, NULL, | ||
NULL, 0); | ||
if (hInternet == NULL) { | ||
err = GetLastError(); | ||
goto cleanup; | ||
} | ||
hConnection = | ||
InternetConnect(hInternet, domain.c_str(), INTERNET_DEFAULT_HTTPS_PORT, | ||
NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0); | ||
if (hConnection == NULL) { | ||
err = GetLastError(); | ||
goto cleanup; | ||
} | ||
const wchar_t *ACCEPTS[] = {L"application/zip", NULL}; | ||
hRequest = HttpOpenRequest(hConnection, L"GET", file.c_str(), NULL, NULL, | ||
ACCEPTS, INTERNET_FLAG_SECURE, 0); | ||
if (hRequest == NULL) { | ||
err = GetLastError(); | ||
goto cleanup; | ||
} | ||
if (!HttpSendRequest(hRequest, NULL, 0, NULL, 0)) { | ||
err = GetLastError(); | ||
goto cleanup; | ||
} | ||
{ | ||
DWORD receivedCount, unused; | ||
fd = CreateFile(jreDownloadedFile.c_str(), GENERIC_WRITE, 0, NULL, | ||
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | ||
if (fd == INVALID_HANDLE_VALUE) { | ||
err = GetLastError(); | ||
goto cleanup; | ||
} | ||
while (InternetReadFile(hRequest, buffer, 4096, &receivedCount) && | ||
receivedCount) { | ||
if (!WriteFile(fd, buffer, receivedCount, &unused, NULL)) { | ||
err = GetLastError(); | ||
goto cleanup; | ||
} | ||
} | ||
} | ||
} | ||
cleanup: | ||
if (hRequest != NULL) { | ||
InternetCloseHandle(hRequest); | ||
} | ||
if (hConnection != NULL) { | ||
InternetCloseHandle(hConnection); | ||
} | ||
if (hInternet != NULL) { | ||
InternetCloseHandle(hInternet); | ||
} | ||
if (fd != NULL) { | ||
CloseHandle(fd); | ||
} | ||
return err; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include "stdafx.h" | ||
|
||
#define HMCL_ERR_INVALID_JRE_PACK -129 | ||
|
||
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.