Skip to content

Commit

Permalink
Make @PlatformOnly work with fields
Browse files Browse the repository at this point in the history
Signed-off-by: shedaniel <[email protected]>
  • Loading branch information
shedaniel committed Nov 28, 2021
1 parent 9e705df commit 9956be0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
base_version=5.1
base_version=5.2
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import dev.architectury.transformer.util.Logger;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldNode;
import org.objectweb.asm.tree.MethodNode;

import java.util.Iterator;
Expand All @@ -42,28 +43,47 @@ public ClassNode doEdit(String name, ClassNode node) {
return node;
}

Iterator<MethodNode> iter = node.methods.iterator();
while (iter.hasNext()) {
MethodNode method = iter.next();
Iterator<MethodNode> methodsIter = node.methods.iterator();
while (methodsIter.hasNext()) {
MethodNode method = methodsIter.next();
AnnotationNode annotation = Optional.ofNullable(method.invisibleAnnotations)
.flatMap(nodes -> nodes.stream().filter(a ->
a.desc.equals(RemapInjectables.PLATFORM_ONLY_LEGACY) || a.desc.equals(RemapInjectables.PLATFORM_ONLY)).findAny())
.orElse(null);
if (shouldRemove(annotation, platform)) {
methodsIter.remove();
}
}

Iterator<FieldNode> fieldsIter = node.fields.iterator();
while (fieldsIter.hasNext()) {
FieldNode field = fieldsIter.next();
AnnotationNode annotation = Optional.ofNullable(field.invisibleAnnotations)
.flatMap(nodes -> nodes.stream().filter(a ->
a.desc.equals(RemapInjectables.PLATFORM_ONLY)).findAny())
.orElse(null);
if (shouldRemove(annotation, platform)) {
fieldsIter.remove();
}
}

return node;
}

private static boolean shouldRemove(AnnotationNode annotation, String platform) {
if (annotation == null) return false;

for (int i = 0; i < annotation.values.size(); i += 2) {
String key = (String) annotation.values.get(i);

if (annotation == null) continue;

for (int i = 0; i < annotation.values.size(); i += 2) {
String key = (String) annotation.values.get(i);

if (key.equals("value")) {
List<?> platforms = (List<?>) annotation.values.get(i + 1);
if (platforms.stream().map(String::valueOf).noneMatch(platform::equals)) {
iter.remove();
}
if (key.equals("value")) {
List<?> platforms = (List<?>) annotation.values.get(i + 1);
if (platforms.stream().map(String::valueOf).noneMatch(platform::equals)) {
return true;
}
}
}

return node;
return false;
}
}

0 comments on commit 9956be0

Please sign in to comment.