give proguard the right jar based on mapping type

This commit is contained in:
babbaj 2018-12-01 06:31:40 -05:00
parent 5ef03ba1d5
commit a017e14a48
4 changed files with 149 additions and 10 deletions

View File

@ -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<String, Object> 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<Object> reobf = (NamedDomainObjectContainer<Object>) this.getProject().getExtensions().getByName("reobf");
final List<MappingType> 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<MappingType> getUsedMappingTypes(Set<Object> 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());

View File

@ -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
}

View File

@ -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);
}
}
}

View File

@ -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'