fix bugs, speed up processing by only reading modified classes

This commit is contained in:
moehreag 2024-05-28 12:18:54 +02:00
parent 1fdb459433
commit 1dcd0c8c05
2 changed files with 138 additions and 129 deletions

View file

@ -1,3 +1,5 @@
accessWidener v2 named
accessible method net/minecraft/world/level/block/TransparentBlock codec ()Lcom/mojang/serialization/MapCodec;
mutable field net/minecraft/client/gui/GuiSpriteManager METADATA_SECTIONS Ljava/util/Set;
mutable field net/minecraft/client/gui/GuiGraphics$ScissorStack stack Ljava/util/Deque;

View file

@ -3,21 +3,25 @@ package org.ecorous.esnesnon.nonsense.loader.impl.plugin.game.minecraft;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import org.ecorous.esnesnon.nonsense.loader.api.mod.ModProperties;
import org.ecorous.esnesnon.nonsense.loader.impl.mod.BuiltinExtensions;
import org.objectweb.asm.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Minimal implementation of accesswideners.
* (Untested)
*/
class AWProcessor {
@ -26,141 +30,145 @@ class AWProcessor {
private static final Predicate<String> HEADER = Pattern.compile("accessWidener\\s+v[12]\\s+.*").asMatchPredicate();
private static final Predicate<String> COMMENT = Pattern.compile("^#.*").asMatchPredicate();
private static final String SEPARATOR = "[\\t ]+";
private static final Logger LOGGER = LoggerFactory.getLogger("AccessWidener");
static void apply(Collection<ModProperties> mods, Path input, Path output) throws IOException {
List<Entry> entries = mods.parallelStream().map(ModProperties::extensions).map(e -> (String)e.get(AW_EXTENSION_NAME))
.filter(Objects::nonNull).map(s -> "/"+s).map(AWProcessor.class::getResourceAsStream).filter(Objects::nonNull)
long startTime = System.currentTimeMillis();
Map<String, Entry> classMap = new ConcurrentHashMap<>();
Map<String, Map<String, Entry>> methods = new ConcurrentHashMap<>();
Map<String, Map<String, Entry>> fields = new ConcurrentHashMap<>();
Map<String, Map<String, Entry>> mutations = new ConcurrentHashMap<>();
Set<String> classNames = new ConcurrentSkipListSet<>();
mods.parallelStream().map(ModProperties::extensions).map(e -> (String) e.get(AW_EXTENSION_NAME))
.filter(Objects::nonNull).map(s -> "/" + s).map(AWProcessor.class::getResourceAsStream).filter(Objects::nonNull)
.map(InputStreamReader::new).map(BufferedReader::new).flatMap(BufferedReader::lines)
.filter(l -> !l.isBlank()).filter(l -> !COMMENT.test(l)).filter(l -> !HEADER.test(l)).distinct()
.map(l -> l.replace("transitive-", "")) // ignore all transitive declarations (just make them normal) as they're only relevant for dev envs
.map(l -> l.split(SEPARATOR)).filter(l -> l.length > 0).map(Entry::new).toList();
.map(l -> l.split(SEPARATOR)).filter(l -> l.length > 0).map(Entry::new).forEach(e -> {
classNames.add(e.className);
if ("class".equals(e.targetType)) {
if (e.type == AccessType.MUTABLE) {
throw new IllegalArgumentException("aw format error: classes can not have a 'mutable' modifier (at: " + e + ")");
}
if (!classMap.containsKey(e.className)) {
classMap.put(e.className, e);
} else {
var other = classMap.get(e.className);
if (e.isAccessGreaterThan(other)) {
classMap.put(e.className, e);
}
}
} else if ("method".equals(e.targetType)) {
if (e.type == AccessType.MUTABLE) {
throw new IllegalArgumentException("aw format error: methods can not have a 'mutable' modifier (at: " + e + ")");
}
var map = methods.computeIfAbsent(e.className, s -> new ConcurrentHashMap<>());
var id = e.name + e.descriptor;
if (!map.containsKey(id)) {
map.put(id, e);
} else {
var other = map.get(id);
if (e.isAccessGreaterThan(other)) {
classMap.put(id, e);
}
}
} else if ("field".equals(e.targetType)) {
if (e.type == AccessType.EXTENDABLE) {
throw new IllegalArgumentException("aw format error: fields can not have a 'extendable' modifier (at: " + e + ")");
}
var map = fields.computeIfAbsent(e.className, s -> new ConcurrentHashMap<>());
var id = e.name + e.descriptor;
if (e.type == AccessType.MUTABLE) {
mutations.computeIfAbsent(e.className, s -> new ConcurrentHashMap<>()).putIfAbsent(id, e);
return;
}
System.out.println(entries.stream().map(Entry::toString).collect(Collectors.joining()));
Map<String, Entry> classMap = new HashMap<>();
Map<String, Map<String, Entry>> methods = new HashMap<>();
Map<String, Map<String, Entry>> fields = new HashMap<>();
Map<String, Map<String, Entry>> mutations = new HashMap<>();
entries.forEach(e -> {
if ("class".equals(e.targetType)) {
if (e.type == AccessType.MUTABLE){
throw new IllegalArgumentException("aw format error: classes can not have a 'mutable' modifier (at: "+e+")");
}
if (!classMap.containsKey(e.className)) {
classMap.put(e.className, e);
} else {
var other = classMap.get(e.className);
if (e.isAccessGreaterThan(other)) {
classMap.put(e.className, e);
if (!map.containsKey(id)) {
map.put(id, e);
} else {
var other = map.get(id);
if (e.isAccessGreaterThan(other)) {
classMap.put(id, e);
}
}
}
}
} else if ("method".equals(e.targetType)) {
if (e.type == AccessType.MUTABLE){
throw new IllegalArgumentException("aw format error: methods can not have a 'mutable' modifier (at: "+e+")");
}
var map = methods.computeIfAbsent(e.className, s -> new HashMap<>());
var id = e.name + e.descriptor;
if (!map.containsKey(id)) {
map.put(id, e);
} else {
var other = map.get(id);
if (e.isAccessGreaterThan(other)) {
classMap.put(id, e);
}
}
} else if ("field".equals(e.targetType)) {
if (e.type == AccessType.EXTENDABLE){
throw new IllegalArgumentException("aw format error: fields can not have a 'extendable' modifier (at: "+e+")");
}
var map = fields.computeIfAbsent(e.className, s -> new HashMap<>());
var id = e.name + e.descriptor;
if (e.type == AccessType.MUTABLE){
mutations.computeIfAbsent(e.className, s -> new HashMap<>()).putIfAbsent(id, e);
return;
}
if (!map.containsKey(id)) {
map.put(id, e);
} else {
var other = map.get(id);
if (e.isAccessGreaterThan(other)) {
classMap.put(id, e);
}
}
}
});
});
Files.deleteIfExists(output);
Files.copy(input, output);
try (FileSystem in = FileSystems.newFileSystem(input.toAbsolutePath());
FileSystem out = FileSystems.newFileSystem(output, Map.of("create", "true"))) {
if (!classNames.isEmpty()) {
try (FileSystem out = FileSystems.newFileSystem(output)) {
Files.walkFileTree(in.getPath("/"), new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Path output = out.getPath(file.toString());
Files.createDirectories(output.getParent());
var className = file.toString().substring(1, file.toString().length() - 6);
if (file.getFileName().toString().endsWith(".class") && (classMap.containsKey(className) || fields.containsKey(className) || methods.containsKey(className) || mutations.containsKey(className))) {
ClassReader reader = new ClassReader(Files.newInputStream(file));
ClassWriter writer = new ClassWriter(0);
ClassVisitor mapper = new ClassVisitor(Opcodes.ASM9, writer) {
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
Entry e = classMap.get(className);
if (e != null) {
access &= ~(Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED | Opcodes.ACC_PUBLIC);
access |= e.type.access;
} else if (fields.containsKey(className) || methods.containsKey(className) || mutations.containsKey(className)) { // make all classes with modifications public as well
access &= ~(Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED | Opcodes.ACC_PUBLIC);
access |= Opcodes.ACC_PUBLIC;
}
super.visit(version, access, name, signature, superName, interfaces);
}
@Override
public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) {
Map<String, Entry> map = fields.get(className);
if (map != null) {
Entry e = map.get(name + descriptor);
if (e != null) {
access &= ~(Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED | Opcodes.ACC_PUBLIC); // remove all access modifiers
access |= e.type.access; // re-add the new one
}
}
if ((map = mutations.get(className)) != null){
var e = map.get(name+descriptor);
if (e != null) {
access |= ~Opcodes.ACC_FINAL; // always AccessType.MUTABLE
}
}
return super.visitField(access, name, descriptor, signature, value);
}
@Override
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
Map<String, Entry> map = methods.get(className);
if (map != null) {
Entry e = map.get(name + descriptor);
if (e != null) {
int old = access;
access &= ~(Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED | Opcodes.ACC_PUBLIC);
access |= e.type.access;
System.out.println("set access of "+name+" from "+old+" to "+access);
}
}
return super.visitMethod(access, name, descriptor, signature, exceptions);
}
};
reader.accept(mapper, 0);
Files.write(output, writer.toByteArray());
} else {
Files.copy(file, output);
classNames.parallelStream().forEach(name -> {
try {
processFile(out.getPath("/" + name + ".class"), classMap, fields, methods, mutations);
} catch (IOException e) {
throw new Error(e);
}
return FileVisitResult.CONTINUE;
});
}
}
LOGGER.info(String.format("Applied AccessWideners in %.2fs", (System.currentTimeMillis() - startTime) / 1000f));
}
private static void processFile(Path file, Map<String, Entry> classMap, Map<String, Map<String, Entry>> fields, Map<String, Map<String, Entry>> methods, Map<String, Map<String, Entry>> mutations) throws IOException {
var className = file.toString().substring(1, file.toString().length() - 6);
if (file.getFileName().toString().endsWith(".class") && (classMap.containsKey(className) || fields.containsKey(className) || methods.containsKey(className) || mutations.containsKey(className))) {
ClassReader reader = new ClassReader(Files.newInputStream(file));
ClassWriter writer = new ClassWriter(0);
ClassVisitor mapper = new ClassVisitor(Opcodes.ASM9, writer) {
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
Entry e = classMap.get(className);
if (e != null) {
access &= ~(Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED | Opcodes.ACC_PUBLIC);
access |= e.type.access;
}
if (fields.containsKey(className) || methods.containsKey(className) || mutations.containsKey(className)) { // make all classes with modifications public as well
access &= ~(Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED | Opcodes.ACC_PUBLIC);
access |= Opcodes.ACC_PUBLIC;
}
super.visit(version, access, name, signature, superName, interfaces);
}
});
@Override
public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) {
Map<String, Entry> map = fields.get(className);
if (map != null) {
Entry e = map.get(name + descriptor);
if (e != null) {
access &= ~(Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED | Opcodes.ACC_PUBLIC); // remove all access modifiers
access |= e.type.access; // re-add the new one
}
}
if ((map = mutations.get(className)) != null) {
var e = map.get(name + descriptor);
if (e != null) {
access &= ~Opcodes.ACC_FINAL; // always AccessType.MUTABLE
}
}
return super.visitField(access, name, descriptor, signature, value);
}
@Override
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
Map<String, Entry> map = methods.get(className);
if (map != null) {
Entry e = map.get(name + descriptor);
if (e != null) {
access &= ~(Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED | Opcodes.ACC_PUBLIC);
access |= e.type.access;
}
}
return super.visitMethod(access, name, descriptor, signature, exceptions);
}
};
reader.accept(mapper, 0);
Files.write(file, writer.toByteArray());
}
}
@ -179,8 +187,7 @@ class AWProcessor {
private enum AccessType {
ACCESSIBLE("accessible", Opcodes.ACC_PUBLIC),
EXTENDABLE("extendable", Opcodes.ACC_PROTECTED),
MUTABLE("mutable", ~Opcodes.ACC_FINAL)
;
MUTABLE("mutable", ~Opcodes.ACC_FINAL);
private final String id;
private final int access;