From 2cb7a3c96d43d5406778f9e42d14f1d88ab115a3 Mon Sep 17 00:00:00 2001 From: Agaricus Date: Wed, 20 Feb 2013 19:03:39 -0800 Subject: [PATCH] Add CLI alternate syntax for shade relocation --srg-in ../jars/1.4.7/cb2pkgmcp.srg --shade-relocation net.minecraft.server=net.minecraft.server.v1_4_R1,org.bouncycastle=net.minecraft.v1_4_R1.org.bouncycastle --srg-in ../jars/1.4.7/cb2pkgmcp.srg@net.minecraft.server=net.minecraft.server.v1_4_R1,org.bouncycastle=net.minecraft.v1_4_R1.org.bouncycastle --- .../net/md_5/specialsource/JarMapping.java | 25 +++++++++++++++++++ .../ShadeRelocationSimulator.java | 10 +++++--- .../net/md_5/specialsource/SpecialSource.java | 20 ++++++++------- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/md_5/specialsource/JarMapping.java b/src/main/java/net/md_5/specialsource/JarMapping.java index 13dace1..2438fb1 100644 --- a/src/main/java/net/md_5/specialsource/JarMapping.java +++ b/src/main/java/net/md_5/specialsource/JarMapping.java @@ -48,6 +48,7 @@ public class JarMapping { loadMappings(reader, shader); } + /** * Set the inheritance map used for caching superclass/interfaces. This call be omitted to * use a local cache, or set to your own global cache. @@ -91,6 +92,30 @@ public class JarMapping { return mapped; } + /** + * Load mappings given a (path) specification, optionally relocated + * through a suffix @oldpath1=newpath1,oldpath2=newpath2... + * + * Intended for convenient command-line usage. + */ + public void loadMappings(String spec) throws IOException { + int n = spec.lastIndexOf('@'); + String path; + ShadeRelocationSimulator shader; + + if (n == -1) { + path = spec; + shader = null; + } else { + path = spec.substring(0, n); + shader = new ShadeRelocationSimulator(spec.substring(n + 1)); + } + + BufferedReader reader = new BufferedReader(new FileReader(path)); + + loadMappings(reader, shader); + } + /** * Load a mapping given a .csrg file * diff --git a/src/main/java/net/md_5/specialsource/ShadeRelocationSimulator.java b/src/main/java/net/md_5/specialsource/ShadeRelocationSimulator.java index f573636..6c5659d 100644 --- a/src/main/java/net/md_5/specialsource/ShadeRelocationSimulator.java +++ b/src/main/java/net/md_5/specialsource/ShadeRelocationSimulator.java @@ -28,10 +28,7 @@ */ package net.md_5.specialsource; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * Simulate a small subset of the maven-shade-plugin class relocation @@ -72,10 +69,15 @@ public class ShadeRelocationSimulator { String pattern = pair.substring(0, index); String shadedPattern = pair.substring(index + 1); + System.out.println("reloc:"+pattern+" -> "+shadedPattern); relocations.put(toInternalName(pattern), toInternalName(shadedPattern)); } } + public ShadeRelocationSimulator(String string) { + this(Arrays.asList(string.split(","))); + } + public String shadeClassName(String className) { for (Map.Entry entry : relocations.entrySet()) { String pattern = entry.getKey(); diff --git a/src/main/java/net/md_5/specialsource/SpecialSource.java b/src/main/java/net/md_5/specialsource/SpecialSource.java index d6d753d..c6ed0d5 100644 --- a/src/main/java/net/md_5/specialsource/SpecialSource.java +++ b/src/main/java/net/md_5/specialsource/SpecialSource.java @@ -33,6 +33,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import com.google.common.base.Joiner; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; import joptsimple.OptionException; @@ -68,7 +69,8 @@ public class SpecialSource { acceptsAll(asList("m", "srg-in"), "Mapping file input") .withRequiredArg() - .ofType(File.class); + .describedAs("path[@relocations...]") + .ofType(String.class); acceptsAll(asList("i", "in-jar"), "Input jar to remap") .withRequiredArg() @@ -131,20 +133,20 @@ public class SpecialSource { jarMapping = new JarMapping(visitor1, visitor2, (File) options.valueOf("srg-out"), options.has("compact"), options.has("generate-dupes")); } else if (options.has("srg-in")) { // Load mappings, possibly shaded - ShadeRelocationSimulator shadeRelocationSimulator = null; + String spec = (String) options.valueOf("srg-in"); + if (options.has("shade-relocation")) { + // legacy command-line option support + // (new way is filename@... from the command-line, or loadMappings() programmatically) @SuppressWarnings("unchecked") List relocations = (List) options.valuesOf("shade-relocation"); - shadeRelocationSimulator = new ShadeRelocationSimulator(relocations); - - for (Map.Entry entry : shadeRelocationSimulator.relocations.entrySet()) { - log("Relocation: " + entry.getKey() + " -> " + entry.getValue()); - } + spec += "@" + Joiner.on(',').join(relocations); } + jarMapping = new JarMapping(); + log("Loading mappings"); - BufferedReader reader = new BufferedReader(new FileReader((File) options.valueOf("srg-in"))); - jarMapping = new JarMapping(reader, shadeRelocationSimulator); + jarMapping.loadMappings(spec); } else { System.err.println("No mappings given, first-jar/second-jar or srg-in required"); parser.printHelpOn(System.err);