Skip to content

Commit

Permalink
Fix HMCL-dev#2470 Enhance the behavior of authlib-injector.json (HMCL…
Browse files Browse the repository at this point in the history
…-dev#2471)

* Fix HMCL-dev#2470

* Revert "Fix HMCL-dev#2470"

This reverts commit b0f3910.

* Refactor Behavior

* Simpify AuthlibInjectorServers.

* Fix merge

* Fix checkstyle.

* Recover the validation of AuthlibInjectorServers.
  • Loading branch information
burningtnt authored Jan 8, 2024
1 parent 4e22585 commit fbc6677
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,70 +22,76 @@
import org.jackhuang.hmcl.task.Schedulers;
import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.gson.TolerableValidationException;
import org.jackhuang.hmcl.util.gson.Validation;
import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.io.JarUtils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.logging.Level;

import static org.jackhuang.hmcl.setting.ConfigHolder.config;
import static org.jackhuang.hmcl.util.Logging.LOG;

public class AuthlibInjectorServers implements Validation {
public final class AuthlibInjectorServers implements Validation {

public static final String CONFIG_FILENAME = "authlib-injectors.json";

private final List<String> urls;
private static final Set<AuthlibInjectorServer> servers = new CopyOnWriteArraySet<>();

public AuthlibInjectorServers(List<String> urls) {
this.urls = urls;
public static Set<AuthlibInjectorServer> getServers() {
return servers;
}

public List<String> getUrls() {
return urls;
private final List<String> urls;

private AuthlibInjectorServers(List<String> urls) {
this.urls = urls;
}

@Override
public void validate() throws JsonParseException {
if (urls == null)
throw new JsonParseException("authlib-injectors.json -> urls cannot be null");
public void validate() throws JsonParseException, TolerableValidationException {
if (this.urls == null) {
throw new JsonParseException("authlib-injectors.json -> urls cannot be null.");
}
}

private static final Path configLocation = Paths.get(CONFIG_FILENAME);
private static AuthlibInjectorServers configInstance;

public synchronized static void init() {
if (configInstance != null) {
throw new IllegalStateException("AuthlibInjectorServers is already loaded");
public static void init() {
Path configLocation;
Path jarPath = JarUtils.thisJarPath();
if (jarPath != null && Files.isRegularFile(jarPath) && Files.isWritable(jarPath)) {
configLocation = jarPath.getParent().resolve(CONFIG_FILENAME);
} else {
configLocation = Paths.get(CONFIG_FILENAME);
}

configInstance = new AuthlibInjectorServers(Collections.emptyList());

if (Files.exists(configLocation)) {
if (ConfigHolder.isNewlyCreated() && Files.exists(configLocation)) {
AuthlibInjectorServers configInstance;
try {
String content = FileUtils.readText(configLocation);
configInstance = JsonUtils.GSON.fromJson(content, AuthlibInjectorServers.class);
} catch (IOException | JsonParseException e) {
LOG.log(Level.WARNING, "Malformed authlib-injectors.json", e);
return;
}
}

if (ConfigHolder.isNewlyCreated() && !AuthlibInjectorServers.getConfigInstance().getUrls().isEmpty()) {
config().setPreferredLoginType(Accounts.getLoginType(Accounts.FACTORY_AUTHLIB_INJECTOR));
for (String url : AuthlibInjectorServers.getConfigInstance().getUrls()) {
Task.supplyAsync(Schedulers.io(), () -> AuthlibInjectorServer.locateServer(url))
.thenAcceptAsync(Schedulers.javafx(), server -> config().getAuthlibInjectorServers().add(server))
.start();
if (!configInstance.urls.isEmpty()) {
config().setPreferredLoginType(Accounts.getLoginType(Accounts.FACTORY_AUTHLIB_INJECTOR));
for (String url : configInstance.urls) {
Task.supplyAsync(Schedulers.io(), () -> AuthlibInjectorServer.locateServer(url))
.thenAcceptAsync(Schedulers.javafx(), server -> {
config().getAuthlibInjectorServers().add(server);
servers.add(server);
})
.start();
}
}
}
}

public static AuthlibInjectorServers getConfigInstance() {
return configInstance;
}
}
19 changes: 15 additions & 4 deletions HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/Versions.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
import javafx.application.Platform;
import javafx.stage.FileChooser;
import org.jackhuang.hmcl.auth.Account;
import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorAccount;
import org.jackhuang.hmcl.download.game.GameAssetDownloadTask;
import org.jackhuang.hmcl.game.GameDirectoryType;
import org.jackhuang.hmcl.game.GameRepository;
import org.jackhuang.hmcl.game.LauncherHelper;
import org.jackhuang.hmcl.mod.RemoteMod;
import org.jackhuang.hmcl.setting.Accounts;
import org.jackhuang.hmcl.setting.Profile;
import org.jackhuang.hmcl.setting.Profiles;
import org.jackhuang.hmcl.setting.*;
import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.task.Schedulers;
import org.jackhuang.hmcl.task.Task;
Expand Down Expand Up @@ -244,7 +243,19 @@ private static boolean checkVersionForLaunching(Profile profile, String id) {

private static void ensureSelectedAccount(Consumer<Account> action) {
Account account = Accounts.getSelectedAccount();
if (account == null) {
if (ConfigHolder.isNewlyCreated() && !AuthlibInjectorServers.getServers().isEmpty() &&
!(account instanceof AuthlibInjectorAccount && AuthlibInjectorServers.getServers().contains(((AuthlibInjectorAccount) account).getServer()))) {
CreateAccountPane dialog = new CreateAccountPane(AuthlibInjectorServers.getServers().iterator().next());
dialog.addEventHandler(DialogCloseEvent.CLOSE, e -> {
Account newAccount = Accounts.getSelectedAccount();
if (newAccount == null) {
// user cancelled operation
} else {
Platform.runLater(() -> action.accept(newAccount));
}
});
Controllers.dialog(dialog);
} else if (account == null) {
CreateAccountPane dialog = new CreateAccountPane();
dialog.addEventHandler(DialogCloseEvent.CLOSE, e -> {
Account newAccount = Accounts.getSelectedAccount();
Expand Down

0 comments on commit fbc6677

Please sign in to comment.