Skip to content

Commit

Permalink
fix: update to Mapping-IO 0.5 (PR #2029)
Browse files Browse the repository at this point in the history
* Update to Mapping-IO 0.5

* Fix Spotless error

* Fix Spotless error (2)

* Sort mappings by name when exporting
  • Loading branch information
NebelNidas committed Oct 9, 2023
1 parent 6433fce commit d129be7
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,16 @@ public RenameMappingsGui(MainWindow mainWindow) {

public void addMenuActions(JMenu menu) {
openMappingsMenu = new JMenu(NLS.str("file.open_mappings"));
openMappingsMenu.add(new ActionHandler(ev -> openMappings(MappingFormat.PROGUARD, true)).withNameAndDesc("Proguard (inverted)"));
openMappingsMenu.add(new ActionHandler(ev -> openMappings(MappingFormat.PROGUARD, false)).withNameAndDesc("Proguard"));
openMappingsMenu.add(new ActionHandler(ev -> openMappings(MappingFormat.PROGUARD_FILE, true))
.withNameAndDesc("Proguard (inverted)"));
openMappingsMenu.add(new ActionHandler(ev -> openMappings(MappingFormat.PROGUARD_FILE, false)).withNameAndDesc("Proguard"));

saveMappingsAction = new ActionHandler(this::saveMappings).withNameAndDesc(NLS.str("file.save_mappings"));

saveMappingsAsMenu = new JMenu(NLS.str("file.save_mappings_as"));

for (MappingFormat mappingFormat : MappingFormat.values()) {
if (mappingFormat != MappingFormat.PROGUARD) {
if (mappingFormat != MappingFormat.PROGUARD_FILE) {
openMappingsMenu.add(new ActionHandler(ev -> openMappings(mappingFormat, false))
.withNameAndDesc(mappingFormat.name));
}
Expand Down
10 changes: 3 additions & 7 deletions jadx-plugins/jadx-rename-mappings/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ plugins {
dependencies {
api(project(":jadx-core"))

// TODO: Switch back to upstream once this PR gets merged:
// https://github.com/FabricMC/mapping-io/pull/19
// implementation 'net.fabricmc:mapping-io:0.3.0'
api(files("libs/mapping-io-0.4.0-SNAPSHOT.jar"))

constraints {
runtimeOnly("org.ow2.asm:asm:9.5")
api("net.fabricmc:mapping-io:0.5.0-beta.1") {
exclude("org.ow2.asm:asm")
exclude("net.fabricmc:tiny-remapper")
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.jetbrains.annotations.Nullable;

import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.mappingio.tree.MappingTreeView;

import jadx.api.plugins.input.data.attributes.IJadxAttrType;
import jadx.api.plugins.input.data.attributes.IJadxAttribute;
Expand All @@ -16,18 +16,18 @@ public class RenameMappingsData implements IJadxAttribute {
return root.getAttributes().get(DATA);
}

public static @Nullable MappingTree getTree(RootNode root) {
public static @Nullable MappingTreeView getTree(RootNode root) {
RenameMappingsData data = getData(root);
return data == null ? null : data.getMappings();
}

private final MappingTree mappings;
private final MappingTreeView mappings;

public RenameMappingsData(MappingTree mappings) {
public RenameMappingsData(MappingTreeView mappings) {
this.mappings = mappings;
}

public MappingTree getMappings() {
public MappingTreeView getMappings() {
return mappings;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package jadx.plugins.mappings.load;

import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.mappingio.tree.MappingTree.ClassMapping;
import net.fabricmc.mappingio.tree.MappingTree.FieldMapping;
import net.fabricmc.mappingio.tree.MappingTree.MethodMapping;
import net.fabricmc.mappingio.tree.MappingTreeView;
import net.fabricmc.mappingio.tree.MappingTreeView.ClassMappingView;
import net.fabricmc.mappingio.tree.MappingTreeView.FieldMappingView;
import net.fabricmc.mappingio.tree.MappingTreeView.MethodMappingView;

import jadx.api.plugins.pass.JadxPassInfo;
import jadx.api.plugins.pass.impl.OrderedJadxPassInfo;
Expand Down Expand Up @@ -34,22 +34,22 @@ public void init(RootNode root) {
if (data == null) {
return;
}
MappingTree mappingTree = data.getMappings();
MappingTreeView mappingTree = data.getMappings();
process(root, mappingTree);
root.registerCodeDataUpdateListener(codeData -> process(root, mappingTree));
}

private void process(RootNode root, MappingTree mappingTree) {
private void process(RootNode root, MappingTreeView mappingTree) {
for (ClassNode cls : root.getClasses()) {
String clsRawName = cls.getClassInfo().getRawName().replace('.', '/');
ClassMapping mapping = mappingTree.getClass(clsRawName);
ClassMappingView mapping = mappingTree.getClass(clsRawName);
if (mapping != null) {
processClass(cls, mapping);
}
}
}

private static void processClass(ClassNode cls, ClassMapping classMapping) {
private static void processClass(ClassNode cls, ClassMappingView classMapping) {
String alias = classMapping.getDstName(0);
if (alias != null) {
cls.rename(alias.replace('/', '.'));
Expand All @@ -60,7 +60,7 @@ private static void processClass(ClassNode cls, ClassMapping classMapping) {
for (FieldNode field : cls.getFields()) {
FieldInfo fieldInfo = field.getFieldInfo();
String signature = TypeGen.signature(fieldInfo.getType());
FieldMapping fieldMapping = classMapping.getField(fieldInfo.getName(), signature);
FieldMappingView fieldMapping = classMapping.getField(fieldInfo.getName(), signature);
if (fieldMapping != null) {
processField(field, fieldMapping);
}
Expand All @@ -69,14 +69,14 @@ private static void processClass(ClassNode cls, ClassMapping classMapping) {
MethodInfo methodInfo = method.getMethodInfo();
String methodName = methodInfo.getName();
String methodDesc = methodInfo.getShortId().substring(methodName.length());
MethodMapping methodMapping = classMapping.getMethod(methodName, methodDesc);
MethodMappingView methodMapping = classMapping.getMethod(methodName, methodDesc);
if (methodMapping != null) {
processMethod(method, methodMapping);
}
}
}

private static void processField(FieldNode field, FieldMapping fieldMapping) {
private static void processField(FieldNode field, FieldMappingView fieldMapping) {
String alias = fieldMapping.getDstName(0);
if (alias != null) {
field.rename(alias);
Expand All @@ -87,7 +87,7 @@ private static void processField(FieldNode field, FieldMapping fieldMapping) {
}
}

private static void processMethod(MethodNode method, MethodMapping methodMapping) {
private static void processMethod(MethodNode method, MethodMappingView methodMapping) {
String alias = methodMapping.getDstName(0);
if (alias != null) {
method.rename(alias);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import java.util.List;
import java.util.Map;

import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.mappingio.tree.MappingTree.ClassMapping;
import net.fabricmc.mappingio.tree.MappingTree.MethodArgMapping;
import net.fabricmc.mappingio.tree.MappingTree.MethodMapping;
import net.fabricmc.mappingio.tree.MappingTreeView;
import net.fabricmc.mappingio.tree.MappingTreeView.ClassMappingView;
import net.fabricmc.mappingio.tree.MappingTreeView.MethodArgMappingView;
import net.fabricmc.mappingio.tree.MappingTreeView.MethodMappingView;

import jadx.api.plugins.pass.JadxPassInfo;
import jadx.api.plugins.pass.impl.OrderedJadxPassInfo;
Expand All @@ -20,7 +20,7 @@
import jadx.plugins.mappings.utils.DalvikToJavaBytecodeUtils;

public class CodeMappingsPass implements JadxDecompilePass {
private Map<String, ClassMapping> clsRenamesMap;
private Map<String, ClassMappingView> clsRenamesMap;

@Override
public JadxPassInfo getInfo() {
Expand All @@ -36,14 +36,14 @@ public void init(RootNode root) {
if (data == null) {
return;
}
MappingTree mappingTree = data.getMappings();
MappingTreeView mappingTree = data.getMappings();
updateMappingsMap(mappingTree);
root.registerCodeDataUpdateListener(codeData -> updateMappingsMap(mappingTree));
}

@Override
public boolean visit(ClassNode cls) {
ClassMapping classMapping = getMapping(cls);
ClassMappingView classMapping = getMapping(cls);
if (classMapping != null) {
applyRenames(cls, classMapping);
}
Expand All @@ -55,20 +55,20 @@ public boolean visit(ClassNode cls) {
public void visit(MethodNode mth) {
}

private static void applyRenames(ClassNode cls, ClassMapping classMapping) {
private static void applyRenames(ClassNode cls, ClassMappingView classMapping) {
for (MethodNode mth : cls.getMethods()) {
String methodName = mth.getMethodInfo().getName();
String methodDesc = mth.getMethodInfo().getShortId().substring(methodName.length());
List<SSAVar> ssaVars = mth.getSVars();
if (ssaVars.isEmpty()) {
continue;
}
MethodMapping methodMapping = classMapping.getMethod(methodName, methodDesc);
MethodMappingView methodMapping = classMapping.getMethod(methodName, methodDesc);
if (methodMapping == null) {
continue;
}
// Method args
for (MethodArgMapping argMapping : methodMapping.getArgs()) {
for (MethodArgMappingView argMapping : methodMapping.getArgs()) {
Integer mappingLvIndex = argMapping.getLvIndex();
for (SSAVar ssaVar : ssaVars) {
Integer actualLvIndex = DalvikToJavaBytecodeUtils.getMethodArgLvIndex(ssaVar, mth);
Expand All @@ -82,18 +82,18 @@ private static void applyRenames(ClassNode cls, ClassMapping classMapping) {
}
}

private ClassMapping getMapping(ClassNode cls) {
private ClassMappingView getMapping(ClassNode cls) {
if (clsRenamesMap == null || clsRenamesMap.isEmpty()) {
return null;
}
String classPath = cls.getClassInfo().makeRawFullName().replace('.', '/');
return clsRenamesMap.get(classPath);
}

private void updateMappingsMap(MappingTree mappings) {
private void updateMappingsMap(MappingTreeView mappings) {
clsRenamesMap = new HashMap<>();
for (ClassMapping cls : mappings.getClasses()) {
for (MethodMapping mth : cls.getMethods()) {
for (ClassMappingView cls : mappings.getClasses()) {
for (MethodMappingView mth : cls.getMethods()) {
if (!mth.getArgs().isEmpty() || !mth.getVars().isEmpty()) {
clsRenamesMap.put(cls.getSrcName(), cls);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import net.fabricmc.mappingio.MappingReader;
import net.fabricmc.mappingio.MappingUtil;
import net.fabricmc.mappingio.adapter.MappingSourceNsSwitch;
import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.mappingio.tree.MappingTreeView;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
import net.fabricmc.mappingio.tree.VisitableMappingTree;

import jadx.api.JadxArgs;
import jadx.api.plugins.pass.JadxPassInfo;
Expand All @@ -33,14 +34,14 @@ public JadxPassInfo getInfo() {

@Override
public void init(RootNode root) {
MappingTree mappings = loadMapping(root.getArgs());
MappingTreeView mappings = loadMapping(root.getArgs());
root.getAttributes().add(new RenameMappingsData(mappings));
}

private MappingTree loadMapping(JadxArgs args) {
private MappingTreeView loadMapping(JadxArgs args) {
try {
Path mappingsPath = args.getUserRenamesMappingsPath();
MemoryMappingTree mappingTree = new MemoryMappingTree();
VisitableMappingTree mappingTree = new MemoryMappingTree();
MappingReader.read(mappingsPath, options.getFormat(), mappingTree);
if (mappingTree.getSrcNamespace() == null) {
mappingTree.setSrcNamespace(MappingUtil.NS_SOURCE_FALLBACK);
Expand All @@ -53,7 +54,7 @@ private MappingTree loadMapping(JadxArgs args) {
mappingTree.getDstNamespaces().size()));
}
if (options.isInvert()) {
MemoryMappingTree invertedMappingTree = new MemoryMappingTree();
VisitableMappingTree invertedMappingTree = new MemoryMappingTree();
String dstNamespace = mappingTree.getDstNamespaces().get(0);
mappingTree.accept(new MappingSourceNsSwitch(invertedMappingTree, dstNamespace));
return invertedMappingTree;
Expand Down

0 comments on commit d129be7

Please sign in to comment.