Skip to content

Commit

Permalink
Merge branch 'feature/refactor-hmclauncher' into prs
Browse files Browse the repository at this point in the history
  • Loading branch information
burningtnt committed Nov 1, 2024
2 parents be16706 + cee22d6 commit 8c92be4
Show file tree
Hide file tree
Showing 15 changed files with 589 additions and 156 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/build-launcher(cmake-gcc).yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Build HMCLauncher (Using CMake and gcc)

on:
push:
paths:
- 'HMCLauncher/**'
- '.github/workflows/build-launcher(cmake-gcc).yml'
pull_request:
paths:
- 'HMCLauncher/**'
- '.github/workflows/build-launcher(cmake-gcc).yml'

jobs:
build:
runs-on: windows-latest
defaults:
run:
shell: msys2 {0}
steps:
- name: 'Setup MSYS2'
uses: msys2/setup-msys2@v2
with:
msystem: mingw32
update: true
install: >-
mingw-w64-i686-toolchain
mingw-w64-i686-ninja
mingw-w64-i686-cmake
git
- name: 'Checkout'
uses: actions/checkout@v4
- name: Build HMCLauncher
run: |
cmake -S ./HMCLauncher/HMCL -B ./HMCLauncher/HMCL/build -DCMAKE_BUILD_TYPE=Release
cmake --build ./HMCLauncher/HMCL/build
- name: Copy HMCLauncher to assets
run: cp ./HMCLauncher/HMCL/build/HMCLauncher.exe ./HMCL/src/main/resources/assets/HMCLauncher.exe
- name: Set up JDK 11
uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: '11'
java-package: 'jdk+fx'
- name: Build with Gradle
run: ./gradlew makeExecutables --no-daemon
env:
MICROSOFT_AUTH_ID: ${{ secrets.MICROSOFT_AUTH_ID }}
MICROSOFT_AUTH_SECRET: ${{ secrets.MICROSOFT_AUTH_SECRET }}
CURSEFORGE_API_KEY: ${{ secrets.CURSEFORGE_API_KEY }}
- name: Get short SHA
uses: benjlevesque/[email protected]
with:
length: 7
- name: Copy HMCLauncher to libs
run: cp ./HMCLauncher/HMCL/build/HMCLauncher.exe ./HMCL/build/libs/HMCLauncher.exe
- name: Upload Artifacts
uses: actions/upload-artifact@v4
with:
name: HMCLauncher-${{ env.SHA }}
path: HMCL/build/libs/*.exe
Binary file modified HMCL/src/main/resources/assets/HMCLauncher.exe
Binary file not shown.
21 changes: 21 additions & 0 deletions HMCLauncher/HMCL/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 3.25)
project(HMCLauncher)
if(MSVC)
add_compile_options(/utf-8 /D_UNICODE /W4)
else()
add_compile_options(-municode -Wall -Wextra -Wpedantic)
endif()
if(MSVC)
add_link_options(/ENTRY:wWinMainCRTStartup)
else()
add_link_options(-municode)
endif()
OPTION(ENABLE_MINGW_STATIC_LINK_LIBSTDCXX "Link the C++ standard library statically to the executable file(mingw only)." ON)
if(ENABLE_MINGW_STATIC_LINK_LIBSTDCXX AND MINGW)
add_link_options(-static)
endif()
set(CMAKE_WIN32_EXECUTABLE ON)
add_executable(HMCLauncher WIN32 HMCL.rc java.cpp main.cpp os.cpp install.cpp stdafx.cpp Version.cpp)
target_link_libraries(HMCLauncher Version)
target_link_libraries(HMCLauncher wininet)
install(TARGETS HMCLauncher)
6 changes: 4 additions & 2 deletions HMCLauncher/HMCL/HMCL.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>version.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>version.lib;wininet.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
Expand Down Expand Up @@ -134,7 +134,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>version.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>version.lib;wininet.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand All @@ -160,6 +160,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="java.h" />
<ClInclude Include="install.h" />
<ClInclude Include="lang.h" />
<ClInclude Include="main.h" />
<ClInclude Include="os.h" />
Expand All @@ -170,6 +171,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="java.cpp" />
<ClCompile Include="install.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="os.cpp" />
<ClCompile Include="stdafx.cpp">
Expand Down
6 changes: 6 additions & 0 deletions HMCLauncher/HMCL/HMCL.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
<ClInclude Include="java.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="install.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="os.h">
<Filter>头文件</Filter>
</ClInclude>
Expand All @@ -53,6 +56,9 @@
<ClCompile Include="java.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="install.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="os.cpp">
<Filter>源文件</Filter>
</ClCompile>
Expand Down
6 changes: 6 additions & 0 deletions HMCLauncher/HMCL/Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ class Version {
if (ver[i] != other.ver[i]) return ver[i] < other.ver[i];
return true;
}

bool operator>=(const Version &other) const {
for (int i = 0; i < 4; ++i)
if (ver[i] != other.ver[i]) return ver[i] > other.ver[i];
return true;
}
};
189 changes: 189 additions & 0 deletions HMCLauncher/HMCL/install.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
#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 runtimeDirectory, jreDownloadedFile, jreDownloadedDirectory;

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;

int err = 0;

err = DownloadHMCLJRE(jreDownloadedFile, domain, file);
if (err != 0) {
goto cleanup;
}

if (!CreateDirectory(jreDownloadedDirectory.c_str(), NULL)) {
err = GetLastError();
goto cleanup;
}

UncompressHMCLJRE(jreDownloadedFile, jreDownloadedDirectory);

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(), runtimeDirectory.c_str())) {
err = GetLastError();
goto cleanup;
}
}

cleanup:
if (hFind != INVALID_HANDLE_VALUE) {
CloseHandle(hFind);
}
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;
}

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;
}
11 changes: 11 additions & 0 deletions HMCLauncher/HMCL/install.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#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);

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

0 comments on commit 8c92be4

Please sign in to comment.