mirror of
https://gitlab.com/Monsterovich/openjdk-downloader.git
synced 2026-06-30 22:57:55 -04:00
Code refactoring
This commit is contained in:
+64
-61
@@ -17,8 +17,6 @@ static const char *OPENJDK_FOLDER_BASE = "jdk";
|
||||
#include <curl/curl.h>
|
||||
#include <elzip/elzip.hpp>
|
||||
|
||||
#include <thread>
|
||||
|
||||
struct CurlData {
|
||||
CURL *curl;
|
||||
FILE *fp;
|
||||
@@ -85,6 +83,65 @@ size_t CurlWriteData(void *ptr, size_t size, size_t nmemb, CurlData *data) {
|
||||
return written;
|
||||
}
|
||||
|
||||
DWORD WINAPI CurlDownloadZip(void *_data) {
|
||||
CurlData *data = (CurlData*)_data;
|
||||
CURL *curl;
|
||||
FILE *fp;
|
||||
CURLcode res;
|
||||
|
||||
char outFileName[FILENAME_MAX] = "temp.zip";
|
||||
|
||||
curl = curl_easy_init();
|
||||
|
||||
if (curl) {
|
||||
fp = fopen(outFileName, "wb");
|
||||
curl_easy_setopt(curl, CURLOPT_URL, OPENJDK_ZIP_URL);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteData);
|
||||
|
||||
data->fp = fp;
|
||||
data->curl = curl;
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, data);
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_CAPATH, "./");
|
||||
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
||||
|
||||
SetWindowTextA(data->hWnd, "OpenJDK downloader: resolving");
|
||||
|
||||
res = curl_easy_perform(curl);
|
||||
|
||||
fflush(fp);
|
||||
fclose(fp);
|
||||
|
||||
if (res == CURLE_OK) {
|
||||
SetWindowTextA(data->hWnd, "OpenJDK downloader: unpacking...");
|
||||
|
||||
elz::extractZip(outFileName, "./");
|
||||
|
||||
if (!DeleteFile(outFileName)) {
|
||||
printf("DeleteFile failed.");
|
||||
}
|
||||
if (!MoveFile(OPENJDK_FOLDER, OPENJDK_FOLDER_BASE)) {
|
||||
printf("MoveFile failed.");
|
||||
}
|
||||
SetWindowTextA(data->hWnd, "OpenJDK downloader: done!");
|
||||
done = true;
|
||||
} else {
|
||||
const char *error = curl_easy_strerror(res);
|
||||
printf("curl error: %s\n", error);
|
||||
SetWindowTextA(data->hWnd, error); if (!DeleteFile(outFileName)) {
|
||||
printf("DeleteFile failed.");
|
||||
}
|
||||
}
|
||||
|
||||
curl_easy_cleanup(curl);
|
||||
|
||||
printf("Done!\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool DirExists(const char *dirName)
|
||||
{
|
||||
DWORD ftyp = GetFileAttributesA(dirName);
|
||||
@@ -167,72 +224,18 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR pCmdLine, int nCmdShow)
|
||||
::ShowWindow(hWnd, SW_SHOWDEFAULT);
|
||||
::UpdateWindow(hWnd);
|
||||
|
||||
std::thread downloadThread([hWnd, hProgressBarCtrl]() {
|
||||
CURL *curl;
|
||||
FILE *fp;
|
||||
CURLcode res;
|
||||
CurlData data;
|
||||
data.hProgressBarCtrl = hProgressBarCtrl;
|
||||
data.hWnd = hWnd;
|
||||
|
||||
char outFileName[FILENAME_MAX] = "temp.zip";
|
||||
|
||||
curl = curl_easy_init();
|
||||
|
||||
if (curl) {
|
||||
fp = fopen(outFileName,"wb");
|
||||
curl_easy_setopt(curl, CURLOPT_URL, OPENJDK_ZIP_URL);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteData);
|
||||
|
||||
CurlData data;
|
||||
data.fp = fp;
|
||||
data.curl = curl;
|
||||
data.hProgressBarCtrl = hProgressBarCtrl;
|
||||
data.hWnd = hWnd;
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_CAPATH, "./");
|
||||
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
||||
|
||||
SetWindowTextA(hWnd, "OpenJDK downloader: resolving");
|
||||
|
||||
res = curl_easy_perform(curl);
|
||||
|
||||
fflush(fp);
|
||||
fclose(fp);
|
||||
|
||||
if(res == CURLE_OK) {
|
||||
SetWindowTextA(hWnd, "OpenJDK downloader: unpacking...");
|
||||
|
||||
elz::extractZip(std::string(outFileName), "./");
|
||||
|
||||
if (!DeleteFile(outFileName)) {
|
||||
printf("DeleteFile failed.");
|
||||
}
|
||||
if (!MoveFile(OPENJDK_FOLDER, OPENJDK_FOLDER_BASE)) {
|
||||
printf("MoveFile failed.");
|
||||
}
|
||||
SetWindowTextA(hWnd, "OpenJDK downloader: done!");
|
||||
done = true;
|
||||
} else {
|
||||
const char *error = curl_easy_strerror(res);
|
||||
printf("curl error: %s\n", error);
|
||||
SetWindowTextA(hWnd, error);
|
||||
if (!DeleteFile(outFileName)) {
|
||||
printf("DeleteFile failed.");
|
||||
}
|
||||
}
|
||||
|
||||
curl_easy_cleanup(curl);
|
||||
|
||||
printf("Done!\n");
|
||||
}
|
||||
});
|
||||
HANDLE hCurlThread = CreateThread(NULL, 0, CurlDownloadZip, &data, 0, NULL);
|
||||
|
||||
MSG msg;
|
||||
while (::GetMessage(&msg, hWnd, 0, 0) > 0 && !done) {
|
||||
::DispatchMessage(&msg);
|
||||
}
|
||||
|
||||
downloadThread.join();
|
||||
WaitForSingleObject(hCurlThread, INFINITE);
|
||||
|
||||
::UnregisterClass(wcex.lpszClassName, hInstance);
|
||||
|
||||
|
||||
@@ -32,4 +32,4 @@ cp include/elzip/* ../prefix/include/elzip
|
||||
cd ..
|
||||
|
||||
# Downloader
|
||||
${host}-c++-posix OpenJDKDownloader.cpp -o OpenJDKDownloader.exe -std=c++17 -lcomctl32 -lgdi32 -static -Iprefix/include/ -Iprefix/include/elzip -lcurl -lelzip -lminizip -lzlibstatic -lwolfssl -lcrypt32 -Lprefix/lib/ -lws2_32 -lwldap32 -mwindows
|
||||
${host}-c++ OpenJDKDownloader.cpp -o OpenJDKDownloader.exe -std=c++17 -lcomctl32 -lgdi32 -static -Iprefix/include/ -Iprefix/include/elzip -lcurl -lelzip -lminizip -lzlibstatic -lwolfssl -lcrypt32 -Lprefix/lib/ -lws2_32 -lwldap32 -lpthread -mwindows
|
||||
@@ -10,8 +10,8 @@ set(CMAKE_SYSTEM_NAME Windows)
|
||||
set(TOOLCHAIN_PREFIX i686-w64-mingw32)
|
||||
|
||||
# cross compilers to use for C, C++ and Fortran
|
||||
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc-posix)
|
||||
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++-posix)
|
||||
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
|
||||
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
|
||||
set(CMAKE_Fortran_COMPILER ${TOOLCHAIN_PREFIX}-gfortran)
|
||||
set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)
|
||||
|
||||
|
||||
@@ -10,8 +10,8 @@ set(CMAKE_SYSTEM_NAME Windows)
|
||||
set(TOOLCHAIN_PREFIX x86_64-w64-mingw32)
|
||||
|
||||
# cross compilers to use for C, C++ and Fortran
|
||||
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc-posix)
|
||||
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++-posix)
|
||||
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
|
||||
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
|
||||
set(CMAKE_Fortran_COMPILER ${TOOLCHAIN_PREFIX}-gfortran)
|
||||
set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user