-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
InspectVanillaJar.kt
290 lines (251 loc) · 9.43 KB
/
InspectVanillaJar.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* paperweight is a Gradle plugin for the PaperMC project.
*
* Copyright (c) 2021 Kyle Wood (DenWav)
* Contributors
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 only, no later versions.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package io.papermc.paperweight.tasks
import io.papermc.paperweight.util.AsmUtil
import io.papermc.paperweight.util.MavenArtifact
import io.papermc.paperweight.util.SyntheticUtil
import io.papermc.paperweight.util.defaultOutput
import io.papermc.paperweight.util.isLibraryJar
import io.papermc.paperweight.util.openZip
import io.papermc.paperweight.util.path
import io.papermc.paperweight.util.walk
import kotlin.io.path.*
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.Classpath
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.TaskAction
import org.objectweb.asm.ClassReader
import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.FieldVisitor
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Opcodes
import org.objectweb.asm.tree.MethodNode
@CacheableTask
abstract class InspectVanillaJar : BaseTask() {
@get:Classpath
abstract val inputJar: RegularFileProperty
@get:Classpath
abstract val libraries: ConfigurableFileCollection
@get:InputFile
@get:PathSensitive(PathSensitivity.NONE)
abstract val mcLibraries: RegularFileProperty
@get:OutputFile
abstract val loggerFile: RegularFileProperty
@get:OutputFile
abstract val syntheticMethods: RegularFileProperty
@get:OutputFile
abstract val serverLibraries: RegularFileProperty
override fun init() {
loggerFile.convention(defaultOutput("$name-loggerFields", "txt"))
syntheticMethods.convention(defaultOutput("$name-syntheticMethods", "txt"))
}
@TaskAction
fun run() {
val loggers = mutableListOf<LoggerFields.Data>()
val synthMethods = mutableListOf<SyntheticMethods.Data>()
var visitor: ClassVisitor
visitor = LoggerFields.Visitor(null, loggers)
visitor = SyntheticMethods.Visitor(null, synthMethods)
inputJar.path.openZip().use { inJar ->
val rootMatcher = inJar.getPathMatcher("glob:/*.class")
val nmsMatcher = inJar.getPathMatcher("glob:/net/minecraft/**/*.class")
inJar.walk().use { stream ->
stream.filter { p -> !p.isDirectory() }
.filter { p -> rootMatcher.matches(p) || nmsMatcher.matches(p) }
.map { p -> p.readBytes() }
.forEach { data ->
ClassReader(data).accept(visitor, 0)
}
}
}
val serverLibs = checkLibraries()
loggerFile.path.bufferedWriter(Charsets.UTF_8).use { writer ->
loggers.sort()
for (loggerField in loggers) {
writer.append(loggerField.className)
writer.append(' ')
writer.append(loggerField.fieldName)
writer.newLine()
}
}
syntheticMethods.path.bufferedWriter(Charsets.UTF_8).use { writer ->
synthMethods.sort()
for ((className, desc, synthName, baseName) in synthMethods) {
writer.append(className)
writer.append(' ')
writer.append(desc)
writer.append(' ')
writer.append(synthName)
writer.append(' ')
writer.append(baseName)
writer.newLine()
}
}
serverLibraries.path.bufferedWriter(Charsets.UTF_8).use { writer ->
serverLibs.map { it.toString() }.sorted().forEach { artifact ->
writer.appendLine(artifact)
}
}
}
private fun checkLibraries(): Set<MavenArtifact> {
val mcLibs = mcLibraries.path.useLines { lines ->
lines.map { MavenArtifact.parse(it) }
.map { it.file to it }
.toMap()
}
val serverLibs = mutableSetOf<MavenArtifact>()
val libs = libraries.files.asSequence()
.map { f -> f.toPath() }
.filter { p -> p.isLibraryJar }
.toList()
inputJar.path.openZip().use { jar ->
for (libFile in libs) {
val artifact = mcLibs[libFile.name] ?: continue
libFile.openZip().use lib@{ libFs ->
val containsClass = libFs.walk().use { stream ->
stream.filter { p -> p.name.endsWith(".class") }
.anyMatch { p -> jar.getPath(p.absolutePathString()).exists() }
}
if (containsClass) {
serverLibs += artifact
return@lib
}
}
}
}
return serverLibs
}
}
abstract class BaseClassVisitor(classVisitor: ClassVisitor?) : ClassVisitor(Opcodes.ASM8, classVisitor), AsmUtil {
protected var currentClass: String? = null
override fun visit(
version: Int,
access: Int,
name: String,
signature: String?,
superName: String?,
interfaces: Array<out String>?
) {
this.currentClass = name
super.visit(version, access, name, signature, superName, interfaces)
}
}
/*
* SpecialSource2 automatically maps all Logger fields to the name LOGGER, without needing mappings defined, so we need
* to make a note of all of those fields
*/
object LoggerFields {
class Visitor(
classVisitor: ClassVisitor?,
private val fields: MutableList<Data>
) : BaseClassVisitor(classVisitor) {
override fun visitField(
access: Int,
name: String,
descriptor: String,
signature: String?,
value: Any?
): FieldVisitor? {
val ret = super.visitField(access, name, descriptor, signature, value)
val className = currentClass ?: return ret
if (Opcodes.ACC_STATIC !in access || Opcodes.ACC_FINAL !in access) {
return ret
}
if (descriptor != "Lorg/apache/logging/log4j/Logger;") {
return ret
}
fields += Data(className, name)
return ret
}
}
data class Data(val className: String, val fieldName: String) : Comparable<Data> {
override fun compareTo(other: Data) = compareValuesBy(
this,
other,
{ it.className },
{ it.fieldName }
)
}
}
/*
* SpecialSource2 automatically handles certain synthetic method renames, which leads to methods which don't match any
* existing mapping. We need to make a note of all of the synthetic methods which match SpecialSource2's checks so we
* can handle it in our generated mappings.
*/
object SyntheticMethods {
class Visitor(
classVisitor: ClassVisitor?,
private val methods: MutableList<Data>
) : BaseClassVisitor(classVisitor) {
override fun visitMethod(
access: Int,
name: String,
descriptor: String,
signature: String?,
exceptions: Array<out String>?
): MethodVisitor? {
val ret = super.visitMethod(access, name, descriptor, signature, exceptions)
val className = currentClass ?: return ret
if (Opcodes.ACC_SYNTHETIC !in access || Opcodes.ACC_BRIDGE in access || name.contains('$')) {
return ret
}
return SynthMethodVisitor(access, name, descriptor, signature, exceptions, className, methods)
}
}
private class SynthMethodVisitor(
access: Int,
name: String,
descriptor: String,
signature: String?,
exceptions: Array<out String>?,
private val className: String,
private val methods: MutableList<Data>
) : MethodNode(Opcodes.ASM9, access, name, descriptor, signature, exceptions) {
override fun visitEnd() {
val (baseName, baseDesc) = SyntheticUtil.findBaseMethod(this, className)
if (baseName != name || baseDesc != desc) {
// Add this method as a synthetic for baseName
methods += Data(className, baseDesc, name, baseName)
}
}
}
data class Data(
val className: String,
val desc: String,
val synthName: String,
val baseName: String
) : Comparable<Data> {
override fun compareTo(other: Data) = compareValuesBy(
this,
other,
{ it.className },
{ it.desc },
{ it.synthName },
{ it.baseName }
)
}
}