idea cleanup launcherapi

This commit is contained in:
dima_dencep 2022-09-24 00:29:22 +07:00
parent 045ed67f12
commit 7c36f341e4
12 changed files with 50 additions and 61 deletions

View file

@ -1,8 +1,5 @@
package pro.gravit.launcher; package pro.gravit.launcher;
import pro.gravit.launcher.serialize.HInput;
import java.io.IOException;
import java.util.*; import java.util.*;
public class ClientPermissions { public class ClientPermissions {
@ -71,7 +68,7 @@ public void addPerm(String perm) {
perms = new ArrayList<>(1); perms = new ArrayList<>(1);
} }
perms.add(perm); perms.add(perm);
if(available == null) { if (available == null) {
available = new ArrayList<>(1); available = new ArrayList<>(1);
} }
available.add(new PermissionPattern(perm)); available.add(new PermissionPattern(perm));
@ -81,7 +78,7 @@ public void removePerm(String action) {
if (perms == null) { if (perms == null) {
return; return;
} }
if(available == null) { if (available == null) {
return; return;
} }
perms.remove(action); perms.remove(action);
@ -110,11 +107,11 @@ public static class PermissionPattern {
public PermissionPattern(String pattern) { public PermissionPattern(String pattern) {
List<String> prepare = new ArrayList<>(); List<String> prepare = new ArrayList<>();
for(int i=0;true;) { for (int i = 0; true; ) {
int pos = pattern.indexOf("*", i); int pos = pattern.indexOf("*", i);
if(pos >= 0) { if (pos >= 0) {
prepare.add(pattern.substring(i, pos)); prepare.add(pattern.substring(i, pos));
i = pos+1; i = pos + 1;
} else { } else {
prepare.add(pattern.substring(i)); prepare.add(pattern.substring(i));
break; break;
@ -129,23 +126,23 @@ public int getPriority() {
} }
public boolean match(String str) { public boolean match(String str) {
if(parts.length == 0) { if (parts.length == 0) {
return true; return true;
} }
if(parts.length == 1) { if (parts.length == 1) {
return parts[0].equals(str); return parts[0].equals(str);
} }
int offset = 0; int offset = 0;
if(!str.startsWith(parts[0])) { if (!str.startsWith(parts[0])) {
return false; return false;
} }
if(!str.endsWith(parts[parts.length-1])) { if (!str.endsWith(parts[parts.length - 1])) {
return false; return false;
} }
for(int i=1;i<parts.length-1;++i) { for (int i = 1; i < parts.length - 1; ++i) {
int pos = str.indexOf(parts[i], offset); int pos = str.indexOf(parts[i], offset);
if(pos >= 0) { if (pos >= 0) {
offset = pos+1; offset = pos + 1;
} else { } else {
return false; return false;
} }

View file

@ -1,7 +1,6 @@
package pro.gravit.launcher.profiles; package pro.gravit.launcher.profiles;
import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.VerifyHelper;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -30,10 +29,10 @@ public PlayerProfile(UUID uuid, String username, Texture skin, Texture cloak, Ma
this.skin = skin; this.skin = skin;
this.cloak = cloak; this.cloak = cloak;
this.assets = new HashMap<>(); this.assets = new HashMap<>();
if(skin != null) { if (skin != null) {
this.assets.put("SKIN", skin); this.assets.put("SKIN", skin);
} }
if(cloak != null) { if (cloak != null) {
this.assets.put("CAPE", cloak); this.assets.put("CAPE", cloak);
} }
this.properties = properties; this.properties = properties;

View file

@ -5,6 +5,7 @@
public class ArchTrigger extends OptionalTrigger { public class ArchTrigger extends OptionalTrigger {
public JVMHelper.ARCH arch; public JVMHelper.ARCH arch;
@Override @Override
protected boolean isTriggered(OptionalFile optional, OptionalTriggerContext context) { protected boolean isTriggered(OptionalFile optional, OptionalTriggerContext context) {
return context.getJavaVersion().arch == arch; return context.getJavaVersion().arch == arch;

View file

@ -28,17 +28,17 @@ public abstract class Request<R extends WebSocketEvent> implements WebSocketRequ
public final UUID requestUUID = UUID.randomUUID(); public final UUID requestUUID = UUID.randomUUID();
private transient final AtomicBoolean started = new AtomicBoolean(false); 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() { public static RequestService getRequestService() {
return requestService; return requestService;
} }
public static void setRequestService(RequestService service) {
requestService = service;
if (service instanceof StdWebSocketService) {
Request.service = (StdWebSocketService) service;
}
}
public static boolean isAvailable() { public static boolean isAvailable() {
return requestService != null; return requestService != null;
} }
@ -121,22 +121,10 @@ public static RequestRestoreReport reconnect() throws Exception {
return restore(); return restore();
} }
public static class RequestRestoreReport {
public final boolean legacySession;
public final boolean refreshed;
public final List<String> invalidExtendedTokens;
public RequestRestoreReport(boolean legacySession, boolean refreshed, List<String> invalidExtendedTokens) {
this.legacySession = legacySession;
this.refreshed = refreshed;
this.invalidExtendedTokens = invalidExtendedTokens;
}
}
public static RequestRestoreReport restore() throws Exception { public static RequestRestoreReport restore() throws Exception {
boolean refreshed = false; boolean refreshed = false;
RestoreRequest request; RestoreRequest request;
if(oauth != null) { if (oauth != null) {
if (isTokenExpired() || oauth.accessToken == null) { if (isTokenExpired() || oauth.accessToken == null) {
RefreshTokenRequest refreshRequest = new RefreshTokenRequest(authId, oauth.refreshToken); RefreshTokenRequest refreshRequest = new RefreshTokenRequest(authId, oauth.refreshToken);
RefreshTokenRequestEvent event = refreshRequest.request(); RefreshTokenRequestEvent event = refreshRequest.request();
@ -197,7 +185,7 @@ public void removeOAuthChangeHandler(BiConsumer<String, AuthRequestEvent.OAuthRe
public R request() throws Exception { public R request() throws Exception {
if (!started.compareAndSet(false, true)) if (!started.compareAndSet(false, true))
throw new IllegalStateException("Request already started"); throw new IllegalStateException("Request already started");
if(!isAvailable()) { if (!isAvailable()) {
throw new RequestException("RequestService not initialized"); throw new RequestException("RequestService not initialized");
} }
return requestDo(requestService); return requestDo(requestService);
@ -224,4 +212,16 @@ public interface ExtendedTokenCallback {
String tryGetNewToken(String name); String tryGetNewToken(String name);
} }
public static class RequestRestoreReport {
public final boolean legacySession;
public final boolean refreshed;
public final List<String> invalidExtendedTokens;
public RequestRestoreReport(boolean legacySession, boolean refreshed, List<String> invalidExtendedTokens) {
this.legacySession = legacySession;
this.refreshed = refreshed;
this.invalidExtendedTokens = invalidExtendedTokens;
}
}
} }

View file

@ -6,8 +6,11 @@
public interface RequestService { public interface RequestService {
<T extends WebSocketEvent> CompletableFuture<T> request(Request<T> request) throws IOException; <T extends WebSocketEvent> CompletableFuture<T> request(Request<T> request) throws IOException;
void registerEventHandler(EventHandler handler); void registerEventHandler(EventHandler handler);
void unregisterEventHandler(EventHandler handler); void unregisterEventHandler(EventHandler handler);
default <T extends WebSocketEvent> T requestSync(Request<T> request) throws IOException { default <T extends WebSocketEvent> T requestSync(Request<T> request) throws IOException {
try { try {
return request(request).get(); return request(request).get();

View file

@ -5,21 +5,13 @@
import pro.gravit.launcher.events.request.LauncherRequestEvent; import pro.gravit.launcher.events.request.LauncherRequestEvent;
import pro.gravit.launcher.request.Request; import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.RequestService; import pro.gravit.launcher.request.RequestService;
import pro.gravit.launcher.request.websockets.StdWebSocketService;
import pro.gravit.launcher.request.websockets.WebSocketRequest; import pro.gravit.launcher.request.websockets.WebSocketRequest;
import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.JVMHelper;
import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.LogHelper;
import pro.gravit.utils.helper.SecurityHelper; import pro.gravit.utils.helper.SecurityHelper;
import java.io.IOException; 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.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
public final class LauncherRequest extends Request<LauncherRequestEvent> implements WebSocketRequest { public final class LauncherRequest extends Request<LauncherRequestEvent> implements WebSocketRequest {
public static final Path BINARY_PATH = IOHelper.getCodeSource(Launcher.class); public static final Path BINARY_PATH = IOHelper.getCodeSource(Launcher.class);

View file

@ -5,7 +5,6 @@
import pro.gravit.launcher.request.Request; import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.websockets.WebSocketRequest; import pro.gravit.launcher.request.websockets.WebSocketRequest;
import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.VerifyHelper;
import java.io.IOException; import java.io.IOException;

View file

@ -4,7 +4,6 @@
import pro.gravit.launcher.events.request.ProfileByUsernameRequestEvent; import pro.gravit.launcher.events.request.ProfileByUsernameRequestEvent;
import pro.gravit.launcher.request.Request; import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.websockets.WebSocketRequest; import pro.gravit.launcher.request.websockets.WebSocketRequest;
import pro.gravit.utils.helper.VerifyHelper;
public final class ProfileByUsernameRequest extends Request<ProfileByUsernameRequestEvent> implements WebSocketRequest { public final class ProfileByUsernameRequest extends Request<ProfileByUsernameRequestEvent> implements WebSocketRequest {
@LauncherNetworkAPI @LauncherNetworkAPI

View file

@ -24,7 +24,6 @@
import java.security.KeyStoreException; import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -112,10 +111,10 @@ public void openAsync(Runnable onConnect, Consumer<Throwable> onFail) {
uri, WebSocketVersion.V13, null, false, EmptyHttpHeaders.INSTANCE, 12800000), this); uri, WebSocketVersion.V13, null, false, EmptyHttpHeaders.INSTANCE, 12800000), this);
ChannelFuture future = bootstrap.connect(uri.getHost(), port); ChannelFuture future = bootstrap.connect(uri.getHost(), port);
future.addListener((l) -> { future.addListener((l) -> {
if(l.isSuccess()) { if (l.isSuccess()) {
ch = future.channel(); ch = future.channel();
webSocketClientHandler.handshakeFuture().addListener((e) -> { webSocketClientHandler.handshakeFuture().addListener((e) -> {
if(e.isSuccess()) { if (e.isSuccess()) {
onConnect.run(); onConnect.run();
} else { } else {
onFail.accept(webSocketClientHandler.handshakeFuture().cause()); onFail.accept(webSocketClientHandler.handshakeFuture().cause());

View file

@ -82,7 +82,7 @@ public void registerRequests() {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public void registerResults() { public void registerResults() {
if(!resultsRegistered) { if (!resultsRegistered) {
results.register("auth", AuthRequestEvent.class); results.register("auth", AuthRequestEvent.class);
results.register("checkServer", CheckServerRequestEvent.class); results.register("checkServer", CheckServerRequestEvent.class);
results.register("joinServer", JoinServerRequestEvent.class); results.register("joinServer", JoinServerRequestEvent.class);

View file

@ -16,26 +16,27 @@
public class OfflineRequestService implements RequestService { public class OfflineRequestService implements RequestService {
private final HashSet<EventHandler> eventHandlers = new HashSet<>(); private final HashSet<EventHandler> eventHandlers = new HashSet<>();
private final Map<Class<?>, RequestProcessor<?, ?>> processors = new ConcurrentHashMap<>(); private final Map<Class<?>, RequestProcessor<?, ?>> processors = new ConcurrentHashMap<>();
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T extends WebSocketEvent> CompletableFuture<T> request(Request<T> request) throws IOException { public <T extends WebSocketEvent> CompletableFuture<T> request(Request<T> request) throws IOException {
RequestProcessor<T, Request<T>> processor = (RequestProcessor<T, Request<T>>) processors.get(request.getClass()); RequestProcessor<T, Request<T>> processor = (RequestProcessor<T, Request<T>>) processors.get(request.getClass());
CompletableFuture<T> future = new CompletableFuture<>(); CompletableFuture<T> future = new CompletableFuture<>();
if(processor == null) { if (processor == null) {
future.completeExceptionally(new RequestException(String.format("Offline mode not support '%s'", request.getType()))); future.completeExceptionally(new RequestException(String.format("Offline mode not support '%s'", request.getType())));
return future; return future;
} }
if(LogHelper.isDevEnabled()) { if (LogHelper.isDevEnabled()) {
LogHelper.dev("Request %s: %s", request.getType(), Launcher.gsonManager.gson.toJson(request)); LogHelper.dev("Request %s: %s", request.getType(), Launcher.gsonManager.gson.toJson(request));
} }
try { try {
T event = processor.process(request); T event = processor.process(request);
if(LogHelper.isDevEnabled()) { if (LogHelper.isDevEnabled()) {
LogHelper.dev("Response %s: %s", event.getType(), Launcher.gsonManager.gson.toJson(event)); LogHelper.dev("Response %s: %s", event.getType(), Launcher.gsonManager.gson.toJson(event));
} }
future.complete(event); future.complete(event);
} catch (Throwable e) { } catch (Throwable e) {
if(e instanceof RequestException) { if (e instanceof RequestException) {
future.completeExceptionally(e); future.completeExceptionally(e);
} else { } else {
future.completeExceptionally(new RequestException(e)); future.completeExceptionally(new RequestException(e));
@ -59,11 +60,11 @@ public boolean isClosed() {
return false; return false;
} }
public<T extends WebSocketEvent, V extends WebSocketRequest> void registerRequestProcessor(Class<V> requestClazz, RequestProcessor<T, V> function) { public <T extends WebSocketEvent, V extends WebSocketRequest> void registerRequestProcessor(Class<V> requestClazz, RequestProcessor<T, V> function) {
processors.put(requestClazz, function); processors.put(requestClazz, function);
} }
public<T extends WebSocketEvent> void unregisterRequestProcessor(Class<Request<T>> requestClazz) { public <T extends WebSocketEvent> void unregisterRequestProcessor(Class<Request<T>> requestClazz) {
processors.remove(requestClazz); processors.remove(requestClazz);
} }

View file

@ -55,7 +55,6 @@ public static CompletableFuture<StdWebSocketService> initWebSockets(String addre
} }
@Deprecated @Deprecated
public void registerEventHandler(ClientWebSocketService.EventHandler handler) { public void registerEventHandler(ClientWebSocketService.EventHandler handler) {
legacyEventHandlers.add(handler); legacyEventHandlers.add(handler);