Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
40 changes: 40 additions & 0 deletions HMCL/src/main/java/org/jackhuang/hmcl/ui/main/SettingsPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
*/
package org.jackhuang.hmcl.ui.main;

import com.jfoenix.controls.JFXButton;
import javafx.application.Platform;
import javafx.beans.InvalidationListener;
import javafx.beans.WeakInvalidationListener;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ObjectProperty;
import javafx.scene.control.ButtonBase;
import javafx.scene.control.ToggleGroup;
import org.jackhuang.hmcl.Metadata;
import org.jackhuang.hmcl.setting.Settings;
Expand All @@ -32,9 +34,11 @@
import org.jackhuang.hmcl.upgrade.UpdateChannel;
import org.jackhuang.hmcl.upgrade.UpdateChecker;
import org.jackhuang.hmcl.upgrade.UpdateHandler;
import org.jackhuang.hmcl.util.Restarter;
import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.i18n.Locales;
import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.logging.Level;
import org.tukaani.xz.XZInputStream;

import java.io.File;
Expand Down Expand Up @@ -64,13 +68,49 @@ public final class SettingsPage extends SettingsView {

private InvalidationListener updateListener;

private boolean ignoreLanguageChange = false;

public SettingsPage() {
FXUtils.smoothScrolling(scroll);

// ==== Languages ====
cboLanguage.getItems().setAll(Locales.LOCALES);
selectedItemPropertyFor(cboLanguage).bindBidirectional(config().localizationProperty());

cboLanguage.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if (ignoreLanguageChange) return;

if (oldValue != null && newValue != null && !oldValue.equals(newValue)) {
JFXButton restartButton = new JFXButton(i18n("button.restart"));
restartButton.getStyleClass().add("dialog-success");
restartButton.setOnAction(e -> {
try {
config().setLocalization(newValue);
Restarter.restartWithLocale(newValue);
} catch (IOException ex) {
LOG.log(Level.WARNING, "Failed to restart", ex);
ignoreLanguageChange = true;
cboLanguage.getSelectionModel().select(oldValue);
ignoreLanguageChange = false;
}
});

Runnable cancelAction = () -> {
ignoreLanguageChange = true;
cboLanguage.getSelectionModel().select(newValue);
ignoreLanguageChange = false;
};

Controllers.confirmAction(
i18n("settings.launcher.language.restart_message"),
i18n("settings.launcher.language.restart_title"),
MessageType.INFO,
restartButton,
cancelAction
);
}
});

disableAutoGameOptionsPane.selectedProperty().bindBidirectional(config().disableAutoGameOptionsProperty());
// ====

Expand Down
10 changes: 6 additions & 4 deletions HMCL/src/main/java/org/jackhuang/hmcl/upgrade/UpdateHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.jackhuang.hmcl.ui.Controllers;
import org.jackhuang.hmcl.ui.UpgradeDialog;
import org.jackhuang.hmcl.ui.construct.MessageDialogPane.MessageType;
import org.jackhuang.hmcl.util.Restarter;
import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.SwingUtils;
import org.jackhuang.hmcl.util.TaskCancellationAction;
Expand Down Expand Up @@ -187,10 +188,11 @@ private static void startJava(Path jar, String... appArgs) throws IOException {
commandline.add(jar.toAbsolutePath().toString());
commandline.addAll(Arrays.asList(appArgs));
LOG.info("Starting process: " + commandline);
new ProcessBuilder(commandline)
.directory(Paths.get("").toAbsolutePath().toFile())
.inheritIO()
.start();

Restarter.builder()
.setJarPath(jar)
.addProgramArguments(Arrays.asList(appArgs))
.restart();
}

private static Optional<Path> tryRename(Path path, String newVersion) {
Expand Down
125 changes: 125 additions & 0 deletions HMCL/src/main/java/org/jackhuang/hmcl/util/Restarter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Hello Minecraft! Launcher
* Copyright (C) 2020 huangyuhui <[email protected]> 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 <https://www.gnu.org/licenses/>.
*/
package org.jackhuang.hmcl.util;

import org.jackhuang.hmcl.EntryPoint;
import org.jackhuang.hmcl.java.JavaRuntime;
import org.jackhuang.hmcl.util.i18n.Locales;
import org.jackhuang.hmcl.util.io.JarUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.jackhuang.hmcl.util.i18n.I18n.setLocale;

/**
* 通用重启工具类,用于:
* - 更新后重启
* - 切换界面语言后重启
* - 任何"必须重新启动才能生效"的场景
*/
public final class Restarter {

private final List<String> jvmOptions = new ArrayList<>();
private final List<String> programArgs = new ArrayList<>();
private Path jarPath;

private Restarter() { }

public static Restarter builder() {
return new Restarter();
}

public Restarter addSystemProperty(String key, String value) {
jvmOptions.add("-D" + key + "=" + value);
return this;
}

public Restarter addProgramArguments(List<String> args) {
programArgs.addAll(args);
return this;
}

/**
* 设置要启动的JAR路径
* 如果不设置,则使用当前运行的JAR
*/
public Restarter setJarPath(Path jarPath) {
this.jarPath = jarPath;
return this;
}

/**
* 立即重启
*/
public void restart() throws IOException {
Path jar = this.jarPath;
if (jar == null) {
jar = JarUtils.thisJarPath();
}
if (jar == null) {
throw new IOException("Cannot locate JAR file");
}

List<String> command = new ArrayList<>();
command.add(JavaRuntime.getDefault().getBinary().toString());

for (Map.Entry<Object, Object> e : System.getProperties().entrySet()) {
Object k = e.getKey();
if (k instanceof String && ((String) k).startsWith("hmcl.")) {
command.add("-D" + k + "=" + e.getValue());
}
}

// 额外JVM选项
command.addAll(jvmOptions);

command.add("-jar");
command.add(jar.toAbsolutePath().toString());

// 程序参数
command.addAll(programArgs);

new ProcessBuilder(command)
.directory(new File(System.getProperty("user.dir")))
.inheritIO()
.start();

EntryPoint.exit(0);
}

/**
* 立刻用完全相同的环境重启
*/
public static void restartWithSameArgs() throws IOException {
builder().restart();
}

/**
* 切换界面语言后重启
*/
public static void restartWithLocale(Locales.SupportedLocale locale) throws IOException {
setLocale(locale);
builder().restart();
}

}
3 changes: 3 additions & 0 deletions HMCL/src/main/resources/assets/lang/I18N.properties
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ button.save_as=Save As
button.select_all=Select All
button.view=View
button.yes=Yes
button.restart=Restart

chat=Join Group Chat

Expand Down Expand Up @@ -1353,6 +1354,8 @@ settings.launcher.font.anti_aliasing.gray=Grayscale
settings.launcher.font.anti_aliasing.lcd=Sub-pixel
settings.launcher.general=General
settings.launcher.language=Language (Applies After Restart)
settings.launcher.language.restart_title=We Need Restart
settings.launcher.language.restart_message=Changing the language requires restarting the launcher to take effect. Restart now?
settings.launcher.launcher_log.export=Export Launcher Logs
settings.launcher.launcher_log.export.failed=Failed to export logs.
settings.launcher.launcher_log.export.success=Logs have been exported to "%s".
Expand Down
3 changes: 3 additions & 0 deletions HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ button.save_as=另存为
button.select_all=全选
button.view=查看
button.yes=是
button.restart=立即重启

chat=官方群组

Expand Down Expand Up @@ -1153,6 +1154,8 @@ settings.launcher.font.anti_aliasing.gray=灰度
settings.launcher.font.anti_aliasing.lcd=子像素
settings.launcher.general=通用
settings.launcher.language=语言 (重启后生效)
settings.launcher.language.restart_title=重启提示
settings.launcher.language.restart_message=切换语言需要重启启动器才能生效。是否立即重启?
settings.launcher.launcher_log.export=导出启动器日志
settings.launcher.launcher_log.export.failed=无法导出日志
settings.launcher.launcher_log.export.success=日志已保存到“%s”
Expand Down