From 589ff323e8e48163f06f75c863e93b3dead78c26 Mon Sep 17 00:00:00 2001 From: Juuz <6596629+Juuxel@users.noreply.github.com> Date: Tue, 6 Sep 2022 20:39:30 +0300 Subject: [PATCH] SrgMerger: Add support for comments and parameters --- .../net/fabricmc/loom/util/srg/SrgMerger.java | 33 +++++++++++++++++-- .../forge/testSrg/expectedOutput.tiny | 6 ++++ .../resources/forge/testSrg/extraInput.tsrg | 1 + src/test/resources/forge/testSrg/proguard.txt | 1 + .../resources/forge/testSrg/srgInput.tsrg | 1 + .../resources/forge/testSrg/tinyInput.tiny | 6 ++++ 6 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/fabricmc/loom/util/srg/SrgMerger.java b/src/main/java/net/fabricmc/loom/util/srg/SrgMerger.java index ad37d30c..90fbb41c 100644 --- a/src/main/java/net/fabricmc/loom/util/srg/SrgMerger.java +++ b/src/main/java/net/fabricmc/loom/util/srg/SrgMerger.java @@ -145,9 +145,11 @@ public final class SrgMerger { for (MappingTree.ClassMapping srgClass : srg.getClasses()) { String[] dstNames = createDstNameArray(srgClass); MappingTree.ClassMapping tinyClass = src.getClass(srgClass.getSrcName()); + String comment = null; if (tinyClass != null) { copyDstNames(dstNames, tinyClass); + comment = tinyClass.getComment(); } else if (lenient) { // Tiny class not found, we'll just use srg names fillMappings(dstNames, srgClass); @@ -156,6 +158,7 @@ public final class SrgMerger { } flatOutput.visitClass(srgClass.getSrcName(), dstNames); + if (comment != null) flatOutput.visitClassComment(srgClass.getSrcName(), comment); for (MappingTree.FieldMapping field : srgClass.getFields()) { mergeField(srgClass, field, tinyClass); @@ -175,6 +178,7 @@ public final class SrgMerger { String[] dstNames = createDstNameArray(srgField); MappingTree.FieldMapping tinyField = null; String srcDesc = srgField.getSrcDesc(); + String comment = null; if (tinyClass != null) { if (srcDesc != null) { @@ -189,12 +193,14 @@ public final class SrgMerger { if (tinyField != null) { copyDstNames(dstNames, tinyField); srcDesc = tinyField.getSrcDesc(); + comment = tinyField.getComment(); } else { fillMappings(dstNames, srgField); } if (srcDesc != null) { flatOutput.visitField(srgClass.getSrcName(), srgField.getSrcName(), srcDesc, dstNames); + if (comment != null) flatOutput.visitFieldComment(srgClass.getSrcName(), srgField.getSrcName(), srcDesc, comment); } else if (!lenient) { throw new MappingException("Could not find descriptor for field " + srgClass.getDstName(0) + '.' + srgField.getDstName(0)); } @@ -204,6 +210,7 @@ public final class SrgMerger { String[] dstNames = createDstNameArray(srgMethod); MappingTree.MethodMapping tinyMethod = null; String intermediaryName, namedName; + String comment = null; if (tinyClass != null) { tinyMethod = tinyClass.getMethod(srgMethod.getSrcName(), srgMethod.getSrcDesc()); @@ -215,6 +222,7 @@ public final class SrgMerger { copyDstNames(dstNames, tinyMethod); intermediaryName = tinyMethod.getName("intermediary"); namedName = tinyMethod.getName("named"); + comment = tinyMethod.getComment(); } else { if (srgMethod.getSrcName().equals(srgMethod.getDstName(0))) { // These are only methods like or toString which have the same name in every NS. @@ -251,6 +259,26 @@ public final class SrgMerger { } flatOutput.visitMethod(srgClass.getSrcName(), srgMethod.getSrcName(), srgMethod.getSrcDesc(), dstNames); + if (comment != null) flatOutput.visitMethodComment(srgClass.getSrcName(), srgMethod.getSrcName(), srgMethod.getSrcDesc(), comment); + + if (tinyMethod != null) { + for (MappingTree.MethodArgMapping arg : tinyMethod.getArgs()) { + String[] argDstNames = new String[output.getDstNamespaces().size()]; + copyDstNames(argDstNames, arg); + flatOutput.visitMethodArg( + srgClass.getSrcName(), srgMethod.getSrcName(), srgMethod.getSrcDesc(), + arg.getArgPosition(), arg.getLvIndex(), arg.getSrcName(), argDstNames + ); + + if (arg.getComment() != null) { + flatOutput.visitMethodArgComment( + srgClass.getSrcName(), srgMethod.getSrcName(), srgMethod.getSrcDesc(), + arg.getArgPosition(), arg.getLvIndex(), arg.getSrcName(), + arg.getComment() + ); + } + } + } } /** @@ -334,8 +362,9 @@ public final class SrgMerger { * The SRG names will add a new namespace called {@code srg} so that the final namespaces * are {@code official, srg, intermediary, named}. * - *

This method does not care about method parameters, local variables or javadoc comments. - * As such, it shouldn't be used for remapping the game jar. + *

This method does not include local variables in the output. + * It can, however, be used for remapping the game jar since it has all + * classes, methods, fields, parameters and javadoc comments. * *

If {@code lenient} is true, the merger will not error when encountering names not present * in the tiny mappings. Instead, the names will be filled from the {@code official} namespace. diff --git a/src/test/resources/forge/testSrg/expectedOutput.tiny b/src/test/resources/forge/testSrg/expectedOutput.tiny index 6fae24e8..fe8fc68c 100644 --- a/src/test/resources/forge/testSrg/expectedOutput.tiny +++ b/src/test/resources/forge/testSrg/expectedOutput.tiny @@ -1,8 +1,14 @@ tiny 2 0 official srg intermediary named c a test/FakeClass class_1 test/SomeClass + c commented class f I a f_1_ field_1 myInt + c commented field m ()I a m_1_ method_1 getMyInt m ()V evilUnobfMethod m_3_ method_2 differentNamedName + m (I)V a m_5_ method_5 setMyInt + c commented method + p 1 myInt + c commented param c b test/FakeSubclass class_2 test/SomeSubclass c test/DeObfClass test/DeObfClass test/DeObfClass test/DeObfClass m ()V iAmNotObfuscated m_2_ iAmNotObfuscated iAmNotObfuscated diff --git a/src/test/resources/forge/testSrg/extraInput.tsrg b/src/test/resources/forge/testSrg/extraInput.tsrg index 941d9cb5..c4e00e63 100644 --- a/src/test/resources/forge/testSrg/extraInput.tsrg +++ b/src/test/resources/forge/testSrg/extraInput.tsrg @@ -4,6 +4,7 @@ a test/FakeClass a I fakeField a ()I getFakeField evilUnobfMethod ()V evilUnobfMethod + a (I)V setFakeField b test/FakeSubclass ()V evilUnobfMethod ()V evilUnobfMethod diff --git a/src/test/resources/forge/testSrg/proguard.txt b/src/test/resources/forge/testSrg/proguard.txt index fa14cc07..f4ef29bd 100644 --- a/src/test/resources/forge/testSrg/proguard.txt +++ b/src/test/resources/forge/testSrg/proguard.txt @@ -3,6 +3,7 @@ test.FakeClass -> a: int fakeField -> a int getFakeField() -> a void evilUnobfMethod() -> evilUnobfMethod + void setFakeField(int) -> a test.FakeSubclass -> b: void () -> void evilUnobfMethod() -> evilUnobfMethod diff --git a/src/test/resources/forge/testSrg/srgInput.tsrg b/src/test/resources/forge/testSrg/srgInput.tsrg index 8a370613..08690dad 100644 --- a/src/test/resources/forge/testSrg/srgInput.tsrg +++ b/src/test/resources/forge/testSrg/srgInput.tsrg @@ -4,6 +4,7 @@ a test/FakeClass a I f_1_ a ()I m_1_ evilUnobfMethod ()V m_3_ + a (I)V m_5_ b test/FakeSubclass ()V a ()I m_1_ diff --git a/src/test/resources/forge/testSrg/tinyInput.tiny b/src/test/resources/forge/testSrg/tinyInput.tiny index 91352e94..e7dcb5d9 100644 --- a/src/test/resources/forge/testSrg/tinyInput.tiny +++ b/src/test/resources/forge/testSrg/tinyInput.tiny @@ -1,8 +1,14 @@ tiny 2 0 official intermediary named c a class_1 test/SomeClass + c commented class f I a field_1 myInt + c commented field m ()I a method_1 getMyInt m ()V evilUnobfMethod method_2 differentNamedName + m (I)V a method_5 setMyInt + c commented method + p 1 myInt + c commented param c b class_2 test/SomeSubclass c c class_3 test/RelatedA m ()V a method_3 fromClassA