feat(multiplayer): support cato --allow.

This commit is contained in:
huanghongxun
2021-09-19 12:15:45 +08:00
parent 543238be4b
commit 3c797252d8
2 changed files with 6 additions and 5 deletions

View File

@@ -51,7 +51,7 @@ import static org.jackhuang.hmcl.util.Logging.LOG;
*/ */
public final class MultiplayerManager { public final class MultiplayerManager {
private static final String CATO_DOWNLOAD_URL = "https://files.huangyuhui.net/maven/"; private static final String CATO_DOWNLOAD_URL = "https://files.huangyuhui.net/maven/";
private static final String CATO_VERSION = "2021-09-01"; private static final String CATO_VERSION = "2021-09-18";
private static final Artifact CATO_ARTIFACT = new Artifact("cato", "cato", CATO_VERSION, private static final Artifact CATO_ARTIFACT = new Artifact("cato", "cato", CATO_VERSION,
OperatingSystem.CURRENT_OS.getCheckedName() + "-" + Architecture.CURRENT.name().toLowerCase(Locale.ROOT), OperatingSystem.CURRENT_OS.getCheckedName() + "-" + Architecture.CURRENT.name().toLowerCase(Locale.ROOT),
OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS ? "exe" : null); OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS ? "exe" : null);
@@ -91,12 +91,12 @@ public final class MultiplayerManager {
return session; return session;
} }
public static CatoSession createSession(String sessionName) throws IOException { public static CatoSession createSession(String sessionName, int port) throws IOException {
Path exe = getCatoExecutable(); Path exe = getCatoExecutable();
if (!Files.isRegularFile(exe)) { if (!Files.isRegularFile(exe)) {
throw new IllegalStateException("Cato file not found"); throw new IllegalStateException("Cato file not found");
} }
String[] commands = new String[]{exe.toString(), "--token", "new"}; String[] commands = new String[]{exe.toString(), "--token", "new", "--allow", String.format("127.0.0.1:%d", port)};
Process process = new ProcessBuilder() Process process = new ProcessBuilder()
.command(commands) .command(commands)
.start(); .start();

View File

@@ -148,15 +148,16 @@ public class MultiplayerPage extends Control implements DecoratorPage {
} }
Controllers.prompt(new PromptDialogPane.Builder(i18n("multiplayer.session.create"), (result, resolve, reject) -> { Controllers.prompt(new PromptDialogPane.Builder(i18n("multiplayer.session.create"), (result, resolve, reject) -> {
int port = Integer.parseInt(((PromptDialogPane.Builder.StringQuestion) result.get(2)).getValue());
try { try {
initCatoSession(MultiplayerManager.createSession(((PromptDialogPane.Builder.StringQuestion) result.get(1)).getValue())); initCatoSession(MultiplayerManager.createSession(((PromptDialogPane.Builder.StringQuestion) result.get(1)).getValue(), port));
} catch (Exception e) { } catch (Exception e) {
LOG.log(Level.WARNING, "Failed to create session", e); LOG.log(Level.WARNING, "Failed to create session", e);
reject.accept(i18n("multiplayer.session.create.error")); reject.accept(i18n("multiplayer.session.create.error"));
return; return;
} }
port.set(Integer.parseInt(((PromptDialogPane.Builder.StringQuestion) result.get(2)).getValue())); this.port.set(port);
setMultiplayerState(MultiplayerManager.State.MASTER); setMultiplayerState(MultiplayerManager.State.MASTER);
resolve.run(); resolve.run();
}) })