From c3fccc141f96154232a229d818b8e5a91d697345 Mon Sep 17 00:00:00 2001 From: Gravita Date: Mon, 22 Nov 2021 15:22:19 +0700 Subject: [PATCH] [FEATURE] ParamsWriter --- .../client/ClientLauncherEntryPoint.java | 21 ++-------- .../launcher/client/params/ParamsWriter.java | 10 +++++ .../client/params/SocketParamsWriter.java | 40 +++++++++++++++++++ 3 files changed, 54 insertions(+), 17 deletions(-) create mode 100644 Launcher/src/main/java/pro/gravit/launcher/client/params/ParamsWriter.java create mode 100644 Launcher/src/main/java/pro/gravit/launcher/client/params/SocketParamsWriter.java diff --git a/Launcher/src/main/java/pro/gravit/launcher/client/ClientLauncherEntryPoint.java b/Launcher/src/main/java/pro/gravit/launcher/client/ClientLauncherEntryPoint.java index 255c0ef4..d69a94f4 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/client/ClientLauncherEntryPoint.java +++ b/Launcher/src/main/java/pro/gravit/launcher/client/ClientLauncherEntryPoint.java @@ -4,6 +4,8 @@ import pro.gravit.launcher.api.AuthService; import pro.gravit.launcher.api.ClientService; import pro.gravit.launcher.client.events.client.*; +import pro.gravit.launcher.client.params.ParamsWriter; +import pro.gravit.launcher.client.params.SocketParamsWriter; import pro.gravit.launcher.events.request.ProfileByUUIDRequestEvent; import pro.gravit.launcher.events.request.ProfileByUsernameRequestEvent; import pro.gravit.launcher.guard.LauncherGuardManager; @@ -55,22 +57,6 @@ public class ClientLauncherEntryPoint { private static ClassLoader classLoader; - private static ClientLauncherProcess.ClientParams readParams(SocketAddress address) throws IOException { - try (Socket socket = IOHelper.newSocket()) { - socket.connect(address); - try (HInput input = new HInput(socket.getInputStream())) { - byte[] serialized = input.readByteArray(0); - ClientLauncherProcess.ClientParams params = Launcher.gsonManager.gson.fromJson(IOHelper.decode(serialized), ClientLauncherProcess.ClientParams.class); - params.clientHDir = new HashedDir(input); - params.assetHDir = new HashedDir(input); - boolean isNeedReadJavaDir = input.readBoolean(); - if (isNeedReadJavaDir) - params.javaHDir = new HashedDir(input); - return params; - } - } - } - public static void main(String[] args) throws Throwable { LauncherEngine engine = LauncherEngine.clientInstance(); JVMHelper.verifySystemProperties(ClientLauncherEntryPoint.class, true); @@ -90,7 +76,8 @@ public static void main(String[] args) throws Throwable { engine.readKeys(); LauncherGuardManager.initGuard(true); LogHelper.debug("Reading ClientLauncher params"); - ClientLauncherProcess.ClientParams params = readParams(new InetSocketAddress("127.0.0.1", Launcher.getConfig().clientPort)); + ParamsWriter paramsWriter = new SocketParamsWriter(new InetSocketAddress("127.0.0.1", Launcher.getConfig().clientPort)); + ClientLauncherProcess.ClientParams params = paramsWriter.read(); if (params.profile.getClassLoaderConfig() != ClientProfile.ClassLoaderConfig.AGENT) { LauncherEngine.verifyNoAgent(); } diff --git a/Launcher/src/main/java/pro/gravit/launcher/client/params/ParamsWriter.java b/Launcher/src/main/java/pro/gravit/launcher/client/params/ParamsWriter.java new file mode 100644 index 00000000..62a55a4e --- /dev/null +++ b/Launcher/src/main/java/pro/gravit/launcher/client/params/ParamsWriter.java @@ -0,0 +1,10 @@ +package pro.gravit.launcher.client.params; + +import pro.gravit.launcher.client.ClientLauncherProcess; + +import java.io.IOException; + +public interface ParamsWriter { + void write(ClientLauncherProcess.ClientParams params) throws IOException; + ClientLauncherProcess.ClientParams read() throws IOException; +} diff --git a/Launcher/src/main/java/pro/gravit/launcher/client/params/SocketParamsWriter.java b/Launcher/src/main/java/pro/gravit/launcher/client/params/SocketParamsWriter.java new file mode 100644 index 00000000..fc12c3e1 --- /dev/null +++ b/Launcher/src/main/java/pro/gravit/launcher/client/params/SocketParamsWriter.java @@ -0,0 +1,40 @@ +package pro.gravit.launcher.client.params; + +import pro.gravit.launcher.client.ClientLauncherProcess; +import pro.gravit.utils.helper.IOHelper; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketAddress; + +public class SocketParamsWriter implements ParamsWriter { + private final SocketAddress address; + + public SocketParamsWriter(SocketAddress address) { + this.address = address; + } + + @Override + public void write(ClientLauncherProcess.ClientParams params) throws IOException { + try(ServerSocket socket = new ServerSocket()) { + socket.bind(address); + Socket client = socket.accept(); + try(DataOutputStream stream = new DataOutputStream(client.getOutputStream())) { + params.write(stream); + } + } + } + + @Override + public ClientLauncherProcess.ClientParams read() throws IOException { + try(Socket socket = IOHelper.newSocket()) { + socket.connect(address, 30*1000); + try(DataInputStream stream = new DataInputStream(socket.getInputStream())) { + return ClientLauncherProcess.ClientParams.read(stream); + } + } + } +}