YggdrasilAccount记录角色uuid而不是名称

注:旧版本无法读取此版本配置(移除了character)。
This commit is contained in:
yushijinhun
2018-06-08 18:27:07 +08:00
parent 4f51e377ae
commit 631337c451
5 changed files with 25 additions and 24 deletions

View File

@@ -20,23 +20,24 @@ package org.jackhuang.hmcl.auth;
import org.jackhuang.hmcl.auth.yggdrasil.GameProfile; import org.jackhuang.hmcl.auth.yggdrasil.GameProfile;
import java.util.List; import java.util.List;
import java.util.UUID;
/** /**
* Select character by name. * Select character by name.
*/ */
public class SpecificCharacterSelector implements CharacterSelector { public class SpecificCharacterSelector implements CharacterSelector {
private final String id; private UUID uuid;
/** /**
* Constructor. * Constructor.
* @param id character's name. * @param uuid character's uuid.
*/ */
public SpecificCharacterSelector(String id) { public SpecificCharacterSelector(UUID uuid) {
this.id = id; this.uuid = uuid;
} }
@Override @Override
public GameProfile select(Account account, List<GameProfile> names) throws NoSelectedCharacterException { public GameProfile select(Account account, List<GameProfile> names) throws NoSelectedCharacterException {
return names.stream().filter(profile -> profile.getName().equals(id)).findAny().orElseThrow(() -> new NoSelectedCharacterException(account)); return names.stream().filter(profile -> profile.getId().equals(uuid)).findAny().orElseThrow(() -> new NoSelectedCharacterException(account));
} }
} }

View File

@@ -31,6 +31,7 @@ import org.jackhuang.hmcl.util.NetworkUtils;
import java.util.Base64; import java.util.Base64;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
@@ -39,8 +40,8 @@ public class AuthlibInjectorAccount extends YggdrasilAccount {
private final String serverBaseURL; private final String serverBaseURL;
private final ExceptionalSupplier<String, ?> injectorJarPath; private final ExceptionalSupplier<String, ?> injectorJarPath;
protected AuthlibInjectorAccount(YggdrasilService service, String serverBaseURL, ExceptionalSupplier<String, ?> injectorJarPath, String username, String character, YggdrasilSession session) { protected AuthlibInjectorAccount(YggdrasilService service, String serverBaseURL, ExceptionalSupplier<String, ?> injectorJarPath, String username, UUID characterUUID, YggdrasilSession session) {
super(service, username, character, session); super(service, username, characterUUID, session);
this.injectorJarPath = injectorJarPath; this.injectorJarPath = injectorJarPath;
this.serverBaseURL = serverBaseURL; this.serverBaseURL = serverBaseURL;

View File

@@ -42,14 +42,14 @@ public class AuthlibInjectorAccountFactory extends AccountFactory<AuthlibInjecto
Objects.requireNonNull(storage); Objects.requireNonNull(storage);
Objects.requireNonNull(proxy); Objects.requireNonNull(proxy);
YggdrasilSession session = YggdrasilSession.fromStorage(storage);
String username = tryCast(storage.get("username"), String.class) String username = tryCast(storage.get("username"), String.class)
.orElseThrow(() -> new IllegalArgumentException("storage does not have username")); .orElseThrow(() -> new IllegalArgumentException("storage does not have username"));
String character = tryCast(storage.get("character"), String.class)
.orElseThrow(() -> new IllegalArgumentException("storage does not have selected character name."));
String apiRoot = tryCast(storage.get("serverBaseURL"), String.class) String apiRoot = tryCast(storage.get("serverBaseURL"), String.class)
.orElseThrow(() -> new IllegalArgumentException("storage does not have API root.")); .orElseThrow(() -> new IllegalArgumentException("storage does not have API root."));
return new AuthlibInjectorAccount(new YggdrasilService(new AuthlibInjectorProvider(apiRoot), proxy), return new AuthlibInjectorAccount(new YggdrasilService(new AuthlibInjectorProvider(apiRoot), proxy),
apiRoot, injectorJarPathSupplier, username, character, YggdrasilSession.fromStorage(storage)); apiRoot, injectorJarPathSupplier, username, session.getSelectedProfile().getId(), session);
} }
} }

View File

@@ -33,13 +33,13 @@ public class YggdrasilAccount extends Account {
private final YggdrasilService service; private final YggdrasilService service;
private boolean isOnline = false; private boolean isOnline = false;
private YggdrasilSession session; private YggdrasilSession session;
private String character; private UUID characterUUID;
protected YggdrasilAccount(YggdrasilService service, String username, String character, YggdrasilSession session) { protected YggdrasilAccount(YggdrasilService service, String username, UUID characterUUID, YggdrasilSession session) {
this.service = service; this.service = service;
this.username = username; this.username = username;
this.session = session; this.session = session;
this.character = character; this.characterUUID = characterUUID;
if (session == null || session.getSelectedProfile() == null || StringUtils.isBlank(session.getAccessToken())) if (session == null || session.getSelectedProfile() == null || StringUtils.isBlank(session.getAccessToken()))
this.session = null; this.session = null;
@@ -67,14 +67,14 @@ public class YggdrasilAccount extends Account {
public AuthInfo logIn() throws AuthenticationException { public AuthInfo logIn() throws AuthenticationException {
if (!canPlayOnline()) { if (!canPlayOnline()) {
logInWithToken(); logInWithToken();
selectProfile(new SpecificCharacterSelector(character)); selectProfile(new SpecificCharacterSelector(characterUUID));
} }
return session.toAuthInfo(); return session.toAuthInfo();
} }
@Override @Override
public final AuthInfo logInWithPassword(String password) throws AuthenticationException { public final AuthInfo logInWithPassword(String password) throws AuthenticationException {
return logInWithPassword(password, new SpecificCharacterSelector(character)); return logInWithPassword(password, new SpecificCharacterSelector(characterUUID));
} }
protected AuthInfo logInWithPassword(String password, CharacterSelector selector) throws AuthenticationException { protected AuthInfo logInWithPassword(String password, CharacterSelector selector) throws AuthenticationException {
@@ -91,7 +91,7 @@ public class YggdrasilAccount extends Account {
session = service.refresh(session.getAccessToken(), session.getClientToken(), selector.select(this, Arrays.asList(session.getAvailableProfiles()))); session = service.refresh(session.getAccessToken(), session.getClientToken(), selector.select(this, Arrays.asList(session.getAvailableProfiles())));
} }
character = session.getSelectedProfile().getName(); characterUUID = session.getSelectedProfile().getId();
} }
private void logInWithToken() throws AuthenticationException { private void logInWithToken() throws AuthenticationException {
@@ -112,13 +112,12 @@ public class YggdrasilAccount extends Account {
@Override @Override
public Map<Object, Object> toStorage() { public Map<Object, Object> toStorage() {
if (session == null)
throw new IllegalStateException("No session is specified");
HashMap<Object, Object> storage = new HashMap<>(); HashMap<Object, Object> storage = new HashMap<>();
storage.put("username", getUsername()); storage.put("username", getUsername());
storage.put("character", character); storage.putAll(session.toStorage());
if (session != null)
storage.putAll(session.toStorage());
return storage; return storage;
} }

View File

@@ -58,12 +58,12 @@ public class YggdrasilAccountFactory extends AccountFactory<YggdrasilAccount> {
Objects.requireNonNull(storage); Objects.requireNonNull(storage);
Objects.requireNonNull(proxy); Objects.requireNonNull(proxy);
YggdrasilSession session = YggdrasilSession.fromStorage(storage);
String username = tryCast(storage.get("username"), String.class) String username = tryCast(storage.get("username"), String.class)
.orElseThrow(() -> new IllegalArgumentException("storage does not have username")); .orElseThrow(() -> new IllegalArgumentException("storage does not have username"));
String character = tryCast(storage.get("character"), String.class)
.orElseThrow(() -> new IllegalArgumentException("storage does not have selected character name."));
return new YggdrasilAccount(new YggdrasilService(provider, proxy), username, character, YggdrasilSession.fromStorage(storage)); return new YggdrasilAccount(new YggdrasilService(provider, proxy), username, session.getSelectedProfile().getId(), session);
} }
public static String randomToken() { public static String randomToken() {