Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

重构 HMCLauncher #3007

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
51b4108
Feature: Support scanning Java from HMCL_JAVA_HOME, JAVA_HOME, PATH a…
burningtnt Apr 26, 2024
7595604
Fix: I forget to remove /* and */. Refactor java.h.
burningtnt Apr 26, 2024
bd04465
Remove useless function.
burningtnt Apr 26, 2024
24b9aae
尝试性的为HMCLauncher添加cmake及gcc支持
YSXX1013 Apr 27, 2024
b63bde4
完善cmake及gcc支持
YSXX1013 Apr 27, 2024
853876c
尝试支持Github Actions
YSXX1013 Apr 27, 2024
185c674
尝试支持Github Actions-2
YSXX1013 Apr 27, 2024
88870c2
尝试支持Github Actions-3
YSXX1013 Apr 27, 2024
e0e30ff
尝试支持Github Actions-4
YSXX1013 Apr 27, 2024
abadd2a
尝试支持Github Actions-5
YSXX1013 Apr 27, 2024
eb95ce4
Merge branch 'HMCL-dev:main' into main
YSXX1013 May 8, 2024
5fdd6f2
Merge branch 'HMCL-dev:main' into main
YSXX1013 May 16, 2024
c8fa232
Merge branch 'HMCL-dev:main' into main
YSXX1013 Jun 3, 2024
89d2bf0
Merge branch 'refs/heads/YSXX1013/main' into feature/refactor-hmclaun…
burningtnt Jun 15, 2024
17d684e
Rrovide a built HMCLauncher.exe
burningtnt Jun 15, 2024
0df1f1d
Feature: Using java from .hmcl/runtime/bin/javaw.exe
burningtnt Jul 5, 2024
46f1984
Feature: Download JRE and detect broken JVM.
burningtnt Jul 22, 2024
8b1c3f5
Fix: msbuild compiling.
burningtnt Jul 22, 2024
9e722ce
Update install.cpp
burningtnt Jul 24, 2024
b7c7ceb
Remove useless functions and Recompile HMCLauncher.
burningtnt Jul 28, 2024
ce87c91
Merge remote-tracking branch 'refs/remotes/official/main' into featur…
burningtnt Nov 1, 2024
cee22d6
Recompile HMCLauncher
burningtnt Nov 1, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/short-sha@v3.0
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;
}
};
190 changes: 190 additions & 0 deletions HMCLauncher/HMCL/install.cpp
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;
}
9 changes: 9 additions & 0 deletions HMCLauncher/HMCL/install.h
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);
Loading