diff --git a/src/main/java/net/runelite/deob/deobfuscators/rename/MappingException.java b/src/main/java/net/runelite/deob/deobfuscators/rename/MappingException.java deleted file mode 100644 index 157c8c9dee..0000000000 --- a/src/main/java/net/runelite/deob/deobfuscators/rename/MappingException.java +++ /dev/null @@ -1,6 +0,0 @@ -package net.runelite.deob.deobfuscators.rename; - -public class MappingException extends Exception -{ - -} diff --git a/src/main/java/net/runelite/deob/deobfuscators/rename/MappingExecutorUtil.java b/src/main/java/net/runelite/deob/deobfuscators/rename/MappingExecutorUtil.java index c344293a41..29b0a0a48a 100644 --- a/src/main/java/net/runelite/deob/deobfuscators/rename/MappingExecutorUtil.java +++ b/src/main/java/net/runelite/deob/deobfuscators/rename/MappingExecutorUtil.java @@ -66,7 +66,7 @@ public class MappingExecutorUtil return map1.equals(map2); } - public static ParallelExecutorMapping map(Method m1, Method m2) throws MappingException + public static ParallelExecutorMapping map(Method m1, Method m2) { ClassGroup group1 = m1.getMethods().getClassFile().getGroup(); ClassGroup group2 = m2.getMethods().getClassFile().getGroup(); @@ -169,16 +169,16 @@ public class MappingExecutorUtil // continue; } - try - { - mi1.map(mappings, p1, p2); - } - catch (Throwable ex) - { - p1.getFrame().stop(); - p2.getFrame().stop(); - ex.printStackTrace(); - } +// try +// { + mi1.map(mappings, p1, p2); +// } +// catch (Throwable ex) +// { +// p1.getFrame().stop(); +// p2.getFrame().stop(); +// ex.printStackTrace(); +// } e.paused = e2.paused = false; } diff --git a/src/main/java/net/runelite/deob/deobfuscators/rename/MethodSignatureMapper.java b/src/main/java/net/runelite/deob/deobfuscators/rename/MethodSignatureMapper.java new file mode 100644 index 0000000000..3be845f0db --- /dev/null +++ b/src/main/java/net/runelite/deob/deobfuscators/rename/MethodSignatureMapper.java @@ -0,0 +1,45 @@ +package net.runelite.deob.deobfuscators.rename; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import net.runelite.deob.ClassFile; +import net.runelite.deob.Method; +import net.runelite.deob.Methods; +import net.runelite.deob.signature.Signature; + +public class MethodSignatureMapper +{ + private Map map = new HashMap<>(); + + private long count(Methods methods, Signature sig) + { + return methods.getMethods().stream().filter(m -> m.getDescriptor().equals(sig)).count(); + } + + private Method get(Methods methods, Signature sig) + { + Optional o = methods.getMethods().stream().filter(m -> m.getDescriptor().equals(sig)).findFirst(); + return o.isPresent() ? o.get() : null; + } + + public void map(ClassFile c1, ClassFile c2) + { + for (Method m : c1.getMethods().getMethods()) + { + if (m.isStatic() || m.getName().equals("") || count(c1.getMethods(), m.getDescriptor()) > 1) + continue; + + Method other = get(c2.getMethods(), m.getDescriptor()); + if (other == null) + continue; + + map.put(m, other); + } + } + + public Map getMap() + { + return map; + } +} diff --git a/src/main/java/net/runelite/deob/deobfuscators/rename/Rename2.java b/src/main/java/net/runelite/deob/deobfuscators/rename/Rename2.java index 66627ca7bf..15a2f0c779 100644 --- a/src/main/java/net/runelite/deob/deobfuscators/rename/Rename2.java +++ b/src/main/java/net/runelite/deob/deobfuscators/rename/Rename2.java @@ -349,15 +349,8 @@ public class Rename2 if (m1.getName().equals("") || m1.getName().equals("")) return; - ParallelExecutorMapping mapping = null; - try - { - mapping = MappingExecutorUtil.map(m1, m2); - } - catch (MappingException ex) - { - throw new RuntimeException(ex); - } + ParallelExecutorMapping mapping = MappingExecutorUtil.map(m1, m2); + System.out.println("EXEC " + count++ + " " + mname(m1) + " " + mname(m2) + " " + mapping); for (Entry e : mapping.getMap().entrySet()) diff --git a/src/main/java/net/runelite/deob/deobfuscators/rename/StaticMethodSignatureMapper.java b/src/main/java/net/runelite/deob/deobfuscators/rename/StaticMethodSignatureMapper.java new file mode 100644 index 0000000000..0c6b73e6f7 --- /dev/null +++ b/src/main/java/net/runelite/deob/deobfuscators/rename/StaticMethodSignatureMapper.java @@ -0,0 +1,44 @@ +package net.runelite.deob.deobfuscators.rename; + +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import net.runelite.deob.ClassFile; +import net.runelite.deob.ClassGroup; +import net.runelite.deob.Method; +import net.runelite.deob.signature.Signature; + +public class StaticMethodSignatureMapper +{ + private Multimap map = ArrayListMultimap.create(); + + private List getStaticMethods(ClassGroup group) + { + List methods = new ArrayList<>(); + for (ClassFile cf : group.getClasses()) + for (Method m : cf.getMethods().getMethods()) + if (m.isStatic() && !m.getName().equals("")) + methods.add(m); + return methods; + } + + private List getStaticMethodsOfSignature(ClassGroup group, Signature sig) + { + return getStaticMethods(group).stream().filter(m -> m.getDescriptor().equals(sig)).collect(Collectors.toList()); + } + + public void map(ClassGroup group1, ClassGroup group2) + { + for (Method m : getStaticMethods(group1)) + { + map.putAll(m, getStaticMethodsOfSignature(group2, m.getDescriptor())); + } + } + + public Multimap getMap() + { + return map; + } +} diff --git a/src/main/java/net/runelite/deob/execution/ParallellMappingExecutor.java b/src/main/java/net/runelite/deob/execution/ParallellMappingExecutor.java index bcf7737766..a2fc02f307 100644 --- a/src/main/java/net/runelite/deob/execution/ParallellMappingExecutor.java +++ b/src/main/java/net/runelite/deob/execution/ParallellMappingExecutor.java @@ -180,7 +180,7 @@ public class ParallellMappingExecutor if (oldf1.otherStatic == oldf2 && oldf2.otherStatic == oldf1) { mappings.map(oldf1.getMethod(), oldf2.getMethod()); - System.out.println("STEP OUT " + oldf1.getMethod() + " <-> " + oldf2.getMethod()); + // System.out.println("STEP OUT " + oldf1.getMethod() + " <-> " + oldf2.getMethod()); } // if (e.frames.size() - s1 != e2.frames.size() - s2) @@ -279,7 +279,7 @@ public class ParallellMappingExecutor stepf2.otherStatic = stepf1; doubleStep.add(stepf1.getMethod()); - System.out.println("STEP " + stepf1.getMethod() + " <-> " + stepf2.getMethod()); + //System.out.println("STEP " + stepf1.getMethod() + " <-> " + stepf2.getMethod()); return step(); } diff --git a/src/test/java/net/runelite/deob/deobfuscators/rename/MapStaticTest.java b/src/test/java/net/runelite/deob/deobfuscators/rename/MapStaticTest.java index 04215c0391..e1457380eb 100644 --- a/src/test/java/net/runelite/deob/deobfuscators/rename/MapStaticTest.java +++ b/src/test/java/net/runelite/deob/deobfuscators/rename/MapStaticTest.java @@ -3,6 +3,7 @@ package net.runelite.deob.deobfuscators.rename; import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -65,7 +66,7 @@ public class MapStaticTest // } @Test - public void testAll() throws IOException, MappingException + public void testAll() throws IOException { ClassGroup group1 = JarUtil.loadJar(new File(JAR1)); ClassGroup group2 = JarUtil.loadJar(new File(JAR2)); @@ -82,7 +83,7 @@ public class MapStaticTest } @Test - public void test() throws IOException, MappingException + public void test() throws IOException { ClassGroup group1 = JarUtil.loadJar(new File(JAR1)); ClassGroup group2 = JarUtil.loadJar(new File(JAR2)); @@ -196,23 +197,98 @@ public class MapStaticTest map(all, pmes, m1, m2); } + for (int i = 0; i < 250; ++i) + { + ClassFile c1 = group1.findClass("class" + i); + ClassFile c2 = group2.findClass("class" + i); + + if (c1 == null || c2 == null) + continue; + + MethodSignatureMapper msm = new MethodSignatureMapper(); + msm.map(c1, c2); + + Map map = msm.getMap(); + for (Entry e : map.entrySet()) + { + HashMap all = new HashMap(); + map(all, pmes, e.getKey(), e.getValue()); + } + } + ParallelExecutorMapping finalm = new ParallelExecutorMapping(); for (ParallelExecutorMapping pme : pmes) finalm.merge(pme); summary(finalm); print(group1); - System.out.println("db step " + ParallellMappingExecutor.doubleStep.size()); +// System.out.println("db step " + ParallellMappingExecutor.doubleStep.size()); +// +// for (Method m : group1.findClass("client").getMethods().getMethods()) +// { +// if (!finalm.getMap().containsKey(m) && !m.isStatic()) +// System.out.println("missing " + m); +// } +// for (Field m : group1.findClass("client").getFields().getFields()) +// { +// if (!finalm.getMap().containsKey(m)) +// System.out.println("missing " + m); +// } + } + + //@Test + public void testMapperMap() throws IOException + { + ClassGroup one = JarUtil.loadJar(new File(JAR1)); + ClassGroup two = JarUtil.loadJar(new File(JAR2)); - for (Method m : group1.findClass("client").getMethods().getMethods()) + List pmes = new ArrayList<>(); + for (int i = 0; i < 250; ++i) { - if (!finalm.getMap().containsKey(m) && !m.isStatic()) - System.out.println("missing " + m); + ClassFile c1 = one.findClass("class" + i); + ClassFile c2 = two.findClass("class" + i); + + if (c1 == null || c2 == null) + continue; + + MethodSignatureMapper msm = new MethodSignatureMapper(); + msm.map(c1, c2); + + Map map = msm.getMap(); + for (Entry e : map.entrySet()) + { + HashMap all = new HashMap(); + map(all, pmes, e.getKey(), e.getValue()); + } } - for (Field m : group1.findClass("client").getFields().getFields()) + ParallelExecutorMapping finalm = new ParallelExecutorMapping(); + for (ParallelExecutorMapping pme : pmes) + finalm.merge(pme); + + summary(finalm); + print(one); + } + + @Test + public void testStaticMapperMap() throws IOException + { + ClassGroup one = JarUtil.loadJar(new File(JAR1)); + ClassGroup two = JarUtil.loadJar(new File(JAR2)); + + StaticMethodSignatureMapper smsm = new StaticMethodSignatureMapper(); + smsm.map(one, two); + + for (Method m : smsm.getMap().keySet()) { - if (!finalm.getMap().containsKey(m)) - System.out.println("missing " + m); + Collection methods = smsm.getMap().get(m); + + if (methods.size() == 1) + { + Method other = methods.stream().findFirst().get(); + ParallelExecutorMapping pme = MappingExecutorUtil.map(m, other); + + System.out.println(m + " " + other + " " + pme.getMap().size()); + } } } @@ -270,19 +346,19 @@ public class MapStaticTest int i=5; } - ParallelExecutorMapping mappings; - try - { + ParallelExecutorMapping +// try +// { mappings = MappingExecutorUtil.map(m1, m2); - } - catch (Throwable ex) - { - ex.printStackTrace(); - System.err.println("Error mapping " + m1 + " to " + m2); - //if (test) - // throw ex; - return; - } +// } +// catch (Throwable ex) +// { +// ex.printStackTrace(); +// System.err.println("Error mapping " + m1 + " to " + m2); +// //if (test) +// // throw ex; +// return; +// } result.add(mappings); diff --git a/src/test/java/net/runelite/deob/deobfuscators/rename/MapTest.java b/src/test/java/net/runelite/deob/deobfuscators/rename/MapTest.java index c799155119..737296ce6e 100644 --- a/src/test/java/net/runelite/deob/deobfuscators/rename/MapTest.java +++ b/src/test/java/net/runelite/deob/deobfuscators/rename/MapTest.java @@ -23,7 +23,7 @@ public class MapTest } @Test - public void test() throws IOException, MappingException + public void test() throws IOException { ClassGroup group1 = JarUtil.loadJar(new File("d:/rs/07/adamin1.jar")); ClassGroup group2 = JarUtil.loadJar(new File("d:/rs/07/adamin2.jar"));