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 607de7bcf..22bb1cfd0 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/setting/Accounts.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/setting/Accounts.java @@ -24,21 +24,17 @@ import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorAccount; import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorAccountFactory; import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorBuildInfo; import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorServer; -import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorServerResponse; import org.jackhuang.hmcl.auth.offline.OfflineAccount; import org.jackhuang.hmcl.auth.offline.OfflineAccountFactory; import org.jackhuang.hmcl.auth.yggdrasil.MojangYggdrasilProvider; import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilAccount; import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilAccountFactory; import org.jackhuang.hmcl.task.FileDownloadTask; -import org.jackhuang.hmcl.util.Constants; import org.jackhuang.hmcl.util.FileUtils; -import org.jackhuang.hmcl.util.NetworkUtils; import java.io.File; import java.io.IOException; import java.net.URL; -import java.util.HashMap; import java.util.Map; import java.util.logging.Level; @@ -62,8 +58,6 @@ public final class Accounts { pair(AUTHLIB_INJECTOR_ACCOUNT_KEY, new AuthlibInjectorAccountFactory(Accounts::downloadAuthlibInjector, Accounts::getOrCreateAuthlibInjectorServer)) ); - private static final Map AUTHLIB_INJECTOR_SERVER_NAMES = new HashMap<>(); - public static String getAccountType(Account account) { if (account instanceof OfflineAccount) return OFFLINE_ACCOUNT_KEY; else if (account instanceof AuthlibInjectorAccount) return AUTHLIB_INJECTOR_ACCOUNT_KEY; @@ -95,31 +89,21 @@ public final class Accounts { return jar.getAbsolutePath(); } - public static String getAuthlibInjectorServerName(String serverIp) throws Exception { - if (AUTHLIB_INJECTOR_SERVER_NAMES.containsKey(serverIp)) - return AUTHLIB_INJECTOR_SERVER_NAMES.get(serverIp); - else { - 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(); - } - } - private static AuthlibInjectorServer getOrCreateAuthlibInjectorServer(String url) { return Settings.SETTINGS.authlibInjectorServers.stream() .filter(server -> url.equals(server.getUrl())) .findFirst() .orElseGet(() -> { // this usually happens when migrating data from an older version - String name; + AuthlibInjectorServer server; try { - name = Accounts.getAuthlibInjectorServerName(url); - LOG.info("Migrated authlib injector server [" + url + "], name=[" + name + "]"); - } catch (Exception e) { - name = url; - LOG.log(Level.WARNING, "Failed to migrate authlib injector server [" + url + "]", e); + server = AuthlibInjectorServer.fetchServerInfo(url); + LOG.info("Migrated authlib injector server " + server); + } catch (IOException e) { + server = new AuthlibInjectorServer(url, url); + LOG.log(Level.WARNING, "Failed to migrate authlib injector server " + url, e); } - AuthlibInjectorServer server = new AuthlibInjectorServer(url, name); + Settings.SETTINGS.authlibInjectorServers.add(server); return server; }); 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 05d2309d4..157c5bda0 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/AuthlibInjectorServersPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/AuthlibInjectorServersPage.java @@ -10,7 +10,6 @@ import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import org.jackhuang.hmcl.Launcher; import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorServer; -import org.jackhuang.hmcl.setting.Accounts; import org.jackhuang.hmcl.setting.Settings; import org.jackhuang.hmcl.task.Schedulers; import org.jackhuang.hmcl.task.Task; @@ -96,7 +95,7 @@ public class AuthlibInjectorServersPage extends StackPane implements DecoratorPa addServerPane.setDisable(true); Task.of(() -> { - serverBeingAdded = new AuthlibInjectorServer(url, Accounts.getAuthlibInjectorServerName(url)); + serverBeingAdded = AuthlibInjectorServer.fetchServerInfo(url); }).finalized(Schedulers.javafx(), (variables, isDependentsSucceeded) -> { progressBar.setVisible(false); addServerPane.setDisable(false); diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorServer.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorServer.java index cbcf50de1..7fc50a786 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorServer.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorServer.java @@ -17,7 +17,35 @@ */ package org.jackhuang.hmcl.auth.authlibinjector; +import static org.jackhuang.hmcl.util.Lang.tryCast; + +import java.io.IOException; +import java.util.Optional; + +import org.jackhuang.hmcl.util.JsonUtils; +import org.jackhuang.hmcl.util.NetworkUtils; + +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSyntaxException; + public class AuthlibInjectorServer { + + public static AuthlibInjectorServer fetchServerInfo(String url) throws IOException { + try { + JsonObject response = JsonUtils.fromNonNullJson(NetworkUtils.doGet(NetworkUtils.toURL(url)), JsonObject.class); + String name = extractServerName(response).orElse(url); + return new AuthlibInjectorServer(url, name); + } catch (JsonSyntaxException e) { + throw new IOException("Malformed response", e); + } + } + + private static Optional extractServerName(JsonObject response){ + return tryCast(response.get("meta"), JsonObject.class) + .flatMap(meta -> tryCast(meta.get("serverName"), JsonPrimitive.class).map(JsonPrimitive::getAsString)); + } + private String url; private String name; @@ -34,6 +62,11 @@ public class AuthlibInjectorServer { return name; } + @Override + public String toString() { + return url + " (" + name + ")"; + } + @Override public int hashCode() { return url.hashCode(); diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorServerResponse.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorServerResponse.java deleted file mode 100644 index 91e5faba0..000000000 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorServerResponse.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Hello Minecraft! Launcher. - * Copyright (C) 2018 huangyuhui - * - * 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 {http://www.gnu.org/licenses/}. - */ -package org.jackhuang.hmcl.auth.authlibinjector; - -public class AuthlibInjectorServerResponse { - - private final Meta meta; - - public AuthlibInjectorServerResponse() { - this(new Meta()); - } - - public AuthlibInjectorServerResponse(Meta meta) { - this.meta = meta; - } - - public Meta getMeta() { - return meta; - } - - public static class Meta { - private final String serverName; - - public Meta() { - this(""); - } - - public Meta(String serverName) { - this.serverName = serverName; - } - - public String getServerName() { - return serverName; - } - } -}