diff --git a/HMCLBoot/src/main/java/org/jackhuang/hmcl/DesktopUtils.java b/HMCLBoot/src/main/java/org/jackhuang/hmcl/DesktopUtils.java new file mode 100644 index 000000000..1440c882b --- /dev/null +++ b/HMCLBoot/src/main/java/org/jackhuang/hmcl/DesktopUtils.java @@ -0,0 +1,99 @@ +/* + * Hello Minecraft! Launcher + * Copyright (C) 2025 huangyuhui and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.jackhuang.hmcl; + +import java.io.File; +import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Locale; + +/// @author Glavo +public final class DesktopUtils { + private static final String[] linuxBrowsers = { + "xdg-open", + "google-chrome", + "firefox", + "microsoft-edge", + "opera", + "konqueror", + "mozilla" + }; + + public static Path which(String command) { + String path = System.getenv("PATH"); + if (path == null) + return null; + + try { + for (String item : path.split(File.pathSeparator)) { + try { + Path program = Paths.get(item, command); + if (Files.isExecutable(program)) + return program.toRealPath(); + } catch (Throwable ignored) { + } + } + } catch (Throwable ignored) { + } + + return null; + } + + public static void openLink(String link) { + String osName = System.getProperty("os.name").toLowerCase(Locale.ROOT); + + Throwable e1 = null; + try { + if (osName.startsWith("windows")) { + Runtime.getRuntime().exec(new String[]{"rundll32.exe", "url.dll,FileProtocolHandler", link}); + return; + } else if (osName.startsWith("mac") || osName.startsWith("darwin")) { + Runtime.getRuntime().exec(new String[]{"open", link}); + return; + } else { + for (String browser : linuxBrowsers) { + Path path = which(browser); + if (path != null) { + try { + Runtime.getRuntime().exec(new String[]{path.toString(), link}); + return; + } catch (Throwable ignored) { + } + } + } + System.err.println("No known browser found"); + } + } catch (Throwable e) { + e1 = e; + } + + try { + java.awt.Desktop.getDesktop().browse(new URI(link)); + } catch (Throwable e2) { + if (e1 != null) + e2.addSuppressed(e1); + + e2.printStackTrace(System.err); + } + } + + private DesktopUtils() { + } +} diff --git a/HMCLBoot/src/main/java/org/jackhuang/hmcl/Main.java b/HMCLBoot/src/main/java/org/jackhuang/hmcl/Main.java index 22e313379..74eefc86b 100644 --- a/HMCLBoot/src/main/java/org/jackhuang/hmcl/Main.java +++ b/HMCLBoot/src/main/java/org/jackhuang/hmcl/Main.java @@ -19,11 +19,15 @@ package org.jackhuang.hmcl; import org.jackhuang.hmcl.util.SwingUtils; +import javax.swing.*; +import java.util.ResourceBundle; + /** * @author Glavo */ public final class Main { private static final int MINIMUM_JAVA_VERSION = 17; + private static final String DOWNLOAD_PAGE = "https://hmcl.huangyuhui.net/download/"; private Main() { } @@ -73,14 +77,36 @@ public final class Main { } } + static void showErrorAndExit(String[] args) { + SwingUtils.initLookAndFeel(); + + ResourceBundle resourceBundle = BootProperties.getResourceBundle(); + String errorTitle = resourceBundle.getString("boot.message.error"); + + if (args.length > 0 && args[0].equals("--apply-to")) { + String errorMessage = resourceBundle.getString("boot.manual_update"); + System.err.println(errorMessage); + int result = JOptionPane.showOptionDialog(null, errorMessage, errorTitle, JOptionPane.YES_NO_OPTION, + JOptionPane.ERROR_MESSAGE, null, null, null); + + if (result == JOptionPane.YES_OPTION) { + System.out.println("Open " + DOWNLOAD_PAGE); + DesktopUtils.openLink(DOWNLOAD_PAGE); + } + } else { + String errorMessage = resourceBundle.getString("boot.unsupported_java_version"); + System.err.println(errorMessage); + SwingUtils.showErrorDialog(errorMessage, errorTitle); + } + + System.exit(1); + } + public static void main(String[] args) throws Throwable { if (getJavaFeatureVersion(System.getProperty("java.version")) >= MINIMUM_JAVA_VERSION) { EntryPoint.main(args); } else { - String errorMessage = BootProperties.getResourceBundle().getString("boot.unsupported_java_version"); - System.err.println(errorMessage); - SwingUtils.showErrorDialog(errorMessage); - System.exit(1); + showErrorAndExit(args); } } } diff --git a/HMCLBoot/src/main/java/org/jackhuang/hmcl/util/SwingUtils.java b/HMCLBoot/src/main/java/org/jackhuang/hmcl/util/SwingUtils.java index 9cddeb30c..225577503 100644 --- a/HMCLBoot/src/main/java/org/jackhuang/hmcl/util/SwingUtils.java +++ b/HMCLBoot/src/main/java/org/jackhuang/hmcl/util/SwingUtils.java @@ -40,14 +40,26 @@ public final class SwingUtils { } public static void showInfoDialog(Object message) { - JOptionPane.showMessageDialog(null, message, "Info", JOptionPane.INFORMATION_MESSAGE); + showInfoDialog(message, "Info"); + } + + public static void showInfoDialog(Object message, String title) { + JOptionPane.showMessageDialog(null, message, title, JOptionPane.INFORMATION_MESSAGE); } public static void showWarningDialog(Object message) { - JOptionPane.showMessageDialog(null, message, "Warning", JOptionPane.WARNING_MESSAGE); + showWarningDialog(message, "Warning"); + } + + public static void showWarningDialog(Object message, String title) { + JOptionPane.showMessageDialog(null, message, title, JOptionPane.WARNING_MESSAGE); } public static void showErrorDialog(Object message) { - JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE); + showErrorDialog(message, "Error"); + } + + public static void showErrorDialog(Object message, String title) { + JOptionPane.showMessageDialog(null, message, title, JOptionPane.ERROR_MESSAGE); } } diff --git a/HMCLBoot/src/main/resources/assets/lang/boot.properties b/HMCLBoot/src/main/resources/assets/lang/boot.properties index effe9bbc3..c6b43070e 100644 --- a/HMCLBoot/src/main/resources/assets/lang/boot.properties +++ b/HMCLBoot/src/main/resources/assets/lang/boot.properties @@ -17,3 +17,6 @@ # boot.unsupported_java_version=HMCL requires Java 17 or later to run, but still supports launching games with Java 6~16. Please install the latest version of Java and try opening HMCL again.\nYou can keep your old version of Java. HMCL can detect and manage multiple Java installations, and will automatically select the appropriate Java version for your game. +boot.manual_update=HMCL cannot complete automatic updates in the current environment. Please download the latest version of HMCL manually.\nWould you like to go to the download page? + +boot.message.error=Error \ No newline at end of file diff --git a/HMCLBoot/src/main/resources/assets/lang/boot_zh.properties b/HMCLBoot/src/main/resources/assets/lang/boot_zh.properties index a65e7488d..6ac05b2c2 100644 --- a/HMCLBoot/src/main/resources/assets/lang/boot_zh.properties +++ b/HMCLBoot/src/main/resources/assets/lang/boot_zh.properties @@ -17,3 +17,6 @@ # boot.unsupported_java_version=HMCL 需要 Java 17 或更高版本才能运行,但依然支持使用 Java 6~16 启动游戏。请安装最新版本的 Java 再尝试启动 HMCL。\n你可以继续保留旧版本 Java。HMCL 能够识别与管理多个 Java,并会自动根据游戏版本为你选择合适的 Java。\n你可以访问 https://docs.hmcl.net/help.html 页面寻求帮助。 +boot.manual_update=HMCL 在当前环境无法完成自动更新,请手动下载最新版本的 HMCL。\n是否前往下载页面? + +boot.message.error=错误 \ No newline at end of file diff --git a/HMCLBoot/src/main/resources/assets/lang/boot_zh_Hant.properties b/HMCLBoot/src/main/resources/assets/lang/boot_zh_Hant.properties index b67fce5d7..78c25c414 100644 --- a/HMCLBoot/src/main/resources/assets/lang/boot_zh_Hant.properties +++ b/HMCLBoot/src/main/resources/assets/lang/boot_zh_Hant.properties @@ -17,3 +17,6 @@ # boot.unsupported_java_version=HMCL 需要 Java 17 或更高版本才能執行,但依然支援使用 Java 6~16 啟動遊戲。請安裝最新版本的 Java 再嘗試開啟 HMCL。\n你可以繼續保留舊版本 Java。HMCL 能夠識別與管理多個 Java,並會自動根據遊戲版本為你選取合適的 Java。 +boot.manual_update=HMCL 在當前環境無法完成自動更新,請手動下載最新版本的 HMCL。\n是否前往下載頁面? + +boot.message.error=錯誤