forked from Blayyke/yarn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
530 lines (431 loc) · 14.3 KB
/
build.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
buildscript {
repositories {
jcenter()
maven {
name "Modmuss50 Repository"
url 'https://maven.modmuss50.me'
}
maven {
name "Jitpack"
url 'https://jitpack.io/'
}
}
dependencies {
classpath "cuchaz:enigma:0.14.1.130"
classpath 'com.github.Chocohead:Stitch:fc82ff6'
classpath "commons-io:commons-io:2.6"
classpath "com.google.guava:guava:28.0-jre"
classpath 'de.undercouch:gradle-download-task:3.4.3'
classpath 'net.fabricmc:tiny-remapper:0.1.0.40' // TODO anything above this fails, bandaid fix.
classpath 'com.github.Chocohead:tiny-mappings-parser:337c8b6'
}
}
plugins {
id 'de.undercouch.download' version '3.4.3'
id 'maven'
id 'maven-publish'
}
def minecraft_version = "1.2.5"
def ENV = System.getenv()
// Fetch build number from Jenkins
def build_number = ENV.BUILD_NUMBER ?: "local"
def yarnVersion = "${minecraft_version}+build.$build_number"
repositories {
mavenCentral()
maven {
name "Modmuss50 Repository"
url 'https://maven.modmuss50.me'
}
maven {
name "Jitpack"
url 'https://jitpack.io/'
}
}
configurations {
enigmaRuntime {
resolutionStrategy {
cacheDynamicVersionsFor 0, "seconds"
cacheChangingModulesFor 0, "seconds"
}
}
}
dependencies {
enigmaRuntime 'com.github.Chocohead:Stitch:fc82ff6'
enigmaRuntime "cuchaz:enigma:0.14.1.130"
}
def mappingsDir = file("mappings")
def cacheFilesMinecraft = file(".gradle/minecraft")
def tempDir = file(".gradle/temp")
def mergedJar = file("${minecraft_version}-merged.jar")
def clientIntermediaryJar = new File(cacheFilesMinecraft, "${minecraft_version}-client-intermediary.jar")
def serverIntermediaryJar = new File(cacheFilesMinecraft, "${minecraft_version}-server-intermediary.jar")
def intermediaryJar = file("${minecraft_version}-intermediary.jar")
def namedJar = file("${minecraft_version}-named.jar")
def versionFile = new File(cacheFilesMinecraft, "${minecraft_version}.json")
def clientJar = new File(cacheFilesMinecraft, "${minecraft_version}-client.jar")
def serverJar = new File(cacheFilesMinecraft, "${minecraft_version}-server.jar")
def libraries = new File(cacheFilesMinecraft, "${minecraft_version}-libraries")
def libs = new File(buildDir, "libs")
import groovy.io.FileType
import groovy.json.JsonSlurper
import org.apache.commons.io.FileUtils
import com.google.common.hash.Hashing
import com.google.common.io.Files
import cuchaz.enigma.command.CheckMappingsCommand
import cuchaz.enigma.command.ConvertMappingsCommand
import net.fabricmc.stitch.commands.CommandFixNesting
import net.fabricmc.stitch.commands.CommandMergeTiny
import net.fabricmc.stitch.commands.CommandRewriteIntermediary
import net.fabricmc.stitch.merge.JarMerger
import net.fabricmc.tinyremapper.OutputConsumerPath
import net.fabricmc.tinyremapper.TinyRemapper
import net.fabricmc.tinyremapper.TinyUtils
import net.fabricmc.mappings.ClassEntry
import net.fabricmc.mappings.MethodEntry
import net.fabricmc.mappings.FieldEntry
import net.fabricmc.mappings.EntryTriple
import net.fabricmc.mappings.Mappings
import net.fabricmc.mappings.MappingsProvider
import net.fabricmc.tinyremapper.IMappingProvider
import net.fabricmc.tinyremapper.MemberInstance
import java.util.zip.GZIPOutputStream
boolean validateChecksum(File file, String checksum) {
if (file != null) {
def hash = Files.asByteSource(file).hash(Hashing.sha1())
def builder = new StringBuilder()
hash.asBytes().each {
builder.append(Integer.toString((it & 0xFF) + 0x100, 16).substring(1))
}
return builder.toString().equals(checksum)
}
return false
}
task downloadVersionsManifest {
//inputs.property "mc_ver", minecraft_version
inputs.property "currenttime", new Date()
def manifestFile = new File(cacheFilesMinecraft, "version_manifest.json")
outputs.file(manifestFile)
doLast {
logger.lifecycle(":downloading minecraft versions manifest")
FileUtils.copyURLToFile(new URL("https://launchermeta.mojang.com/mc/game/version_manifest.json"), manifestFile)
}
}
def getManifestVersion(File manifestFile, String minecraft_version) {
def manifest = manifestFile.exists() ? new JsonSlurper().parseText(FileUtils.readFileToString(manifestFile)) : null
return manifest != null ? manifest.versions.stream().filter({
it.id.equals(minecraft_version)
}).findFirst() : java.util.Optional.empty()
}
task downloadWantedVersionManifest(dependsOn: downloadVersionsManifest) {
def manifestFile = downloadVersionsManifest.outputs.files.singleFile
def manifestVersion = getManifestVersion(manifestFile, minecraft_version)
//have to grab the release time as there's a current timestamp on each element?!
inputs.property "releaseTime", manifestVersion.isPresent() ? manifestVersion.get().releaseTime : -1
outputs.file versionFile
doLast {
manifestVersion = getManifestVersion(manifestFile, minecraft_version)
//nb need to re-read here in case it didn't exist before
if (manifestVersion.isPresent() || versionFile.exists()) {
if (manifestVersion.isPresent()) {
FileUtils.copyURLToFile(new URL(manifestVersion.get().url), versionFile)
}
} else {
throw new RuntimeException("No version data for Minecraft version ${minecraft_version}")
}
}
}
task downloadMcJars(dependsOn: downloadWantedVersionManifest) {
inputs.files versionFile
outputs.files(clientJar, serverJar)
outputs.upToDateWhen {
def version = new JsonSlurper().parseText(FileUtils.readFileToString(versionFile))
return clientJar.exists() && serverJar.exists() && validateChecksum(clientJar, version.downloads.client.sha1) && validateChecksum(serverJar, version.downloads.server.sha1)
}
doLast {
if (!versionFile.exists()) {
throw new RuntimeException("Can't download the jars without the ${versionFile.name} file!")
}
//reload in case it changed
def version = new JsonSlurper().parseText(FileUtils.readFileToString(versionFile))
logger.lifecycle(":downloading minecraft jars")
if (!clientJar.exists() || !validateChecksum(clientJar, version.downloads.client.sha1)) {
logger.lifecycle(":downloading minecraft client")
FileUtils.copyURLToFile(new URL(version.downloads.client.url), clientJar)
}
if (!serverJar.exists() || !validateChecksum(serverJar, version.downloads.server.sha1)) {
logger.lifecycle(":downloading minecraft server")
FileUtils.copyURLToFile(new URL(version.downloads.server.url), serverJar)
}
}
}
task downloadMcLibs(dependsOn: downloadWantedVersionManifest) {
inputs.files versionFile
outputs.file(libraries)
outputs.upToDateWhen {
return false
}
doLast {
if (!versionFile.exists()) {
throw new RuntimeException("Can't download the jars without the ${versionFile.name} file!")
}
def version = new JsonSlurper().parseText(FileUtils.readFileToString(versionFile))
logger.lifecycle(":downloading minecraft libraries")
if (!libraries.exists()) {
libraries.mkdirs()
}
version.libraries.each {
def downloadUrl = it.downloads?.artifact?.url
if (downloadUrl == null) return
download {
src downloadUrl
dest new File(libraries, downloadUrl.substring(downloadUrl.lastIndexOf("/") + 1))
overwrite false
}
}
}
}
task downloadIntermediary(type: Download) {
//TODO: Don't hardcode me to just 1.2.5 Intermediaries
def url = "https://gist.githubusercontent.com/Chocohead/b7ea04058776495a93ed2d13f34d697a/raw/1.2.5 Merge.tiny"
src url.replaceAll(" ", "%20")
dest new File(cacheFilesMinecraft, "${minecraft_version}-intermediary.tiny")
}
task remapClientJar(dependsOn: [downloadMcJars, downloadMcLibs, downloadIntermediary]) {
inputs.file clientJar
inputs.files downloadMcLibs.outputs.files.files
outputs.file clientIntermediaryJar
doLast {
logger.lifecycle(":mapping client to intermediary")
mapJar(clientIntermediaryJar, clientJar, downloadIntermediary.dest, libraries, "client", "intermediary")
}
}
task remapServerJar(dependsOn: [downloadMcJars, downloadMcLibs, downloadIntermediary]) {
inputs.file serverJar
inputs.files downloadMcLibs.outputs.files.files
outputs.file serverIntermediaryJar
doLast {
logger.lifecycle(":mapping server to intermediary")
mapJar(serverIntermediaryJar, serverJar, downloadIntermediary.dest, libraries, "server", "intermediary")
}
}
task mergeJars(dependsOn: [remapClientJar, remapServerJar]) {
inputs.files([clientIntermediaryJar, serverIntermediaryJar])
outputs.file(mergedJar)
doLast {
logger.lifecycle(":merging jars")
if (mergedJar.exists()) {
return
}
def jarMerger = new JarMerger(clientIntermediaryJar, serverIntermediaryJar, mergedJar)
jarMerger.merge()
jarMerger.close()
CommandFixNesting.run(mergedJar)
}
}
task setupYarn(dependsOn: mergeJars) {
}
task yarn(dependsOn: setupYarn) {
doLast {
ant.java(
classname: 'cuchaz.enigma.Main',
classpath: configurations.enigmaRuntime.asPath,
fork: true,
spawn: true
) {
jvmarg(value: "-Xmx2048m")
arg(value: '-jar')
arg(value: mergedJar.getAbsolutePath())
arg(value: '-mappings')
arg(value: mappingsDir.getAbsolutePath())
arg(value: '-profile')
arg(value: 'enigma_profile.json')
}
}
}
task buildEnigma(type: Zip) {
from mappingsDir
include "**/*"
archiveName "yarn-enigma-${yarnVersion}.zip"
destinationDir(libs)
}
task checkMappings {
inputs.dir mappingsDir
doLast {
logger.lifecycle(":checking mappings")
String[] args = [
mergedJar.getAbsolutePath(),
mappingsDir.getAbsolutePath()
]
new CheckMappingsCommand().run(args)
}
}
task buildYarnTiny(dependsOn: "mergeJars", type: FileOutput) {
inputs.dir mappingsDir
if (!libs.exists()) {
libs.mkdirs()
}
def yarnTiny = new File(tempDir, "yarn-mappings.tiny")
fileOutput = yarnTiny
outputs.upToDateWhen { return false }
doLast {
logger.lifecycle(":generating tiny mappings")
String[] args = [
"enigma",
mappingsDir.getAbsolutePath(),
"tiny:intermediary:named",
yarnTiny.getAbsolutePath()
]
new ConvertMappingsCommand().run(args)
}
}
task mergeTiny(dependsOn: [downloadIntermediary, buildYarnTiny], type: FileOutput) {
def yarnTinyInput = buildYarnTiny.fileOutput
def outputFile = new File(tempDir, "mappings.tiny")
outputs.file(outputFile)
fileOutput = outputFile
outputs.upToDateWhen { return false }
doLast {
logger.lifecycle(":merging yarn and intermediary")
String[] args = [
downloadIntermediary.dest.getAbsolutePath(),
yarnTinyInput.getAbsolutePath(),
outputFile.getAbsolutePath()
]
new CommandMergeTiny().run(args)
}
}
task tinyJar(type: Jar, dependsOn: "mergeTiny") {
outputs.upToDateWhen { return false }
archiveName = "yarn-${yarnVersion}.jar"
destinationDir(libs)
classifier = ""
from(mergeTiny.fileOutput) {
rename { "mappings/mappings.tiny" }
}
}
task compressTiny(dependsOn: ["tinyJar", "mergeTiny"], type: FileOutput) {
def outputFile = new File(libs, "yarn-tiny-${yarnVersion}.gz")
outputs.file(outputFile)
fileOutput = outputFile
def inputFile = mergeTiny.fileOutput
outputs.upToDateWhen { return false }
doLast {
logger.lifecycle(":compressing tiny mappings")
def buffer = new byte[1024]
def fileOutputStream = new FileOutputStream(outputFile)
def outputStream = new GZIPOutputStream(fileOutputStream)
def fileInputStream = new FileInputStream(inputFile)
def length
while ((length = fileInputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length)
}
fileInputStream.close()
outputStream.finish()
outputStream.close()
inputFile.delete()
}
}
clean.doFirst {
delete tempDir, cacheFilesMinecraft
}
tasks.build.dependsOn "compressTiny"
tasks.build.dependsOn "buildEnigma"
tasks.build.dependsOn "tinyJar"
task mapIntermediaryJar(dependsOn: [mergeJars, mergeTiny]) {
inputs.files downloadMcLibs.outputs.files.files
outputs.file(intermediaryJar)
//Force the task to always run
outputs.upToDateWhen {
return false
}
doLast {
logger.lifecycle(":mapping minecraft to intermdiary")
mapJar(intermediaryJar, clientJar, mergeTiny.fileOutput, libraries, "official", "intermediary")
}
}
task mapNamedJar(dependsOn: [mergeJars, mergeTiny]) {
inputs.files downloadMcLibs.outputs.files.files
outputs.file(namedJar)
//Force the task to always run
outputs.upToDateWhen {
return false
}
doLast {
logger.lifecycle(":mapping minecraft to named")
mapJar(namedJar, mergedJar, mergeTiny.fileOutput, libraries, "intermediary", "named")
}
}
publishing {
publications {
maven(MavenPublication) {
groupId 'net.fabricmc'
artifactId "yarn"
version yarnVersion
artifact(compressTiny.fileOutput) {
classifier "tiny"
builtBy compressTiny
}
artifact(buildEnigma) {
classifier "enigma"
}
artifact(tinyJar)
}
}
repositories {
maven {
url "http://mavenupload.modmuss50.me/"
if (project.hasProperty('mavenPass')) {
credentials {
username 'buildslave'
password project.getProperty('mavenPass')
}
}
}
}
}
IMappingProvider createMappingsProvider(input, from, to) {
Mappings mappings = MappingsProvider.readTinyMappings(java.nio.file.Files.newInputStream(input), false)
return {classMap, fieldMap, methodMap ->
for (ClassEntry entry : mappings.getClassEntries()) {
classMap.put(entry.get(from), entry.get(to));
}
for (FieldEntry entry : mappings.getFieldEntries()) {
EntryTriple fromTriple = entry.get(from);
if (fromTriple == null) continue;
fieldMap.put(fromTriple.getOwner() + "/" + MemberInstance.getFieldId(fromTriple.getName(), fromTriple.getDesc()), entry.get(to).getName());
}
for (MethodEntry entry : mappings.getMethodEntries()) {
EntryTriple fromTriple = entry.get(from);
if (fromTriple == null) continue;
methodMap.put(fromTriple.getOwner() + "/" + MemberInstance.getMethodId(fromTriple.getName(), fromTriple.getDesc()), entry.get(to).getName());
}
};
}
void mapJar(File output, File input, File mappings, File libraries, String from, String to) {
if (output.exists()) {
output.delete()
}
def remapper = TinyRemapper.newRemapper()
.withMappings(createMappingsProvider(mappings.toPath(), from, to))
.renameInvalidLocals(true)
.rebuildSourceFilenames(true)
.build()
try {
def outputConsumer = new OutputConsumerPath(output.toPath())
outputConsumer.addNonClassFiles(input.toPath())
remapper.readInputs(input.toPath())
libraries.eachFileRecurse(FileType.FILES) { file ->
remapper.readClassPath(file.toPath())
}
remapper.apply(outputConsumer)
outputConsumer.close()
remapper.finish()
} catch (Exception e) {
remapper.finish()
throw new RuntimeException("Failed to remap jar", e);
}
}
class FileOutput extends DefaultTask {
@OutputFile
File fileOutput
}