@@ -47,6 +47,7 @@ public class OfflineAccount extends Account {
|
||||
throw new IllegalArgumentException("Username cannot be blank");
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUUID() {
|
||||
return UUIDTypeAdapter.fromString(uuid);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package org.jackhuang.hmcl.auth.yggdrasil;
|
||||
|
||||
import com.google.gson.JsonParseException;
|
||||
import org.jackhuang.hmcl.util.Constants;
|
||||
import org.jackhuang.hmcl.util.Immutable;
|
||||
import org.jackhuang.hmcl.util.Lang;
|
||||
import org.jackhuang.hmcl.util.JsonUtils;
|
||||
import org.jackhuang.hmcl.util.NetworkUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -36,7 +35,7 @@ public final class AuthlibInjectorBuildInfo {
|
||||
}
|
||||
|
||||
public static AuthlibInjectorBuildInfo requestBuildInfo(String updateUrl) throws IOException, JsonParseException {
|
||||
return Lang.requireJsonNonNull(Constants.GSON.fromJson(NetworkUtils.doGet(NetworkUtils.toURL(updateUrl)), AuthlibInjectorBuildInfo.class));
|
||||
return JsonUtils.fromNonNullJson(NetworkUtils.doGet(NetworkUtils.toURL(updateUrl)), AuthlibInjectorBuildInfo.class);
|
||||
}
|
||||
|
||||
public static final String UPDATE_URL = "https://authlib-injector.to2mbn.org/api/buildInfo";
|
||||
|
||||
@@ -198,7 +198,7 @@ public class DefaultLauncher extends Launcher {
|
||||
}
|
||||
|
||||
private final Map<String, Supplier<Boolean>> forbiddens = Lang.mapOf(
|
||||
new Pair<>("-Xincgc", () -> options.getJava().getParsedVersion() >= JavaVersion.JAVA_9)
|
||||
new Pair<String, Supplier<Boolean>>("-Xincgc", () -> options.getJava().getParsedVersion() >= JavaVersion.JAVA_9)
|
||||
);
|
||||
|
||||
protected Map<String, Supplier<Boolean>> getForbiddens() {
|
||||
|
||||
@@ -20,15 +20,13 @@ package org.jackhuang.hmcl.mod;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import org.jackhuang.hmcl.util.CompressingUtils;
|
||||
import org.jackhuang.hmcl.util.Constants;
|
||||
import org.jackhuang.hmcl.util.Immutable;
|
||||
import org.jackhuang.hmcl.util.Lang;
|
||||
import org.jackhuang.hmcl.util.JsonUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -120,7 +118,7 @@ public final class CurseManifest {
|
||||
*/
|
||||
public static Modpack readCurseForgeModpackManifest(File f) throws IOException, JsonParseException {
|
||||
String json = CompressingUtils.readTextZipEntry(f, "manifest.json");
|
||||
CurseManifest manifest = Lang.requireJsonNonNull(Constants.GSON.fromJson(json, CurseManifest.class));
|
||||
CurseManifest manifest = JsonUtils.fromNonNullJson(json, CurseManifest.class);
|
||||
return new Modpack(manifest.getName(), manifest.getAuthor(), manifest.getVersion(), manifest.getMinecraft().getGameVersion(),
|
||||
CompressingUtils.readTextZipEntryQuietly(f, "modlist.html").orElse( "No description"), manifest);
|
||||
}
|
||||
|
||||
@@ -94,4 +94,8 @@ public final class Constants {
|
||||
public static <T> Predicate<T> falsePredicate() {
|
||||
return s -> false;
|
||||
}
|
||||
|
||||
public static <T> Consumer<T> emptyConsumer() {
|
||||
return x -> {};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ public class ImmediateBooleanProperty extends SimpleBooleanProperty {
|
||||
super.unbind();
|
||||
}
|
||||
|
||||
private Consumer<Boolean> listener = Lang.EMPTY_CONSUMER;
|
||||
private Consumer<Boolean> listener = Constants.emptyConsumer();
|
||||
private final ChangeListener<Boolean> changeListener = (a, b, newValue) -> listener.accept(newValue);
|
||||
|
||||
public void setChangedListener(Consumer<Boolean> listener) {
|
||||
|
||||
@@ -49,7 +49,7 @@ public class ImmediateDoubleProperty extends SimpleDoubleProperty {
|
||||
super.unbind();
|
||||
}
|
||||
|
||||
private Consumer<Double> listener = Lang.EMPTY_CONSUMER;
|
||||
private Consumer<Double> listener = Constants.emptyConsumer();
|
||||
private final ChangeListener<Number> changeListener = (a, b, newValue) -> listener.accept(newValue.doubleValue());
|
||||
|
||||
public void setChangedListener(Consumer<Double> listener) {
|
||||
|
||||
@@ -48,7 +48,7 @@ public class ImmediateIntegerProperty extends SimpleIntegerProperty {
|
||||
super.unbind();
|
||||
}
|
||||
|
||||
private Consumer<Integer> listener = Lang.EMPTY_CONSUMER;
|
||||
private Consumer<Integer> listener = Constants.emptyConsumer();
|
||||
private final ChangeListener<Number> changeListener = (a, b, newValue) -> listener.accept(newValue.intValue());
|
||||
|
||||
public void setChangedListener(Consumer<Integer> listener) {
|
||||
|
||||
@@ -48,7 +48,7 @@ public class ImmediateObjectProperty<T> extends SimpleObjectProperty<T> {
|
||||
super.unbind();
|
||||
}
|
||||
|
||||
private Consumer<T> listener = Lang.EMPTY_CONSUMER;
|
||||
private Consumer<T> listener = Constants.emptyConsumer();
|
||||
private final ChangeListener<T> changeListener = (a, b, newValue) -> listener.accept(newValue);
|
||||
|
||||
public void setChangedListener(Consumer<T> listener) {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.jackhuang.hmcl.util;
|
||||
|
||||
import com.google.gson.JsonParseException;
|
||||
|
||||
public final class JsonUtils {
|
||||
|
||||
private JsonUtils() {}
|
||||
|
||||
public static <T> T fromNonNullJson(String json, Class<T> classOfT) throws JsonParseException {
|
||||
T parsed = Constants.GSON.fromJson(json, classOfT);
|
||||
if (parsed == null)
|
||||
throw new JsonParseException("Json object cannot be null.");
|
||||
return parsed;
|
||||
}
|
||||
}
|
||||
@@ -5,8 +5,6 @@
|
||||
*/
|
||||
package org.jackhuang.hmcl.util;
|
||||
|
||||
import com.google.gson.JsonParseException;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Consumer;
|
||||
@@ -22,19 +20,7 @@ public final class Lang {
|
||||
private Lang() {
|
||||
}
|
||||
|
||||
public static final Consumer EMPTY_CONSUMER = a -> {
|
||||
};
|
||||
|
||||
public static <T> T requireJsonNonNull(T obj) throws JsonParseException {
|
||||
return requireJsonNonNull(obj, "Json object cannot be null.");
|
||||
}
|
||||
|
||||
public static <T> T requireJsonNonNull(T obj, String message) throws JsonParseException {
|
||||
if (obj == null)
|
||||
throw new JsonParseException(message);
|
||||
return obj;
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public static <K, V> Map<K, V> mapOf(Pair<K, V>... pairs) {
|
||||
HashMap<K, V> map = new HashMap<>();
|
||||
for (Pair<K, V> pair : pairs)
|
||||
@@ -42,14 +28,14 @@ public final class Lang {
|
||||
return map;
|
||||
}
|
||||
|
||||
public static <K, V> V getOrPut(Map<K, V> map, K key, Supplier<V> defaultValue) {
|
||||
public static <K, V> V computeIfAbsent(Map<K, V> map, K key, Supplier<V> computingFunction) {
|
||||
V value = map.get(key);
|
||||
if (value == null) {
|
||||
V answer = defaultValue.get();
|
||||
map.put(key, answer);
|
||||
return answer;
|
||||
} else
|
||||
return value;
|
||||
V newValue = computingFunction.get();
|
||||
map.put(key, newValue);
|
||||
return newValue;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
public static <E extends Throwable> void throwable(Throwable exception) throws E {
|
||||
@@ -230,8 +216,8 @@ public final class Lang {
|
||||
return convert(map.get(key), clazz, defaultValue);
|
||||
}
|
||||
|
||||
public static <T> List<T> merge(Collection<T> a, Collection<T> b) {
|
||||
LinkedList<T> result = new LinkedList<>();
|
||||
public static <T> List<T> merge(Collection<? extends T> a, Collection<? extends T> b) {
|
||||
List<T> result = new ArrayList<>();
|
||||
if (a != null)
|
||||
result.addAll(a);
|
||||
if (b != null)
|
||||
@@ -239,17 +225,6 @@ public final class Lang {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static <T> List<T> merge(Collection<T> a, Collection<T> b, Collection<T> c) {
|
||||
LinkedList<T> result = new LinkedList<>();
|
||||
if (a != null)
|
||||
result.addAll(a);
|
||||
if (b != null)
|
||||
result.addAll(b);
|
||||
if (c != null)
|
||||
result.addAll(c);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void executeDelayed(Runnable runnable, TimeUnit timeUnit, long timeout, boolean isDaemon) {
|
||||
thread(() -> {
|
||||
try {
|
||||
@@ -322,6 +297,7 @@ public final class Lang {
|
||||
}
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public static <T> T nonNull(T... t) {
|
||||
for (T a : t) if (a != null) return a;
|
||||
return null;
|
||||
|
||||
@@ -17,10 +17,6 @@
|
||||
*/
|
||||
package org.jackhuang.hmcl.util;
|
||||
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
@@ -28,9 +24,6 @@ import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.Proxy;
|
||||
import java.net.URL;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Supplier;
|
||||
@@ -46,41 +39,6 @@ public final class NetworkUtils {
|
||||
private NetworkUtils() {
|
||||
}
|
||||
|
||||
private static final X509TrustManager XTM = new X509TrustManager() {
|
||||
@Override
|
||||
public void checkClientTrusted(X509Certificate[] xcs, String string) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkServerTrusted(X509Certificate[] xcs, String string) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return new X509Certificate[0];
|
||||
}
|
||||
};
|
||||
|
||||
private static final HostnameVerifier HNV = (a, b) -> true;
|
||||
|
||||
private static volatile boolean initHttps = false;
|
||||
|
||||
private static synchronized void initHttps() {
|
||||
if (initHttps)
|
||||
return;
|
||||
|
||||
initHttps = true;
|
||||
|
||||
System.setProperty("https.protocols", "SSLv3,TLSv1");
|
||||
try {
|
||||
SSLContext c = SSLContext.getInstance("SSL");
|
||||
c.init(null, new X509TrustManager[] { XTM }, new SecureRandom());
|
||||
HttpsURLConnection.setDefaultSSLSocketFactory(c.getSocketFactory());
|
||||
} catch (GeneralSecurityException ignore) {
|
||||
}
|
||||
HttpsURLConnection.setDefaultHostnameVerifier(HNV);
|
||||
}
|
||||
|
||||
private static Supplier<String> userAgentSupplier = RandomUserAgent::randomUserAgent;
|
||||
|
||||
public static String getUserAgent() {
|
||||
@@ -92,7 +50,6 @@ public final class NetworkUtils {
|
||||
}
|
||||
|
||||
public static HttpURLConnection createConnection(URL url, Proxy proxy) throws IOException {
|
||||
initHttps();
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
|
||||
connection.setDoInput(true);
|
||||
connection.setUseCaches(false);
|
||||
|
||||
@@ -3,6 +3,9 @@ package org.jackhuang.hmcl.util;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Optifine has blocked Java's useragent, so we have to make up one.
|
||||
*/
|
||||
public class RandomUserAgent {
|
||||
|
||||
private static final HashMap<String, String[]> uaMap = new HashMap<>();
|
||||
|
||||
@@ -52,14 +52,14 @@ public final class ReflectionHelper {
|
||||
}
|
||||
|
||||
PRIMITIVES = Lang.mapOf(
|
||||
new Pair("byte", Byte.class),
|
||||
new Pair("short", Short.class),
|
||||
new Pair("int", Integer.class),
|
||||
new Pair("long", Long.class),
|
||||
new Pair("char", Character.class),
|
||||
new Pair("float", Float.class),
|
||||
new Pair("double", Double.class),
|
||||
new Pair("boolean", Boolean.class)
|
||||
new Pair<>("byte", Byte.class),
|
||||
new Pair<>("short", Short.class),
|
||||
new Pair<>("int", Integer.class),
|
||||
new Pair<>("long", Long.class),
|
||||
new Pair<>("char", Character.class),
|
||||
new Pair<>("float", Float.class),
|
||||
new Pair<>("double", Double.class),
|
||||
new Pair<>("boolean", Boolean.class)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -62,7 +62,7 @@ public final class SimpleMultimap<K, V> {
|
||||
}
|
||||
|
||||
public Collection<V> get(K key) {
|
||||
return Lang.getOrPut(map, key, valuer);
|
||||
return Lang.computeIfAbsent(map, key, valuer);
|
||||
}
|
||||
|
||||
public void put(K key, V value) {
|
||||
|
||||
Reference in New Issue
Block a user