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 1d64ee03..0ec6872e 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 @@ -3,6 +3,7 @@ import pro.gravit.launcher.core.LauncherNetworkAPI; import pro.gravit.launcher.core.backend.UserSettings; +import java.util.HashMap; import java.util.Map; import java.util.UUID; @@ -10,7 +11,7 @@ public class BackendSettings extends UserSettings { @LauncherNetworkAPI public AuthorizationData auth; @LauncherNetworkAPI - public Map settings; + public Map settings = new HashMap<>(); public static class AuthorizationData { @LauncherNetworkAPI public String accessToken; 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 cfd85d0b..6b9557d1 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 @@ -4,6 +4,7 @@ import pro.gravit.launcher.base.profiles.ClientProfile; import pro.gravit.launcher.core.api.LauncherAPIHolder; import pro.gravit.launcher.core.api.features.AuthFeatureAPI; +import pro.gravit.launcher.core.api.features.CoreFeatureAPI; import pro.gravit.launcher.core.api.features.ProfileFeatureAPI; import pro.gravit.launcher.core.api.method.AuthMethod; import pro.gravit.launcher.core.api.method.AuthMethodPassword; @@ -15,6 +16,8 @@ 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.ServerPinger; +import pro.gravit.launcher.runtime.debug.DebugMain; import pro.gravit.launcher.runtime.managers.SettingsManager; import pro.gravit.launcher.runtime.utils.LauncherUpdater; import pro.gravit.utils.helper.JavaHelper; @@ -27,7 +30,10 @@ import java.time.ZoneOffset; import java.time.temporal.ChronoUnit; import java.util.List; +import java.util.Map; +import java.util.UUID; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.function.Function; @@ -47,6 +53,7 @@ public class LauncherBackendImpl implements LauncherBackendAPI { private volatile SelfUser selfUser; private volatile List availableJavas; private volatile CompletableFuture> availableJavasFuture; + private final Map> pingFutures = new ConcurrentHashMap<>(); @Override public void setCallback(MainCallback callback) { @@ -75,7 +82,13 @@ public CompletableFuture init() { } catch (Throwable e) { return CompletableFuture.failedFuture(e); } - return LauncherAPIHolder.core().checkUpdates().thenCombineAsync(LauncherAPIHolder.core().getAuthMethods(), (updatesInfo, authMethods) -> { + CompletableFuture feature; + if(isTestMode()) { + feature = CompletableFuture.completedFuture(new CoreFeatureAPI.LauncherUpdateInfo(null, "Unknown", false, false)); + } else { + feature = LauncherAPIHolder.core().checkUpdates(); + } + return feature.thenCombineAsync(LauncherAPIHolder.core().getAuthMethods(), (updatesInfo, authMethods) -> { if(updatesInfo.required()) { try { LauncherUpdater.prepareUpdate(URI.create(updatesInfo.url()).toURL()); @@ -131,6 +144,7 @@ private void setAuthToken(AuthFeatureAPI.AuthToken authToken) { } private void onAuthorize(SelfUser selfUser) { + this.selfUser = selfUser; permissions = selfUser.getPermissions(); callback.onAuthorize(selfUser); } @@ -182,7 +196,7 @@ public CompletableFuture downloadProfile(ProfileFeatureAPI.ClientP @Override public CompletableFuture fetchTexture(Texture texture) { - return null; + return CompletableFuture.failedFuture(new UnsupportedOperationException()); } @SuppressWarnings({"rawtypes", "unchecked"}) @@ -201,6 +215,22 @@ public CompletableFuture> getAvailableJava() { return CompletableFuture.completedFuture(availableJavas); } + @Override + public CompletableFuture pingServer(ProfileFeatureAPI.ClientProfile profile) { + return pingFutures.computeIfAbsent(profile.getUUID(), (k) -> { + CompletableFuture future = new CompletableFuture<>(); + executorService.submit(() -> { + try { + ServerPinger pinger = new ServerPinger((ClientProfile) profile); + future.complete(pinger.ping()); + } catch (Throwable e) { + future.completeExceptionally(e); + } + }); + return future; + }); + } + @Override public void registerUserSettings(String name, Class clazz) { UserSettings.providers.register(name, clazz); @@ -231,6 +261,15 @@ public SelfUser getSelfUser() { return selfUser; } + @Override + public boolean isTestMode() { + try { + return DebugMain.IS_DEBUG.get(); + } catch (Throwable ex) { + return false; + } + } + @Override public T getExtension(Class clazz) { return null; @@ -238,11 +277,15 @@ public T getExtension(Class clazz) { @Override public void shutdown() { - executorService.shutdownNow(); - try { - settingsManager.saveConfig(); - } catch (IOException e) { - LogHelper.error("Config not saved", e); + if(executorService != null) { + executorService.shutdownNow(); + } + if(settingsManager != null) { + try { + settingsManager.saveConfig(); + } catch (IOException e) { + LogHelper.error("Config not saved", e); + } } } } diff --git a/Launcher/src/main/java/pro/gravit/launcher/runtime/client/ServerPinger.java b/Launcher/src/main/java/pro/gravit/launcher/runtime/client/ServerPinger.java index 53f113b2..7f703e30 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/runtime/client/ServerPinger.java +++ b/Launcher/src/main/java/pro/gravit/launcher/runtime/client/ServerPinger.java @@ -5,6 +5,7 @@ import com.google.gson.JsonParser; import pro.gravit.launcher.base.profiles.ClientProfile; import pro.gravit.launcher.base.profiles.ClientProfileVersions; +import pro.gravit.launcher.core.backend.LauncherBackendAPI; import pro.gravit.launcher.core.serialize.HInput; import pro.gravit.launcher.core.serialize.HOutput; import pro.gravit.utils.helper.IOHelper; @@ -18,6 +19,7 @@ import java.nio.charset.StandardCharsets; import java.time.Duration; import java.time.Instant; +import java.util.List; import java.util.Objects; import java.util.regex.Pattern; @@ -208,7 +210,7 @@ public Result ping() throws IOException { } } - public static final class Result { + public static final class Result implements LauncherBackendAPI.ServerPingInfo { public final int onlinePlayers; @@ -228,5 +230,20 @@ public Result(int onlinePlayers, int maxPlayers, String raw) { public boolean isOverfilled() { return onlinePlayers >= maxPlayers; } + + @Override + public int getMaxOnline() { + return maxPlayers; + } + + @Override + public int getOnline() { + return onlinePlayers; + } + + @Override + public List getPlayerNames() { + return List.of(); + } } } diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/base/profiles/ClientProfile.java b/LauncherAPI/src/main/java/pro/gravit/launcher/base/profiles/ClientProfile.java index 137cd961..829f8651 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/base/profiles/ClientProfile.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/base/profiles/ClientProfile.java @@ -380,6 +380,11 @@ public Map getProperties() { return Collections.unmodifiableMap(properties); } + @Override + public ServerInfo getServer() { + return getDefaultServerProfile(); + } + public List getCompatClasses() { return Collections.unmodifiableList(compatClasses); } @@ -525,7 +530,7 @@ public JsonElement serialize(Version src, Type typeOfSrc, JsonSerializationConte } } - public static class ServerProfile { + public static class ServerProfile implements ServerInfo { public String name; public String serverAddress; public int serverPort; @@ -552,6 +557,16 @@ public ServerProfile(String name, String serverAddress, int serverPort, boolean public InetSocketAddress toSocketAddress() { return InetSocketAddress.createUnresolved(serverAddress, serverPort); } + + @Override + public String getAddress() { + return serverAddress; + } + + @Override + public int getPort() { + return serverPort; + } } public static class ProfileDefaultSettings { 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 6aaed9ae..f828e36f 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 @@ -42,7 +42,11 @@ public CompletableFuture getCurrentUser() { @Override public CompletableFuture auth(String login, AuthMethodPassword password) { - return request.request(new AuthRequest(login, convertAuthPasswordAll(password), authId, false, AuthRequest.ConnectTypes.CLIENT)) + AuthRequest.ConnectTypes connectType = AuthRequest.ConnectTypes.API; + if(Request.getExtendedTokens() != null && Request.getExtendedTokens().get("launcher") != null) { + connectType = AuthRequest.ConnectTypes.CLIENT; + } + return request.request(new AuthRequest(login, convertAuthPasswordAll(password), authId, false, connectType)) .thenApply(response -> new AuthResponse(response.makeUserInfo(), response.oauth)); } 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 8149911b..6d5abbfa 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 @@ -23,6 +23,12 @@ interface ClientProfile { List getOptionalMods(); String getProperty(String name); Map getProperties(); + ServerInfo getServer(); + + interface ServerInfo { + String getAddress(); + int getPort(); + } } interface OptionalMod { 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 7fa4a491..4851f3a0 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 @@ -28,6 +28,7 @@ public interface LauncherBackendAPI { // Tools CompletableFuture fetchTexture(Texture texture); CompletableFuture> getAvailableJava(); + CompletableFuture pingServer(ProfileFeatureAPI.ClientProfile profile); // Settings void registerUserSettings(String name, Class clazz); UserSettings getUserSettings(String name, Function ifNotExist); @@ -36,6 +37,7 @@ public interface LauncherBackendAPI { boolean hasPermission(String permission); String getUsername(); SelfUser getSelfUser(); + boolean isTestMode(); // Extensions T getExtension(Class clazz); void shutdown(); @@ -163,4 +165,10 @@ interface Java { int getMajorVersion(); Path getPath(); } + + interface ServerPingInfo { + int getMaxOnline(); + int getOnline(); + List getPlayerNames(); + } }