From c38094ef3fd7533806ab6dd429c493936278fcb1 Mon Sep 17 00:00:00 2001 From: asie Date: Thu, 1 Nov 2018 22:15:51 +0100 Subject: [PATCH] obfuscate mixin methods/fields in output mod JARs --- .../loom/mixin/MixinMappingProviderTiny.java | 2 +- .../net/fabricmc/loom/util/Constants.java | 2 +- .../net/fabricmc/loom/util/ModRemapper.java | 23 +++++++++++-------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/fabricmc/loom/mixin/MixinMappingProviderTiny.java b/src/main/java/net/fabricmc/loom/mixin/MixinMappingProviderTiny.java index a4d58566..e20ff440 100644 --- a/src/main/java/net/fabricmc/loom/mixin/MixinMappingProviderTiny.java +++ b/src/main/java/net/fabricmc/loom/mixin/MixinMappingProviderTiny.java @@ -45,7 +45,7 @@ public class MixinMappingProviderTiny extends MappingProvider { this.to = to; } - private static final String[] removeFirst(String[] src, int count) { + private static String[] removeFirst(String[] src, int count) { if (count >= src.length) { return new String[0]; } else { diff --git a/src/main/java/net/fabricmc/loom/util/Constants.java b/src/main/java/net/fabricmc/loom/util/Constants.java index 6293b89e..655bbe68 100644 --- a/src/main/java/net/fabricmc/loom/util/Constants.java +++ b/src/main/java/net/fabricmc/loom/util/Constants.java @@ -51,7 +51,7 @@ public class Constants { public static final IDelayed POMF_DIR = new DelayedFile(extension -> new File(extension.getUserCache(), "pomf")); public static IDelayed MAPPINGS_TINY_GZ = new DelayedFile(extension -> new File(POMF_DIR.get(extension), "pomf-tiny-" + extension.version + "." + extension.pomfVersion + ".gz")); public static final IDelayed MAPPINGS_TINY = new DelayedFile(extension -> new File(POMF_DIR.get(extension), "pomf-tiny-" + extension.version + "." + extension.pomfVersion)); - public static final IDelayed MAPPINGS_MIXIN_EXPORT = new DelayedFile(extension -> new File(CACHE_FILES, "mixin-map-" + extension.version + "." + extension.pomfVersion + ".mappings")); + public static final IDelayed MAPPINGS_MIXIN_EXPORT = new DelayedFile(extension -> new File(CACHE_FILES, "mixin-map-" + extension.version + "." + extension.pomfVersion + ".tiny")); public static IDelayed MAPPINGS_ENIGMA_ZIP = new DelayedFile(extension -> new File(POMF_DIR.get(extension), "pomf-enigma-" + extension.version + "." + extension.pomfVersion + ".zip")); public static final IDelayed MAPPINGS_ENIGMA_DIR = new DelayedFile(extension -> new File(POMF_DIR.get(extension), "pomf-enigma-" + extension.version + "." + extension.pomfVersion + "")); diff --git a/src/main/java/net/fabricmc/loom/util/ModRemapper.java b/src/main/java/net/fabricmc/loom/util/ModRemapper.java index 241aa7fd..cbe61009 100644 --- a/src/main/java/net/fabricmc/loom/util/ModRemapper.java +++ b/src/main/java/net/fabricmc/loom/util/ModRemapper.java @@ -41,15 +41,17 @@ public class ModRemapper { public static void remap(Project project) throws IOException { LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class); - //TODO whats the proper way of doing this??? + // TODO: What's the proper way of doing this? File libsDir = new File(project.getBuildDir(), "libs"); File deobfJar = new File(libsDir, project.getName() + "-" + project.getVersion() + "-deobf.jar"); File modJar = new File(libsDir, project.getName() + "-" + project.getVersion() + ".jar"); + if (!modJar.exists()) { - project.getLogger().error("Could not find mod jar @" + deobfJar.getAbsolutePath()); + project.getLogger().error("Could not find mod .JAR at" + deobfJar.getAbsolutePath()); project.getLogger().error("This is can be fixed by adding a 'settings.gradle' file specifying 'rootProject.name'"); return; } + if (deobfJar.exists()) { deobfJar.delete(); } @@ -69,18 +71,21 @@ public class ModRemapper { classpathFiles.addAll(project.getConfigurations().getByName("compile").getFiles()); classpathFiles.addAll(project.getConfigurations().getByName(Constants.CONFIG_MINECRAFT).getFiles()); - Path[] classpath = new Path[classpathFiles.size()]; - for (int i = 0; i < classpathFiles.size(); i++) { - classpath[i] = classpathFiles.get(i).toPath(); + Path[] classpath = classpathFiles.stream().map(File::toPath).toArray(Path[]::new); + + File mixinMapFile = Constants.MAPPINGS_MIXIN_EXPORT.get(extension); + Path mixinMapPath = mixinMapFile.toPath(); + + TinyRemapper.Builder remapperBuilder = TinyRemapper.newRemapper(); + remapperBuilder = remapperBuilder.withMappings(TinyUtils.createTinyMappingProvider(mappings, fromM, toM)); + if (mixinMapFile.exists()) { + remapperBuilder = remapperBuilder.withMappings(TinyUtils.createTinyMappingProvider(mixinMapPath, fromM, toM)); } - TinyRemapper remapper = TinyRemapper.newRemapper() - .withMappings(TinyUtils.createTinyMappingProvider(mappings, fromM, toM)) - .build(); + TinyRemapper remapper = remapperBuilder.build(); try { OutputConsumerPath outputConsumer = new OutputConsumerPath(modJar.toPath()); - //Rebof the deobf jar outputConsumer.addNonClassFiles(deobfJar.toPath()); remapper.read(deobfJar.toPath()); remapper.read(classpath);