Skip to content

Commit

Permalink
Add automatic renaming feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Runemoro committed Jun 17, 2018
1 parent 864203d commit 46eadfc
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 5 deletions.
41 changes: 37 additions & 4 deletions src/main/java/org/dimdev/javaremapper/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import org.dimdev.srg2jam.Srg2Jam;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.*;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public final class CommandLine {
public static void main(String... args) throws IOException {
Expand All @@ -15,6 +17,7 @@ public static void main(String... args) throws IOException {
System.out.println();
System.out.println("Subcommands:");
System.out.println(" remap <jar> <target> <mappings> - Remaps a jar file using a JAM mapping file");
System.out.println(" rename <jar> <target> <mappings> - Generates mappings with unique identifiers for everything");
System.out.println(" srg2jam <path to MCP config folder> - Converts a MCP config folder to a JAM file");
System.out.println(" help - Displays this help message");
return;
Expand All @@ -37,6 +40,36 @@ public static void main(String... args) throws IOException {
break;
}

case "rename" : {
File inputFile = new File(args[1]);
File remapTarget = new File(args[2]);
File mappingTarget = new File(args[3]);

if (remapTarget.exists()) remapTarget.delete();
if (mappingTarget.exists()) mappingTarget.delete();

Set<String> classesInJar = new HashSet<>();
try (JarFile jar = new JarFile(inputFile)) {
Enumeration<JarEntry> entries = jar.entries();
do {
JarEntry entry = entries.nextElement();
String name = entry.getName();
if (name.endsWith(".class")) {
String className = name.substring(0, name.length() - 6);
classesInJar.add(className);
}
} while (entries.hasMoreElements());
}

InheritanceProvider inheritanceProvider = JavaRemapper.makeInheritanceProvider(inputFile);
Mapping mapping = new GeneratingMapping(inheritanceProvider, classesInJar);
new JavaRemapper(mapping).remapJar(inputFile, remapTarget);

mapping.writeToJAM(new FileWriter(mappingTarget));

break;
}

case "srg2jam": {
Srg2Jam.convert(new File(args[1]));
break;
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/org/dimdev/javaremapper/GeneratingMapping.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.dimdev.javaremapper;

import java.util.Set;

public class GeneratingMapping extends Mapping {
private InheritanceProvider inheritanceProvider;
private Set<String> classFilter;
private int classIndex = 0;
private int fieldIndex = 0;
private int methodIndex = 0;

public GeneratingMapping(InheritanceProvider inheritanceProvider, Set<String> classFilter) {
this.inheritanceProvider = inheritanceProvider;
this.classFilter = classFilter;
}

public String getClass(String className) {
// Don't remap classes not in jar
if (!classFilter.contains(className)) return null;

String result = super.getClass(className);
if (result == null) {
result = "Class" + classIndex++;
addClass(className, result);
}
return result;
}

public String getField(String className, String fieldName, String fieldDescriptor) {
// Don't remap classes not in jar
if (!classFilter.contains(className)) return null;

// Don't remap inherited fields, their name is inherited from the parent's mapping
for (String superclass : inheritanceProvider.getAllSuperclasses(className)) {
if (inheritanceProvider.getInheritableFields(superclass).contains(new MemberRef(fieldName, fieldDescriptor))) {
return null;
}
}

String result = super.getField(className, fieldName, fieldDescriptor);
if (result == null) {
result = "field" + fieldIndex++;
addField(className, fieldName, fieldDescriptor, result);
}
return result;
}

public String getMethod(String className, String methodName, String methodDescriptor) {
// Don't remap classes not in jar
if (!classFilter.contains(className)) return null;

// Don't remap inherited methods, their name is inherited from the parent's mapping
for (String superclass : inheritanceProvider.getAllSuperclasses(className)) {
if (inheritanceProvider.getInheritableMethods(superclass).contains(new MemberRef(methodName, methodDescriptor))) {
return null;
}
}

String result = super.getField(className, methodName, methodDescriptor);
if (result == null) {
result = "method" + fieldIndex++;
addMethod(className, methodName, methodDescriptor, "method" + methodIndex++);
}
return result;
}
}
9 changes: 8 additions & 1 deletion src/main/java/org/dimdev/javaremapper/JavaRemapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public JavaRemapper(Mapping mapping) {
}

public void remapJar(File inputFile, File remapTarget) throws IOException {
remapJar(inputFile, remapTarget, makeInheritanceProvider(inputFile));
}

public static InheritanceProvider makeInheritanceProvider(File inputFile) throws IOException {
// Make the inheritance map
InheritanceMapper inheritanceMapper = new InheritanceMapper();
try (JarFile jar = new JarFile(inputFile)) {
Expand All @@ -40,9 +44,12 @@ public void remapJar(File inputFile, File remapTarget) throws IOException {
}
}
}
return inheritanceMapper;
}

public void remapJar(File inputFile, File remapTarget, InheritanceProvider inheritanceProvider) throws IOException {
// Initialize the remapper using the mapping and inheritance provider
SimpleRemapper remapper = new SimpleRemapper(mapping, inheritanceMapper);
SimpleRemapper remapper = new SimpleRemapper(mapping, inheritanceProvider);

// Copy jar classes, remapping them if necessary
try (JarFile jar = new JarFile(inputFile);
Expand Down

0 comments on commit 46eadfc

Please sign in to comment.