-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclassremapper.gradle
264 lines (227 loc) · 9.29 KB
/
classremapper.gradle
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
buildscript {
repositories {
maven { url = 'https://maven.minecraftforge.net' }
mavenCentral()
}
dependencies {
classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '5.+', changing: true
classpath 'com.google.code.gson:gson:2.8.7'
}
}
repositories {
maven { url = 'https://maven.minecraftforge.net' }
mavenCentral()
}
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import net.minecraftforge.gradle.common.tasks.ApplyRangeMap
import net.minecraftforge.gradle.common.tasks.ExtractExistingFiles
import net.minecraftforge.gradle.common.tasks.ExtractRangeMap
import net.minecraftforge.gradle.common.util.MinecraftRepo
import net.minecraftforge.srgutils.IMappingBuilder
import net.minecraftforge.srgutils.IMappingFile
import net.minecraftforge.srgutils.IRenamer
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
import java.io.InputStream
import java.io.InputStreamReader
import java.net.URL
def doingUpdate = Boolean.parseBoolean(findProperty('UPDATE_CLASSNAMES'))
def isReversed = Boolean.parseBoolean(findProperty('REVERSED'))
if (doingUpdate) {
afterEvaluate { project ->
def javaCompile = project.tasks.getByName('compileJava') as JavaCompile
def sourceSets = project.sourceSets
Set<File> srcDirs = new HashSet<>()
(project.findProperty('UPDATE_SOURCESETS') ?: 'main').toString().split(';').each {
srcDirs.addAll(sourceSets.getByName(it).java.srcDirs)
}
if (srcDirs.empty)
throw new RuntimeException("No java source directories found to update!")
task extractRangeMap(type: ExtractRangeMap) {
sources.from srcDirs
dependencies.from javaCompile.getClasspath()
}
task chainClassnames(type: CreateChainedClassnames) {
def version = project.getExtensions().getExtraProperties().get('MC_VERSION') as String
if (!version) throw new IllegalStateException('The minecraft version could not be found!')
version = version.substring(version.lastIndexOf('-') + 1)
// mcVersion = version
reversed = isReversed
// input = tasks.extractSrg.output
}
task applyRangeMap(type: ApplyRangeMap) {
rangeMap = tasks.extractRangeMap.output
srgFiles.from chainClassnames.output //, tasks.createMcpToSrg.output
sources.from srcDirs
}
task extractMappedNew(type: ExtractExistingFiles) {
archive = tasks.applyRangeMap.output
targets.from srcDirs
}
task chainOldToNewSrg(type: ChainOldToNewSrg) {
dependsOn(chainClassnames)
reversed = isReversed
renamer = chainClassnames.output.map {
def mappings = IMappingFile.load(it.asFile)
if (isReversed) mappings = mappings.reverse()
return new IRenamer() {
@Override
String rename(IMappingFile.IClass value) {
return mappings.remapClass(value.getOriginal())
}
}
}
}
task updateAts(type: UpdateATs) {
if (minecraft) accessTransformers.from minecraft.accessTransformers
mappings = chainOldToNewSrg.output
}
task updateClassnames {
dependsOn(extractMappedNew, updateAts)
}
defaultTasks 'updateClassnames'
}
}
abstract class CreateChainedClassnames extends DefaultTask {
private static final String MANIFEST_URL = "https://launchermeta.mojang.com/mc/game/version_manifest.json"
private static final Gson GSON = new GsonBuilder().create()
// @Input abstract Property<String> getMcVersion()
// @InputFile abstract RegularFileProperty getInput()
@OutputFile abstract RegularFileProperty getOutput()
@Input @Optional abstract Property<Boolean> getReversed()
CreateChainedClassnames() {
output.convention(project.layout.buildDirectory.dir(name).map { d -> d.file('chainedmappings.tsrg') })
}
@TaskAction
void doTask() {
// def mojToObf = IMappingFile.load(getMojmaps(mcVersion.get()))
def mojToObf = IMappingFile.load(getMojmaps('1.16.5'))
def obfToSrg = ChainOldToNewSrg.getJoinedSrg(ChainOldToNewSrg.OLD_SRG)
def mojToSrg = mojToObf.chain(obfToSrg)
def builder = IMappingBuilder.create()
mojToSrg.getClasses().each { builder.addClass(it.mapped, it.original) }
def built = builder.build().getMap('left', 'right')
built.write(output.get().asFile.toPath(), IMappingFile.Format.TSRG2, reversed.getOrElse(false))
}
static InputStream getMojmaps(String version) {
def manIn = new URL(MANIFEST_URL).openStream()
try {
def url = GSON.fromJson(new InputStreamReader(manIn), ManifestJson.class).getUrl(version)
def verIn = url.openStream()
try {
def json = GSON.fromJson(new InputStreamReader(verIn), VersionJson.class)
def download = json.downloads.get("client_mappings")
return download.url.openStream()
} finally {
verIn.close()
}
} finally {
manIn.close()
}
}
}
abstract class ChainOldToNewSrg extends DefaultTask {
public static final NEW_SRG = 'https://maven.minecraftforge.net/de/oceanlabs/mcp/mcp_config/1.16.5-20210303.130956/mcp_config-1.16.5-20210303.130956.zip'
public static final OLD_SRG = 'https://maven.minecraftforge.net/de/oceanlabs/mcp/mcp_config/1.16.5-20210115.111550/mcp_config-1.16.5-20210115.111550.zip'
@OutputFile abstract RegularFileProperty getOutput()
@Input @Optional abstract Property<Boolean> getReversed()
@Internal abstract Property<IRenamer> getRenamer()
ChainOldToNewSrg() {
output.convention(project.layout.buildDirectory.dir(name).map { d -> d.file('chained.tsrg') })
}
@TaskAction
void doTask() {
def newSrg = getJoinedSrg(NEW_SRG)
def oldSrg = getJoinedSrg(OLD_SRG)
def chained = oldSrg.reverse().chain(newSrg)
def renamer = renamer.getOrNull()
if (renamer) chained = chained.rename(renamer)
chained.write(output.get().asFile.toPath(), IMappingFile.Format.TSRG2, reversed.getOrElse(false))
}
static getJoinedSrg(url) {
def baos = new ByteArrayOutputStream()
def zis = new ZipInputStream(new URL(url).openStream())
try {
ZipEntry entry
while ((entry = zis.getNextEntry()) != null) {
if (entry.name != 'config/joined.tsrg') continue
def buf = new byte[1024]
int length
while ((length = zis.read(buf)) > 0) {
baos.write(buf, 0, length)
}
}
return IMappingFile.load(new ByteArrayInputStream(baos.toByteArray()))
} finally {
baos.close()
zis.close()
}
}
}
abstract class UpdateATs extends DefaultTask {
@InputFile abstract RegularFileProperty getMappings()
@InputFiles abstract ConfigurableFileCollection getAccessTransformers()
@TaskAction
void doTask() {
def mappings = IMappingFile.load(mappings.get().asFile)
for (def file : accessTransformers.getFiles()) {
if (!file.exists()) continue
def text = file.text
def crlf = text.contains('\r')
def lines = text.split('\r?\n')
def newLines = lines.collect { line ->
def original = line
if (!line) return original
def idx = line.indexOf('#')
def comment = idx == -1 ? null : line.substring(idx)
line = idx == -1 ? line : line.substring(0, idx)
if (!line) return original
def split = line.split(' ')
if (split.length < 2) return original
def cls = mappings.getClass(split[1].replace('.', '/'))
if (cls) split[1] = cls.getMapped().replace('/', '.')
if (cls && split.length >= 3) {
def entry = split[2]
idx = entry.indexOf('(')
if (idx == -1) {
split[2] = cls.remapField(entry)
} else {
def mtdName = entry.substring(0, idx)
def desc = entry.substring(idx)
def newMethod = cls.getMethod(mtdName, desc)
split[2] = newMethod ? newMethod.mapped + newMethod.mappedDescriptor : entry
}
}
return String.join(' ', split) + (comment ? ' ' + comment : '')
}
file.text = String.join(crlf ? '\r\n' : '\n', newLines)
}
}
}
class ManifestJson {
VersionInfo[] versions
static class VersionInfo {
String id
URL url
}
URL getUrl(String version) {
if (version == null) {
return null
}
for (VersionInfo info : versions) {
if (version == info.id) {
return info.url
}
}
return null
}
}
class VersionJson {
Map<String, Download> downloads
static class Download {
String sha1
int size
URL url
}
}