[FIX][EXPERIMENTAL] API bug fixes

This commit is contained in:
Gravita 2024-06-29 19:09:46 +07:00
parent 83cc441574
commit d4d6491e52
No known key found for this signature in database
GPG key ID: 543A8F335C9CD633
7 changed files with 105 additions and 11 deletions

View file

@ -3,6 +3,7 @@
import pro.gravit.launcher.core.LauncherNetworkAPI; import pro.gravit.launcher.core.LauncherNetworkAPI;
import pro.gravit.launcher.core.backend.UserSettings; import pro.gravit.launcher.core.backend.UserSettings;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
@ -10,7 +11,7 @@ public class BackendSettings extends UserSettings {
@LauncherNetworkAPI @LauncherNetworkAPI
public AuthorizationData auth; public AuthorizationData auth;
@LauncherNetworkAPI @LauncherNetworkAPI
public Map<UUID, ProfileSettingsImpl> settings; public Map<UUID, ProfileSettingsImpl> settings = new HashMap<>();
public static class AuthorizationData { public static class AuthorizationData {
@LauncherNetworkAPI @LauncherNetworkAPI
public String accessToken; public String accessToken;

View file

@ -4,6 +4,7 @@
import pro.gravit.launcher.base.profiles.ClientProfile; import pro.gravit.launcher.base.profiles.ClientProfile;
import pro.gravit.launcher.core.api.LauncherAPIHolder; import pro.gravit.launcher.core.api.LauncherAPIHolder;
import pro.gravit.launcher.core.api.features.AuthFeatureAPI; 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.features.ProfileFeatureAPI;
import pro.gravit.launcher.core.api.method.AuthMethod; import pro.gravit.launcher.core.api.method.AuthMethod;
import pro.gravit.launcher.core.api.method.AuthMethodPassword; 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.exceptions.LauncherBackendException;
import pro.gravit.launcher.core.backend.extensions.Extension; import pro.gravit.launcher.core.backend.extensions.Extension;
import pro.gravit.launcher.runtime.NewLauncherSettings; 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.managers.SettingsManager;
import pro.gravit.launcher.runtime.utils.LauncherUpdater; import pro.gravit.launcher.runtime.utils.LauncherUpdater;
import pro.gravit.utils.helper.JavaHelper; import pro.gravit.utils.helper.JavaHelper;
@ -27,7 +30,10 @@
import java.time.ZoneOffset; import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.function.Function; import java.util.function.Function;
@ -47,6 +53,7 @@ public class LauncherBackendImpl implements LauncherBackendAPI {
private volatile SelfUser selfUser; private volatile SelfUser selfUser;
private volatile List<Java> availableJavas; private volatile List<Java> availableJavas;
private volatile CompletableFuture<List<Java>> availableJavasFuture; private volatile CompletableFuture<List<Java>> availableJavasFuture;
private final Map<UUID, CompletableFuture<ServerPingInfo>> pingFutures = new ConcurrentHashMap<>();
@Override @Override
public void setCallback(MainCallback callback) { public void setCallback(MainCallback callback) {
@ -75,7 +82,13 @@ public CompletableFuture<LauncherInitData> init() {
} catch (Throwable e) { } catch (Throwable e) {
return CompletableFuture.failedFuture(e); return CompletableFuture.failedFuture(e);
} }
return LauncherAPIHolder.core().checkUpdates().thenCombineAsync(LauncherAPIHolder.core().getAuthMethods(), (updatesInfo, authMethods) -> { CompletableFuture<CoreFeatureAPI.LauncherUpdateInfo> 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()) { if(updatesInfo.required()) {
try { try {
LauncherUpdater.prepareUpdate(URI.create(updatesInfo.url()).toURL()); LauncherUpdater.prepareUpdate(URI.create(updatesInfo.url()).toURL());
@ -131,6 +144,7 @@ private void setAuthToken(AuthFeatureAPI.AuthToken authToken) {
} }
private void onAuthorize(SelfUser selfUser) { private void onAuthorize(SelfUser selfUser) {
this.selfUser = selfUser;
permissions = selfUser.getPermissions(); permissions = selfUser.getPermissions();
callback.onAuthorize(selfUser); callback.onAuthorize(selfUser);
} }
@ -182,7 +196,7 @@ public CompletableFuture<ReadyProfile> downloadProfile(ProfileFeatureAPI.ClientP
@Override @Override
public CompletableFuture<byte[]> fetchTexture(Texture texture) { public CompletableFuture<byte[]> fetchTexture(Texture texture) {
return null; return CompletableFuture.failedFuture(new UnsupportedOperationException());
} }
@SuppressWarnings({"rawtypes", "unchecked"}) @SuppressWarnings({"rawtypes", "unchecked"})
@ -201,6 +215,22 @@ public CompletableFuture<List<Java>> getAvailableJava() {
return CompletableFuture.completedFuture(availableJavas); return CompletableFuture.completedFuture(availableJavas);
} }
@Override
public CompletableFuture<ServerPingInfo> pingServer(ProfileFeatureAPI.ClientProfile profile) {
return pingFutures.computeIfAbsent(profile.getUUID(), (k) -> {
CompletableFuture<ServerPingInfo> 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 @Override
public void registerUserSettings(String name, Class<? extends UserSettings> clazz) { public void registerUserSettings(String name, Class<? extends UserSettings> clazz) {
UserSettings.providers.register(name, clazz); UserSettings.providers.register(name, clazz);
@ -231,6 +261,15 @@ public SelfUser getSelfUser() {
return selfUser; return selfUser;
} }
@Override
public boolean isTestMode() {
try {
return DebugMain.IS_DEBUG.get();
} catch (Throwable ex) {
return false;
}
}
@Override @Override
public <T extends Extension> T getExtension(Class<T> clazz) { public <T extends Extension> T getExtension(Class<T> clazz) {
return null; return null;
@ -238,7 +277,10 @@ public <T extends Extension> T getExtension(Class<T> clazz) {
@Override @Override
public void shutdown() { public void shutdown() {
if(executorService != null) {
executorService.shutdownNow(); executorService.shutdownNow();
}
if(settingsManager != null) {
try { try {
settingsManager.saveConfig(); settingsManager.saveConfig();
} catch (IOException e) { } catch (IOException e) {
@ -246,3 +288,4 @@ public void shutdown() {
} }
} }
} }
}

View file

@ -5,6 +5,7 @@
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import pro.gravit.launcher.base.profiles.ClientProfile; import pro.gravit.launcher.base.profiles.ClientProfile;
import pro.gravit.launcher.base.profiles.ClientProfileVersions; 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.HInput;
import pro.gravit.launcher.core.serialize.HOutput; import pro.gravit.launcher.core.serialize.HOutput;
import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.IOHelper;
@ -18,6 +19,7 @@
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.regex.Pattern; 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; public final int onlinePlayers;
@ -228,5 +230,20 @@ public Result(int onlinePlayers, int maxPlayers, String raw) {
public boolean isOverfilled() { public boolean isOverfilled() {
return onlinePlayers >= maxPlayers; return onlinePlayers >= maxPlayers;
} }
@Override
public int getMaxOnline() {
return maxPlayers;
}
@Override
public int getOnline() {
return onlinePlayers;
}
@Override
public List<String> getPlayerNames() {
return List.of();
}
} }
} }

View file

@ -380,6 +380,11 @@ public Map<String, String> getProperties() {
return Collections.unmodifiableMap(properties); return Collections.unmodifiableMap(properties);
} }
@Override
public ServerInfo getServer() {
return getDefaultServerProfile();
}
public List<String> getCompatClasses() { public List<String> getCompatClasses() {
return Collections.unmodifiableList(compatClasses); 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 name;
public String serverAddress; public String serverAddress;
public int serverPort; public int serverPort;
@ -552,6 +557,16 @@ public ServerProfile(String name, String serverAddress, int serverPort, boolean
public InetSocketAddress toSocketAddress() { public InetSocketAddress toSocketAddress() {
return InetSocketAddress.createUnresolved(serverAddress, serverPort); return InetSocketAddress.createUnresolved(serverAddress, serverPort);
} }
@Override
public String getAddress() {
return serverAddress;
}
@Override
public int getPort() {
return serverPort;
}
} }
public static class ProfileDefaultSettings { public static class ProfileDefaultSettings {

View file

@ -42,7 +42,11 @@ public CompletableFuture<SelfUser> getCurrentUser() {
@Override @Override
public CompletableFuture<AuthResponse> auth(String login, AuthMethodPassword password) { public CompletableFuture<AuthResponse> 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)); .thenApply(response -> new AuthResponse(response.makeUserInfo(), response.oauth));
} }

View file

@ -23,6 +23,12 @@ interface ClientProfile {
List<OptionalMod> getOptionalMods(); List<OptionalMod> getOptionalMods();
String getProperty(String name); String getProperty(String name);
Map<String, String> getProperties(); Map<String, String> getProperties();
ServerInfo getServer();
interface ServerInfo {
String getAddress();
int getPort();
}
} }
interface OptionalMod { interface OptionalMod {

View file

@ -28,6 +28,7 @@ public interface LauncherBackendAPI {
// Tools // Tools
CompletableFuture<byte[]> fetchTexture(Texture texture); CompletableFuture<byte[]> fetchTexture(Texture texture);
CompletableFuture<List<Java>> getAvailableJava(); CompletableFuture<List<Java>> getAvailableJava();
CompletableFuture<ServerPingInfo> pingServer(ProfileFeatureAPI.ClientProfile profile);
// Settings // Settings
void registerUserSettings(String name, Class<? extends UserSettings> clazz); void registerUserSettings(String name, Class<? extends UserSettings> clazz);
UserSettings getUserSettings(String name, Function<String, UserSettings> ifNotExist); UserSettings getUserSettings(String name, Function<String, UserSettings> ifNotExist);
@ -36,6 +37,7 @@ public interface LauncherBackendAPI {
boolean hasPermission(String permission); boolean hasPermission(String permission);
String getUsername(); String getUsername();
SelfUser getSelfUser(); SelfUser getSelfUser();
boolean isTestMode();
// Extensions // Extensions
<T extends Extension> T getExtension(Class<T> clazz); <T extends Extension> T getExtension(Class<T> clazz);
void shutdown(); void shutdown();
@ -163,4 +165,10 @@ interface Java {
int getMajorVersion(); int getMajorVersion();
Path getPath(); Path getPath();
} }
interface ServerPingInfo {
int getMaxOnline();
int getOnline();
List<String> getPlayerNames();
}
} }