Removed static login method

This commit is contained in:
huanghongxun 2018-10-08 21:23:01 +08:00
parent 0eb81c29df
commit daf659373f
5 changed files with 19 additions and 49 deletions

View File

@ -142,7 +142,7 @@ public final class LauncherHelper {
.then(Task.of(Schedulers.javafx(), () -> emitStatus(LoadingState.LOGGING_IN)))
.then(Task.of(i18n("account.methods"), variables -> {
try {
variables.set("account", Accounts.logIn(account));
variables.set("account", account.logIn());
} catch (CredentialExpiredException e) {
variables.set("account", DialogController.logIn(account));
} catch (AuthenticationException e) {

View File

@ -26,7 +26,6 @@ import javafx.collections.ObservableList;
import org.jackhuang.hmcl.Launcher;
import org.jackhuang.hmcl.auth.Account;
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.AuthlibInjectorAccountFactory;
@ -40,11 +39,8 @@ import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilAccountFactory;
import org.jackhuang.hmcl.task.Schedulers;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.logging.Level;
import static java.util.stream.Collectors.toList;
@ -165,11 +161,6 @@ public final class Accounts {
if (initialized)
throw new IllegalStateException("Already initialized");
selectedAccount.addListener((a, b, newValue) -> {
if (newValue != null)
startLoggingIn(newValue);
});
// load accounts
config().getAccountStorages().forEach(storage -> {
AccountFactory<?> factory = type2factory.get(storage.get("type"));
@ -192,11 +183,20 @@ public final class Accounts {
config().getAuthlibInjectorServers().addListener(onInvalidating(Accounts::removeDanglingAuthlibInjectorAccounts));
// load selected account
selectedAccount.set(
accounts.stream()
.filter(it -> accountId(it).equals(config().getSelectedAccount()))
.findFirst()
.orElse(null));
Account selected = accounts.stream()
.filter(it -> accountId(it).equals(config().getSelectedAccount()))
.findFirst()
.orElse(null);
selectedAccount.set(selected);
Schedulers.io().schedule(() -> {
if (selected != null)
try {
selected.logIn();
} catch (AuthenticationException e) {
LOG.log(Level.WARNING, "Failed to log " + selected + " in", e);
}
});
}
public static ObservableList<Account> getAccounts() {
@ -269,34 +269,4 @@ public final class Accounts {
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();
}
}

View File

@ -47,7 +47,7 @@ public class AuthlibInjectorAccount extends YggdrasilAccount {
}
@Override
public AuthInfo logIn() throws AuthenticationException {
public synchronized AuthInfo logIn() throws AuthenticationException {
return inject(super::logIn);
}

View File

@ -64,7 +64,7 @@ public class YggdrasilAccount extends Account {
}
@Override
public AuthInfo logIn() throws AuthenticationException {
public synchronized AuthInfo logIn() throws AuthenticationException {
if (!canPlayOnline()) {
if (service.validate(session.getAccessToken(), session.getClientToken())) {
isOnline = true;
@ -84,7 +84,7 @@ public class YggdrasilAccount extends Account {
}
@Override
public AuthInfo logInWithPassword(String password) throws AuthenticationException {
public synchronized AuthInfo logInWithPassword(String password) throws AuthenticationException {
return logInWithPassword(password, new SpecificCharacterSelector(characterUUID));
}

View File

@ -62,7 +62,7 @@ public class CacheRepository {
public void tryCacheFile(Path path, String algorithm, String hash) throws IOException {
Path cache = getFile(algorithm, hash);
if (Files.exists(cache)) return;
if (Files.isRegularFile(cache)) return;
FileUtils.copyFile(path.toFile(), cache.toFile());
}