From a017e14a48dc7770991edc541c971501488720a0 Mon Sep 17 00:00:00 2001 From: babbaj Date: Sat, 1 Dec 2018 06:31:40 -0500 Subject: [PATCH 1/3] give proguard the right jar based on mapping type --- .../baritone/gradle/task/ProguardTask.java | 105 ++++++++++++++++-- .../baritone/gradle/util/MappingType.java | 9 ++ .../baritone/gradle/util/ReobfWrapper.java | 40 +++++++ scripts/proguard.pro | 5 +- 4 files changed, 149 insertions(+), 10 deletions(-) create mode 100644 buildSrc/src/main/java/baritone/gradle/util/MappingType.java create mode 100644 buildSrc/src/main/java/baritone/gradle/util/ReobfWrapper.java diff --git a/buildSrc/src/main/java/baritone/gradle/task/ProguardTask.java b/buildSrc/src/main/java/baritone/gradle/task/ProguardTask.java index 350f7446..bae59bb7 100644 --- a/buildSrc/src/main/java/baritone/gradle/task/ProguardTask.java +++ b/buildSrc/src/main/java/baritone/gradle/task/ProguardTask.java @@ -18,10 +18,17 @@ package baritone.gradle.task; import baritone.gradle.util.Determinizer; +import baritone.gradle.util.MappingType; +import baritone.gradle.util.ReobfWrapper; import com.google.gson.*; +import java.lang.reflect.Field; +import java.nio.file.StandardCopyOption; +import java.util.stream.Collectors; import org.apache.commons.io.IOUtils; +import org.gradle.api.NamedDomainObjectContainer; import org.gradle.api.artifacts.Configuration; import org.gradle.api.artifacts.Dependency; +import org.gradle.api.internal.plugins.DefaultConvention; import org.gradle.api.tasks.Input; import org.gradle.api.tasks.TaskAction; import org.gradle.internal.Pair; @@ -163,15 +170,13 @@ public class ProguardTask extends BaritoneGradleTask { // Iterate the required libraries to copy them to tempLibraries for (String lib : this.requiredLibraries) { - // Download the version jar from the URL acquired from the version manifest - if (lib.startsWith("minecraft")) { - String version = lib.split("-")[1]; - Path versionJar = getTemporaryFile("tempLibraries/" + lib + ".jar"); - if (!Files.exists(versionJar)) { - JsonObject versionJson = PARSER.parse(new InputStreamReader(new URL(this.versionDownloadMap.get(version)).openStream())).getAsJsonObject(); - String url = versionJson.getAsJsonObject("downloads").getAsJsonObject("client").getAsJsonPrimitive("url").getAsString(); - write(new URL(url).openStream(), versionJar); - } + // copy from the forgegradle cache + if (lib.equals("minecraft")) { + final Path cachedJar = getMinecraftJar(); + final Path inTempDir = getTemporaryFile("tempLibraries/" + "minecraft.jar"); + // TODO: maybe try not to copy every time + Files.copy(cachedJar, inTempDir, StandardCopyOption.REPLACE_EXISTING); + continue; } @@ -195,6 +200,88 @@ public class ProguardTask extends BaritoneGradleTask { } } + // a bunch of bullshit to get the path to the cached jar + private Path getMinecraftJar() throws Exception { + MappingType mappingType; + try { + mappingType = getMappingType(); + } catch (Exception e) { + System.err.println("Failed to get mapping type, assuming NOTCH"); + mappingType = MappingType.SEARGE; + } + + String suffix; + switch (mappingType) { + case NOTCH: + suffix = ""; + break; + case SEARGE: + suffix = "-srgBin"; + break; + case CUSTOM: + throw new IllegalStateException("custom mappings not supported"); + default: + throw new IllegalStateException("Unknown mapping type: " + mappingType); + } + + DefaultConvention convention = (DefaultConvention)this.getProject().getConvention(); + final Object extension = convention.getAsMap().get("minecraft"); + Objects.requireNonNull(extension); + + // for some reason cant use Class.forName + Class class_baseExtension = extension.getClass().getSuperclass().getSuperclass().getSuperclass(); + Field f_replacer = class_baseExtension.getDeclaredField("replacer"); + f_replacer.setAccessible(true); + final Object replacer = f_replacer.get(extension); + Class class_replacementProvider = replacer.getClass(); + Field replacement_replaceMap = class_replacementProvider.getDeclaredField("replaceMap"); + replacement_replaceMap.setAccessible(true); + + final Map replacements = (Map)replacement_replaceMap.get(replacer); + final String cacheDir = replacements.get("CACHE_DIR").toString() + "/net/minecraft"; + final String mcVersion = replacements.get("MC_VERSION").toString(); + final String mcpInsert = replacements.get("MAPPING_CHANNEL").toString() + "/" + replacements.get("MAPPING_VERSION").toString(); + final String fullJarName = "minecraft" + "-" + mcVersion + suffix + ".jar"; + + final String baseDir = String.format("%s/minecraft/%s/", cacheDir, mcVersion); + + String jarPath; + if (mappingType == MappingType.SEARGE) { + jarPath = String.format("%s/%s/%s", baseDir, mcpInsert, fullJarName); + + } else { + jarPath = baseDir + fullJarName; + } + jarPath = jarPath + .replace("/", File.separator) + .replace("\\", File.separator);// fucking regex + + return new File(jarPath).toPath(); + } + + // throws IllegalStateException if mapping type is ambiguous or it fails to find it + private MappingType getMappingType() { + // if it fails to find this then its probably a forgegradle version problem + final Set reobf = (NamedDomainObjectContainer) this.getProject().getExtensions().getByName("reobf"); + + final List mappingTypes = getUsedMappingTypes(reobf); + final long mappingTypesUsed = mappingTypes.size(); + if (mappingTypesUsed == 0) + throw new IllegalStateException("Failed to find mapping type (no jar task?)"); + if (mappingTypesUsed > 1) + throw new IllegalStateException("Ambiguous mapping type (multiple jars with different mapping types?)"); + + return mappingTypes.get(0); + } + + private List getUsedMappingTypes(Set reobf) { + return reobf.stream() + .map(ReobfWrapper::new) + .map(ReobfWrapper::getMappingType) + .distinct() + .collect(Collectors.toList()); + } + private void proguardApi() throws Exception { runProguard(getTemporaryFile(PROGUARD_API_CONFIG)); Determinizer.determinize(this.proguardOut.toString(), this.artifactApiPath.toString()); diff --git a/buildSrc/src/main/java/baritone/gradle/util/MappingType.java b/buildSrc/src/main/java/baritone/gradle/util/MappingType.java new file mode 100644 index 00000000..1de59d76 --- /dev/null +++ b/buildSrc/src/main/java/baritone/gradle/util/MappingType.java @@ -0,0 +1,9 @@ +package baritone.gradle.util; + +// this should be the same as in ForgeGradle +// credit 2 asmlibgradle (i made this btw) +public enum MappingType { + SEARGE, + NOTCH, + CUSTOM // forgegradle +} \ No newline at end of file diff --git a/buildSrc/src/main/java/baritone/gradle/util/ReobfWrapper.java b/buildSrc/src/main/java/baritone/gradle/util/ReobfWrapper.java new file mode 100644 index 00000000..128a1941 --- /dev/null +++ b/buildSrc/src/main/java/baritone/gradle/util/ReobfWrapper.java @@ -0,0 +1,40 @@ +package baritone.gradle.util; + +import java.lang.reflect.Field; +import java.util.Objects; + +public class ReobfWrapper { + + private final Object instance; + private final Class type; + + public ReobfWrapper(Object instance) { + this.instance = instance; + Objects.requireNonNull(instance); + this.type = instance.getClass(); + } + + public String getName() { + try { + Field nameField = type.getDeclaredField("name"); + nameField.setAccessible(true); + return (String)nameField.get(this.instance); + } catch (ReflectiveOperationException ex) { + throw new Error(ex); + } + } + + public MappingType getMappingType() { + try { + Field enumField = type.getDeclaredField("mappingType"); + enumField.setAccessible(true); + Enum meme = (Enum) enumField.get(this.instance); + MappingType mappingType = MappingType.values()[meme.ordinal()]; + if (!meme.name().equals(mappingType.name())) + throw new IllegalStateException("ForgeGradle ReobfMappingType is not equivalent to MappingType (version error?)"); + return mappingType; + } catch (ReflectiveOperationException ex) { + throw new Error(ex); + } + } +} \ No newline at end of file diff --git a/scripts/proguard.pro b/scripts/proguard.pro index 270a0631..24650dda 100644 --- a/scripts/proguard.pro +++ b/scripts/proguard.pro @@ -31,7 +31,10 @@ # copy all necessary libraries into tempLibraries to build --libraryjars 'tempLibraries/minecraft-1.12.2.jar' +-libraryjars 'tempLibraries/minecraft.jar' + +# The correct jar will be copied from the forgegradle cache based on the mapping type being compiled with +#-libraryjars 'tempLibraries/minecraft.jar' -libraryjars 'tempLibraries/SimpleTweaker-1.2.jar' From 0a2e45b64bbe73c50c9d68a639030d41e6dc38b5 Mon Sep 17 00:00:00 2001 From: babbaj Date: Sat, 1 Dec 2018 06:42:21 -0500 Subject: [PATCH 2/3] make codacy happy --- buildSrc/src/main/java/baritone/gradle/task/ProguardTask.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/buildSrc/src/main/java/baritone/gradle/task/ProguardTask.java b/buildSrc/src/main/java/baritone/gradle/task/ProguardTask.java index bae59bb7..f9da3118 100644 --- a/buildSrc/src/main/java/baritone/gradle/task/ProguardTask.java +++ b/buildSrc/src/main/java/baritone/gradle/task/ProguardTask.java @@ -175,7 +175,7 @@ public class ProguardTask extends BaritoneGradleTask { final Path cachedJar = getMinecraftJar(); final Path inTempDir = getTemporaryFile("tempLibraries/" + "minecraft.jar"); // TODO: maybe try not to copy every time - Files.copy(cachedJar, inTempDir, StandardCopyOption.REPLACE_EXISTING); + Files.copy(cachedJar, inTempDir, REPLACE_EXISTING); continue; } From 7f73b4554aaa82d257a9c7e87166cb40de2c7983 Mon Sep 17 00:00:00 2001 From: 0x22 <0x22@futureclient.net> Date: Sat, 1 Dec 2018 13:22:11 -0500 Subject: [PATCH 3/3] Feed codacy and leijurv at the same time? --- build.gradle | 1 - .../baritone/gradle/task/ProguardTask.java | 51 +++--------- .../baritone/gradle/util/MappingType.java | 30 +++++-- .../baritone/gradle/util/ReobfWrapper.java | 79 ++++++++++++------- scripts/proguard.pro | 4 +- 5 files changed, 90 insertions(+), 75 deletions(-) diff --git a/build.gradle b/build.gradle index e0b556a2..80a2de1c 100755 --- a/build.gradle +++ b/build.gradle @@ -107,7 +107,6 @@ jar { task proguard(type: ProguardTask) { url 'https://downloads.sourceforge.net/project/proguard/proguard/6.0/proguard6.0.3.zip' extract 'proguard6.0.3/lib/proguard.jar' - versionManifest 'https://launchermeta.mojang.com/mc/game/version_manifest.json' } task createDist(type: CreateDistTask, dependsOn: proguard) diff --git a/buildSrc/src/main/java/baritone/gradle/task/ProguardTask.java b/buildSrc/src/main/java/baritone/gradle/task/ProguardTask.java index f9da3118..84f9663b 100644 --- a/buildSrc/src/main/java/baritone/gradle/task/ProguardTask.java +++ b/buildSrc/src/main/java/baritone/gradle/task/ProguardTask.java @@ -20,9 +20,7 @@ package baritone.gradle.task; import baritone.gradle.util.Determinizer; import baritone.gradle.util.MappingType; import baritone.gradle.util.ReobfWrapper; -import com.google.gson.*; import java.lang.reflect.Field; -import java.nio.file.StandardCopyOption; import java.util.stream.Collectors; import org.apache.commons.io.IOUtils; import org.gradle.api.NamedDomainObjectContainer; @@ -59,10 +57,6 @@ public class ProguardTask extends BaritoneGradleTask { @Input private String extract; - @Input - private String versionManifest; - - private Map versionDownloadMap; private List requiredLibraries; @TaskAction @@ -74,7 +68,6 @@ public class ProguardTask extends BaritoneGradleTask { downloadProguard(); extractProguard(); generateConfigs(); - downloadVersionManifest(); acquireDependencies(); proguardApi(); proguardStandalone(); @@ -139,20 +132,6 @@ public class ProguardTask extends BaritoneGradleTask { }); } - private void downloadVersionManifest() throws Exception { - Path manifestJson = getTemporaryFile(VERSION_MANIFEST); - write(new URL(this.versionManifest).openStream(), manifestJson); - - // Place all the versions in the map with their download URL - this.versionDownloadMap = new HashMap<>(); - JsonObject json = readJson(Files.readAllLines(manifestJson)).getAsJsonObject(); - JsonArray versions = json.getAsJsonArray("versions"); - versions.forEach(element -> { - JsonObject object = element.getAsJsonObject(); - this.versionDownloadMap.put(object.get("id").getAsString(), object.get("url").getAsString()); - }); - } - private void acquireDependencies() throws Exception { // Create a map of all of the dependencies that we are able to access in this project @@ -173,7 +152,7 @@ public class ProguardTask extends BaritoneGradleTask { // copy from the forgegradle cache if (lib.equals("minecraft")) { final Path cachedJar = getMinecraftJar(); - final Path inTempDir = getTemporaryFile("tempLibraries/" + "minecraft.jar"); + final Path inTempDir = getTemporaryFile("tempLibraries/minecraft.jar"); // TODO: maybe try not to copy every time Files.copy(cachedJar, inTempDir, REPLACE_EXISTING); @@ -200,14 +179,14 @@ public class ProguardTask extends BaritoneGradleTask { } } - // a bunch of bullshit to get the path to the cached jar + // a bunch of epic stuff to get the path to the cached jar private Path getMinecraftJar() throws Exception { MappingType mappingType; try { mappingType = getMappingType(); } catch (Exception e) { - System.err.println("Failed to get mapping type, assuming NOTCH"); - mappingType = MappingType.SEARGE; + System.err.println("Failed to get mapping type, assuming NOTCH."); + mappingType = MappingType.NOTCH; } String suffix; @@ -219,12 +198,12 @@ public class ProguardTask extends BaritoneGradleTask { suffix = "-srgBin"; break; case CUSTOM: - throw new IllegalStateException("custom mappings not supported"); + throw new IllegalStateException("Custom mappings not supported!"); default: throw new IllegalStateException("Unknown mapping type: " + mappingType); } - DefaultConvention convention = (DefaultConvention)this.getProject().getConvention(); + DefaultConvention convention = (DefaultConvention) this.getProject().getConvention(); final Object extension = convention.getAsMap().get("minecraft"); Objects.requireNonNull(extension); @@ -237,24 +216,23 @@ public class ProguardTask extends BaritoneGradleTask { Field replacement_replaceMap = class_replacementProvider.getDeclaredField("replaceMap"); replacement_replaceMap.setAccessible(true); - final Map replacements = (Map)replacement_replaceMap.get(replacer); + final Map replacements = (Map) replacement_replaceMap.get(replacer); final String cacheDir = replacements.get("CACHE_DIR").toString() + "/net/minecraft"; final String mcVersion = replacements.get("MC_VERSION").toString(); final String mcpInsert = replacements.get("MAPPING_CHANNEL").toString() + "/" + replacements.get("MAPPING_VERSION").toString(); - final String fullJarName = "minecraft" + "-" + mcVersion + suffix + ".jar"; + final String fullJarName = "minecraft-" + mcVersion + suffix + ".jar"; final String baseDir = String.format("%s/minecraft/%s/", cacheDir, mcVersion); String jarPath; if (mappingType == MappingType.SEARGE) { jarPath = String.format("%s/%s/%s", baseDir, mcpInsert, fullJarName); - } else { jarPath = baseDir + fullJarName; } jarPath = jarPath - .replace("/", File.separator) - .replace("\\", File.separator);// fucking regex + .replace("/", File.separator) + .replace("\\", File.separator); // hecking regex return new File(jarPath).toPath(); } @@ -266,10 +244,11 @@ public class ProguardTask extends BaritoneGradleTask { final List mappingTypes = getUsedMappingTypes(reobf); final long mappingTypesUsed = mappingTypes.size(); - if (mappingTypesUsed == 0) + if (mappingTypesUsed == 0) { throw new IllegalStateException("Failed to find mapping type (no jar task?)"); - if (mappingTypesUsed > 1) + } if (mappingTypesUsed > 1) { throw new IllegalStateException("Ambiguous mapping type (multiple jars with different mapping types?)"); + } return mappingTypes.get(0); } @@ -306,10 +285,6 @@ public class ProguardTask extends BaritoneGradleTask { this.extract = extract; } - public void setVersionManifest(String versionManifest) { - this.versionManifest = versionManifest; - } - private void runProguard(Path config) throws Exception { // Delete the existing proguard output file. Proguard probably handles this already, but why not do it ourselves if (Files.exists(this.proguardOut)) { diff --git a/buildSrc/src/main/java/baritone/gradle/util/MappingType.java b/buildSrc/src/main/java/baritone/gradle/util/MappingType.java index 1de59d76..4a6c10ff 100644 --- a/buildSrc/src/main/java/baritone/gradle/util/MappingType.java +++ b/buildSrc/src/main/java/baritone/gradle/util/MappingType.java @@ -1,9 +1,29 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Baritone 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Baritone. If not, see . + */ + package baritone.gradle.util; -// this should be the same as in ForgeGradle -// credit 2 asmlibgradle (i made this btw) +/** + * All credits go to AsmLibGradle and its contributors. + * + * @see Original Source + */ public enum MappingType { - SEARGE, - NOTCH, - CUSTOM // forgegradle + SEARGE, + NOTCH, + CUSTOM // forgegradle } \ No newline at end of file diff --git a/buildSrc/src/main/java/baritone/gradle/util/ReobfWrapper.java b/buildSrc/src/main/java/baritone/gradle/util/ReobfWrapper.java index 128a1941..92f38a9f 100644 --- a/buildSrc/src/main/java/baritone/gradle/util/ReobfWrapper.java +++ b/buildSrc/src/main/java/baritone/gradle/util/ReobfWrapper.java @@ -1,40 +1,63 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Baritone 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Baritone. If not, see . + */ + package baritone.gradle.util; import java.lang.reflect.Field; import java.util.Objects; +/** + * All credits go to AsmLibGradle and its contributors. + * + * @see Original Source + */ public class ReobfWrapper { - private final Object instance; - private final Class type; + private final Object instance; + private final Class type; - public ReobfWrapper(Object instance) { - this.instance = instance; - Objects.requireNonNull(instance); - this.type = instance.getClass(); - } - - public String getName() { - try { - Field nameField = type.getDeclaredField("name"); - nameField.setAccessible(true); - return (String)nameField.get(this.instance); - } catch (ReflectiveOperationException ex) { - throw new Error(ex); + public ReobfWrapper(Object instance) { + this.instance = instance; + Objects.requireNonNull(instance); + this.type = instance.getClass(); } - } - public MappingType getMappingType() { - try { - Field enumField = type.getDeclaredField("mappingType"); - enumField.setAccessible(true); - Enum meme = (Enum) enumField.get(this.instance); - MappingType mappingType = MappingType.values()[meme.ordinal()]; - if (!meme.name().equals(mappingType.name())) - throw new IllegalStateException("ForgeGradle ReobfMappingType is not equivalent to MappingType (version error?)"); - return mappingType; - } catch (ReflectiveOperationException ex) { - throw new Error(ex); + public String getName() { + try { + Field nameField = type.getDeclaredField("name"); + nameField.setAccessible(true); + return (String) nameField.get(this.instance); + } catch (ReflectiveOperationException ex) { + throw new IllegalStateException(ex); + } + } + + public MappingType getMappingType() { + try { + Field enumField = type.getDeclaredField("mappingType"); + enumField.setAccessible(true); + Enum aEnum = (Enum) enumField.get(this.instance); + MappingType mappingType = MappingType.values()[aEnum.ordinal()]; + if (!aEnum.name().equals(mappingType.name())) { + throw new IllegalStateException("ForgeGradle ReobfMappingType is not equivalent to MappingType (version error?)"); + } + return mappingType; + } catch (ReflectiveOperationException ex) { + throw new IllegalStateException(ex); + } } - } } \ No newline at end of file diff --git a/scripts/proguard.pro b/scripts/proguard.pro index 24650dda..07e1ec41 100644 --- a/scripts/proguard.pro +++ b/scripts/proguard.pro @@ -31,10 +31,8 @@ # copy all necessary libraries into tempLibraries to build --libraryjars 'tempLibraries/minecraft.jar' - # The correct jar will be copied from the forgegradle cache based on the mapping type being compiled with -#-libraryjars 'tempLibraries/minecraft.jar' +-libraryjars 'tempLibraries/minecraft.jar' -libraryjars 'tempLibraries/SimpleTweaker-1.2.jar'