Skip to content

Commit

Permalink
Remove ASM dependency from the core project (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
NebelNidas authored Aug 29, 2024
1 parent d906757 commit 3bc4d8e
Show file tree
Hide file tree
Showing 37 changed files with 164 additions and 123 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added missing `visitElementContent` calls to CSRG and Recaf Simple readers
- Fixed member mapping merging via tree-API in `MemoryMappingTree`
- Fixed duplicate mapping definitions not being handled correctly in multiple readers
- Removed ASM dependency from core project

## [0.6.1] - 2024-04-15
- Fixed CSRG and JAM writers sometimes skipping elements whose parents have incomplete destination names
Expand Down
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ allprojects {
}

dependencies {
implementation "org.ow2.asm:asm:${project.asm_version}"
compileOnly "org.jetbrains:annotations:${project.jetbrains_annotations_version}"

testImplementation "net.neoforged:srgutils:${srgutils_version}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.jetbrains.annotations.Nullable;
import org.objectweb.asm.Type;

import net.fabricmc.mappingio.MappedElementKind;
import net.fabricmc.mappingio.MappingWriter;
Expand Down Expand Up @@ -157,20 +157,19 @@ public boolean visitElementContent(MappedElementKind targetKind) throws IOExcept
writer.write(srcName);
break;
case METHOD:
Type type = Type.getMethodType(srcDesc);
writeIndent();
writer.write(toJavaType(type.getReturnType().getDescriptor()));
writer.write(toJavaType(srcDesc.substring(srcDesc.indexOf(')', 1) + 1)));
writer.write(' ');
writer.write(srcName);
writer.write('(');
Type[] args = type.getArgumentTypes();
List<String> argTypes = extractArgumentTypes(srcDesc);

for (int i = 0; i < args.length; i++) {
for (int i = 0; i < argTypes.size(); i++) {
if (i > 0) {
writer.write(',');
}

writer.write(toJavaType(args[i].getDescriptor()));
writer.write(argTypes.get(i));
}

writer.write(')');
Expand Down Expand Up @@ -251,4 +250,27 @@ private static String toJavaType(String descriptor) {

return result.toString();
}

private List<String> extractArgumentTypes(String desc) {
List<String> argTypes = new ArrayList<>();
int index = 1; // First char is always '('

while (desc.charAt(index) != ')') {
int start = index;

while (desc.charAt(index) == '[') {
index++;
}

if (desc.charAt(index) == 'L') {
index = desc.indexOf(';', index) + 1;
} else {
index++;
}

argTypes.add(toJavaType(desc.substring(start, index)));
}

return argTypes;
}
}
20 changes: 16 additions & 4 deletions src/test/java/net/fabricmc/mappingio/TestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import net.neoforged.srgutils.IMappingFile;
Expand Down Expand Up @@ -259,15 +260,15 @@ private static void visitInnerClass(MemoryMappingTree tree, int nestLevel, int..
}

private static void visitField(MemoryMappingTree tree, int... dstNs) {
tree.visitField(nameGen.src(fldKind), fldDesc);
tree.visitField(nameGen.src(fldKind), nameGen.desc(fldKind));

for (int ns : dstNs) {
tree.visitDstName(fldKind, ns, nameGen.dst(fldKind, ns));
}
}

private static void visitMethod(MemoryMappingTree tree, int... dstNs) {
tree.visitMethod(nameGen.src(mthKind), mthDesc);
tree.visitMethod(nameGen.src(mthKind), nameGen.desc(mthKind));

for (int ns : dstNs) {
tree.visitDstName(mthKind, ns, nameGen.dst(mthKind, ns));
Expand Down Expand Up @@ -373,6 +374,17 @@ public String dst(MappedElementKind kind, int ns) {
return sb.toString();
}

public String desc(MappedElementKind kind) {
switch (kind) {
case FIELD:
return fldDescs.get((fldNum.get().get() - 1) % fldDescs.size());
case METHOD:
return mthDescs.get((mthNum.get().get() - 1) % mthDescs.size());
default:
throw new IllegalArgumentException("Invalid kind: " + kind);
}
}

public AtomicInteger getCounter() {
return counter.get();
}
Expand Down Expand Up @@ -442,8 +454,8 @@ public static MemoryMappingTree getCorrespondingTree(Path dir) {
public static final Path REPEATED_ELEMENTS = getResource("/read/repeated-elements/");
}

private static final String fldDesc = "I";
private static final String mthDesc = "()I";
private static final List<String> fldDescs = Arrays.asList("I", "Lcls;", "Lpkg/cls;", "[I");
private static final List<String> mthDescs = Arrays.asList("()I", "(I)V", "(Lcls;)Lcls;", "(ILcls;)Lpkg/cls;", "(Lcls;[I)[[B");
private static final String comment = "This is a comment";
private static final NameGen nameGen = new NameGen();
private static final MappedElementKind clsKind = MappedElementKind.CLASS;
Expand Down
21 changes: 14 additions & 7 deletions src/test/java/net/fabricmc/mappingio/write/WriteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,22 @@ private void readWithSrgUtils(MappingTreeView tree, MappingFormat format, Map<St
IMappingFile.Format srgUtilsFormat = TestHelper.toSrgUtilsFormat(format);
if (srgUtilsFormat == null) return;

// TODO: Remove once https://github.com/neoforged/SRGUtils/issues/7 is fixed
if (format == MappingFormat.PROGUARD_FILE) return;

// SrgUtils can't handle empty dst names
VisitableMappingTree dstNsCompTree = new MemoryMappingTree();
tree.accept(new MappingNsCompleter(new ForwardingMappingVisitor(dstNsCompTree) {
@Override
public boolean visitElementContent(MappedElementKind targetKind) throws IOException {
// SrgUtil's Tiny v2 reader crashes on var sub-elements
return !(format == MappingFormat.TINY_2_FILE && targetKind == MappedElementKind.METHOD_VAR);
}
}, nsAltMap));
tree.accept(
// TODO: Remove once https://github.com/neoforged/SRGUtils/issues/9 is fixed
new MappingNsCompleter(
// TODO: Remove once https://github.com/neoforged/SRGUtils/issues/8 is fixed
new ForwardingMappingVisitor(dstNsCompTree) {
@Override
public boolean visitElementContent(MappedElementKind targetKind) throws IOException {
return !(format == MappingFormat.TINY_2_FILE && targetKind == MappedElementKind.METHOD_VAR);
}
},
nsAltMap));

Path path = TestHelper.writeToDir(dstNsCompTree, dir, format);
INamedMappingFile.load(path.toFile());
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/read/repeated-elements/enigma.mappings
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ CLASS class_1 class1Ns0Rename
COMMENT This is a comment.
CLASS class_2 class2Ns0Rename
COMMENT This is a comment
FIELD field_2 field2Ns0Rename0 I
FIELD field_2 field2Ns0Rename I
FIELD field_2 field2Ns0Rename0 Lcls;
FIELD field_2 field2Ns0Rename Lcls;
CLASS class_3 class3Ns0Rename0
CLASS class_3 class3Ns0Rename
4 changes: 2 additions & 2 deletions src/test/resources/read/repeated-elements/jam.jam
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ MD class_1 method_1 ()I method1Ns0Rename0
MD class_1 method_1 ()I method1Ns0Rename
MP class_1 method_1 ()I 0 param1Ns0Rename0
MP class_1 method_1 ()I 0 param1Ns0Rename
FD class_1$class_2 field_2 I field2Ns0Rename0
FD class_1$class_2 field_2 I field2Ns0Rename
FD class_1$class_2 field_2 Lcls; field2Ns0Rename0
FD class_1$class_2 field_2 Lcls; field2Ns0Rename
4 changes: 2 additions & 2 deletions src/test/resources/read/repeated-elements/jobf.jobf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ m class_1.method_1()I = method1Ns0Rename0
m class_1.method_1()I = method1Ns0Rename
c class_1$class_2 = class1Ns0Rename$class2Ns0Rename0
c class_1$class_2 = class1Ns0Rename$class2Ns0Rename
f class_1$class_2.field_2:I = field2Ns0Rename0
f class_1$class_2.field_2:I = field2Ns0Rename
f class_1$class_2.field_2:Lcls; = field2Ns0Rename0
f class_1$class_2.field_2:Lcls; = field2Ns0Rename
c class_3 = class3Ns0Rename0
c class_3 = class3Ns0Rename
4 changes: 2 additions & 2 deletions src/test/resources/read/repeated-elements/proguard.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class_1 -> class1Ns0Rename:
int method_1() -> method1Ns0Rename
class_1$class_2 -> class1Ns0Rename$class2Ns0Rename0:
class_1$class_2 -> class1Ns0Rename$class2Ns0Rename:
int field_2 -> field2Ns0Rename0
int field_2 -> field2Ns0Rename
cls field_2 -> field2Ns0Rename0
cls field_2 -> field2Ns0Rename
class_3 -> class3Ns0Rename0:
class_3 -> class3Ns0Rename:
4 changes: 2 additions & 2 deletions src/test/resources/read/repeated-elements/recaf-simple.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class_1.method_1()I method1Ns0Rename0
class_1.method_1()I method1Ns0Rename
class_1$class_2 class1Ns0Rename$class2Ns0Rename0
class_1$class_2 class1Ns0Rename$class2Ns0Rename
class_1$class_2.field_2 I field2Ns0Rename0
class_1$class_2.field_2 I field2Ns0Rename
class_1$class_2.field_2 Lcls; field2Ns0Rename0
class_1$class_2.field_2 Lcls; field2Ns0Rename
class_3 class3Ns0Rename0
class_3 class3Ns0Rename
4 changes: 2 additions & 2 deletions src/test/resources/read/repeated-elements/tiny.tiny
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ METHOD class_1 ()I method_1 method1Ns0Rename0 method1Ns1Rename0
METHOD class_1 ()I method_1 method1Ns0Rename method1Ns1Rename
CLASS class_1$class_2 class1Ns0Rename$class2Ns0Rename0 class1Ns1Rename$class2Ns1Rename0
CLASS class_1$class_2 class1Ns0Rename$class2Ns0Rename class1Ns1Rename$class2Ns1Rename
FIELD class_1$class_2 I field_2 field2Ns0Rename0 field2Ns1Rename0
FIELD class_1$class_2 I field_2 field2Ns0Rename field2Ns1Rename
FIELD class_1$class_2 Lcls; field_2 field2Ns0Rename0 field2Ns1Rename0
FIELD class_1$class_2 Lcls; field_2 field2Ns0Rename field2Ns1Rename
CLASS class_3 class3Ns0Rename0 class3Ns1Rename0
CLASS class_3 class3Ns0Rename class3Ns1Rename
4 changes: 2 additions & 2 deletions src/test/resources/read/repeated-elements/tinyV2.tiny
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ c class_1$class_2 class1Ns0Rename$class2Ns0Rename0 class1Ns1Rename$class2Ns1Rena
c class_1$class_2 class1Ns0Rename$class2Ns0Rename class1Ns1Rename$class2Ns1Rename
c This is a comment.
c This is a comment
f I field_2 field2Ns0Rename0 field2Ns1Rename0
f I field_2 field2Ns0Rename field2Ns1Rename
f Lcls; field_2 field2Ns0Rename0 field2Ns1Rename0
f Lcls; field_2 field2Ns0Rename field2Ns1Rename
c class_3 class3Ns0Rename0 class3Ns1Rename0
c class_3 class3Ns0Rename class3Ns1Rename
4 changes: 2 additions & 2 deletions src/test/resources/read/repeated-elements/tsrgV2.tsrg
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class_1 class1Ns0Rename class1Ns1Rename
1 param_1 param1Ns0Rename param1Ns1Rename
class_1$class_2 class1Ns0Rename$class2Ns0Rename0 class1Ns1Rename$class2Ns1Rename0
class_1$class_2 class1Ns0Rename$class2Ns0Rename class1Ns1Rename$class2Ns1Rename
field_2 I field2Ns0Rename0 field2Ns1Rename0
field_2 I field2Ns0Rename field2Ns1Rename
field_2 Lcls; field2Ns0Rename0 field2Ns1Rename0
field_2 Lcls; field2Ns0Rename field2Ns1Rename
class_3 class3Ns0Rename0 class3Ns1Rename0
class_3 class3Ns0Rename class3Ns1Rename
4 changes: 2 additions & 2 deletions src/test/resources/read/repeated-elements/xsrg.xsrg
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ MD: class_1/method_1 ()I class1Ns0Rename/method1Ns0Rename0 ()I
MD: class_1/method_1 ()I class1Ns0Rename/method1Ns0Rename ()I
CL: class_1$class_2 class1Ns0Rename$class2Ns0Rename0
CL: class_1$class_2 class1Ns0Rename$class2Ns0Rename
FD: class_1$class_2/field_2 I class1Ns0Rename$class2Ns0Rename/field2Ns0Rename0 I
FD: class_1$class_2/field_2 I class1Ns0Rename$class2Ns0Rename/field2Ns0Rename I
FD: class_1$class_2/field_2 Lcls; class1Ns0Rename$class2Ns0Rename/field2Ns0Rename0 Lcls;
FD: class_1$class_2/field_2 Lcls; class1Ns0Rename$class2Ns0Rename/field2Ns0Rename Lcls;
CL: class_3 class3Ns0Rename0
CL: class_3 class3Ns0Rename
12 changes: 6 additions & 6 deletions src/test/resources/read/valid-with-holes/csrg.csrg
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class_32 field_4 field_4
class_32 field_5 field5Ns0Rename
class_32 field_6 field_6
class_32 method_1 ()I method_1
class_32 method_2 ()I method2Ns0Rename
class_32 method_3 ()I method_3
class_32 method_4 ()I method_4
class_32 method_5 ()I method5Ns0Rename
class_32 method_2 (I)V method2Ns0Rename
class_32 method_3 (Lcls;)Lcls; method_3
class_32 method_4 (ILcls;)Lpkg/cls; method_4
class_32 method_5 (Lcls;[I)[[B method5Ns0Rename
class_32 method_6 ()I method_6
class_32 method_7 ()I method_7
class_32 method_8 ()I method_8
class_32 method_7 (I)V method_7
class_32 method_8 (Lcls;)Lcls; method_8
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
CLASS class_32
FIELD field_1 I
FIELD field_2 field2Ns0Rename I
FIELD field_3 I
FIELD field_4 I
FIELD field_2 field2Ns0Rename Lcls;
FIELD field_3 Lpkg/cls;
FIELD field_4 [I
COMMENT This is a comment
FIELD field_5 field5Ns0Rename I
COMMENT This is a comment
FIELD field_6 I
FIELD field_6 Lcls;
COMMENT This is a comment
METHOD method_1 ()I
METHOD method_2 method2Ns0Rename ()I
METHOD method_3 ()I
METHOD method_4 ()I
METHOD method_2 method2Ns0Rename (I)V
METHOD method_3 (Lcls;)Lcls;
METHOD method_4 (ILcls;)Lpkg/cls;
COMMENT This is a comment
METHOD method_5 method5Ns0Rename ()I
METHOD method_5 method5Ns0Rename (Lcls;[I)[[B
COMMENT This is a comment
METHOD method_6 ()I
COMMENT This is a comment
METHOD method_7 ()I
METHOD method_7 (I)V
ARG 1
ARG 3
ARG 5 param3Ns0Rename
Expand All @@ -27,4 +27,4 @@ CLASS class_32
COMMENT This is a comment
ARG 11
COMMENT This is a comment
METHOD method_8 ()I
METHOD method_8 (Lcls;)Lcls;
20 changes: 10 additions & 10 deletions src/test/resources/read/valid-with-holes/enigma.mappings
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,24 @@ CLASS class_29
COMMENT This is a comment
CLASS class_32
FIELD field_1 I
FIELD field_2 field2Ns0Rename I
FIELD field_3 I
FIELD field_4 I
FIELD field_2 field2Ns0Rename Lcls;
FIELD field_3 Lpkg/cls;
FIELD field_4 [I
COMMENT This is a comment
FIELD field_5 field5Ns0Rename I
COMMENT This is a comment
FIELD field_6 I
FIELD field_6 Lcls;
COMMENT This is a comment
METHOD method_1 ()I
METHOD method_2 method2Ns0Rename ()I
METHOD method_3 ()I
METHOD method_4 ()I
METHOD method_2 method2Ns0Rename (I)V
METHOD method_3 (Lcls;)Lcls;
METHOD method_4 (ILcls;)Lpkg/cls;
COMMENT This is a comment
METHOD method_5 method5Ns0Rename ()I
METHOD method_5 method5Ns0Rename (Lcls;[I)[[B
COMMENT This is a comment
METHOD method_6 ()I
COMMENT This is a comment
METHOD method_7 ()I
METHOD method_7 (I)V
ARG 1
ARG 3
ARG 5 param3Ns0Rename
Expand All @@ -67,4 +67,4 @@ CLASS class_32
COMMENT This is a comment
ARG 11
COMMENT This is a comment
METHOD method_8 ()I
METHOD method_8 (Lcls;)Lcls;
10 changes: 5 additions & 5 deletions src/test/resources/read/valid-with-holes/jam.jam
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ CL class_7$class_8 class_7$class8Ns0Rename
CL class_13$class_14 class_13$class14Ns0Rename
CL class_17$class_18$class_19 class_17$class_18$class19Ns0Rename
CL class_26$class_27$class_28 class_26$class_27$class28Ns0Rename
FD class_32 field_2 I field2Ns0Rename
FD class_32 field_2 Lcls; field2Ns0Rename
FD class_32 field_5 I field5Ns0Rename
MD class_32 method_2 ()I method2Ns0Rename
MD class_32 method_5 ()I method5Ns0Rename
MP class_32 method_7 ()I 4 param3Ns0Rename
MP class_32 method_7 ()I 8 param5Ns0Rename
MD class_32 method_2 (I)V method2Ns0Rename
MD class_32 method_5 (Lcls;[I)[[B method5Ns0Rename
MP class_32 method_7 (I)V 4 param3Ns0Rename
MP class_32 method_7 (I)V 8 param5Ns0Rename
6 changes: 3 additions & 3 deletions src/test/resources/read/valid-with-holes/jobf.jobf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ c class_7$class_8 = class_7$class8Ns0Rename
c class_13$class_14 = class_13$class14Ns0Rename
c class_17$class_18$class_19 = class_17$class_18$class19Ns0Rename
c class_26$class_27$class_28 = class_26$class_27$class28Ns0Rename
f class_32.field_2:I = field2Ns0Rename
f class_32.field_2:Lcls; = field2Ns0Rename
f class_32.field_5:I = field5Ns0Rename
m class_32.method_2()I = method2Ns0Rename
m class_32.method_5()I = method5Ns0Rename
m class_32.method_2(I)V = method2Ns0Rename
m class_32.method_5(Lcls;[I)[[B = method5Ns0Rename
6 changes: 3 additions & 3 deletions src/test/resources/read/valid-with-holes/proguard.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class_13$class_14 -> class_13$class14Ns0Rename:
class_17$class_18$class_19 -> class_17$class_18$class19Ns0Rename:
class_26$class_27$class_28 -> class_26$class_27$class28Ns0Rename:
class_32 -> class_32:
int field_2 -> field2Ns0Rename
cls field_2 -> field2Ns0Rename
int field_5 -> field5Ns0Rename
int method_2() -> method2Ns0Rename
int method_5() -> method5Ns0Rename
void method_2(int) -> method2Ns0Rename
byte[][] method_5(cls,int[]) -> method5Ns0Rename
6 changes: 3 additions & 3 deletions src/test/resources/read/valid-with-holes/recaf-simple.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class_7$class_8 class_7$class8Ns0Rename
class_13$class_14 class_13$class14Ns0Rename
class_17$class_18$class_19 class_17$class_18$class19Ns0Rename
class_26$class_27$class_28 class_26$class_27$class28Ns0Rename
class_32.field_2 I field2Ns0Rename
class_32.field_2 Lcls; field2Ns0Rename
class_32.field_5 I field5Ns0Rename
class_32.method_2()I method2Ns0Rename
class_32.method_5()I method5Ns0Rename
class_32.method_2(I)V method2Ns0Rename
class_32.method_5(Lcls;[I)[[B method5Ns0Rename
Loading

0 comments on commit 3bc4d8e

Please sign in to comment.