[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.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<UUID, ProfileSettingsImpl> settings;
public Map<UUID, ProfileSettingsImpl> settings = new HashMap<>();
public static class AuthorizationData {
@LauncherNetworkAPI
public String accessToken;

View file

@ -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<Java> availableJavas;
private volatile CompletableFuture<List<Java>> availableJavasFuture;
private final Map<UUID, CompletableFuture<ServerPingInfo>> pingFutures = new ConcurrentHashMap<>();
@Override
public void setCallback(MainCallback callback) {
@ -75,7 +82,13 @@ public CompletableFuture<LauncherInitData> init() {
} catch (Throwable 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()) {
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<ReadyProfile> downloadProfile(ProfileFeatureAPI.ClientP
@Override
public CompletableFuture<byte[]> fetchTexture(Texture texture) {
return null;
return CompletableFuture.failedFuture(new UnsupportedOperationException());
}
@SuppressWarnings({"rawtypes", "unchecked"})
@ -201,6 +215,22 @@ public CompletableFuture<List<Java>> getAvailableJava() {
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
public void registerUserSettings(String name, Class<? extends UserSettings> 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 extends Extension> T getExtension(Class<T> clazz) {
return null;
@ -238,11 +277,15 @@ public <T extends Extension> T getExtension(Class<T> 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);
}
}
}
}

View file

@ -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<String> getPlayerNames() {
return List.of();
}
}
}

View file

@ -380,6 +380,11 @@ public Map<String, String> getProperties() {
return Collections.unmodifiableMap(properties);
}
@Override
public ServerInfo getServer() {
return getDefaultServerProfile();
}
public List<String> 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 {

View file

@ -42,7 +42,11 @@ public CompletableFuture<SelfUser> getCurrentUser() {
@Override
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));
}

View file

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

View file

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