From 1e5bfcd1e79d869592bc7333291ce8a0f550c36c Mon Sep 17 00:00:00 2001 From: 33 Date: Wed, 27 Jul 2022 17:23:50 +0800 Subject: [PATCH] Fix: Minecraft version getting logic --- .../customskinloader/CustomSkinLoader.java | 2 +- .../customskinloader/utils/MinecraftUtil.java | 88 ++++++++----------- 2 files changed, 40 insertions(+), 50 deletions(-) diff --git a/Common/source/customskinloader/CustomSkinLoader.java b/Common/source/customskinloader/CustomSkinLoader.java index 8d60ae9..7510f8d 100644 --- a/Common/source/customskinloader/CustomSkinLoader.java +++ b/Common/source/customskinloader/CustomSkinLoader.java @@ -207,7 +207,7 @@ public class CustomSkinLoader { logger.info("Operating System: " + System.getProperty("os.name") + " (" + System.getProperty("os.arch") + ") version " + System.getProperty("os.version")); logger.info("Java Version: " + System.getProperty("java.version") + ", " + System.getProperty("java.vendor")); logger.info("Java VM Version: " + System.getProperty("java.vm.name") + " (" + System.getProperty("java.vm.info") + "), " + System.getProperty("java.vm.vendor")); - logger.info("Minecraft: " + MinecraftUtil.getMinecraftMainVersion() + "(" + MinecraftUtil.getMinecraftVersionText() + ")"); + logger.info("Minecraft: " + MinecraftUtil.getMinecraftMainVersion()); return logger; } } diff --git a/Common/source/customskinloader/utils/MinecraftUtil.java b/Common/source/customskinloader/utils/MinecraftUtil.java index 9d6633c..22335ac 100644 --- a/Common/source/customskinloader/utils/MinecraftUtil.java +++ b/Common/source/customskinloader/utils/MinecraftUtil.java @@ -1,16 +1,26 @@ package customskinloader.utils; import java.io.File; +import java.io.FileReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.reflect.Field; import java.net.URL; import java.net.URLDecoder; import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import com.mojang.authlib.GameProfile; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.texture.TextureManager; import net.minecraft.client.resources.SkinManager; +import net.minecraft.realms.RealmsSharedConstants; import org.apache.commons.lang3.StringUtils; /** @@ -29,42 +39,40 @@ public class MinecraftUtil { public static SkinManager getSkinManager() { return Minecraft.getMinecraft().getSkinManager(); } - - - private static ArrayList minecraftVersion = new ArrayList(); private static String minecraftMainVersion = null; - private final static Pattern MINECRAFT_VERSION_PATTERN = Pattern.compile(".*?(\\d+\\.\\d+[\\.]?\\d*).*?"); - - public static ArrayList getMinecraftVersions() { - if (minecraftVersion != null && !minecraftVersion.isEmpty()) - return minecraftVersion; - testProbe(); - return minecraftVersion; - } - - public static String getMinecraftVersionText() { - StringBuilder sb = new StringBuilder(); - for (String version : getMinecraftVersions()) - sb.append(version).append(" "); - return StringUtils.trim(sb.toString()); - } public static String getMinecraftMainVersion() { - if (minecraftMainVersion != null) + //Check if cached version found + if (minecraftMainVersion != null) { return minecraftMainVersion; - for (String version : getMinecraftVersions()) { - Matcher m = null; - try { - m = MINECRAFT_VERSION_PATTERN.matcher(version); - } catch (Exception e) { - e.printStackTrace(); - } - if (m == null || !m.matches()) - continue; - minecraftMainVersion = m.group(m.groupCount()); - break; } - return minecraftMainVersion; + + //version.json can be found in 1.14+ + URL versionFile = MinecraftUtil.class.getResource("/version.json"); + if (versionFile != null) { + try ( + InputStream is = versionFile.openStream(); + InputStreamReader isr = new InputStreamReader(is) + ) { + JsonObject obj = new JsonParser().parse(isr).getAsJsonObject(); + minecraftMainVersion = obj.get("name").getAsString(); + return minecraftMainVersion; + } catch (Exception ignored) { + + } + } + + //RealmsSharedConstants.VERSION_STRING is available in 1.16- + try { + Class realmsSharedConstants = Class.forName("net.minecraft.realms.RealmsSharedConstants"); + MethodHandle mh = MethodHandles.publicLookup().findStaticGetter(realmsSharedConstants, "VERSION_STRING", String.class); + minecraftMainVersion = (String) mh.invoke(); + return minecraftMainVersion; + } catch (Throwable ignored) { + } + + //No version can be found + return null; } // (domain|ip)(:port) @@ -84,24 +92,6 @@ public class MinecraftUtil { return HttpUtil0.isLanServer(getStandardServerAddress()); } - private final static Pattern MINECRAFT_CORE_FILE_PATTERN = Pattern.compile("^(.*?)/versions/([^\\/\\\\]*?)/([^\\/\\\\]*?).jar$"); - - private static void testProbe() { - minecraftVersion.clear(); - URL urls[] = JavaUtil.getClasspath(); - for (URL url : urls) { - Matcher m = null; - try { - m = MINECRAFT_CORE_FILE_PATTERN.matcher(URLDecoder.decode(url.getPath(), "UTF-8")); - } catch (Exception e) { - e.printStackTrace(); - } - if (m == null || !m.matches()) - continue; - minecraftVersion.add(m.group(2)); - } - } - public static String getCredential(GameProfile profile) { return (profile == null || profile.hashCode() == 0) ? null : (profile.getId() == null ? profile.getName() : String.format("%s-%s", profile.getName(), profile.getId()));