From 7534eaf006e030f2f446a3c793090d7640fb2361 Mon Sep 17 00:00:00 2001 From: TheKodeToad Date: Sat, 12 Nov 2022 14:48:42 +0000 Subject: [PATCH] Only use DatatypeConverter as a fallback Signed-off-by: TheKodeToad --- libraries/launcher/CMakeLists.txt | 2 + .../org/prismlauncher/fix/skins/Handler.java | 5 +- .../org/prismlauncher/fix/skins/SkinFix.java | 21 +------ .../org/prismlauncher/utils/Base64.java | 59 +++++++++++++++++++ .../org/prismlauncher/utils/JsonParser.java | 2 +- .../org/prismlauncher/utils/UrlUtils.java | 2 +- 6 files changed, 67 insertions(+), 24 deletions(-) create mode 100644 libraries/launcher/org/prismlauncher/utils/Base64.java diff --git a/libraries/launcher/CMakeLists.txt b/libraries/launcher/CMakeLists.txt index d022d2532..acc276218 100644 --- a/libraries/launcher/CMakeLists.txt +++ b/libraries/launcher/CMakeLists.txt @@ -19,6 +19,8 @@ set(SRC org/prismlauncher/fix/Fixes.java org/prismlauncher/fix/skins/SkinFix.java org/prismlauncher/fix/skins/Handler.java + org/prismlauncher/utils/Base64.java + org/prismlauncher/utils/JsonParser.java org/prismlauncher/utils/Parameters.java org/prismlauncher/utils/ReflectionUtils.java org/prismlauncher/utils/UrlUtils.java diff --git a/libraries/launcher/org/prismlauncher/fix/skins/Handler.java b/libraries/launcher/org/prismlauncher/fix/skins/Handler.java index 26a3d205e..2bffeae96 100644 --- a/libraries/launcher/org/prismlauncher/fix/skins/Handler.java +++ b/libraries/launcher/org/prismlauncher/fix/skins/Handler.java @@ -43,8 +43,7 @@ import java.net.URLConnection; import java.net.URLStreamHandler; import java.util.Map; -import javax.xml.bind.DatatypeConverter; - +import org.prismlauncher.utils.Base64; import org.prismlauncher.utils.JsonParser; import org.prismlauncher.utils.UrlUtils; @@ -115,7 +114,7 @@ final class Handler extends URLStreamHandler { for (Map property : (Iterable>) profile.get("properties")) { if (property.get("name").equals("textures")) { Map result = (Map) JsonParser - .parse(new String(DatatypeConverter.parseBase64Binary((String) property.get("value")))); + .parse(new String(Base64.decode((String) property.get("value")))); result = (Map) result.get("textures"); return result; } diff --git a/libraries/launcher/org/prismlauncher/fix/skins/SkinFix.java b/libraries/launcher/org/prismlauncher/fix/skins/SkinFix.java index 80cf5323e..f5a9e0df4 100644 --- a/libraries/launcher/org/prismlauncher/fix/skins/SkinFix.java +++ b/libraries/launcher/org/prismlauncher/fix/skins/SkinFix.java @@ -59,27 +59,10 @@ public final class SkinFix implements Fix, URLStreamHandlerFactory { @Override 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"); + Log.warning("Using an older Java version will probably fix this"); + Log.warning("Alternatively, turning off legacy skin fix in Settings > Miscellaneous will silence the warnings"); return false; } diff --git a/libraries/launcher/org/prismlauncher/utils/Base64.java b/libraries/launcher/org/prismlauncher/utils/Base64.java new file mode 100644 index 000000000..20a189f46 --- /dev/null +++ b/libraries/launcher/org/prismlauncher/utils/Base64.java @@ -0,0 +1,59 @@ +// SPDX-License-Identifier: GPL-3.0-only +/* + * Prism Launcher - Minecraft Launcher + * Copyright (C) 2022 TheKodeToad + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * Linking this library statically or dynamically with other modules is + * making a combined work based on this library. Thus, the terms and + * conditions of the GNU General Public License cover the whole + * combination. + * + * As a special exception, the copyright holders of this library give + * you permission to link this library with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also meet, + * for each linked independent module, the terms and conditions of the + * license of that module. An independent module is a module which is + * not derived from or based on this library. If you modify this + * library, you may extend this exception to your version of the + * library, but you are not obliged to do so. If you do not wish to do + * so, delete this exception statement from your version. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.prismlauncher.utils; + +import java.nio.charset.StandardCharsets; + +import javax.xml.bind.DatatypeConverter; + +public final class Base64 { + + private static boolean legacy; + + public static byte[] decode(String input) { + if (!legacy) { + try { + return java.util.Base64.getDecoder().decode(input.getBytes(StandardCharsets.UTF_8)); + } catch (NoClassDefFoundError e) { + legacy = true; + } + } + + // support for Java versions < 8 + return DatatypeConverter.parseBase64Binary(input); + } + +} diff --git a/libraries/launcher/org/prismlauncher/utils/JsonParser.java b/libraries/launcher/org/prismlauncher/utils/JsonParser.java index e1703972b..d1803c896 100644 --- a/libraries/launcher/org/prismlauncher/utils/JsonParser.java +++ b/libraries/launcher/org/prismlauncher/utils/JsonParser.java @@ -420,7 +420,7 @@ public final class JsonParser { return character() == 'n' && read() == 'u' && read() == 'l' && read() == 'l'; } - public class JsonParseException extends IOException { + public static class JsonParseException extends IOException { private static final long serialVersionUID = 1L; diff --git a/libraries/launcher/org/prismlauncher/utils/UrlUtils.java b/libraries/launcher/org/prismlauncher/utils/UrlUtils.java index c73c7600b..362feb4e3 100644 --- a/libraries/launcher/org/prismlauncher/utils/UrlUtils.java +++ b/libraries/launcher/org/prismlauncher/utils/UrlUtils.java @@ -50,7 +50,7 @@ import org.prismlauncher.utils.logging.Log; * A utility class for URLs which uses reflection to access hidden methods. * Unfortunately not supported on newer Java versions. */ -public class UrlUtils { +public final class UrlUtils { private static URLStreamHandler http; private static MethodHandle openConnection;