fix remaining issues, improve speed

This commit is contained in:
moehreag 2024-05-28 14:15:21 +02:00
parent 80a9fc3fdc
commit 67c253f2f0
3 changed files with 75 additions and 78 deletions

View file

@ -26,7 +26,7 @@ dependencies {
implementation("com.google.code.gson:gson:2.10.1")
implementation("org.vineflower:vineflower:1.10.1")
testImplementation(kotlin("test"))
implementation("com.electronwill.night-config:toml:3.7.1")
implementation("com.electronwill.night-config:toml:3.7.2")
implementation("com.google.jimfs:jimfs:1.3.0")
}

View file

@ -7,12 +7,15 @@ import org.gradle.api.Project
import org.gradle.api.plugins.JavaPluginExtension
import org.objectweb.asm.*
import java.nio.charset.StandardCharsets
import java.nio.file.*
import java.nio.file.attribute.BasicFileAttributes
import java.nio.file.FileSystems
import java.nio.file.Path
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentSkipListSet
import java.util.regex.Pattern
import java.util.stream.Stream
import kotlin.io.path.bufferedReader
import kotlin.io.path.exists
import kotlin.io.path.readBytes
import kotlin.io.path.readLines
import kotlin.io.path.writeBytes
object AccessWidener {
@ -21,7 +24,6 @@ object AccessWidener {
private var awHash = ""
private val HEADER = Pattern.compile("accessWidener\\s+v[12]\\s+.*").asMatchPredicate()
private val COMMENT = Pattern.compile("^#.*").asMatchPredicate()
private val SEPARATOR = "[\\t ]+".toRegex()
private fun getAWFile(project: Project): Path? {
@ -58,37 +60,39 @@ object AccessWidener {
return false
}
private fun readAW(project: Project): List<String> {
private fun readAW(project: Project): Stream<String> {
val awFile = getAWFile(project)
println("Found accesswidener in project: $awFile")
return awFile?.readLines(StandardCharsets.UTF_8)?.map { it.replace("transitive-", "") }?.toList()
?: listOf()
return awFile?.bufferedReader(StandardCharsets.UTF_8)?.lines()?.map { it.replace("transitive-", "") }
?: Stream.empty()
}
private fun readTransitiveAW(path: Path): List<String> {
return path.bufferedReader(StandardCharsets.UTF_8).lines().filter { it.startsWith("transitive-") }.toList()
private fun readTransitiveAW(path: Path): Stream<String> {
return path.bufferedReader(StandardCharsets.UTF_8).lines().filter { it.startsWith("transitive-") }
}
private fun parseAW(entries: List<String>): List<Entry> {
return entries.asSequence().filter { it.isNotBlank() }
.filter { !COMMENT.test(it) }.filter { !HEADER.test(it) }.distinct()
.map { it.split(SEPARATOR) }.filter { it.isNotEmpty() } .map { Entry(it) }.toList()
}
private fun readAllAWs(project: Project): List<Entry> {
return findDependencyAWs(project).flatMap { readTransitiveAW(it) }.plus(readAW(project))
.let { parseAW(it) }
private fun readAllAWs(project: Project): Stream<Entry> {
return Stream.concat(
findDependencyAWs(project).parallelStream().flatMap { i -> readTransitiveAW(i) },
readAW(project)
)
.filter { it.isNotBlank() }
.map { if (it.contains("#")) it.split("#")[0] else it }.filter { !HEADER.test(it) }
.filter { it.isNotBlank() }
.unordered().distinct()
.map { it.split(SEPARATOR) }.filter { it.isNotEmpty() }.map { Entry(it) }
}
fun apply(project: Project, input: Path) {
val startTime = System.currentTimeMillis()
val entries: List<Entry> = readAllAWs(project)
val classMap: MutableMap<String, Entry> = mutableMapOf()
val methods: MutableMap<String, MutableMap<String, Entry>> = mutableMapOf()
val fields: MutableMap<String, MutableMap<String, Entry>> = mutableMapOf()
val mutations: MutableMap<String, MutableMap<String, Entry>> = mutableMapOf()
val classNames: MutableSet<String> = ConcurrentSkipListSet()
val classMap: MutableMap<String, Entry> = ConcurrentHashMap()
val methods: MutableMap<String, MutableMap<String, Entry>> = ConcurrentHashMap()
val fields: MutableMap<String, MutableMap<String, Entry>> = ConcurrentHashMap()
val mutations: MutableMap<String, MutableMap<String, Entry>> = ConcurrentHashMap()
val entries = readAllAWs(project)
entries.forEach { e ->
classNames.add(e.className)
if ("class" == e.targetType) {
if (e.type == AccessType.MUTABLE) {
throw IllegalArgumentException("aw format error: classes can not have a 'mutable' modifier (at: $e)")
@ -105,7 +109,7 @@ object AccessWidener {
if (e.type == AccessType.MUTABLE) {
throw IllegalArgumentException("aw format error: methods can not have a 'mutable' modifier (at: $e)")
}
val map = methods.computeIfAbsent(e.className) { mutableMapOf() }
val map = methods.computeIfAbsent(e.className) { ConcurrentHashMap() }
val id = e.name + e.descriptor
if (!map.containsKey(id)) {
map[id] = e
@ -119,13 +123,11 @@ object AccessWidener {
if (e.type == AccessType.EXTENDABLE) {
throw IllegalArgumentException("aw format error: fields can not have a 'extendable' modifier (at: $e)")
}
val map = fields.computeIfAbsent(e.className) { mutableMapOf() }
val map = fields.computeIfAbsent(e.className) { ConcurrentHashMap() }
val id = e.name + e.descriptor
if (e.type == AccessType.MUTABLE) {
mutations.computeIfAbsent(e.className) { mutableMapOf() }.putIfAbsent(id, e)
return
}
mutations.computeIfAbsent(e.className) { ConcurrentHashMap() }.putIfAbsent(id, e)
} else {
if (!map.containsKey(id)) {
map[id] = e
} else {
@ -136,11 +138,28 @@ object AccessWidener {
}
}
}
}
FileSystems.newFileSystem(input).use {
Files.walkFileTree(it.getPath("/"), AWFileVisitor(classMap, fields, methods, mutations))
classNames.parallelStream().forEach { className ->
val file = it.getPath("/$className.class")
if (file.exists()) {
val reader = ClassReader(file.readBytes())
val writer = ClassWriter(0)
val mapper = AWClassVisitor(writer, classMap, fields, methods, mutations, className)
reader.accept(mapper, 0)
file.writeBytes(writer.toByteArray())
}
println("Finished widening ${classMap.size} classes, ${methods.size} methods and ${fields.size+mutations.size} fields in ${"%2f".format(System.currentTimeMillis()-startTime)}")
}
}
println(
"Applied AccessWideners in ${
"%.2fs".format(
(System.currentTimeMillis() - startTime) / 1000f
)
}"
)
}
}
@ -171,7 +190,8 @@ enum class AccessType(val id: String, val access: Int) {
companion object {
fun of(name: String): AccessType {
return entries.first { a -> a.id == name }
return entries.firstOrNull { a -> a.id == name }
?: throw IllegalArgumentException("Access type '$name' is not known. Known types: ${entries.map { it.id }} (and transitive variants)")
}
}
}
@ -195,31 +215,6 @@ private enum class Access(val index: Int) {
}
}
class AWFileVisitor(
private val classMap: Map<String, Entry>,
private val fields: Map<String, Map<String, Entry>>,
private val methods: Map<String, Map<String, Entry>>,
private val mutations: Map<String, Map<String, Entry>>,
) : SimpleFileVisitor<Path>() {
override fun visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult {
if (file.fileName.toString().endsWith(".class")) {
val className = file.toString().substring(1, file.toString().length - 6)
if (classMap.containsKey(className) || fields.containsKey(className) || methods.containsKey(className) || mutations.containsKey(className)) {
val reader = ClassReader(file.readBytes())
val writer = ClassWriter(0)
val mapper = AWClassVisitor(writer, classMap, fields, methods, mutations, className)
reader.accept(mapper, 0)
file.writeBytes(writer.toByteArray())
}
}
return FileVisitResult.CONTINUE
}
}
class AWClassVisitor(
visitor: ClassVisitor,
private val classMap: Map<String, Entry>,
@ -239,14 +234,15 @@ class AWClassVisitor(
var access = acc
val e = classMap[className]
if (e != null) {
access = access and ((Opcodes.ACC_PRIVATE or Opcodes.ACC_PROTECTED or Opcodes.ACC_PUBLIC).inv())
access = access.or(e.type.access)
} else if (fields.containsKey(className) || methods.containsKey(className) || mutations.containsKey(
access = access and (Opcodes.ACC_PRIVATE or Opcodes.ACC_PROTECTED or Opcodes.ACC_PUBLIC).inv()
access = access or e.type.access
}
if (fields.containsKey(className) || methods.containsKey(className) || mutations.containsKey(
className
)
) { // make all classes with modifications public as well
access = access and ((Opcodes.ACC_PRIVATE or Opcodes.ACC_PROTECTED or Opcodes.ACC_PUBLIC).inv())
access = access.or(Opcodes.ACC_PUBLIC)
access = access and (Opcodes.ACC_PRIVATE or Opcodes.ACC_PROTECTED or Opcodes.ACC_PUBLIC).inv()
access = access or Opcodes.ACC_PUBLIC
}
super.visit(version, access, name, signature, superName, interfaces)
}
@ -263,15 +259,16 @@ class AWClassVisitor(
if (map != null) {
val e = map[name + descriptor]
if (e != null) {
access = access and ((Opcodes.ACC_PRIVATE or Opcodes.ACC_PROTECTED or Opcodes.ACC_PUBLIC).inv())// remove all access modifiers
access = access.or(e.type.access)// re-add the new one
access =
access and (Opcodes.ACC_PRIVATE or Opcodes.ACC_PROTECTED or Opcodes.ACC_PUBLIC).inv() // remove all access modifiers
access = access or e.type.access // re-add the new one
}
}
map = mutations[className]
if (map != null) {
val e = map[name + descriptor]
if (e != null) {
access = access.or(Opcodes.ACC_FINAL.inv()) // always AccessType.MUTABLE
access = access and Opcodes.ACC_FINAL.inv() // always AccessType.MUTABLE
}
}
return super.visitField(access, name, descriptor, signature, value)
@ -289,8 +286,9 @@ class AWClassVisitor(
if (map != null) {
val e = map[name + descriptor]
if (e != null) {
access = access and ((Opcodes.ACC_PRIVATE or Opcodes.ACC_PROTECTED or Opcodes.ACC_PUBLIC).inv())// remove all access modifiers
access = access.or(e.type.access)// re-add the new one
access =
access and ((Opcodes.ACC_PRIVATE or Opcodes.ACC_PROTECTED or Opcodes.ACC_PUBLIC).inv())// remove all access modifiers
access = access or (e.type.access)// re-add the new one
}
}
return super.visitMethod(access, name, descriptor, signature, exceptions)

View file

@ -28,8 +28,8 @@ fun Project.minecraft(
val remappedJar = clientJar.resolveSibling("client-$version-remapped.jar")
PhytotelmaPlugin.remappedGameJarPath = remappedJar
println("Time to setup Minecraft!")
var applyAW = false
if (remappedJar.notExists() || AccessWidener.needsUpdate(this)) {
val applyAW = AccessWidener.needsUpdate(this)
if (remappedJar.notExists() || applyAW) {
println("Remapping the game...")
val data = ProguardParser.read(MojmapProvider.get(version, clientJar.resolveSibling("client-$version.txt")).orElseThrow()).reverse()
val paramMappings = ParchmentProvider.getParchment(
@ -37,7 +37,6 @@ fun Project.minecraft(
PhytotelmaPlugin.nonsenseCacheDir.resolve("org/parchmentmc/parchment/$version/$parchmentVersion")
)
NonsenseRemapper.remap(data, clientJar, remappedJar, true, paramMappings)
applyAW = true
}
println("Adding dependencies...")
dependencies.add("implementation","net.minecrell:terminalconsoleappender:1.2.0")