From 7c36f341e47d2259314bc3660914dabb681a6969 Mon Sep 17 00:00:00 2001 From: dima_dencep Date: Sat, 24 Sep 2022 00:29:22 +0700 Subject: [PATCH] idea cleanup launcherapi --- .../gravit/launcher/ClientPermissions.java | 27 ++++++------ .../launcher/profiles/PlayerProfile.java | 5 +-- .../optional/triggers/ArchTrigger.java | 1 + .../pro/gravit/launcher/request/Request.java | 42 +++++++++---------- .../launcher/request/RequestService.java | 5 ++- .../request/update/LauncherRequest.java | 8 ---- .../uuid/BatchProfileByUsernameRequest.java | 1 - .../uuid/ProfileByUsernameRequest.java | 1 - .../request/websockets/ClientJSONPoint.java | 5 +-- .../websockets/ClientWebSocketService.java | 2 +- .../websockets/OfflineRequestService.java | 13 +++--- .../websockets/StdWebSocketService.java | 1 - 12 files changed, 50 insertions(+), 61 deletions(-) diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/ClientPermissions.java b/LauncherAPI/src/main/java/pro/gravit/launcher/ClientPermissions.java index a080c8ad..e42164cc 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/ClientPermissions.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/ClientPermissions.java @@ -1,8 +1,5 @@ package pro.gravit.launcher; -import pro.gravit.launcher.serialize.HInput; - -import java.io.IOException; import java.util.*; public class ClientPermissions { @@ -71,7 +68,7 @@ public void addPerm(String perm) { perms = new ArrayList<>(1); } perms.add(perm); - if(available == null) { + if (available == null) { available = new ArrayList<>(1); } available.add(new PermissionPattern(perm)); @@ -81,7 +78,7 @@ public void removePerm(String action) { if (perms == null) { return; } - if(available == null) { + if (available == null) { return; } perms.remove(action); @@ -110,11 +107,11 @@ public static class PermissionPattern { public PermissionPattern(String pattern) { List prepare = new ArrayList<>(); - for(int i=0;true;) { + for (int i = 0; true; ) { int pos = pattern.indexOf("*", i); - if(pos >= 0) { + if (pos >= 0) { prepare.add(pattern.substring(i, pos)); - i = pos+1; + i = pos + 1; } else { prepare.add(pattern.substring(i)); break; @@ -129,23 +126,23 @@ public int getPriority() { } public boolean match(String str) { - if(parts.length == 0) { + if (parts.length == 0) { return true; } - if(parts.length == 1) { + if (parts.length == 1) { return parts[0].equals(str); } int offset = 0; - if(!str.startsWith(parts[0])) { + if (!str.startsWith(parts[0])) { return false; } - if(!str.endsWith(parts[parts.length-1])) { + if (!str.endsWith(parts[parts.length - 1])) { return false; } - for(int i=1;i= 0) { - offset = pos+1; + if (pos >= 0) { + offset = pos + 1; } else { return false; } diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/profiles/PlayerProfile.java b/LauncherAPI/src/main/java/pro/gravit/launcher/profiles/PlayerProfile.java index 018bb008..3678e0ea 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/profiles/PlayerProfile.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/profiles/PlayerProfile.java @@ -1,7 +1,6 @@ package pro.gravit.launcher.profiles; import pro.gravit.utils.helper.IOHelper; -import pro.gravit.utils.helper.VerifyHelper; import java.util.HashMap; import java.util.Map; @@ -30,10 +29,10 @@ public PlayerProfile(UUID uuid, String username, Texture skin, Texture cloak, Ma this.skin = skin; this.cloak = cloak; this.assets = new HashMap<>(); - if(skin != null) { + if (skin != null) { this.assets.put("SKIN", skin); } - if(cloak != null) { + if (cloak != null) { this.assets.put("CAPE", cloak); } this.properties = properties; diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/profiles/optional/triggers/ArchTrigger.java b/LauncherAPI/src/main/java/pro/gravit/launcher/profiles/optional/triggers/ArchTrigger.java index 1222af88..b507186a 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/profiles/optional/triggers/ArchTrigger.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/profiles/optional/triggers/ArchTrigger.java @@ -5,6 +5,7 @@ public class ArchTrigger extends OptionalTrigger { public JVMHelper.ARCH arch; + @Override protected boolean isTriggered(OptionalFile optional, OptionalTriggerContext context) { return context.getJavaVersion().arch == arch; 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 cd26f963..b3d4df69 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/Request.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/Request.java @@ -28,17 +28,17 @@ public abstract class Request implements WebSocketRequ public final UUID requestUUID = UUID.randomUUID(); private transient final AtomicBoolean started = new AtomicBoolean(false); - public static void setRequestService(RequestService service) { - requestService = service; - if(service instanceof StdWebSocketService) { - Request.service = (StdWebSocketService) service; - } - } - public static RequestService getRequestService() { return requestService; } + public static void setRequestService(RequestService service) { + requestService = service; + if (service instanceof StdWebSocketService) { + Request.service = (StdWebSocketService) service; + } + } + public static boolean isAvailable() { return requestService != null; } @@ -121,22 +121,10 @@ public static RequestRestoreReport reconnect() throws Exception { return restore(); } - public static class RequestRestoreReport { - public final boolean legacySession; - public final boolean refreshed; - public final List invalidExtendedTokens; - - public RequestRestoreReport(boolean legacySession, boolean refreshed, List invalidExtendedTokens) { - this.legacySession = legacySession; - this.refreshed = refreshed; - this.invalidExtendedTokens = invalidExtendedTokens; - } - } - public static RequestRestoreReport restore() throws Exception { boolean refreshed = false; RestoreRequest request; - if(oauth != null) { + if (oauth != null) { if (isTokenExpired() || oauth.accessToken == null) { RefreshTokenRequest refreshRequest = new RefreshTokenRequest(authId, oauth.refreshToken); RefreshTokenRequestEvent event = refreshRequest.request(); @@ -197,7 +185,7 @@ public void removeOAuthChangeHandler(BiConsumer invalidExtendedTokens; + + public RequestRestoreReport(boolean legacySession, boolean refreshed, List invalidExtendedTokens) { + this.legacySession = legacySession; + this.refreshed = refreshed; + this.invalidExtendedTokens = invalidExtendedTokens; + } + } + } diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/RequestService.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/RequestService.java index 8d652b85..39cc2f27 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/RequestService.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/RequestService.java @@ -6,9 +6,12 @@ public interface RequestService { CompletableFuture request(Request request) throws IOException; + void registerEventHandler(EventHandler handler); + void unregisterEventHandler(EventHandler handler); - default T requestSync(Request request) throws IOException { + + default T requestSync(Request request) throws IOException { try { return request(request).get(); } catch (InterruptedException e) { diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/update/LauncherRequest.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/update/LauncherRequest.java index cc79cb9d..b2345410 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/update/LauncherRequest.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/update/LauncherRequest.java @@ -5,21 +5,13 @@ import pro.gravit.launcher.events.request.LauncherRequestEvent; import pro.gravit.launcher.request.Request; import pro.gravit.launcher.request.RequestService; -import pro.gravit.launcher.request.websockets.StdWebSocketService; import pro.gravit.launcher.request.websockets.WebSocketRequest; import pro.gravit.utils.helper.IOHelper; -import pro.gravit.utils.helper.JVMHelper; import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.SecurityHelper; import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.net.URLConnection; -import java.nio.file.Files; import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; public final class LauncherRequest extends Request implements WebSocketRequest { public static final Path BINARY_PATH = IOHelper.getCodeSource(Launcher.class); diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/uuid/BatchProfileByUsernameRequest.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/uuid/BatchProfileByUsernameRequest.java index 20708419..a3f1ab2e 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/uuid/BatchProfileByUsernameRequest.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/uuid/BatchProfileByUsernameRequest.java @@ -5,7 +5,6 @@ import pro.gravit.launcher.request.Request; import pro.gravit.launcher.request.websockets.WebSocketRequest; import pro.gravit.utils.helper.IOHelper; -import pro.gravit.utils.helper.VerifyHelper; import java.io.IOException; diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/uuid/ProfileByUsernameRequest.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/uuid/ProfileByUsernameRequest.java index 43c5c02a..d164b28e 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/uuid/ProfileByUsernameRequest.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/uuid/ProfileByUsernameRequest.java @@ -4,7 +4,6 @@ import pro.gravit.launcher.events.request.ProfileByUsernameRequestEvent; import pro.gravit.launcher.request.Request; import pro.gravit.launcher.request.websockets.WebSocketRequest; -import pro.gravit.utils.helper.VerifyHelper; public final class ProfileByUsernameRequest extends Request implements WebSocketRequest { @LauncherNetworkAPI diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/ClientJSONPoint.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/ClientJSONPoint.java index 7ef2b881..d4ce4ec0 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/ClientJSONPoint.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/ClientJSONPoint.java @@ -24,7 +24,6 @@ import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; -import java.util.concurrent.CompletableFuture; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; @@ -112,10 +111,10 @@ public void openAsync(Runnable onConnect, Consumer onFail) { uri, WebSocketVersion.V13, null, false, EmptyHttpHeaders.INSTANCE, 12800000), this); ChannelFuture future = bootstrap.connect(uri.getHost(), port); future.addListener((l) -> { - if(l.isSuccess()) { + if (l.isSuccess()) { ch = future.channel(); webSocketClientHandler.handshakeFuture().addListener((e) -> { - if(e.isSuccess()) { + if (e.isSuccess()) { onConnect.run(); } else { onFail.accept(webSocketClientHandler.handshakeFuture().cause()); diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/ClientWebSocketService.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/ClientWebSocketService.java index 4cd44b77..02959606 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/ClientWebSocketService.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/ClientWebSocketService.java @@ -82,7 +82,7 @@ public void registerRequests() { @SuppressWarnings("deprecation") public void registerResults() { - if(!resultsRegistered) { + if (!resultsRegistered) { results.register("auth", AuthRequestEvent.class); results.register("checkServer", CheckServerRequestEvent.class); results.register("joinServer", JoinServerRequestEvent.class); diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/OfflineRequestService.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/OfflineRequestService.java index fd353447..09aee311 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/OfflineRequestService.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/OfflineRequestService.java @@ -16,26 +16,27 @@ public class OfflineRequestService implements RequestService { private final HashSet eventHandlers = new HashSet<>(); private final Map, RequestProcessor> processors = new ConcurrentHashMap<>(); + @Override @SuppressWarnings("unchecked") public CompletableFuture request(Request request) throws IOException { RequestProcessor> processor = (RequestProcessor>) processors.get(request.getClass()); CompletableFuture future = new CompletableFuture<>(); - if(processor == null) { + if (processor == null) { future.completeExceptionally(new RequestException(String.format("Offline mode not support '%s'", request.getType()))); return future; } - if(LogHelper.isDevEnabled()) { + if (LogHelper.isDevEnabled()) { LogHelper.dev("Request %s: %s", request.getType(), Launcher.gsonManager.gson.toJson(request)); } try { T event = processor.process(request); - if(LogHelper.isDevEnabled()) { + if (LogHelper.isDevEnabled()) { LogHelper.dev("Response %s: %s", event.getType(), Launcher.gsonManager.gson.toJson(event)); } future.complete(event); } catch (Throwable e) { - if(e instanceof RequestException) { + if (e instanceof RequestException) { future.completeExceptionally(e); } else { future.completeExceptionally(new RequestException(e)); @@ -59,11 +60,11 @@ public boolean isClosed() { return false; } - public void registerRequestProcessor(Class requestClazz, RequestProcessor function) { + public void registerRequestProcessor(Class requestClazz, RequestProcessor function) { processors.put(requestClazz, function); } - public void unregisterRequestProcessor(Class> requestClazz) { + public void unregisterRequestProcessor(Class> requestClazz) { processors.remove(requestClazz); } diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/StdWebSocketService.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/StdWebSocketService.java index 9e395db9..ad6212e1 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/StdWebSocketService.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/StdWebSocketService.java @@ -55,7 +55,6 @@ public static CompletableFuture initWebSockets(String addre } - @Deprecated public void registerEventHandler(ClientWebSocketService.EventHandler handler) { legacyEventHandlers.add(handler);