From 7c8ac4fee8386af25b8cfc361fe10c4ce11dc2c9 Mon Sep 17 00:00:00 2001 From: huangyuhui Date: Wed, 28 Feb 2018 20:36:32 +0800 Subject: [PATCH] more precise warning in authlib injector add server page --- .../java/org/jackhuang/hmcl/setting/Accounts.java | 12 ++++-------- .../java/org/jackhuang/hmcl/ui/AccountsPage.java | 12 +++++++++++- .../hmcl/ui/AuthlibInjectorServersPage.java | 14 ++++++++++++-- .../assets/fxml/authlib-injector-servers.fxml | 2 ++ .../src/main/resources/assets/lang/I18N.properties | 1 + .../resources/assets/lang/I18N_zh_CN.properties | 1 + 6 files changed, 31 insertions(+), 11 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/setting/Accounts.java b/HMCL/src/main/java/org/jackhuang/hmcl/setting/Accounts.java index b4e31c246..ca15f364a 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/setting/Accounts.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/setting/Accounts.java @@ -88,17 +88,13 @@ public final class Accounts { return jar.getAbsolutePath(); } - public static String getAuthlibInjectorServerName(String serverIp) { + public static String getAuthlibInjectorServerName(String serverIp) throws Exception { if (AUTHLIB_INJECTOR_SERVER_NAMES.containsKey(serverIp)) return AUTHLIB_INJECTOR_SERVER_NAMES.get(serverIp); else { - try { - AuthlibInjectorServerResponse response = Constants.GSON.fromJson(NetworkUtils.doGet(NetworkUtils.toURL(serverIp)), AuthlibInjectorServerResponse.class); - AUTHLIB_INJECTOR_SERVER_NAMES.put(serverIp, response.getMeta().getServerName()); - return response.getMeta().getServerName(); - } catch (JsonParseException | IOException | NullPointerException e) { - return null; - } + AuthlibInjectorServerResponse response = Constants.GSON.fromJson(NetworkUtils.doGet(NetworkUtils.toURL(serverIp)), AuthlibInjectorServerResponse.class); + AUTHLIB_INJECTOR_SERVER_NAMES.put(serverIp, response.getMeta().getServerName()); + return response.getMeta().getServerName(); } } } diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/AccountsPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/AccountsPage.java index 34c9c31b9..bc5c020fb 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/AccountsPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/AccountsPage.java @@ -49,13 +49,16 @@ import org.jackhuang.hmcl.ui.construct.AdvancedListBox; import org.jackhuang.hmcl.ui.construct.IconedItem; import org.jackhuang.hmcl.ui.construct.Validator; import org.jackhuang.hmcl.ui.wizard.DecoratorPage; +import org.jackhuang.hmcl.util.Logging; import java.util.Collection; import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.concurrent.CountDownLatch; +import java.util.logging.Level; import java.util.stream.Collectors; +import java.util.stream.Stream; public final class AccountsPage extends StackPane implements DecoratorPage { private final StringProperty title = new SimpleStringProperty(this, "title", Main.i18n("account")); @@ -133,7 +136,14 @@ public final class AccountsPage extends StackPane implements DecoratorPage { public void loadServers() { Task.ofResult("list", () -> Settings.INSTANCE.getAuthlibInjectorServerURLs().parallelStream() - .map(serverURL -> new TwoLineListItem(Accounts.getAuthlibInjectorServerName(serverURL), serverURL)) + .flatMap(serverURL -> { + try { + return Stream.of(new TwoLineListItem(Accounts.getAuthlibInjectorServerName(serverURL), serverURL)); + } catch (Exception e) { + Logging.LOG.log(Level.WARNING, "Authlib-injector server root " + serverURL + " cannot be recognized.", e); + return Stream.empty(); + } + }) .collect(Collectors.toList())) .subscribe(Task.of(Schedulers.javafx(), variables -> { cboServers.getItems().setAll(variables.>get("list")); diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/AuthlibInjectorServersPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/AuthlibInjectorServersPage.java index f9e874077..b16eaf2f0 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/AuthlibInjectorServersPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/AuthlibInjectorServersPage.java @@ -18,11 +18,14 @@ import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.ui.animation.ContainerAnimations; import org.jackhuang.hmcl.ui.animation.TransitionHandler; import org.jackhuang.hmcl.ui.wizard.DecoratorPage; +import org.jackhuang.hmcl.util.Logging; import org.jackhuang.hmcl.util.NetworkUtils; import java.util.Collection; import java.util.Objects; +import java.util.logging.Level; import java.util.stream.Collectors; +import java.util.stream.Stream; public class AuthlibInjectorServersPage extends StackPane implements DecoratorPage { private final StringProperty title = new SimpleStringProperty(this, "title", Main.i18n("account.injector.server")); @@ -69,7 +72,14 @@ public class AuthlibInjectorServersPage extends StackPane implements DecoratorPa spinner.setVisible(true); Task.ofResult("list", () -> Settings.INSTANCE.getAuthlibInjectorServerURLs().parallelStream() - .map(serverURL -> new AuthlibInjectorServerItem(new AuthlibInjectorServerInfo(serverURL, Accounts.getAuthlibInjectorServerName(serverURL)), this::removeServer)) + .flatMap(serverURL -> { + try { + return Stream.of(new AuthlibInjectorServerItem(new AuthlibInjectorServerInfo(serverURL, Accounts.getAuthlibInjectorServerName(serverURL)), this::removeServer)); + } catch (Exception e) { + Logging.LOG.log(Level.WARNING, "Authlib-injector server root " + serverURL + " cannot be recognized.", e); + return Stream.empty(); + } + }) .collect(Collectors.toList())) .subscribe(Task.of(Schedulers.javafx(), variables -> { listPane.getChildren().setAll(variables.>get("list")); @@ -105,7 +115,7 @@ public class AuthlibInjectorServersPage extends StackPane implements DecoratorPa progressBar.setVisible(true); addServerPane.setDisable(true); - Task.ofResult("serverName", () -> Objects.requireNonNull(Accounts.getAuthlibInjectorServerName(serverIp))) + Task.ofResult("serverName", () -> Accounts.getAuthlibInjectorServerName(serverIp)) .finalized(Schedulers.javafx(), (variables, isDependentsSucceeded) -> { progressBar.setVisible(false); addServerPane.setDisable(false); diff --git a/HMCL/src/main/resources/assets/fxml/authlib-injector-servers.fxml b/HMCL/src/main/resources/assets/fxml/authlib-injector-servers.fxml index 05ea1066d..c3e330f1b 100644 --- a/HMCL/src/main/resources/assets/fxml/authlib-injector-servers.fxml +++ b/HMCL/src/main/resources/assets/fxml/authlib-injector-servers.fxml @@ -35,6 +35,8 @@ + + diff --git a/HMCL/src/main/resources/assets/lang/I18N.properties b/HMCL/src/main/resources/assets/lang/I18N.properties index 382514688..b0984cbd9 100644 --- a/HMCL/src/main/resources/assets/lang/I18N.properties +++ b/HMCL/src/main/resources/assets/lang/I18N.properties @@ -136,6 +136,7 @@ input.email=The username must be an e-mail. input.number=Must be a number. input.not_empty=Input Requrired! input.url=Must be a valid URL. +input.url.http=Only Http or Https accepted. install=Install New Game install.failed=Failed to install diff --git a/HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties b/HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties index 6c5f09b7a..c98e03852 100644 --- a/HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties +++ b/HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties @@ -136,6 +136,7 @@ input.email=用户名必须是邮箱 input.number=必须是数字 input.not_empty=必填项 input.url=必须是合法的链接 +input.url.http=只支持Http或Https协议 install=添加游戏 install.failed=安装失败