From e1c0e117bbd174b2c53e541d2655f11eb3d2aa09 Mon Sep 17 00:00:00 2001 From: Kaydax Date: Fri, 26 Jan 2024 02:45:51 -0500 Subject: [PATCH 1/2] Backport project to support java 8 --- build.gradle | 17 ++-- gradlew.bat | 92 +++++++++++++++++++ .../java/xyz/zuoyx/multiyggdrasil/Config.java | 15 ++- .../httpd/MultiQueryProfileFilter.java | 6 +- .../transform/ClassTransformer.java | 4 +- .../transform/LdcTransformUnit.java | 23 ++--- .../zuoyx/multiyggdrasil/util/JsonUtils.java | 54 +++++------ .../util/UnsupportedURLException.java | 11 +-- .../yggdrasil/NamespacedID.java | 3 +- 9 files changed, 160 insertions(+), 65 deletions(-) create mode 100644 gradlew.bat diff --git a/build.gradle b/build.gradle index 62f6fc9..e0ff2b4 100644 --- a/build.gradle +++ b/build.gradle @@ -9,9 +9,8 @@ repositories { } dependencies { - implementation 'com.google.code.gson:gson:2.10.1' + implementation 'com.google.code.gson:gson:2.10.1' implementation 'org.ow2.asm:asm:9.6' - testImplementation 'org.junit.jupiter:junit-jupiter:5.10.1' } test { @@ -19,7 +18,8 @@ test { } compileJava { - options.release = 17 + options.release = 8 + options.deprecation = true } def buildNumber = System.getenv('AI_BUILD_NUMBER') @@ -54,9 +54,14 @@ processResources { } shadowJar { - exclude 'META-INF/maven/**' - exclude 'module-info.class' - exclude '**/module-info.class' + archiveClassifier.set(null) + + exclude 'META-INF/maven/**' + exclude 'module-info.class' + exclude '**/module-info.class' + + relocate 'com.google.code', 'xyz.zuoyx.multiyggdrasil.internal.com.google.code' + relocate 'org.objectweb.asm', 'xyz.zuoyx.multiyggdrasil.internal.org.objectweb.asm' } defaultTasks 'clean', 'shadowJar' diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..93e3f59 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,92 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/src/main/java/xyz/zuoyx/multiyggdrasil/Config.java b/src/main/java/xyz/zuoyx/multiyggdrasil/Config.java index aca4a7c..05c5f92 100644 --- a/src/main/java/xyz/zuoyx/multiyggdrasil/Config.java +++ b/src/main/java/xyz/zuoyx/multiyggdrasil/Config.java @@ -74,14 +74,14 @@ private static void initDebugOptions() { } else { for (String option : prop.split(",")) { switch (option) { - case "verbose" -> verboseLogging = true; - case "authlib" -> authlibLogging = true; - case "printUntransformed" -> { + case "verbose": verboseLogging = true; + case "authlib": authlibLogging = true; + case "printUntransformed": { printUntransformedClass = true; verboseLogging = true; } - case "dumpClass" -> dumpClass = true; - default -> { + case "dumpClass": dumpClass = true; + default: { log(ERROR, "Unrecognized debug option: " + option); throw new InitializationException(); } @@ -131,13 +131,12 @@ private static void initMojangProxy() { int port = Integer.parseInt(matcher.group("port")); switch (protocol) { - case "socks" -> mojangProxy = new Proxy(Type.SOCKS, new InetSocketAddress(host, port)); - default -> { + case "socks": mojangProxy = new Proxy(Type.SOCKS, new InetSocketAddress(host, port)); + default: { log(ERROR, "Unsupported proxy protocol: " + protocol); throw new InitializationException(); } } - log(INFO, "Mojang proxy: " + mojangProxy); } private static FeatureOption parseFeatureOption(String property) { diff --git a/src/main/java/xyz/zuoyx/multiyggdrasil/httpd/MultiQueryProfileFilter.java b/src/main/java/xyz/zuoyx/multiyggdrasil/httpd/MultiQueryProfileFilter.java index 8206c18..00632c9 100644 --- a/src/main/java/xyz/zuoyx/multiyggdrasil/httpd/MultiQueryProfileFilter.java +++ b/src/main/java/xyz/zuoyx/multiyggdrasil/httpd/MultiQueryProfileFilter.java @@ -61,7 +61,7 @@ public void handle(String domain, String path, HttpExchange exchange) throws Uns if (!matcher.find()) throw new UnsupportedURLException(); - UUID uuid; + UUID uuid; try { uuid = fromUnsignedUUID(matcher.group("uuid")); } catch (IllegalArgumentException e) { @@ -78,9 +78,9 @@ public void handle(String domain, String path, HttpExchange exchange) throws Uns Optional response; if (uuid.version() == 4) { response = mojangClient.queryProfile(uuid, withSignature); - } else { + } else { response = customClient.queryProfile(uuid, withSignature); - response.ifPresent(profile -> profile.name = new NamespacedID(profile.name, namespace).toString()); + response.ifPresent(profile -> profile.name = new NamespacedID(profile.name, namespace).toString()); } if (response.isPresent()) { diff --git a/src/main/java/xyz/zuoyx/multiyggdrasil/transform/ClassTransformer.java b/src/main/java/xyz/zuoyx/multiyggdrasil/transform/ClassTransformer.java index 494ca8e..a8124e5 100644 --- a/src/main/java/xyz/zuoyx/multiyggdrasil/transform/ClassTransformer.java +++ b/src/main/java/xyz/zuoyx/multiyggdrasil/transform/ClassTransformer.java @@ -143,7 +143,7 @@ public void accept(TransformUnit... units) { for (int i = units.length - 1; i >= 0; i--) { TransformContextImpl ctx = new TransformContextImpl(); Optional visitor = units[i].transform(classLoader, className, chain, ctx); - if (visitor.isEmpty()) + if (!visitor.isPresent()) continue; ctxs[i] = ctx; chain = visitor.get(); @@ -287,7 +287,7 @@ public byte[] transform(ClassLoader loader, String internalClassName, Class c handle.accept(unitsArray); Optional transformResult = handle.finish(); - if (Config.printUntransformedClass && transformResult.isEmpty()) { + if (Config.printUntransformedClass && !transformResult.isPresent()) { log(DEBUG, "No transformation is applied to [" + className + "]"); } diff --git a/src/main/java/xyz/zuoyx/multiyggdrasil/transform/LdcTransformUnit.java b/src/main/java/xyz/zuoyx/multiyggdrasil/transform/LdcTransformUnit.java index feee58f..33f7559 100644 --- a/src/main/java/xyz/zuoyx/multiyggdrasil/transform/LdcTransformUnit.java +++ b/src/main/java/xyz/zuoyx/multiyggdrasil/transform/LdcTransformUnit.java @@ -58,17 +58,18 @@ public void visitLdcInsn(Object cst) { } } - @Override - public void visitInvokeDynamicInsn(String name, String descriptor, Handle bootstrapMethodHandle, Object... bootstrapMethodArguments) { - for (int i = 0; i < bootstrapMethodArguments.length; i++) { - if (bootstrapMethodArguments[i] instanceof String constant) { - Optional transformed = transformLdc(constant); - if (transformed.isPresent() && !transformed.get().equals(constant)) { - ctx.markModified(); - bootstrapMethodArguments[i] = transformed.get(); - } - } - } + @Override + public void visitInvokeDynamicInsn(String name, String descriptor, Handle bootstrapMethodHandle, Object... bootstrapMethodArguments) { + for (int i = 0; i < bootstrapMethodArguments.length; i++) { + if (bootstrapMethodArguments[i] instanceof String) { + String constant = (String) bootstrapMethodArguments[i]; + Optional transformed = transformLdc(constant); + if (transformed.isPresent() && !transformed.get().equals(constant)) { + ctx.markModified(); + bootstrapMethodArguments[i] = transformed.get(); + } + } + } super.visitInvokeDynamicInsn(name, descriptor, bootstrapMethodHandle, bootstrapMethodArguments); } }; diff --git a/src/main/java/xyz/zuoyx/multiyggdrasil/util/JsonUtils.java b/src/main/java/xyz/zuoyx/multiyggdrasil/util/JsonUtils.java index 029a4d3..90bd9bf 100644 --- a/src/main/java/xyz/zuoyx/multiyggdrasil/util/JsonUtils.java +++ b/src/main/java/xyz/zuoyx/multiyggdrasil/util/JsonUtils.java @@ -28,37 +28,37 @@ public final class JsonUtils { - private static final Gson gson = new Gson(); + private static final Gson gson = new Gson(); - public static JsonElement parseJson(String jsonText) { - return JsonParser.parseString(jsonText); - } + public static JsonElement parseJson(String jsonText) { + return JsonParser.parseString(jsonText); + } - public static String toJsonString(Object json) { - return gson.toJson(json); - } + public static String toJsonString(Object json) { + return gson.toJson(json); + } - public static String asJsonString(JsonElement json) { - return json.getAsJsonPrimitive().getAsString(); - } - public static boolean asBoolean(JsonElement json) { - if (json != null) { - return json.getAsJsonPrimitive().getAsBoolean(); - } else { - return false; - } - } + public static String asJsonString(JsonElement json) { + return json.getAsJsonPrimitive().getAsString(); + } + public static boolean asBoolean(JsonElement json) { + if (json != null) { + return json.getAsJsonPrimitive().getAsBoolean(); + } else { + return false; + } + } - public static List toJavaList(JsonArray json) { - Type listType = new TypeToken>(){}.getType(); - return gson.fromJson(json, listType); - } + public static List toJavaList(JsonArray json) { + Type listType = new TypeToken>(){}.getType(); + return gson.fromJson(json, listType); + } - public static Map toJavaMap(JsonObject json) { - Type mapType = new TypeToken>(){}.getType(); - return gson.fromJson(json, mapType); - } + public static Map toJavaMap(JsonObject json) { + Type mapType = new TypeToken>(){}.getType(); + return gson.fromJson(json, mapType); + } - private JsonUtils() {} + private JsonUtils() {} -} +} \ No newline at end of file diff --git a/src/main/java/xyz/zuoyx/multiyggdrasil/util/UnsupportedURLException.java b/src/main/java/xyz/zuoyx/multiyggdrasil/util/UnsupportedURLException.java index bc2f756..3185725 100644 --- a/src/main/java/xyz/zuoyx/multiyggdrasil/util/UnsupportedURLException.java +++ b/src/main/java/xyz/zuoyx/multiyggdrasil/util/UnsupportedURLException.java @@ -16,25 +16,22 @@ */ package xyz.zuoyx.multiyggdrasil.util; -import java.io.Serial; - public class UnsupportedURLException extends Exception { - @Serial private static final long serialVersionUID = 7895188952767140345L; public UnsupportedURLException() { - this(null, null); + super(); } public UnsupportedURLException(String message) { - this(message, null); + super(message); } public UnsupportedURLException(String message, Throwable cause) { - super(message, cause, false, false); + super(message, cause); } public UnsupportedURLException(Throwable cause) { - this(null, cause); + super(cause); } } diff --git a/src/main/java/xyz/zuoyx/multiyggdrasil/yggdrasil/NamespacedID.java b/src/main/java/xyz/zuoyx/multiyggdrasil/yggdrasil/NamespacedID.java index f1c8ff6..17c0771 100644 --- a/src/main/java/xyz/zuoyx/multiyggdrasil/yggdrasil/NamespacedID.java +++ b/src/main/java/xyz/zuoyx/multiyggdrasil/yggdrasil/NamespacedID.java @@ -93,7 +93,8 @@ public String toString() { @Override public boolean equals(Object obj) { - if (obj instanceof NamespacedID other) { + if (obj instanceof NamespacedID) { + NamespacedID other = (NamespacedID) obj; return this.namespace.equals(other.namespace) && this.id.equals(other.id); } return false; From 42499b5a6804c9abbbab8d2b27e452f6bf4e3ea8 Mon Sep 17 00:00:00 2001 From: Kaydax Date: Fri, 26 Jan 2024 03:12:07 -0500 Subject: [PATCH 2/2] Fix gson relocation --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index e0ff2b4..e99e833 100644 --- a/build.gradle +++ b/build.gradle @@ -60,7 +60,7 @@ shadowJar { exclude 'module-info.class' exclude '**/module-info.class' - relocate 'com.google.code', 'xyz.zuoyx.multiyggdrasil.internal.com.google.code' + relocate 'com.google.gson', 'xyz.zuoyx.multiyggdrasil.internal.com.google.gson' relocate 'org.objectweb.asm', 'xyz.zuoyx.multiyggdrasil.internal.org.objectweb.asm' }