Login when launcher opened to reduce launching time

This commit is contained in:
huanghongxun
2018-10-08 01:27:48 +08:00
parent f1d97454c2
commit 8aea89c103
2 changed files with 43 additions and 1 deletions

View File

@@ -30,6 +30,7 @@ import org.jackhuang.hmcl.launch.*;
import org.jackhuang.hmcl.mod.CurseCompletionException; import org.jackhuang.hmcl.mod.CurseCompletionException;
import org.jackhuang.hmcl.mod.CurseCompletionTask; import org.jackhuang.hmcl.mod.CurseCompletionTask;
import org.jackhuang.hmcl.mod.ModpackConfiguration; import org.jackhuang.hmcl.mod.ModpackConfiguration;
import org.jackhuang.hmcl.setting.Accounts;
import org.jackhuang.hmcl.setting.LauncherVisibility; import org.jackhuang.hmcl.setting.LauncherVisibility;
import org.jackhuang.hmcl.setting.Profile; import org.jackhuang.hmcl.setting.Profile;
import org.jackhuang.hmcl.setting.VersionSetting; import org.jackhuang.hmcl.setting.VersionSetting;
@@ -141,7 +142,7 @@ public final class LauncherHelper {
.then(Task.of(Schedulers.javafx(), () -> emitStatus(LoadingState.LOGGING_IN))) .then(Task.of(Schedulers.javafx(), () -> emitStatus(LoadingState.LOGGING_IN)))
.then(Task.of(i18n("account.methods"), variables -> { .then(Task.of(i18n("account.methods"), variables -> {
try { try {
variables.set("account", account.logIn()); variables.set("account", Accounts.logIn(account));
} catch (CredentialExpiredException e) { } catch (CredentialExpiredException e) {
variables.set("account", DialogController.logIn(account)); variables.set("account", DialogController.logIn(account));
} catch (AuthenticationException e) { } catch (AuthenticationException e) {

View File

@@ -26,6 +26,8 @@ import javafx.collections.ObservableList;
import org.jackhuang.hmcl.Launcher; import org.jackhuang.hmcl.Launcher;
import org.jackhuang.hmcl.auth.Account; import org.jackhuang.hmcl.auth.Account;
import org.jackhuang.hmcl.auth.AccountFactory; import org.jackhuang.hmcl.auth.AccountFactory;
import org.jackhuang.hmcl.auth.AuthInfo;
import org.jackhuang.hmcl.auth.AuthenticationException;
import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorAccount; import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorAccount;
import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorAccountFactory; import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorAccountFactory;
import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorDownloader; import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorDownloader;
@@ -35,10 +37,14 @@ import org.jackhuang.hmcl.auth.offline.OfflineAccountFactory;
import org.jackhuang.hmcl.auth.yggdrasil.MojangYggdrasilProvider; import org.jackhuang.hmcl.auth.yggdrasil.MojangYggdrasilProvider;
import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilAccount; import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilAccount;
import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilAccountFactory; import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilAccountFactory;
import org.jackhuang.hmcl.task.Schedulers;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.logging.Level; import java.util.logging.Level;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
@@ -159,6 +165,11 @@ public final class Accounts {
if (initialized) if (initialized)
throw new IllegalStateException("Already initialized"); throw new IllegalStateException("Already initialized");
selectedAccount.addListener((a, b, newValue) -> {
if (newValue != null)
startLoggingIn(newValue);
});
// load accounts // load accounts
config().getAccountStorages().forEach(storage -> { config().getAccountStorages().forEach(storage -> {
AccountFactory<?> factory = type2factory.get(storage.get("type")); AccountFactory<?> factory = type2factory.get(storage.get("type"));
@@ -258,4 +269,34 @@ public final class Accounts {
return getAccountTypeName(getAccountFactory(account)); return getAccountTypeName(getAccountFactory(account));
} }
// ==== // ====
private static final Map<Account, Future<?>> accountFutures = new HashMap<>();
public static void startLoggingIn(Account account) {
if (!accountFutures.containsKey(account)) {
Future<?> future = Schedulers.computation().schedule(account::logIn);
accountFutures.put(account, future);
}
}
public static AuthInfo logIn(Account account) throws AuthenticationException {
try {
if (!accountFutures.containsKey(account)) {
Future<?> future = Schedulers.computation().schedule(account::logIn);
accountFutures.put(account, future);
future.get();
} else {
accountFutures.get(account).get();
}
} catch (ExecutionException e) {
if (e.getCause() instanceof AuthenticationException)
throw (AuthenticationException) e.getCause();
else
LOG.log(Level.WARNING, "Unable to logIn", e);
} catch (InterruptedException e) {
// account.logIn()
}
return account.logIn();
}
} }