fix(multiplayer): get ip from hiper log.

This commit is contained in:
huanghongxun 2022-09-12 22:45:38 +08:00
parent f1eb2da57c
commit b69c400a08
2 changed files with 33 additions and 2 deletions

View File

@ -217,6 +217,7 @@ public final class MultiplayerManager {
public static class HiperSession extends ManagedProcess {
private final EventManager<HiperExitEvent> onExit = new EventManager<>();
private final EventManager<HiperIPEvent> onIPAllocated = new EventManager<>();
private final BufferedWriter writer;
HiperSession(Process process, List<String> commands) {
@ -262,6 +263,11 @@ public final class MultiplayerManager {
public EventManager<HiperExitEvent> onExit() {
return onExit;
}
public EventManager<HiperIPEvent> onIPAllocated() {
return onIPAllocated;
}
}
public static class HiperExitEvent extends Event {
@ -282,6 +288,19 @@ public final class MultiplayerManager {
public static final int CERTIFICATE_EXPIRED = 11;
}
public static class HiperIPEvent extends Event {
private final String ip;
public HiperIPEvent(Object source, String ip) {
super(source);
this.ip = ip;
}
public String getIP() {
return ip;
}
}
public static class HiperExitException extends RuntimeException {
private final int exitCode;
private final boolean ready;

View File

@ -39,7 +39,6 @@ import org.jetbrains.annotations.Nullable;
import java.util.concurrent.CancellationException;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.logging.Level;
import static org.jackhuang.hmcl.setting.ConfigHolder.globalConfig;
@ -57,6 +56,7 @@ public class MultiplayerPage extends DecoratorAnimatedPage implements DecoratorP
private final StringProperty address = new SimpleStringProperty();
private Consumer<MultiplayerManager.HiperExitEvent> onExit;
private Consumer<MultiplayerManager.HiperIPEvent> onIPAllocated;
public MultiplayerPage() {
testNAT();
@ -205,7 +205,11 @@ public class MultiplayerPage extends DecoratorAnimatedPage implements DecoratorP
public void start() {
MultiplayerManager.startHiper(globalConfig().getMultiplayerToken())
.thenAcceptAsync(this.session::set, Schedulers.javafx())
.thenAcceptAsync(session -> {
this.session.set(session);
onExit = session.onExit().registerWeak(this::onExit);
onIPAllocated = session.onIPAllocated().registerWeak(this::onIPAllocated);
}, Schedulers.javafx())
.exceptionally(throwable -> {
runInFX(() -> Controllers.dialog(localizeErrorMessage(throwable), null, MessageDialogPane.MessageType.ERROR));
return null;
@ -221,6 +225,14 @@ public class MultiplayerPage extends DecoratorAnimatedPage implements DecoratorP
private void clearSession() {
this.session.set(null);
this.onExit = null;
this.onIPAllocated = null;
}
private void onIPAllocated(MultiplayerManager.HiperIPEvent event) {
runInFX(() -> {
this.address.set(event.getIP());
});
}
private void onExit(MultiplayerManager.HiperExitEvent event) {