Support -Dauthlibinjector.side=client argument

See yushijinhun/authlib-injector#16
This commit is contained in:
yushijinhun
2018-10-06 01:13:54 +08:00
parent 2a42ecda78
commit c3dcb0292c

View File

@@ -20,21 +20,20 @@ package org.jackhuang.hmcl.auth.authlibinjector;
import org.jackhuang.hmcl.auth.AuthInfo; import org.jackhuang.hmcl.auth.AuthInfo;
import org.jackhuang.hmcl.auth.AuthenticationException; import org.jackhuang.hmcl.auth.AuthenticationException;
import org.jackhuang.hmcl.auth.CharacterSelector; import org.jackhuang.hmcl.auth.CharacterSelector;
import org.jackhuang.hmcl.auth.ServerDisconnectException;
import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilAccount; import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilAccount;
import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilService; import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilService;
import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilSession; import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilSession;
import org.jackhuang.hmcl.game.Arguments; import org.jackhuang.hmcl.game.Arguments;
import org.jackhuang.hmcl.task.GetTask;
import org.jackhuang.hmcl.util.Lang;
import org.jackhuang.hmcl.util.function.ExceptionalSupplier; import org.jackhuang.hmcl.util.function.ExceptionalSupplier;
import org.jackhuang.hmcl.util.io.NetworkUtils;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.*; import java.util.*;
import java.util.logging.Level; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import static java.nio.charset.StandardCharsets.UTF_8; import java.util.concurrent.ExecutionException;
import static org.jackhuang.hmcl.util.Logging.LOG; import static org.jackhuang.hmcl.util.io.IOUtils.readFullyWithoutClosing;
public class AuthlibInjectorAccount extends YggdrasilAccount { public class AuthlibInjectorAccount extends YggdrasilAccount {
private AuthlibInjectorServer server; private AuthlibInjectorServer server;
@@ -58,41 +57,44 @@ public class AuthlibInjectorAccount extends YggdrasilAccount {
} }
private AuthInfo inject(ExceptionalSupplier<AuthInfo, AuthenticationException> loginAction) throws AuthenticationException { private AuthInfo inject(ExceptionalSupplier<AuthInfo, AuthenticationException> loginAction) throws AuthenticationException {
// Pre-fetch metadata CompletableFuture<byte[]> prefetchedMetaTask = CompletableFuture.supplyAsync(() -> {
GetTask metadataFetchTask = new GetTask(NetworkUtils.toURL(server.getUrl())); try (InputStream in = new URL(server.getUrl()).openStream()) {
Thread metadataFetchThread = Lang.thread(() -> { return readFullyWithoutClosing(in);
try { } catch (IOException e) {
metadataFetchTask.run(); throw new CompletionException(new ServerDisconnectException(e));
} catch (Exception e) {
LOG.log(Level.WARNING, "Failed to pre-fetch Yggdrasil metadata", e);
} }
}, "Yggdrasil metadata fetch thread"); });
// Update authlib-injector CompletableFuture<AuthlibInjectorArtifactInfo> artifactTask = CompletableFuture.supplyAsync(() -> {
try {
return authlibInjectorDownloader.get();
} catch (IOException e) {
throw new CompletionException(new AuthenticationException("Failed to download authlib-injector", e));
}
});
AuthInfo auth = loginAction.get();
byte[] prefetchedMeta;
AuthlibInjectorArtifactInfo artifact; AuthlibInjectorArtifactInfo artifact;
try {
artifact = authlibInjectorDownloader.get();
} catch (IOException e) {
throw new AuthenticationException("Failed to download authlib-injector", e);
}
// Perform authentication
AuthInfo info = loginAction.get();
Arguments arguments = new Arguments().addJVMArguments("-javaagent:" + artifact.getLocation().toString() + "=" + server.getUrl());
// Wait for metadata to be fetched
try { try {
metadataFetchThread.join(); prefetchedMeta = prefetchedMetaTask.get();
artifact = artifactTask.get();
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} throw new AuthenticationException(e);
Optional<String> metadata = Optional.ofNullable(metadataFetchTask.getResult()); } catch (ExecutionException e) {
if (metadata.isPresent()) { if (e.getCause() instanceof AuthenticationException) {
arguments = arguments.addJVMArguments( throw (AuthenticationException) e.getCause();
"-Dorg.to2mbn.authlibinjector.config.prefetched=" + Base64.getEncoder().encodeToString(metadata.get().getBytes(UTF_8))); } else {
throw new AuthenticationException(e.getCause());
}
} }
return info.withArguments(arguments); return auth.withArguments(new Arguments().addJVMArguments(
"-javaagent:" + artifact.getLocation().toString() + "=" + server.getUrl(),
"-Dauthlibinjector.side=client",
"-Dorg.to2mbn.authlibinjector.config.prefetched=" + Base64.getEncoder().encodeToString(prefetchedMeta)));
} }
@Override @Override