From f1559183a121593f79803651eeae31138b9b6320 Mon Sep 17 00:00:00 2001 From: Gravita <12893402+gravit0@users.noreply.github.com> Date: Fri, 8 Nov 2024 15:33:53 +0700 Subject: [PATCH] [FIX} New API bug fixes --- .../runtime/backend/BackendSettings.java | 20 +++++++++++++ .../runtime/backend/ClientDownloadImpl.java | 14 ++++++--- .../runtime/backend/LauncherBackendImpl.java | 15 +++++++++- .../runtime/backend/ProfileSettingsImpl.java | 30 ++++++++++++++++++- .../runtime/backend/ReadyProfileImpl.java | 11 +++++-- .../runtime/client/ClientLauncherProcess.java | 13 ++++++-- .../base/events/request/AuthRequestEvent.java | 6 ++++ .../base/request/RequestFeatureAPIImpl.java | 8 +++++ .../core/api/features/ProfileFeatureAPI.java | 1 + .../core/backend/LauncherBackendAPI.java | 1 + build.gradle | 2 +- 11 files changed, 109 insertions(+), 12 deletions(-) diff --git a/Launcher/src/main/java/pro/gravit/launcher/runtime/backend/BackendSettings.java b/Launcher/src/main/java/pro/gravit/launcher/runtime/backend/BackendSettings.java index 0ec6872e..c42a2837 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/runtime/backend/BackendSettings.java +++ b/Launcher/src/main/java/pro/gravit/launcher/runtime/backend/BackendSettings.java @@ -1,6 +1,7 @@ package pro.gravit.launcher.runtime.backend; import pro.gravit.launcher.core.LauncherNetworkAPI; +import pro.gravit.launcher.core.api.features.AuthFeatureAPI; import pro.gravit.launcher.core.backend.UserSettings; import java.util.HashMap; @@ -19,5 +20,24 @@ public static class AuthorizationData { public String refreshToken; @LauncherNetworkAPI public long expireIn; + + public AuthFeatureAPI.AuthToken toToken() { + return new AuthFeatureAPI.AuthToken() { + @Override + public String getAccessToken() { + return accessToken; + } + + @Override + public String getRefreshToken() { + return refreshToken; + } + + @Override + public long getExpire() { + return expireIn; + } + }; + } } } diff --git a/Launcher/src/main/java/pro/gravit/launcher/runtime/backend/ClientDownloadImpl.java b/Launcher/src/main/java/pro/gravit/launcher/runtime/backend/ClientDownloadImpl.java index 70601963..c9eb88ea 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/runtime/backend/ClientDownloadImpl.java +++ b/Launcher/src/main/java/pro/gravit/launcher/runtime/backend/ClientDownloadImpl.java @@ -37,7 +37,8 @@ CompletableFuture downloadProfile(ClientProfile AtomicReference clientRef = new AtomicReference<>(); AtomicReference assetRef = new AtomicReference<>(); AtomicReference javaRef = new AtomicReference<>(); - return downloadDir(profile.getDir(), profile.getClientUpdateMatcher(), settings.view, callback).thenCompose((clientDir -> { + return LauncherAPIHolder.profile().changeCurrentProfile(profile) + .thenCompose(vv -> downloadDir(profile.getDir(), profile.getClientUpdateMatcher(), settings.view, callback)).thenCompose((clientDir -> { clientRef.set(clientDir); return downloadAsset(profile.getAssetDir(), profile.getAssetUpdateMatcher(), profile.getAssetIndex(), callback); })).thenCompose(assetDir -> { @@ -53,7 +54,7 @@ CompletableFuture downloadProfile(ClientProfile CompletableFuture downloadAsset(String dirName, FileNameMatcher matcher, String assetIndexString, LauncherBackendAPI.DownloadCallback callback) { Path targetDir = DirBridge.dirUpdates.resolve(dirName); - Path assetIndexPath = targetDir.resolve("indexes").resolve(assetIndexString); + Path assetIndexPath = targetDir.resolve("indexes").resolve(assetIndexString+".json"); return LauncherAPIHolder.profile().fetchUpdateInfo(dirName).thenComposeAsync((response) -> { callback.onStage(LauncherBackendAPI.DownloadCallback.STAGE_ASSET_VERIFY); return verifyAssetIndex(assetIndexString, response, assetIndexPath, targetDir); @@ -116,7 +117,7 @@ CompletableFuture downloadDir(Path targetDir, ProfileFeatureAPI.U callback.onStage(LauncherBackendAPI.DownloadCallback.STAGE_HASHING); HashedDir realFiles = new HashedDir(targetDir, matcher, false, true); callback.onStage(LauncherBackendAPI.DownloadCallback.STAGE_DIFF); - return realFiles.diff(updateInfo.getHashedDir(), matcher); + return updateInfo.getHashedDir().diff(realFiles, matcher); } catch (IOException e) { throw new RuntimeException(e); } @@ -165,7 +166,12 @@ private List collectFilesAndCreateDirectories(Path dir, Ha List files = new ArrayList<>(); mismatch.walk(File.separator, (path, name, entry) -> { if(entry.getType() == HashedEntry.Type.DIR) { - Files.createDirectory(dir.resolve(path)); + var dirPath = dir.resolve(path); + if(!Files.exists(dirPath)) { + Files.createDirectory(dirPath); + } else if (!Files.isDirectory(dirPath)) { + throw new IOException(String.format("%s is not a directory", path)); + } return HashedDir.WalkAction.CONTINUE; } String pathFixed = path.replace(File.separatorChar, '/'); diff --git a/Launcher/src/main/java/pro/gravit/launcher/runtime/backend/LauncherBackendImpl.java b/Launcher/src/main/java/pro/gravit/launcher/runtime/backend/LauncherBackendImpl.java index 6b9557d1..a8191c9b 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/runtime/backend/LauncherBackendImpl.java +++ b/Launcher/src/main/java/pro/gravit/launcher/runtime/backend/LauncherBackendImpl.java @@ -16,6 +16,7 @@ import pro.gravit.launcher.core.backend.exceptions.LauncherBackendException; import pro.gravit.launcher.core.backend.extensions.Extension; import pro.gravit.launcher.runtime.NewLauncherSettings; +import pro.gravit.launcher.runtime.client.DirBridge; import pro.gravit.launcher.runtime.client.ServerPinger; import pro.gravit.launcher.runtime.debug.DebugMain; import pro.gravit.launcher.runtime.managers.SettingsManager; @@ -73,6 +74,7 @@ private void doInit() throws Exception { allSettings = settingsManager.getConfig(); backendSettings = (BackendSettings) getUserSettings("backend", (k) -> new BackendSettings()); permissions = new ClientPermissions(); + DirBridge.dirUpdates = DirBridge.defaultUpdatesDir; } @Override @@ -102,6 +104,14 @@ public CompletableFuture init() { }, executorService); } + public AuthFeatureAPI.AuthToken getAuthToken() { + return backendSettings.auth.toToken(); + } + + public AuthMethod getAuthMethod() { + return authMethod; + } + @Override public void selectAuthMethod(AuthMethod method) { this.authMethod = method; @@ -175,9 +185,11 @@ public ClientProfileSettings makeClientProfileSettings(ProfileFeatureAPI.ClientP var settings = backendSettings.settings.get(profile.getUUID()); if(settings == null) { settings = new ProfileSettingsImpl((ClientProfile) profile); + settings.backend = this; + settings.updateEnabledMods(); } else { settings = settings.copy(); - settings.initAfterGson((ClientProfile) profile, this); + //settings.initAfterGson((ClientProfile) profile, this); } return settings; } @@ -211,6 +223,7 @@ public CompletableFuture> getAvailableJava() { return e; }); } + return availableJavasFuture; } return CompletableFuture.completedFuture(availableJavas); } diff --git a/Launcher/src/main/java/pro/gravit/launcher/runtime/backend/ProfileSettingsImpl.java b/Launcher/src/main/java/pro/gravit/launcher/runtime/backend/ProfileSettingsImpl.java index 7e919d98..cd175ec4 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/runtime/backend/ProfileSettingsImpl.java +++ b/Launcher/src/main/java/pro/gravit/launcher/runtime/backend/ProfileSettingsImpl.java @@ -12,6 +12,7 @@ import pro.gravit.utils.helper.JavaHelper; import java.util.*; +import java.util.concurrent.ExecutionException; public class ProfileSettingsImpl implements LauncherBackendAPI.ClientProfileSettings { transient ClientProfile profile; @@ -120,6 +121,30 @@ public JavaHelper.JavaVersion getSelectedJava() { return selectedJava; } + @Override + public JavaHelper.JavaVersion getRecommendedJava() { + JavaHelper.JavaVersion result = null; + try { + for(var java : backend.getAvailableJava().get()) { + if(isRecommended(java)) { + return (JavaHelper.JavaVersion) java; + } + if(isCompatible(java)) { + if(result == null) { + result = (JavaHelper.JavaVersion) java; + continue; + } + if(result.getMajorVersion() < java.getMajorVersion()) { + result = (JavaHelper.JavaVersion) java; + } + } + } + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + return result; + } + @Override public void setSelectedJava(LauncherBackendAPI.Java java) { selectedJava = (JavaHelper.JavaVersion) java; @@ -138,6 +163,7 @@ public boolean isCompatible(LauncherBackendAPI.Java java) { @Override public ProfileSettingsImpl copy() { ProfileSettingsImpl cloned = new ProfileSettingsImpl(); + cloned.backend = backend; cloned.profile = profile; cloned.ram = new HashMap<>(ram); cloned.flags = new HashSet<>(flags); @@ -155,7 +181,9 @@ public void updateEnabledMods() { for(var e : view.enabled) { enabled.add(e.name); } - saveJavaPath = selectedJava.getPath().toAbsolutePath().toString(); + if(selectedJava != null) { + saveJavaPath = selectedJava.getPath().toAbsolutePath().toString(); + } } public void initAfterGson(ClientProfile profile, LauncherBackendImpl backend) { diff --git a/Launcher/src/main/java/pro/gravit/launcher/runtime/backend/ReadyProfileImpl.java b/Launcher/src/main/java/pro/gravit/launcher/runtime/backend/ReadyProfileImpl.java index 7649f40b..c3092161 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/runtime/backend/ReadyProfileImpl.java +++ b/Launcher/src/main/java/pro/gravit/launcher/runtime/backend/ReadyProfileImpl.java @@ -1,9 +1,11 @@ package pro.gravit.launcher.runtime.backend; import pro.gravit.launcher.base.Launcher; +import pro.gravit.launcher.base.events.request.AuthRequestEvent; import pro.gravit.launcher.base.profiles.ClientProfile; import pro.gravit.launcher.base.profiles.ClientProfileBuilder; import pro.gravit.launcher.base.profiles.PlayerProfile; +import pro.gravit.launcher.core.api.LauncherAPIHolder; import pro.gravit.launcher.core.api.features.ProfileFeatureAPI; import pro.gravit.launcher.core.backend.LauncherBackendAPI; import pro.gravit.launcher.runtime.client.ClientLauncherProcess; @@ -61,9 +63,14 @@ public void run(LauncherBackendAPI.RunCallback callback) { builder.setUpdateExclusions(new ArrayList<>()); profile = builder.createClientProfile(); } - process = new ClientLauncherProcess(clientDir.path(), assetDir.path(), settings.getSelectedJava(), clientDir.path().resolve("resourcepacks"), + var java = settings.getSelectedJava(); + if(java == null) { + java = settings.getRecommendedJava(); + } + process = new ClientLauncherProcess(clientDir.path(), assetDir.path(), java, clientDir.path().resolve("resourcepacks"), profile, new PlayerProfile(backend.getSelfUser()), settings.view, backend.getSelfUser().getAccessToken(), - clientDir.dir(), assetDir.dir(), javaDir == null ? null : javaDir.dir()); + clientDir.dir(), assetDir.dir(), javaDir == null ? null : javaDir.dir(), + new AuthRequestEvent.OAuthRequestEvent(backend.getAuthToken()), backend.getAuthMethod().getName()); process.params.ram = (int) (settings.getReservedMemoryBytes(LauncherBackendAPI.ClientProfileSettings.MemoryClass.TOTAL) >> 20); if (process.params.ram > 0) { process.jvmArgs.add("-Xms" + process.params.ram + 'M'); diff --git a/Launcher/src/main/java/pro/gravit/launcher/runtime/client/ClientLauncherProcess.java b/Launcher/src/main/java/pro/gravit/launcher/runtime/client/ClientLauncherProcess.java index dc1b5bcd..ea3e12a4 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/runtime/client/ClientLauncherProcess.java +++ b/Launcher/src/main/java/pro/gravit/launcher/runtime/client/ClientLauncherProcess.java @@ -2,6 +2,7 @@ import pro.gravit.launcher.base.Launcher; import pro.gravit.launcher.base.LauncherConfig; +import pro.gravit.launcher.base.events.request.AuthRequestEvent; import pro.gravit.launcher.client.ClientLauncherEntryPoint; import pro.gravit.launcher.client.ClientParams; import pro.gravit.launcher.runtime.LauncherEngine; @@ -70,6 +71,12 @@ public ClientLauncherProcess(Path clientDir, Path assetDir, public ClientLauncherProcess(Path clientDir, Path assetDir, JavaHelper.JavaVersion javaVersion, Path resourcePackDir, ClientProfile profile, PlayerProfile playerProfile, OptionalView view, String accessToken, HashedDir clientHDir, HashedDir assetHDir, HashedDir jvmHDir) { + this(clientDir, assetDir, javaVersion, resourcePackDir, profile, playerProfile, view, accessToken, clientHDir, assetHDir, jvmHDir, null, null); + } + + public ClientLauncherProcess(Path clientDir, Path assetDir, JavaHelper.JavaVersion javaVersion, Path resourcePackDir, + ClientProfile profile, PlayerProfile playerProfile, OptionalView view, String accessToken, + HashedDir clientHDir, HashedDir assetHDir, HashedDir jvmHDir, AuthRequestEvent.OAuthRequestEvent oAuthRequestEvent, String authId) { this.javaVersion = javaVersion; this.workDir = clientDir.toAbsolutePath(); this.executeFile = IOHelper.resolveJavaBin(this.javaVersion.jvmDir); @@ -78,6 +85,8 @@ public ClientLauncherProcess(Path clientDir, Path assetDir, JavaHelper.JavaVersi this.params.resourcePackDir = resourcePackDir.toAbsolutePath().toString(); this.params.assetDir = assetDir.toAbsolutePath().toString(); this.params.timestamp = System.currentTimeMillis(); + this.params.oauth = oAuthRequestEvent; + this.params.authId = authId; Path nativesPath; if(profile.hasFlag(ClientProfile.CompatibilityFlags.LEGACY_NATIVES_DIR)) { nativesPath = workDir.resolve("natives"); @@ -120,10 +129,8 @@ private void applyClientProfile() { if (params.ram > 0) { this.jvmArgs.add("-Xmx" + params.ram + 'M'); } - this.params.oauth = Request.getOAuth(); if (this.params.oauth == null) { - throw new UnsupportedOperationException("Legacy session not supported"); - } else { + this.params.oauth = Request.getOAuth(); this.params.authId = Request.getAuthId(); this.params.oauthExpiredTime = Request.getTokenExpiredTime(); this.params.extendedTokens = Request.getExtendedTokens(); diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/base/events/request/AuthRequestEvent.java b/LauncherAPI/src/main/java/pro/gravit/launcher/base/events/request/AuthRequestEvent.java index 7f6c5f61..a7e0584e 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/base/events/request/AuthRequestEvent.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/base/events/request/AuthRequestEvent.java @@ -87,6 +87,12 @@ public OAuthRequestEvent(String accessToken, String refreshToken, long expire) { this.expire = expire; } + public OAuthRequestEvent(AuthFeatureAPI.AuthToken token) { + this.accessToken = token.getAccessToken(); + this.refreshToken = token.getRefreshToken(); + this.expire = token.getExpire(); + } + @Override public String getAccessToken() { return accessToken; diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/base/request/RequestFeatureAPIImpl.java b/LauncherAPI/src/main/java/pro/gravit/launcher/base/request/RequestFeatureAPIImpl.java index f828e36f..39994d24 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/base/request/RequestFeatureAPIImpl.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/base/request/RequestFeatureAPIImpl.java @@ -1,6 +1,7 @@ package pro.gravit.launcher.base.request; import pro.gravit.launcher.base.Launcher; +import pro.gravit.launcher.base.profiles.ClientProfile; import pro.gravit.launcher.base.request.auth.*; import pro.gravit.launcher.base.request.auth.password.*; import pro.gravit.launcher.base.request.update.ProfilesRequest; @@ -89,6 +90,8 @@ private AuthRequest.AuthPasswordInterface convertAuthPassword(AuthMethodPassword return new AuthCodePassword(oauth.redirectUrl()); } else if(password instanceof AuthRequest.AuthPasswordInterface custom) { return custom; + } else if(password == null) { + return null; } else { throw new UnsupportedOperationException(); @@ -164,6 +167,11 @@ public CompletableFuture> getProfiles() { return request.request(new ProfilesRequest()).thenApply(response -> (List) response.profiles); } + @Override + public CompletableFuture changeCurrentProfile(ClientProfile profile) { + return request.request(new SetProfileRequest((pro.gravit.launcher.base.profiles.ClientProfile) profile)).thenApply(response -> null); + } + @Override public CompletableFuture fetchUpdateInfo(String dirName) { return request.request(new UpdateRequest(dirName)).thenApply(response -> new UpdateInfoData(response.hdir, response.url)); diff --git a/LauncherCore/src/main/java/pro/gravit/launcher/core/api/features/ProfileFeatureAPI.java b/LauncherCore/src/main/java/pro/gravit/launcher/core/api/features/ProfileFeatureAPI.java index 6d5abbfa..afd541bf 100644 --- a/LauncherCore/src/main/java/pro/gravit/launcher/core/api/features/ProfileFeatureAPI.java +++ b/LauncherCore/src/main/java/pro/gravit/launcher/core/api/features/ProfileFeatureAPI.java @@ -9,6 +9,7 @@ public interface ProfileFeatureAPI extends FeatureAPI { CompletableFuture> getProfiles(); + CompletableFuture changeCurrentProfile(ClientProfile profile); CompletableFuture fetchUpdateInfo(String dirName); interface UpdateInfo { diff --git a/LauncherCore/src/main/java/pro/gravit/launcher/core/backend/LauncherBackendAPI.java b/LauncherCore/src/main/java/pro/gravit/launcher/core/backend/LauncherBackendAPI.java index 4851f3a0..e367f11b 100644 --- a/LauncherCore/src/main/java/pro/gravit/launcher/core/backend/LauncherBackendAPI.java +++ b/LauncherCore/src/main/java/pro/gravit/launcher/core/backend/LauncherBackendAPI.java @@ -63,6 +63,7 @@ interface ClientProfileSettings { void enableOptional(ProfileFeatureAPI.OptionalMod mod, ChangedOptionalStatusCallback callback); void disableOptional(ProfileFeatureAPI.OptionalMod mod, ChangedOptionalStatusCallback callback); Java getSelectedJava(); + Java getRecommendedJava(); void setSelectedJava(Java java); boolean isRecommended(Java java); boolean isCompatible(Java java); diff --git a/build.gradle b/build.gradle index 1a1f5a77..a658cec2 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ id 'org.openjfx.javafxplugin' version '0.1.0' apply false } group = 'pro.gravit.launcher' -version = '5.6.8' +version = '5.7.0-SNAPSHOT' apply from: 'props.gradle'