From be7f81421ab98b439a4b285dd47bd293b61dc860 Mon Sep 17 00:00:00 2001 From: TheKodeToad Date: Fri, 6 Jan 2023 10:45:59 +0000 Subject: [PATCH] Finishing touches Signed-off-by: TheKodeToad --- .../launcher/legacy/net/minecraft/Launcher.java | 5 ++--- .../org/prismlauncher/legacy/LegacyFrame.java | 2 +- .../prismlauncher/legacy/LegacyLauncher.java | 14 ++++++-------- .../org/prismlauncher/legacy/LegacyProxy.java | 1 + .../legacy/fix/online/OnlineFixes.java | 8 ++++---- .../legacy/fix/online/SkinFix.java | 15 +++++++++------ .../prismlauncher/legacy/utils/JsonParser.java | 17 +++++++++-------- .../legacy/utils/api/MojangApi.java | 11 ++++++----- 8 files changed, 38 insertions(+), 35 deletions(-) diff --git a/libraries/launcher/legacy/net/minecraft/Launcher.java b/libraries/launcher/legacy/net/minecraft/Launcher.java index 646e2e3ea..4c7fcb6ee 100644 --- a/libraries/launcher/legacy/net/minecraft/Launcher.java +++ b/libraries/launcher/legacy/net/minecraft/Launcher.java @@ -93,12 +93,11 @@ public final class Launcher extends Applet implements AppletStub { try { if (documentBase == null) { - if (applet.getClass().getPackage().getName().startsWith("com.mojang.")) { + if (applet.getClass().getPackage().getName().startsWith("com.mojang")) // Special case only for Classic versions documentBase = new URL("http://www.minecraft.net:80/game/"); - } else { + else documentBase = new URL("http://www.minecraft.net/game/"); - } } } catch (MalformedURLException e) { throw new AssertionError(e); diff --git a/libraries/launcher/legacy/org/prismlauncher/legacy/LegacyFrame.java b/libraries/launcher/legacy/org/prismlauncher/legacy/LegacyFrame.java index 553b07eb1..49a126e7f 100644 --- a/libraries/launcher/legacy/org/prismlauncher/legacy/LegacyFrame.java +++ b/libraries/launcher/legacy/org/prismlauncher/legacy/LegacyFrame.java @@ -131,7 +131,7 @@ final class LegacyFrame extends JFrame { launcher.setParameter("username", user); launcher.setParameter("sessionid", session); - launcher.setParameter("stand-alone", true); // Show the quit button. TODO: why won't this work? + launcher.setParameter("stand-alone", true); // Show the quit button. This often doesn't seem to work. launcher.setParameter("haspaid", true); // Some old versions need this for world saves to work. launcher.setParameter("demo", demo); launcher.setParameter("fullscreen", false); diff --git a/libraries/launcher/legacy/org/prismlauncher/legacy/LegacyLauncher.java b/libraries/launcher/legacy/org/prismlauncher/legacy/LegacyLauncher.java index dae737224..f84b971cb 100644 --- a/libraries/launcher/legacy/org/prismlauncher/legacy/LegacyLauncher.java +++ b/libraries/launcher/legacy/org/prismlauncher/legacy/LegacyLauncher.java @@ -71,7 +71,7 @@ import org.prismlauncher.utils.ReflectionUtils; import org.prismlauncher.utils.logging.Log; /** - * Used to launch old versions that support applets. + * Used to launch old versions which support applets. */ final class LegacyLauncher extends AbstractLauncher { @@ -119,8 +119,8 @@ final class LegacyLauncher extends AbstractLauncher { } } - // find and invoke the main method, this time without size parameters - // in all versions that support applets, these are ignored + // find and invoke the main method, this time without size parameters - in all + // versions that support applets, these are ignored MethodHandle method = ReflectionUtils.findMainMethod(main); method.invokeExact(gameArgs.toArray(new String[0])); } @@ -128,18 +128,16 @@ final class LegacyLauncher extends AbstractLauncher { private static Applet createAppletClass(String clazz) throws Throwable { Class appletClass = ClassLoader.getSystemClassLoader().loadClass(clazz); - MethodHandle appletConstructor = MethodHandles.lookup().findConstructor(appletClass, MethodType.methodType(void.class)); + MethodHandle appletConstructor = MethodHandles.lookup().findConstructor(appletClass, + MethodType.methodType(void.class)); return (Applet) appletConstructor.invoke(); } private static Field findMinecraftGameDirField(Class clazz) { - Log.debug("Resolving minecraft game directory field"); - // search for private static File for (Field field : clazz.getDeclaredFields()) { - if (field.getType() != File.class) { + if (field.getType() != File.class) continue; - } int fieldModifiers = field.getModifiers(); diff --git a/libraries/launcher/legacy/org/prismlauncher/legacy/LegacyProxy.java b/libraries/launcher/legacy/org/prismlauncher/legacy/LegacyProxy.java index f2e65087f..11c9d6692 100644 --- a/libraries/launcher/legacy/org/prismlauncher/legacy/LegacyProxy.java +++ b/libraries/launcher/legacy/org/prismlauncher/legacy/LegacyProxy.java @@ -4,6 +4,7 @@ import org.prismlauncher.launcher.Launcher; import org.prismlauncher.legacy.fix.online.OnlineFixes; import org.prismlauncher.utils.Parameters; +// implementation of LegacyProxy public final class LegacyProxy { public static Launcher createLauncher(Parameters params) { diff --git a/libraries/launcher/legacy/org/prismlauncher/legacy/fix/online/OnlineFixes.java b/libraries/launcher/legacy/org/prismlauncher/legacy/fix/online/OnlineFixes.java index d4e4c27d5..56849cdd9 100644 --- a/libraries/launcher/legacy/org/prismlauncher/legacy/fix/online/OnlineFixes.java +++ b/libraries/launcher/legacy/org/prismlauncher/legacy/fix/online/OnlineFixes.java @@ -62,12 +62,12 @@ public final class OnlineFixes implements URLStreamHandlerFactory { return; } - try { + try { URL.setURLStreamHandlerFactory(new OnlineFixes()); - } catch (Error e) { + } catch (Error e) { Log.warning("Cannot apply skin fix: URLStreamHandlerFactory is already set"); - Log.warning("Turning off legacy skin fix in Settings > Miscellaneous will silence the warnings"); - } + Log.warning("Turning off legacy skin fix in Settings > Miscellaneous will silence the warnings"); + } } @Override diff --git a/libraries/launcher/legacy/org/prismlauncher/legacy/fix/online/SkinFix.java b/libraries/launcher/legacy/org/prismlauncher/legacy/fix/online/SkinFix.java index 1c4888312..7cfd297aa 100644 --- a/libraries/launcher/legacy/org/prismlauncher/legacy/fix/online/SkinFix.java +++ b/libraries/launcher/legacy/org/prismlauncher/legacy/fix/online/SkinFix.java @@ -12,8 +12,10 @@ import java.net.URLConnection; import javax.imageio.ImageIO; -import org.prismlauncher.legacy.utils.api.*; -import org.prismlauncher.legacy.utils.url.*; +import org.prismlauncher.legacy.utils.api.MojangApi; +import org.prismlauncher.legacy.utils.api.Texture; +import org.prismlauncher.legacy.utils.url.CustomUrlConnection; +import org.prismlauncher.legacy.utils.url.UrlUtils; final class SkinFix { @@ -25,7 +27,8 @@ final class SkinFix { String capeOwner = findCapeOwner(address); if (capeOwner != null) { - // since we do not need to process the image, open a direct connection bypassing Handler + // since we do not need to process the image, open a direct connection bypassing + // Handler Texture texture = MojangApi.getTexture(MojangApi.getUuid(capeOwner), "CAPE"); if (texture == null) return null; @@ -45,7 +48,7 @@ final class SkinFix { try (InputStream in = connection.getInputStream()) { // thank you craftycodie! // this is heavily based on - // https://github.com/Mojang/LegacyLauncher/pull/33/files#diff-b61023785a9260651ca0a223573ea9acb5be5eec478bff626dafb7abe13ffebaR99 + // https://github.com/craftycodie/MineOnline/blob/4f4f86f9d051e0a6fd7ff0b95b2a05f7437683d7/src/main/java/gg/codie/mineonline/gui/textures/TextureHelper.java#L17 BufferedImage image = ImageIO.read(in); Graphics2D graphics = image.createGraphics(); graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); @@ -59,7 +62,7 @@ final class SkinFix { } if (texture.isSlim()) { - // convert slim to wide + // convert slim to classic subimage = image.getSubimage(45, 16, 9, 16); graphics.drawImage(subimage, 46, 16, null); @@ -72,7 +75,7 @@ final class SkinFix { graphics.dispose(); - // crop the image + // crop the image - old versions disregard all secondary layers besides the hat ByteArrayOutputStream out = new ByteArrayOutputStream(); image = image.getSubimage(0, 0, 64, 32); ImageIO.write(image, "png", out); diff --git a/libraries/launcher/legacy/org/prismlauncher/legacy/utils/JsonParser.java b/libraries/launcher/legacy/org/prismlauncher/legacy/utils/JsonParser.java index 4339f98e6..73258fd09 100644 --- a/libraries/launcher/legacy/org/prismlauncher/legacy/utils/JsonParser.java +++ b/libraries/launcher/legacy/org/prismlauncher/legacy/utils/JsonParser.java @@ -47,7 +47,8 @@ import java.util.List; import java.util.Map; /** - * Single-file JSON parser to allow for usage in versions without GSON. + * A lightweight portable JSON parser used instead of GSON since it is not + * available in a lot of versions. */ public final class JsonParser { @@ -55,15 +56,15 @@ public final class JsonParser { private char[] buffer; private int pos, length; - public static Object parse(String in) throws JsonParseException, IOException { + public static Object parse(String in) throws IOException { return parse(new StringReader(in)); } - public static Object parse(InputStream in) throws JsonParseException, IOException { + public static Object parse(InputStream in) throws IOException { return parse(new InputStreamReader(in, StandardCharsets.UTF_8)); } - public static Object parse(Reader in) throws JsonParseException, IOException { + public static Object parse(Reader in) throws IOException { return new JsonParser(in).readSingleValue(); } @@ -336,6 +337,7 @@ public final class JsonParser { if (character() == '0') { result.append((char) character()); read(); + if (isDigit()) throw new JsonParseException("Found superfluous leading zero"); } else if (!isDigit()) @@ -395,11 +397,10 @@ public final class JsonParser { if (read() == 'r' && read() == 'u' && read() == 'e') { return true; } - } else if (character() == 'f') { - if (read() == 'a' && read() == 'l' && read() == 's' && read() == 'e') { - return false; - } + } else if (character() == 'f' && read() == 'a' && read() == 'l' && read() == 's' && read() == 'e') { + return false; } + return null; } diff --git a/libraries/launcher/legacy/org/prismlauncher/legacy/utils/api/MojangApi.java b/libraries/launcher/legacy/org/prismlauncher/legacy/utils/api/MojangApi.java index 6159a4e72..e15ebe7f5 100644 --- a/libraries/launcher/legacy/org/prismlauncher/legacy/utils/api/MojangApi.java +++ b/libraries/launcher/legacy/org/prismlauncher/legacy/utils/api/MojangApi.java @@ -40,10 +40,11 @@ import java.io.InputStream; import java.net.URL; import java.util.Map; -import org.prismlauncher.legacy.utils.*; +import org.prismlauncher.legacy.utils.Base64; +import org.prismlauncher.legacy.utils.JsonParser; /** - * Basic access to Mojang's Minecraft API. + * Basic wrapper for Mojang's Minecraft API. */ @SuppressWarnings("unchecked") public final class MojangApi { @@ -55,18 +56,18 @@ public final class MojangApi { } } - public static Texture getTexture(String player, String name) throws IOException { + public static Texture getTexture(String player, String id) throws IOException { Map map = getTextures(player); if (map != null) { - map = (Map) map.get(name); + map = (Map) map.get(id); if (map == null) return null; URL url = new URL((String) map.get("url")); boolean slim = false; - if (name.equals("SKIN")) { + if (id.equals("SKIN")) { map = (Map) map.get("metadata"); if (map != null && "slim".equals(map.get("model"))) slim = true;