aboutsummaryrefslogtreecommitdiff
path: root/build.gradle.kts
blob: 8fa8dd146ab21c8b2d1c76b96b5108cc14c83998 (plain)
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
import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import cuchaz.enigma.command.ConvertMappingsCommand
import moe.nea.zwirn.Zwirn
import net.fabricmc.stitch.commands.tinyv2.TinyV2Reader
import net.fabricmc.stitch.commands.tinyv2.TinyV2Writer
import net.fabricmc.tinyremapper.OutputConsumerPath
import net.fabricmc.tinyremapper.TinyRemapper
import net.fabricmc.tinyremapper.TinyUtils
import net.minecraftforge.srgutils.IMappingFile
import net.minecraftforge.srgutils.INamedMappingFile
import java.net.URL
import java.nio.file.FileSystems
import java.util.regex.Pattern
import java.util.zip.ZipFile


buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
        maven("https://maven.architectury.dev")
        maven("https://maven.fabricmc.net")
        maven("https://maven.minecraftforge.net")
    }
    dependencies {
        classpath("moe.nea:zwirn:1.0-SNAPSHOT")
        classpath("cuchaz:enigma-cli:2.4.1")
    }
}

plugins {
    java
}

group = "moe.nea"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
    maven("https://maven.minecraftforge.net") {
        this.metadataSources {
            artifact()
        }
    }
    maven("https://maven.fabricmc.net")
}

val minecraftVersion = "1.8.9"
val mcpVersion = "22-1.8.9"
val combinedMappingsDir = file("build/mappings")
val overlayMappingsDir = file("mappings")

val mcpSrg = configurations.detachedConfiguration(dependencies.create("de.oceanlabs.mcp:mcp:$minecraftVersion:srg@zip"))
val enigmaSwing = configurations.detachedConfiguration(dependencies.create("cuchaz:enigma-swing:2.4.1:all"))
val mcpStable = configurations.detachedConfiguration(dependencies.create("de.oceanlabs.mcp:mcp_stable:$mcpVersion@zip"))

abstract class DownloadMinecraft : DefaultTask() {
    @get:Input
    abstract val version: Property<String>

    @get:OutputFile
    abstract val destination: RegularFileProperty

    @TaskAction
    fun downloadMinecraft() {
        val gson = Gson()
        val meta =
            URL("https://launchermeta.mojang.com/mc/game/version_manifest.json").openStream().bufferedReader().use {
                gson.fromJson(it, JsonObject::class.java)
            }
        val version = (meta["versions"] as JsonArray).map { it as JsonObject }.find {
            it["id"].asString == version.get()
        }
        val versionUrl = version!!["url"].asString
        val versionManifest = URL(versionUrl).openStream().bufferedReader().use {
            gson.fromJson(it, JsonObject::class.java)
        }
        val downloadUrl =
            versionManifest.getAsJsonObject("downloads").getAsJsonObject("client").getAsJsonPrimitive("url").asString
        destination.get().asFile.outputStream().use { os ->
            URL(downloadUrl).openStream().use { ins -> ins.copyTo(os) }
        }
    }
}

val downloadMinecraft by tasks.register("downloadMinecraft", DownloadMinecraft::class) {
    destination.set(layout.buildDirectory.file("minecraft-obf.jar"))
    version.set(minecraftVersion)
}


abstract class SeargeToTiny : DefaultTask() {
    @get:InputFiles
    abstract var srgArchive: FileCollection

    @get:OutputFile
    abstract val srgTinyFile: RegularFileProperty

    @TaskAction
    fun load() {
        val zipFile = ZipFile(srgArchive.singleFile)
        val srgFile = INamedMappingFile.load(zipFile.getInputStream(zipFile.getEntry("joined.srg")))
        zipFile.close()
        srgFile.write(srgTinyFile.get().asFile.toPath(), IMappingFile.Format.TINY)
    }
}

abstract class EnrichMcp : DefaultTask() {
    @get:InputFiles
    abstract var mcpArchive: FileCollection

    @get: InputFile
    abstract val srgTinyFile: RegularFileProperty

    @get:OutputFile
    abstract val mcpOutputFile: RegularFileProperty

    @TaskAction
    fun merge() {
        val mcpFs = FileSystems.newFileSystem(mcpArchive.singleFile.toPath())
        val enriched =
            Zwirn.enrichSeargeWithMCP(TinyV2Reader.read(srgTinyFile.get().asFile.toPath()), mcpFs.getPath("/"))
        mcpFs.close()
        TinyV2Writer.write(enriched, mcpOutputFile.asFile.get().toPath())
    }
}

abstract class MapJarTask : DefaultTask() {
    @get:InputFiles
    abstract var inputJar: FileCollection

    @get:InputFile
    abstract val inputTinyFile: RegularFileProperty

    @get:Input
    abstract val inputNamespace: Property<String>

    @get:Input
    abstract val outputNamespace: Property<String>

    @get:OutputFile
    abstract val outputJar: RegularFileProperty

    @TaskAction
    fun mapJar() {
        val remapper = TinyRemapper.newRemapper().withMappings(
            TinyUtils.createTinyMappingProvider(
                inputTinyFile.get().asFile.toPath(), inputNamespace.get(), outputNamespace.get()
            )
        ).renameInvalidLocals(true).rebuildSourceFilenames(true).invalidLvNamePattern(Pattern.compile("\\$\\$\\d+"))
            .inferNameFromSameLvIndex(true).build()
        OutputConsumerPath.Builder(outputJar.get().asFile.toPath()).build().use { outputConsumer ->
            remapper.readInputsAsync(inputJar.singleFile.toPath())
            remapper.apply(outputConsumer)
            remapper.finish()
        }
    }
}

abstract class ReorderNamespaces : DefaultTask() {
    @get:InputFile
    abstract val inputTiny: RegularFileProperty

    @get:OutputFile
    abstract val outputTiny: RegularFileProperty

    @get:Input
    abstract val namespaceOrder: ListProperty<String>


    @TaskAction
    fun reorderNamespaces() {
        val old = TinyV2Reader.read(inputTiny.asFile.get().toPath())
        val new = Zwirn.renameNamespaces(old, namespaceOrder.get().map { Zwirn.RenameCommand(it, it) })
        TinyV2Writer.write(new, outputTiny.get().asFile.toPath())
    }
}

abstract class UnpackMappings : DefaultTask() {
    @get:InputFile
    abstract val inputTiny: RegularFileProperty

    @get:OutputDirectory
    abstract val outputEnigma: DirectoryProperty

    @TaskAction
    fun reorderNamespaces() {
        ConvertMappingsCommand().run(
            "tinyv2", inputTiny.asFile.get().absolutePath, "enigma", outputEnigma.get().asFile.absolutePath
        )
    }
}

abstract class RepackMappings : DefaultTask() {
    @get:InputDirectory
    abstract val inputEnigma: DirectoryProperty

    @get:OutputFile
    abstract val outputTiny: RegularFileProperty

    @get:Input
    abstract val obfuscatedNamespace: Property<String>

    @get:Input
    abstract val namedNamespace: Property<String>

    @TaskAction
    fun reorderNamespaces() {
        ConvertMappingsCommand().run(
            "enigma",
            inputEnigma.asFile.get().absolutePath,
            "tinyv2:${obfuscatedNamespace.get()}:${namedNamespace.get()}",
            outputTiny.get().asFile.absolutePath
        )
    }
}

abstract class FixFieldDescriptors : DefaultTask() {
    @get:InputFile
    abstract val jarInFirstNameSpace: RegularFileProperty

    @get:InputFile
    abstract val tinyFileIn: RegularFileProperty

    @get:OutputFile
    abstract val tinyFileOut: RegularFileProperty

    @TaskAction
    fun fixFields() {
        val tinyIn = TinyV2Reader.read(tinyFileIn.get().asFile.toPath())
        val fs = FileSystems.newFileSystem(jarInFirstNameSpace.get().asFile.toPath())
        val fixed = Zwirn.fixFieldDescriptorsFromJar(tinyIn, fs.getPath("/"))
        fs.close()
        TinyV2Writer.write(fixed, tinyFileOut.get().asFile.toPath())
    }
}

val seargeTiny by tasks.register("seargeTiny", SeargeToTiny::class) {
    srgArchive = (mcpSrg)
    srgTinyFile.set(layout.buildDirectory.file("mcpSrg.tiny"))
}

val fixFieldDescriptors by tasks.register("fixFieldDescriptor", FixFieldDescriptors::class) {
    tinyFileIn.set(seargeTiny.srgTinyFile)
    tinyFileOut.set(layout.buildDirectory.file("mcpSrgWithFields.tiny"))
    jarInFirstNameSpace.set(downloadMinecraft.destination)
}

val enrichMcp by tasks.register("enrichMcp", EnrichMcp::class) {
    mcpArchive = mcpStable
    srgTinyFile.set(fixFieldDescriptors.tinyFileOut)
    mcpOutputFile.set(layout.buildDirectory.file("mcpEnriched.tiny"))
}

val generateOverlayTiny by tasks.register("overlayTiny", RepackMappings::class) {
    inputEnigma.set(overlayMappingsDir)
    outputTiny.set(layout.buildDirectory.file("overlay.tiny"))
    obfuscatedNamespace.set("searge")
    namedNamespace.set("mcp")
}

abstract class MergeTinies : DefaultTask() {
    @get:InputFile
    abstract val baseTiny: RegularFileProperty

    @get:InputFile
    abstract val overlayTiny: RegularFileProperty

    @get:Input
    abstract val sharedNamespace: Property<String>

    @get:OutputFile
    abstract val outputTiny: RegularFileProperty

    @TaskAction
    fun mergeTinies() {
        val merged = Zwirn.mergeTinyFile(
            TinyV2Reader.read(baseTiny.get().asFile.toPath()),
            TinyV2Reader.read(overlayTiny.get().asFile.toPath()),
            sharedNamespace.get()
        )
        TinyV2Writer.write(merged, outputTiny.get().asFile.toPath())
    }
}

val mergeMcpAndOverlay by tasks.register("mergeMcpAndOverlay", MergeTinies::class) {
    this.baseTiny.set(enrichMcp.mcpOutputFile)
    this.overlayTiny.set(generateOverlayTiny.outputTiny)
    this.sharedNamespace.set("searge")
    this.outputTiny.set(layout.buildDirectory.file("mcpCummedOn.tiny"))
}

val tinyFromMergedEnigma by tasks.register("tinyFromMergedEnigma", RepackMappings::class) {
    this.namedNamespace.set("mcp")
    this.obfuscatedNamespace.set("searge")
    this.inputEnigma.set(combinedMappingsDir)
    this.outputTiny.set(layout.buildDirectory.file("fromWork.tiny"))
}

abstract class DiffTinyFile : DefaultTask() {
    @get:InputFile
    abstract val mergedTiny: RegularFileProperty

    @get:InputFile
    abstract val baseTiny: RegularFileProperty

    @get:OutputFile
    abstract val outputTiny: RegularFileProperty

    @TaskAction
    fun diffTiny() {
        val merged = TinyV2Reader.read(mergedTiny.get().asFile.toPath())
        val base = TinyV2Reader.read(baseTiny.get().asFile.toPath())
        // TODO: unhardcode those namespaces
        val remerged = Zwirn.mergeTinyFile(base, merged, "searge")
        val overlay = Zwirn.createOverlayTinyFile(base, remerged, listOf("searge", "mcp"), "searge")
        TinyV2Writer.write(overlay, outputTiny.get().asFile.toPath())
    }

}

val generateDiffTiny by tasks.register("generateDiffTiny", DiffTinyFile::class) {
    this.mergedTiny.set(tinyFromMergedEnigma.outputTiny)
    this.baseTiny.set(mcpExclusiveBase.outputTiny)
    this.outputTiny.set(layout.buildDirectory.file("overlayGenerated.tiny"))
}

val generateMappingPatches by tasks.register("generateMappingPatches", UnpackMappings::class) {
    this.inputTiny.set(generateDiffTiny.outputTiny)
    this.outputEnigma.set(overlayMappingsDir)
    doFirst {
        overlayMappingsDir.mkdirs()
    }
}

val mcpExclusiveMerged by tasks.register("mcpExclusiveMerged", ReorderNamespaces::class) {
    inputTiny.set(mergeMcpAndOverlay.outputTiny)
    outputTiny.set(layout.buildDirectory.file("mcpExclusiveMerged.tiny"))
    namespaceOrder.set(listOf("searge", "mcp"))
}

val mcpExclusiveBase by tasks.register("mcpExclusiveBase", ReorderNamespaces::class) {
    inputTiny.set(enrichMcp.mcpOutputFile)
    outputTiny.set(layout.buildDirectory.file("mcpExclusiveBase.tiny"))
    namespaceOrder.set(listOf("searge", "mcp"))
}

val unpackMappings by tasks.register("unpackMappings", UnpackMappings::class) {
    this.inputTiny.set(mcpExclusiveMerged.outputTiny)
    this.outputEnigma.set(combinedMappingsDir)
    doFirst {
        combinedMappingsDir.mkdirs()
    }
}

val mapMinecraft by tasks.register("mapMinecraft", MapJarTask::class) {
    this.inputJar = project.files(downloadMinecraft)
    this.inputNamespace.set("notch")
    this.outputNamespace.set("searge")
    this.inputTinyFile.set(enrichMcp.mcpOutputFile)
    this.outputJar.set(project.layout.buildDirectory.file("minecraft-searge.jar"))
}

val launchEnigma by tasks.register("launchEnigma", JavaExec::class) {
    this.classpath(enigmaSwing)
    this.mainClass.set("cuchaz.enigma.gui.Main")
    dependsOn(mapMinecraft)
    this.args(
        "--jar",
        mapMinecraft.outputJar.get().asFile.absolutePath,
        "--mappings",
        combinedMappingsDir.absolutePath,
        "--no-edit-classes",
        "--single-class-tree"
    )
}