mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-13 05:46:59 -04:00
Remove some methods in Lang
This commit is contained in:
parent
e21f315239
commit
2c7ca6d354
@ -44,7 +44,6 @@ import javafx.util.Callback;
|
||||
import javafx.util.Duration;
|
||||
import javafx.util.StringConverter;
|
||||
import org.jackhuang.hmcl.util.*;
|
||||
import org.jackhuang.hmcl.util.function.ExceptionalSupplier;
|
||||
import org.jackhuang.hmcl.util.i18n.I18n;
|
||||
import org.jackhuang.hmcl.util.io.FileUtils;
|
||||
import org.jackhuang.hmcl.util.javafx.SelectedItemProperties;
|
||||
@ -52,6 +51,7 @@ import org.jackhuang.hmcl.util.platform.OperatingSystem;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URI;
|
||||
@ -223,7 +223,11 @@ public final class FXUtils {
|
||||
FXMLLoader loader = new FXMLLoader(node.getClass().getResource(absolutePath), I18n.getResourceBundle());
|
||||
loader.setRoot(node);
|
||||
loader.setController(node);
|
||||
Lang.invoke((ExceptionalSupplier<Object, IOException>) loader::load);
|
||||
try {
|
||||
loader.load();
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void installTooltip(Node node, String tooltip) {
|
||||
|
@ -17,16 +17,16 @@
|
||||
*/
|
||||
package org.jackhuang.hmcl.auth.authlibinjector;
|
||||
|
||||
import static org.jackhuang.hmcl.util.io.NetworkUtils.decodeURL;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import javafx.event.EventHandler;
|
||||
import javafx.scene.input.DragEvent;
|
||||
import javafx.scene.input.Dragboard;
|
||||
import javafx.scene.input.TransferMode;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* @author yushijinhun
|
||||
* @see https://github.com/yushijinhun/authlib-injector/wiki/%E5%90%AF%E5%8A%A8%E5%99%A8%E6%8A%80%E6%9C%AF%E8%A7%84%E8%8C%83#dnd-%E6%96%B9%E5%BC%8F%E6%B7%BB%E5%8A%A0-yggdrasil-%E6%9C%8D%E5%8A%A1%E7%AB%AF
|
||||
@ -44,11 +44,7 @@ public final class AuthlibInjectorDnD {
|
||||
|
||||
String[] uriElements = uri.split(":");
|
||||
if (uriElements.length == 3 && SCHEME.equals(uriElements[0]) && PATH_YGGDRASIL_SERVER.equals(uriElements[1])) {
|
||||
try {
|
||||
return Optional.of(URLDecoder.decode(uriElements[2], "UTF-8"));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
return Optional.of(decodeURL(uriElements[2]));
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
@ -16,7 +16,6 @@ import java.net.URL;
|
||||
import java.util.*;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static org.jackhuang.hmcl.util.Lang.liftFunction;
|
||||
import static org.jackhuang.hmcl.util.Lang.mapOf;
|
||||
import static org.jackhuang.hmcl.util.Pair.pair;
|
||||
|
||||
@ -114,11 +113,15 @@ public class YggdrasilService {
|
||||
public Optional<Map<TextureType, Texture>> getTextures(GameProfile profile) throws AuthenticationException {
|
||||
Objects.requireNonNull(profile);
|
||||
|
||||
return Optional.ofNullable(profile.getProperties())
|
||||
.flatMap(properties -> Optional.ofNullable(properties.get("textures")))
|
||||
.map(encodedTextures -> new String(Base64.getDecoder().decode(encodedTextures), UTF_8))
|
||||
.map(liftFunction(textures -> fromJson(textures, TextureResponse.class)))
|
||||
.flatMap(response -> Optional.ofNullable(response.textures));
|
||||
Optional<String> encodedTextures = Optional.ofNullable(profile.getProperties())
|
||||
.flatMap(properties -> Optional.ofNullable(properties.get("textures")));
|
||||
|
||||
if (encodedTextures.isPresent()) {
|
||||
TextureResponse texturePayload = fromJson(new String(Base64.getDecoder().decode(encodedTextures.get()), UTF_8), TextureResponse.class);
|
||||
return Optional.ofNullable(texturePayload.textures);
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
private static YggdrasilSession handleAuthenticationResponse(String responseText, String clientToken) throws AuthenticationException {
|
||||
|
@ -101,7 +101,7 @@ public class LibraryDownloadTask extends Task {
|
||||
|
||||
try {
|
||||
URL packXz = NetworkUtils.toURL(url + ".pack.xz");
|
||||
if (NetworkUtils.URLExists(packXz)) {
|
||||
if (NetworkUtils.urlExists(packXz)) {
|
||||
task = new FileDownloadTask(packXz, xzFile, null);
|
||||
xz = true;
|
||||
} else {
|
||||
|
@ -19,7 +19,6 @@ package org.jackhuang.hmcl.task;
|
||||
|
||||
import org.jackhuang.hmcl.util.*;
|
||||
import org.jackhuang.hmcl.util.function.ExceptionalRunnable;
|
||||
import org.jackhuang.hmcl.util.function.ExceptionalSupplier;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
@ -79,7 +78,12 @@ public final class TaskExecutor {
|
||||
taskListeners.forEach(it -> it.onStop(flag.get(), this));
|
||||
});
|
||||
workerQueue.add(future);
|
||||
Lang.invoke((ExceptionalSupplier<?, Exception>) future::get);
|
||||
try {
|
||||
future.get();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (ExecutionException | CancellationException e) {
|
||||
}
|
||||
return flag.get();
|
||||
}
|
||||
|
||||
|
@ -20,11 +20,6 @@ package org.jackhuang.hmcl.util;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.jackhuang.hmcl.util.function.ExceptionalConsumer;
|
||||
import org.jackhuang.hmcl.util.function.ExceptionalFunction;
|
||||
import org.jackhuang.hmcl.util.function.ExceptionalRunnable;
|
||||
import org.jackhuang.hmcl.util.function.ExceptionalSupplier;
|
||||
|
||||
@ -57,104 +52,6 @@ public final class Lang {
|
||||
return Collections.unmodifiableList(Arrays.asList(elements));
|
||||
}
|
||||
|
||||
public static <K, V> V computeIfAbsent(Map<K, V> map, K key, Supplier<V> computingFunction) {
|
||||
V value = map.get(key);
|
||||
if (value == null) {
|
||||
V newValue = computingFunction.get();
|
||||
map.put(key, newValue);
|
||||
return newValue;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <E extends Throwable> void throwable(Throwable exception) throws E {
|
||||
throw (E) exception;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will call a method without checked exceptions
|
||||
* by treating the compiler.
|
||||
*
|
||||
* If this method throws a checked exception,
|
||||
* it will still abort the application because of the exception.
|
||||
*
|
||||
* @param <T> type of argument.
|
||||
* @param <R> type of result.
|
||||
* @param function your method.
|
||||
* @return the result of the method to invoke.
|
||||
*/
|
||||
public static <T, R, E extends Exception> R invoke(ExceptionalFunction<T, R, E> function, T t) {
|
||||
try {
|
||||
return function.apply(t);
|
||||
} catch (Exception e) {
|
||||
throwable(e);
|
||||
throw new Error(); // won't get to here.
|
||||
}
|
||||
}
|
||||
|
||||
public static <T, R, E extends Exception> Function<T, R> hideFunction(ExceptionalFunction<T, R, E> function) {
|
||||
return r -> invoke(function, r);
|
||||
}
|
||||
|
||||
public static <T, R, E extends Exception> Function<T, R> liftFunction(ExceptionalFunction<T, R, E> function) throws E {
|
||||
return hideFunction(function);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will call a method without checked exceptions
|
||||
* by treating the compiler.
|
||||
*
|
||||
* If this method throws a checked exception,
|
||||
* it will still abort the application because of the exception.
|
||||
*
|
||||
* @param <T> type of result.
|
||||
* @param supplier your method.
|
||||
* @return the result of the method to invoke.
|
||||
*/
|
||||
public static <T, E extends Exception> T invoke(ExceptionalSupplier<T, E> supplier) {
|
||||
try {
|
||||
return supplier.get();
|
||||
} catch (Exception e) {
|
||||
throwable(e);
|
||||
throw new Error(); // won't get to here.
|
||||
}
|
||||
}
|
||||
|
||||
public static <T, E extends Exception> Supplier<T> hideException(ExceptionalSupplier<T, E> supplier) {
|
||||
return () -> invoke(supplier);
|
||||
}
|
||||
|
||||
public static <T, E extends Exception> Supplier<T> liftException(ExceptionalSupplier<T, E> supplier) throws E {
|
||||
return hideException(supplier);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will call a method without checked exceptions
|
||||
* by treating the compiler.
|
||||
*
|
||||
* If this method throws a checked exception,
|
||||
* it will still abort the application because of the exception.
|
||||
*
|
||||
* @param <T> type of result.
|
||||
* @param consumer your method.
|
||||
*/
|
||||
public static <T, E extends Exception> void invokeConsumer(ExceptionalConsumer<T, E> consumer, T t) {
|
||||
try {
|
||||
consumer.accept(t);
|
||||
} catch (Exception e) {
|
||||
throwable(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T, E extends Exception> Consumer<T> hideConsumer(ExceptionalConsumer<T, E> consumer) {
|
||||
return it -> invokeConsumer(consumer, it);
|
||||
}
|
||||
|
||||
public static <T, E extends Exception> Consumer<T> liftConsumer(ExceptionalConsumer<T, E> consumer) throws E {
|
||||
return hideConsumer(consumer);
|
||||
}
|
||||
|
||||
public static <E extends Exception> boolean test(ExceptionalSupplier<Boolean, E> r) {
|
||||
try {
|
||||
return r.get();
|
||||
@ -163,40 +60,6 @@ public final class Lang {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will call a method without checked exceptions
|
||||
* by treating the compiler.
|
||||
*
|
||||
* If this method throws a checked exception,
|
||||
* it will still abort the application because of the exception.
|
||||
*
|
||||
* @param r your method.
|
||||
*/
|
||||
public static <E extends Exception> void invoke(ExceptionalRunnable<E> r) {
|
||||
try {
|
||||
r.run();
|
||||
} catch (Exception e) {
|
||||
throwable(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <E extends Exception> Runnable hideException(ExceptionalRunnable<E> r) {
|
||||
return () -> invoke(r);
|
||||
}
|
||||
|
||||
public static <E extends Exception> Runnable liftException(ExceptionalRunnable<E> r) {
|
||||
return hideException(r);
|
||||
}
|
||||
|
||||
public static <E extends Exception> boolean test(ExceptionalRunnable<E> r) {
|
||||
try {
|
||||
r.run();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T ignoringException(ExceptionalSupplier<T, ?> supplier) {
|
||||
return ignoringException(supplier, null);
|
||||
}
|
||||
@ -309,24 +172,6 @@ public final class Lang {
|
||||
return thread;
|
||||
}
|
||||
|
||||
public static <T> Iterator<T> asIterator(Enumeration<T> enumeration) {
|
||||
return new Iterator<T>() {
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return enumeration.hasMoreElements();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next() {
|
||||
return enumeration.nextElement();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static <T> Iterable<T> asIterable(Enumeration<T> enumeration) {
|
||||
return () -> asIterator(enumeration);
|
||||
}
|
||||
|
||||
public static int parseInt(Object string, int defaultValue) {
|
||||
try {
|
||||
return Integer.parseInt(string.toString());
|
||||
|
@ -62,7 +62,7 @@ public final class SimpleMultimap<K, V> {
|
||||
}
|
||||
|
||||
public Collection<V> get(K key) {
|
||||
return Lang.computeIfAbsent(map, key, valuer);
|
||||
return map.computeIfAbsent(key, any -> valuer.get());
|
||||
}
|
||||
|
||||
public void put(K key, V value) {
|
||||
|
@ -113,7 +113,12 @@ public final class FileUtils {
|
||||
}
|
||||
|
||||
public static boolean deleteDirectoryQuietly(File directory) {
|
||||
return Lang.test(() -> deleteDirectory(directory));
|
||||
try {
|
||||
deleteDirectory(directory);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void copyDirectory(Path src, Path dest) throws IOException {
|
||||
|
@ -19,12 +19,9 @@ package org.jackhuang.hmcl.util.io;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.jackhuang.hmcl.util.Lang;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static org.jackhuang.hmcl.util.StringUtils.*;
|
||||
|
||||
@ -125,13 +122,18 @@ public final class NetworkUtils {
|
||||
String disposition = conn.getHeaderField("Content-Disposition");
|
||||
if (disposition == null || !disposition.contains("filename=")) {
|
||||
String u = conn.getURL().toString();
|
||||
return Lang.invoke(() -> URLDecoder.decode(substringAfterLast(u, '/'), StandardCharsets.UTF_8.name()));
|
||||
} else
|
||||
return Lang.invoke(() -> URLDecoder.decode(removeSurrounding(substringAfter(disposition, "filename="), "\""), StandardCharsets.UTF_8.name()));
|
||||
return decodeURL(substringAfterLast(u, '/'));
|
||||
} else {
|
||||
return decodeURL(removeSurrounding(substringAfter(disposition, "filename="), "\""));
|
||||
}
|
||||
}
|
||||
|
||||
public static URL toURL(String str) {
|
||||
return Lang.invoke(() -> new URL(str));
|
||||
try {
|
||||
return new URL(str);
|
||||
} catch (MalformedURLException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isURL(String str) {
|
||||
@ -143,11 +145,29 @@ public final class NetworkUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean URLExists(URL url) throws IOException {
|
||||
public static boolean urlExists(URL url) throws IOException {
|
||||
try (InputStream stream = url.openStream()) {
|
||||
return true;
|
||||
} catch (FileNotFoundException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// ==== Shortcut methods for encoding/decoding URLs in UTF-8 ====
|
||||
public static String encodeURL(String toEncode) {
|
||||
try {
|
||||
return URLEncoder.encode(toEncode, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new Error();
|
||||
}
|
||||
}
|
||||
|
||||
public static String decodeURL(String toDecode) {
|
||||
try {
|
||||
return URLDecoder.decode(toDecode, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new Error();
|
||||
}
|
||||
}
|
||||
// ====
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user