From 6ffbc5515f0e5085dd759d5ed77f8867224fa295 Mon Sep 17 00:00:00 2001 From: Gravita <12893402+gravit0@users.noreply.github.com> Date: Mon, 7 Aug 2023 20:58:35 +0700 Subject: [PATCH] [FEATURE] Client debug --- .../pro/gravit/launcher/LauncherEngine.java | 15 ++- .../launcher/debug/ClientRuntimeProvider.java | 116 ++++++++++++++++++ .../pro/gravit/launcher/debug/DebugMain.java | 2 +- .../pro/gravit/launcher/request/Request.java | 13 +- 4 files changed, 137 insertions(+), 9 deletions(-) create mode 100644 Launcher/src/main/java/pro/gravit/launcher/debug/ClientRuntimeProvider.java diff --git a/Launcher/src/main/java/pro/gravit/launcher/LauncherEngine.java b/Launcher/src/main/java/pro/gravit/launcher/LauncherEngine.java index ab0955ac..f1653539 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/LauncherEngine.java +++ b/Launcher/src/main/java/pro/gravit/launcher/LauncherEngine.java @@ -53,10 +53,11 @@ public class LauncherEngine { public RuntimeProvider runtimeProvider; public ECPublicKey publicKey; public ECPrivateKey privateKey; + public Class basicRuntimeProvider; - private LauncherEngine(boolean clientInstance) { - + private LauncherEngine(boolean clientInstance, Class basicRuntimeProvider) { this.clientInstance = clientInstance; + this.basicRuntimeProvider = basicRuntimeProvider; } //JVMHelper.getCertificates @@ -124,7 +125,7 @@ public static void main(String... args) throws Throwable { Launcher.getConfig(); // init config long startTime = System.currentTimeMillis(); try { - new LauncherEngine(false).start(args); + newInstance(false).start(args); } catch (Exception e) { LogHelper.error(e); return; @@ -157,7 +158,11 @@ public static RequestService initOffline() { } public static LauncherEngine newInstance(boolean clientInstance) { - return new LauncherEngine(clientInstance); + return new LauncherEngine(clientInstance, NoRuntimeProvider.class); + } + + public static LauncherEngine newInstance(boolean clientInstance, Class basicRuntimeProvider) { + return new LauncherEngine(clientInstance, basicRuntimeProvider); } public ECPublicKey getClientPublicKey() { @@ -195,7 +200,7 @@ public void start(String... args) throws Throwable { ClientPreGuiPhase event = new ClientPreGuiPhase(null); LauncherEngine.modulesManager.invokeEvent(event); runtimeProvider = event.runtimeProvider; - if (runtimeProvider == null) runtimeProvider = new NoRuntimeProvider(); + if (runtimeProvider == null) runtimeProvider = basicRuntimeProvider.getConstructor().newInstance(); runtimeProvider.init(clientInstance); //runtimeProvider.preLoad(); if (!Request.isAvailable()) { diff --git a/Launcher/src/main/java/pro/gravit/launcher/debug/ClientRuntimeProvider.java b/Launcher/src/main/java/pro/gravit/launcher/debug/ClientRuntimeProvider.java new file mode 100644 index 00000000..df849c32 --- /dev/null +++ b/Launcher/src/main/java/pro/gravit/launcher/debug/ClientRuntimeProvider.java @@ -0,0 +1,116 @@ +package pro.gravit.launcher.debug; + +import pro.gravit.launcher.ClientPermissions; +import pro.gravit.launcher.Launcher; +import pro.gravit.launcher.LauncherEngine; +import pro.gravit.launcher.api.AuthService; +import pro.gravit.launcher.api.ClientService; +import pro.gravit.launcher.client.ClientParams; +import pro.gravit.launcher.events.request.AuthRequestEvent; +import pro.gravit.launcher.events.request.ProfilesRequestEvent; +import pro.gravit.launcher.gui.RuntimeProvider; +import pro.gravit.launcher.profiles.ClientProfile; +import pro.gravit.launcher.request.Request; +import pro.gravit.launcher.request.auth.AuthRequest; +import pro.gravit.launcher.request.auth.password.AuthPlainPassword; +import pro.gravit.launcher.request.update.ProfilesRequest; +import pro.gravit.utils.helper.LogHelper; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.UUID; + +public class ClientRuntimeProvider implements RuntimeProvider { + @Override + public void run(String[] args) { + ArrayList newArgs = new ArrayList<>(Arrays.asList(args)); + try { + String username = System.getProperty("launcher.runtime.username", null); + String uuid = System.getProperty("launcher.runtime.uuid", null); + String login = System.getProperty("launcher.runtime.login", username); + String password = System.getProperty("launcher.runtime.password", "Player"); + String authId = System.getProperty("launcher.runtime.auth.authid", "std"); + String accessToken = System.getProperty("launcher.runtime.auth.accesstoken", null); + String refreshToken = System.getProperty("launcher.runtime.auth.refreshtoken", null); + String minecraftAccessToken = System.getProperty("launcher.runtime.auth.minecraftaccesstoken", "DEBUG"); + long expire = Long.parseLong(System.getProperty("launcher.runtime.auth.expire", "0")); + String profileUUID = System.getProperty("launcher.runtime.profileuuid", null); + String mainClass = System.getProperty("launcher.runtime.mainclass", null); + ClientPermissions permissions = new ClientPermissions(); + if(mainClass == null) { + throw new NullPointerException("Add `-Dlauncher.runtime.mainclass=YOUR_MAIN_CLASS` to jvmArgs"); + } + if(accessToken != null) { + Request.setOAuth(authId, new AuthRequestEvent.OAuthRequestEvent(accessToken, refreshToken, expire)); + Request.RequestRestoreReport report = Request.restore(true); + permissions = report.userInfo.permissions; + username = report.userInfo.playerProfile.username; + uuid = report.userInfo.playerProfile.uuid.toString(); + if(report.userInfo.accessToken != null) { + minecraftAccessToken = report.userInfo.accessToken; + } + } else if(password != null) { + AuthRequest request = new AuthRequest(login, password, authId, AuthRequest.ConnectTypes.API); + AuthRequestEvent event = request.request(); + Request.setOAuth(authId, event.oauth); + if(event.accessToken != null) { + minecraftAccessToken = event.accessToken; + } + username = event.playerProfile.username; + uuid = event.playerProfile.uuid.toString(); + } + if(profileUUID != null) { + UUID profileUuid = UUID.fromString(profileUUID); + ProfilesRequest profiles = new ProfilesRequest(); + ProfilesRequestEvent event = profiles.request(); + for(ClientProfile profile : event.profiles) { + if(profile.getUUID().equals(profileUuid)) { + AuthService.profile = profile; + } + } + } + if(username == null) { + username = "Player"; + } + if(uuid == null) { + uuid = "a7899336-e61c-4e51-b480-0c815b18aed8"; + } + replaceOrCreateArgument(newArgs, "--username", username); + replaceOrCreateArgument(newArgs, "--uuid", uuid); + replaceOrCreateArgument(newArgs, "--accessToken", minecraftAccessToken); + AuthService.uuid = UUID.fromString(uuid); + AuthService.username = username; + AuthService.permissions = permissions; + Class mainClazz = Class.forName(mainClass); + mainClazz.getMethod("main", String[].class).invoke(null, (Object) newArgs.toArray(new String[0])); + } catch (Throwable e) { + LogHelper.error(e); + LauncherEngine.exitLauncher(-15); + } + } + + public void replaceOrCreateArgument(ArrayList args, String name, String value) { + int index = args.indexOf(name); + if(index < 0) { + args.add(name); + if(value != null) { + args.add(value); + } + } + if(value != null) { + int valueIndex = index+1; + args.set(valueIndex, value); + } + } + + @Override + public void preLoad() { + + } + + @Override + public void init(boolean clientInstance) { + + } +} diff --git a/Launcher/src/main/java/pro/gravit/launcher/debug/DebugMain.java b/Launcher/src/main/java/pro/gravit/launcher/debug/DebugMain.java index ab345cd1..191ad440 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/debug/DebugMain.java +++ b/Launcher/src/main/java/pro/gravit/launcher/debug/DebugMain.java @@ -69,7 +69,7 @@ public static void main(String[] args) throws Throwable { } Request.setRequestService(service); LogHelper.debug("Initialization LauncherEngine"); - LauncherEngine instance = LauncherEngine.newInstance(false); + LauncherEngine instance = LauncherEngine.newInstance(false, ClientRuntimeProvider.class); instance.start(args); } diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/Request.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/Request.java index 0c6f1cc5..cb4d40ba 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/Request.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/Request.java @@ -2,6 +2,7 @@ import pro.gravit.launcher.LauncherNetworkAPI; import pro.gravit.launcher.events.request.AuthRequestEvent; +import pro.gravit.launcher.events.request.CurrentUserRequestEvent; import pro.gravit.launcher.events.request.RefreshTokenRequestEvent; import pro.gravit.launcher.events.request.RestoreRequestEvent; import pro.gravit.launcher.request.auth.RefreshTokenRequest; @@ -122,6 +123,10 @@ public static void reconnect() throws Exception { } public static RequestRestoreReport restore() throws Exception { + return restore(false); + } + + public static RequestRestoreReport restore(boolean needUserInfo) throws Exception { boolean refreshed = false; RestoreRequest request; if (oauth != null) { @@ -131,7 +136,7 @@ public static RequestRestoreReport restore() throws Exception { setOAuth(authId, event.oauth); refreshed = true; } - request = new RestoreRequest(authId, oauth.accessToken, extendedTokens, false); + request = new RestoreRequest(authId, oauth.accessToken, extendedTokens, needUserInfo); } else { request = new RestoreRequest(authId, null, extendedTokens, false); } @@ -159,7 +164,7 @@ public static RequestRestoreReport restore() throws Exception { } invalidTokens = event.invalidTokens; } - return new RequestRestoreReport(false, refreshed, invalidTokens); + return new RequestRestoreReport(false, refreshed, invalidTokens, event.userInfo); } public static void requestError(String message) throws RequestException { @@ -216,11 +221,13 @@ public static class RequestRestoreReport { public final boolean legacySession; public final boolean refreshed; public final List invalidExtendedTokens; + public final CurrentUserRequestEvent.UserInfo userInfo; - public RequestRestoreReport(boolean legacySession, boolean refreshed, List invalidExtendedTokens) { + public RequestRestoreReport(boolean legacySession, boolean refreshed, List invalidExtendedTokens, CurrentUserRequestEvent.UserInfo userInfo) { this.legacySession = legacySession; this.refreshed = refreshed; this.invalidExtendedTokens = invalidExtendedTokens; + this.userInfo = userInfo; } }