Fixed library download source

This commit is contained in:
huangyuhui 2017-02-11 23:47:19 +08:00
parent b415a2c2fa
commit bbfd8db190
5 changed files with 66 additions and 35 deletions

View File

@ -34,6 +34,7 @@ public class GameProfile {
public final UUID id;
public final String name;
public final PropertyMap properties = new PropertyMap();
private boolean legacy;
public GameProfile(UUID id, String name) {
if (id == null && StrUtils.isBlank(name))
@ -47,6 +48,10 @@ public class GameProfile {
return id != null && StrUtils.isNotBlank(name);
}
public boolean isLegacy() {
return legacy;
}
@Override
public int hashCode() {
int hash = 7;

View File

@ -28,6 +28,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.jackhuang.hellominecraft.util.ArrayUtils;
import org.jackhuang.hellominecraft.util.C;
import org.jackhuang.hellominecraft.util.log.HMCLog;
import org.jackhuang.hellominecraft.util.net.NetUtils;
@ -43,6 +44,7 @@ public class YggdrasilAuthentication {
protected static final String BASE_URL = "https://authserver.mojang.com/";
protected static final URL ROUTE_AUTHENTICATE = NetUtils.constantURL(BASE_URL + "authenticate");
protected static final URL ROUTE_REFRESH = NetUtils.constantURL(BASE_URL + "refresh");
protected static final URL ROUTE_VALIDATE = NetUtils.constantURL(BASE_URL + "validate");
protected static final String STORAGE_KEY_ACCESS_TOKEN = "accessToken";
protected static final String STORAGE_KEY_PROFILE_NAME = "displayName";
@ -57,6 +59,7 @@ public class YggdrasilAuthentication {
private final PropertyMap userProperties = new PropertyMap();
private String userid, username, password, accessToken;
private UserType userType;
private GameProfile selectedProfile;
private GameProfile[] profiles;
private boolean isOnline;
@ -106,6 +109,10 @@ public class YggdrasilAuthentication {
return this.accessToken;
}
public String getClientToken() {
return clientToken;
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Log In/Out">
public boolean canPlayOnline() {
@ -130,32 +137,28 @@ public class YggdrasilAuthentication {
userid = username;
else
throw new AuthenticationException(C.i18n("login.invalid_uuid_and_username"));
loggedIn(ROUTE_REFRESH, new RefreshRequest(getAuthenticatedToken(), clientToken));
if (checkTokenValidity()) {
isOnline = true;
return;
}
logInImpl(ROUTE_REFRESH, new RefreshRequest(getAuthenticatedToken(), clientToken));
} else if (StrUtils.isNotBlank(password))
loggedIn(ROUTE_AUTHENTICATE, new AuthenticationRequest(username, password, clientToken));
logInImpl(ROUTE_AUTHENTICATE, new AuthenticationRequest(username, password, clientToken));
else
throw new AuthenticationException(C.i18n("login.invalid_password"));
}
private void loggedIn(URL url, Object input) throws AuthenticationException {
try {
String jsonResult = input == null ? NetUtils.get(url, proxy) : NetUtils.post(url, GSON.toJson(input), "application/json", proxy);
Response response = (Response) GSON.fromJson(jsonResult, Response.class);
if (response == null)
throw new AuthenticationException("No valid response here");
if (StrUtils.isNotBlank(response.error)) {
HMCLog.err("Failed to log in, the auth server returned an error: " + response.error + ", message: " + response.errorMessage + ", cause: " + response.cause);
if (response.errorMessage != null && response.errorMessage.contains("Invalid token"))
response.errorMessage = C.i18n("login.invalid_token");
throw new AuthenticationException("Request error: " + response.errorMessage);
}
private void logInImpl(URL url, Object input) throws AuthenticationException {
Response response = makeRequest(url, input, Response.class);
if (!clientToken.equals(response.clientToken))
throw new AuthenticationException(C.i18n("login.changed_client_token"));
if (response.selectedProfile != null)
userType = UserType.byLegacy(response.selectedProfile.isLegacy());
else if (ArrayUtils.isNotEmpty(response.availableProfiles))
userType = UserType.byLegacy(response.availableProfiles[0].isLegacy());
User user = response.user;
userid = user != null && user.getId() != null ? user.getId() : username;
@ -167,11 +170,39 @@ public class YggdrasilAuthentication {
if (user != null && user.getProperties() != null)
userProperties.putAll(user.getProperties());
}
protected <T extends Response> T makeRequest(URL url, Object input, Class<T> clazz)
throws AuthenticationException {
try {
String jsonResult = input == null ? NetUtils.get(url, proxy) : NetUtils.post(url, GSON.toJson(input), "application/json", proxy);
T response = (T) GSON.fromJson(jsonResult, clazz);
if (response == null)
return null;
if (StrUtils.isNotBlank(response.error)) {
HMCLog.err("Failed to log in, the auth server returned an error: " + response.error + ", message: " + response.errorMessage + ", cause: " + response.cause);
if (response.errorMessage != null && response.errorMessage.contains("Invalid token"))
response.errorMessage = C.i18n("login.invalid_token");
throw new AuthenticationException("Request error: " + response.errorMessage);
}
return response;
} catch (IOException | IllegalStateException | JsonParseException e) {
throw new AuthenticationException(C.i18n("login.failed.connect_authentication_server"), e);
}
}
protected boolean checkTokenValidity() {
ValidateRequest request = new ValidateRequest(this);
try {
makeRequest(ROUTE_VALIDATE, request, Response.class);
return true;
} catch (AuthenticationException ex) {
return false;
}
}
public void logOut() {
password = null;
userid = null;

View File

@ -37,7 +37,7 @@ public class LibraryDownloadInfo extends GameDownloadInfo {
public String getUrl(DownloadType dt, boolean allowSelf) {
String myURL = (forgeURL == null ? dt.getProvider().getLibraryDownloadURL() : forgeURL);
if (StrUtils.isNotBlank(url) && allowSelf)
myURL = url;
myURL = dt.getProvider().getParsedDownloadURL(url);
if (!myURL.endsWith(".jar"))
if (path == null)
return null;

View File

@ -81,19 +81,15 @@ public final class Profile {
return launcher;
}
private transient final VersionSetting defaultVersionSetting = new VersionSetting();
public VersionSetting getSelectedVersionSetting() {
VersionSetting vs = getVersionSetting(getSelectedVersion());
if (vs == null)
vs = defaultVersionSetting;
return vs;
return getVersionSetting(getSelectedVersion());
}
public VersionSetting getVersionSetting(String id) {
VersionSetting vs = ((HMCLMinecraftService) service()).getVersionSetting(id);
if (vs == null || vs.isUsesGlobal()) {
global.isGlobal = true;
global.id = id;
return global;
} else
return vs;

View File

@ -90,8 +90,7 @@ public class VersionSetting {
public transient final EventHandler<String> propertyChanged = new EventHandler<>(this);
public VersionSetting() {
fullscreen = false;
usesGlobal = true;
fullscreen = usesGlobal = false;
launcherVisibility = 1;
gameDirType = 0;
javaDir = java = minecraftArgs = serverIp = precalledCommand = wrapper = "";