Removed static login method
This commit is contained in:
@@ -142,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", Accounts.logIn(account));
|
variables.set("account", account.logIn());
|
||||||
} catch (CredentialExpiredException e) {
|
} catch (CredentialExpiredException e) {
|
||||||
variables.set("account", DialogController.logIn(account));
|
variables.set("account", DialogController.logIn(account));
|
||||||
} catch (AuthenticationException e) {
|
} catch (AuthenticationException e) {
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ 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.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;
|
||||||
@@ -40,11 +39,8 @@ import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilAccountFactory;
|
|||||||
import org.jackhuang.hmcl.task.Schedulers;
|
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;
|
||||||
@@ -165,11 +161,6 @@ 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"));
|
||||||
@@ -192,11 +183,20 @@ public final class Accounts {
|
|||||||
config().getAuthlibInjectorServers().addListener(onInvalidating(Accounts::removeDanglingAuthlibInjectorAccounts));
|
config().getAuthlibInjectorServers().addListener(onInvalidating(Accounts::removeDanglingAuthlibInjectorAccounts));
|
||||||
|
|
||||||
// load selected account
|
// load selected account
|
||||||
selectedAccount.set(
|
Account selected = accounts.stream()
|
||||||
accounts.stream()
|
.filter(it -> accountId(it).equals(config().getSelectedAccount()))
|
||||||
.filter(it -> accountId(it).equals(config().getSelectedAccount()))
|
.findFirst()
|
||||||
.findFirst()
|
.orElse(null);
|
||||||
.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() {
|
public static ObservableList<Account> getAccounts() {
|
||||||
@@ -269,34 +269,4 @@ 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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ public class AuthlibInjectorAccount extends YggdrasilAccount {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AuthInfo logIn() throws AuthenticationException {
|
public synchronized AuthInfo logIn() throws AuthenticationException {
|
||||||
return inject(super::logIn);
|
return inject(super::logIn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ public class YggdrasilAccount extends Account {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AuthInfo logIn() throws AuthenticationException {
|
public synchronized AuthInfo logIn() throws AuthenticationException {
|
||||||
if (!canPlayOnline()) {
|
if (!canPlayOnline()) {
|
||||||
if (service.validate(session.getAccessToken(), session.getClientToken())) {
|
if (service.validate(session.getAccessToken(), session.getClientToken())) {
|
||||||
isOnline = true;
|
isOnline = true;
|
||||||
@@ -84,7 +84,7 @@ public class YggdrasilAccount extends Account {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AuthInfo logInWithPassword(String password) throws AuthenticationException {
|
public synchronized AuthInfo logInWithPassword(String password) throws AuthenticationException {
|
||||||
return logInWithPassword(password, new SpecificCharacterSelector(characterUUID));
|
return logInWithPassword(password, new SpecificCharacterSelector(characterUUID));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ public class CacheRepository {
|
|||||||
|
|
||||||
public void tryCacheFile(Path path, String algorithm, String hash) throws IOException {
|
public void tryCacheFile(Path path, String algorithm, String hash) throws IOException {
|
||||||
Path cache = getFile(algorithm, hash);
|
Path cache = getFile(algorithm, hash);
|
||||||
if (Files.exists(cache)) return;
|
if (Files.isRegularFile(cache)) return;
|
||||||
FileUtils.copyFile(path.toFile(), cache.toFile());
|
FileUtils.copyFile(path.toFile(), cache.toFile());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user