Yggdrasil account offline logging in

This commit is contained in:
huangyuhui
2018-02-14 14:51:42 +08:00
parent 8d1e335abf
commit c9e7bf7ca5
4 changed files with 37 additions and 4 deletions

View File

@@ -20,10 +20,7 @@ package org.jackhuang.hmcl.game;
import com.jfoenix.concurrency.JFXUtilities;
import javafx.application.Platform;
import org.jackhuang.hmcl.Main;
import org.jackhuang.hmcl.auth.Account;
import org.jackhuang.hmcl.auth.AuthInfo;
import org.jackhuang.hmcl.auth.AuthenticationException;
import org.jackhuang.hmcl.auth.SpecificCharacterSelector;
import org.jackhuang.hmcl.auth.*;
import org.jackhuang.hmcl.download.DefaultDependencyManager;
import org.jackhuang.hmcl.launch.*;
import org.jackhuang.hmcl.mod.CurseCompletionTask;
@@ -91,6 +88,11 @@ public final class LauncherHelper {
.then(Task.of(Main.i18n("account.methods"), variables -> {
try {
variables.set("account", account.logIn(new SpecificCharacterSelector(Accounts.getCurrentCharacter(account)), Settings.INSTANCE.getProxy()));
} catch (ServerDisconnectException e) {
if (account.canPlayOffline())
variables.set("account", account.playOffline());
else
throw e;
} catch (AuthenticationException e) {
variables.set("account", DialogController.logIn(account));
JFXUtilities.runInFX(() -> Controllers.dialog(launchingStepsPane));

View File

@@ -57,6 +57,14 @@ public abstract class Account {
*/
public abstract AuthInfo logIn(MultiCharacterSelector selector, Proxy proxy) throws AuthenticationException;
public abstract boolean canPlayOffline();
/**
* Play offline.
* @return the specific offline player's info.
*/
public abstract AuthInfo playOffline();
public abstract void logOut();
protected abstract Map<Object, Object> toStorageImpl();

View File

@@ -69,6 +69,16 @@ public class OfflineAccount extends Account {
// Offline account need not log out.
}
@Override
public boolean canPlayOffline() {
return false;
}
@Override
public AuthInfo playOffline() {
throw new IllegalStateException();
}
@Override
public Map<Object, Object> toStorageImpl() {
return Lang.mapOf(

View File

@@ -171,6 +171,19 @@ public final class YggdrasilAccount extends Account {
userProperties.putAll(user.getProperties());
}
@Override
public boolean canPlayOffline() {
return isLoggedIn() && getSelectedProfile() != null && !canPlayOnline();
}
@Override
public AuthInfo playOffline() {
if (!canPlayOffline())
throw new IllegalStateException("Current account " + this + " cannot play offline.");
return new AuthInfo(selectedProfile, accessToken, userType, GSON.toJson(userProperties));
}
@Override
public void logOut() {
password = null;