mirror of
https://github.com/GravitLauncher/Launcher
synced 2025-04-01 22:14:01 +03:00
idea cleanup launcherapi
This commit is contained in:
parent
045ed67f12
commit
7c36f341e4
12 changed files with 50 additions and 61 deletions
|
@ -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<String> 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<parts.length-1;++i) {
|
||||
for (int i = 1; i < parts.length - 1; ++i) {
|
||||
int pos = str.indexOf(parts[i], offset);
|
||||
if(pos >= 0) {
|
||||
offset = pos+1;
|
||||
if (pos >= 0) {
|
||||
offset = pos + 1;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -28,17 +28,17 @@ public abstract class Request<R extends WebSocketEvent> 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<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 {
|
||||
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<String, AuthRequestEvent.OAuthRe
|
|||
public R request() throws Exception {
|
||||
if (!started.compareAndSet(false, true))
|
||||
throw new IllegalStateException("Request already started");
|
||||
if(!isAvailable()) {
|
||||
if (!isAvailable()) {
|
||||
throw new RequestException("RequestService not initialized");
|
||||
}
|
||||
return requestDo(requestService);
|
||||
|
@ -224,4 +212,16 @@ public interface ExtendedTokenCallback {
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,9 +6,12 @@
|
|||
|
||||
public interface RequestService {
|
||||
<T extends WebSocketEvent> CompletableFuture<T> request(Request<T> request) throws IOException;
|
||||
|
||||
void registerEventHandler(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 {
|
||||
return request(request).get();
|
||||
} catch (InterruptedException e) {
|
||||
|
|
|
@ -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<LauncherRequestEvent> implements WebSocketRequest {
|
||||
public static final Path BINARY_PATH = IOHelper.getCodeSource(Launcher.class);
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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<ProfileByUsernameRequestEvent> implements WebSocketRequest {
|
||||
@LauncherNetworkAPI
|
||||
|
|
|
@ -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<Throwable> 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());
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -16,26 +16,27 @@
|
|||
public class OfflineRequestService implements RequestService {
|
||||
private final HashSet<EventHandler> eventHandlers = new HashSet<>();
|
||||
private final Map<Class<?>, RequestProcessor<?, ?>> processors = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
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());
|
||||
CompletableFuture<T> 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<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);
|
||||
}
|
||||
|
||||
public<T extends WebSocketEvent> void unregisterRequestProcessor(Class<Request<T>> requestClazz) {
|
||||
public <T extends WebSocketEvent> void unregisterRequestProcessor(Class<Request<T>> requestClazz) {
|
||||
processors.remove(requestClazz);
|
||||
}
|
||||
|
||||
|
|
|
@ -55,7 +55,6 @@ public static CompletableFuture<StdWebSocketService> initWebSockets(String addre
|
|||
}
|
||||
|
||||
|
||||
|
||||
@Deprecated
|
||||
public void registerEventHandler(ClientWebSocketService.EventHandler handler) {
|
||||
legacyEventHandlers.add(handler);
|
||||
|
|
Loading…
Reference in a new issue