Add separate util class

Signed-off-by: TheKodeToad <TheKodeToad@proton.me>
This commit is contained in:
TheKodeToad
2022-11-12 12:55:31 +00:00
parent b544661e81
commit 8a81aaaa0a
5 changed files with 177 additions and 58 deletions

View File

@ -39,10 +39,25 @@ import org.prismlauncher.utils.Parameters;
public interface Fix {
/**
* Gets the name of the fix. If the name isn't passed into the program, the fix
* won't run.
*
* @return The name
*/
String getName();
boolean isApplicable(Parameters parameters);
/**
* Determines whether the fix will be run. This is additional to the name check.
*
* @param params The parameters
* @return <code>true</code> to proceed to applying the fix
*/
boolean isApplicable(Parameters params);
/**
* Applies the fix.
*/
void apply();
}

View File

@ -46,9 +46,10 @@ import java.util.Map;
import javax.xml.bind.DatatypeConverter;
import org.prismlauncher.utils.JsonParser;
import org.prismlauncher.utils.UrlUtils;
@SuppressWarnings("unchecked")
final class SkinFixUrlStreamHandler extends URLStreamHandler {
final class Handler extends URLStreamHandler {
private URL redirect(URL address) throws IOException {
String skinOwner = findSkinOwner(address);
@ -67,27 +68,13 @@ final class SkinFixUrlStreamHandler extends URLStreamHandler {
@Override
protected URLConnection openConnection(URL address) throws IOException {
address = redirect(address);
try {
return SkinFix.openConnection(address);
} catch (RuntimeException | Error e) {
throw e;
} catch (Throwable e) {
throw new IllegalStateException(e);
}
return UrlUtils.openHttpConnection(address);
}
@Override
protected URLConnection openConnection(URL address, Proxy proxy) throws IOException {
address = redirect(address);
try {
return SkinFix.openConnection(address, proxy);
} catch (RuntimeException | Error e) {
throw e;
} catch (Throwable e) {
throw new IllegalStateException(e);
}
return UrlUtils.openHttpConnection(address, proxy);
}
private URL convertSkin(URL defaultUrl, String owner) throws IOException {

View File

@ -35,60 +35,55 @@
package org.prismlauncher.fix.skins;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;
import org.prismlauncher.fix.Fix;
import org.prismlauncher.utils.Parameters;
import org.prismlauncher.utils.UrlUtils;
import org.prismlauncher.utils.logging.Log;
/**
* Fixes skins by redirecting to other URLs.
*
* @see {@link Handler}
* @see {@link UrlUtils}
*/
public final class SkinFix implements Fix, URLStreamHandlerFactory {
private static URLStreamHandler http;
private static MethodHandle openConnection;
private static MethodHandle openConnection2;
static {
try {
Method getURLStreamHandler = URL.class.getDeclaredMethod("getURLStreamHandler", String.class);
getURLStreamHandler.setAccessible(true);
http = (URLStreamHandler) getURLStreamHandler.invoke(null, "http");
Method openConnectionReflect = URLStreamHandler.class.getDeclaredMethod("openConnection", URL.class);
openConnectionReflect.setAccessible(true);
openConnection = MethodHandles.lookup().unreflect(openConnectionReflect);
Method openConnectionReflect2 = URLStreamHandler.class.getDeclaredMethod("openConnection", URL.class,
Proxy.class);
openConnectionReflect2.setAccessible(true);
openConnection2 = MethodHandles.lookup().unreflect(openConnectionReflect2);
} catch (Throwable e) {
Log.error("Could not perform URL reflection; skin fix will not be availble", e);
}
}
static URLConnection openConnection(URL url) throws Throwable {
return (URLConnection) openConnection.invokeExact(http, url);
}
static URLConnection openConnection(URL url, Proxy proxy) throws Throwable {
return (URLConnection) openConnection2.invokeExact(http, url, proxy);
}
@Override
public String getName() {
return "legacySkinFix";
}
@Override
public boolean isApplicable(Parameters parameters) {
return http != null && openConnection != null;
public boolean isApplicable(Parameters params) {
if (!isSupported()) {
Log.warning("Using Java 8 will probably fix this");
Log.warning("Alternatively, turning off legacy skin fix in Settings > Miscellaneous will silence the warnings");
return false;
}
return true;
}
private boolean isSupported() {
// check for DatatypeConverter first
// most users will just be annoyed by the big stacktrace
try {
Class.forName("javax.xml.bind.DatatypeConverter");
} catch (ClassNotFoundException e) {
Log.warning("Cannot find DatatypeConverter - required for skin fix");
return false;
}
if (!UrlUtils.isSupported()) {
Log.warning("Cannot access the necessary Java internals for skin fix");
return false;
}
return true;
}
@Override
@ -99,7 +94,7 @@ public final class SkinFix implements Fix, URLStreamHandlerFactory {
@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
if ("http".equals(protocol))
return new SkinFixUrlStreamHandler();
return new Handler();
return null;
}