-
-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathGenerateDevBundle.kt
More file actions
210 lines (180 loc) · 7.56 KB
/
GenerateDevBundle.kt
File metadata and controls
210 lines (180 loc) · 7.56 KB
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
/*
* paperweight is a Gradle plugin for the PaperMC project.
*
* Copyright (c) 2023 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.codechicken.diffpatch.cli.DiffOperation
import io.codechicken.diffpatch.util.Input as DiffInput
import io.codechicken.diffpatch.util.LogLevel
import io.codechicken.diffpatch.util.Output as DiffOutput
import io.papermc.paperweight.util.*
import io.papermc.paperweight.util.constants.*
import java.io.PrintStream
import java.nio.file.Files
import java.nio.file.Path
import javax.inject.Inject
import kotlin.io.path.*
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.ProviderFactory
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
abstract class GenerateDevBundle : BaseTask() {
@get:InputFiles
abstract val sourceDirectories: ConfigurableFileCollection
@get:InputDirectory
abstract val vanillaJavaDir: DirectoryProperty
@get:Input
abstract val minecraftVersion: Property<String>
@get:InputFile
abstract val mojangMappedPaperclipFile: RegularFileProperty
@get:Input
abstract val libraryRepositories: ListProperty<String>
@get:Input
abstract val macheUrl: Property<String>
@get:Input
abstract val macheDep: Property<String>
@get:OutputFile
abstract val devBundleFile: RegularFileProperty
@get:Inject
abstract val providers: ProviderFactory
@TaskAction
fun run() {
temporaryDir.toPath().deleteRecursive()
temporaryDir.toPath().createDirectories()
val devBundle = devBundleFile.path
devBundle.deleteForcefully()
devBundle.createParentDirectories()
val tempPatchDir = temporaryDir.toPath().resolve("patches")
generatePatches(tempPatchDir)
val dataDir = "data"
val patchesDir = "patches"
val config = createBundleConfig(dataDir, patchesDir)
devBundle.writeZip().use { zip ->
zip.getPath("config.json").bufferedWriter(Charsets.UTF_8).use { writer ->
gson.toJson(config, writer)
}
zip.getPath("data-version.txt").writeText(currentDataVersion.toString())
val dataZip = zip.getPath(dataDir)
dataZip.createDirectories()
mojangMappedPaperclipFile.path.copyTo(dataZip.resolve(mojangMappedPaperclipFileName))
val patchesZip = zip.getPath(patchesDir)
tempPatchDir.copyRecursivelyTo(patchesZip)
}
temporaryDir.toPath().deleteRecursive()
}
private fun generatePatches(output: Path) {
val workingDir = temporaryDir.toPath().resolve("work")
workingDir.createDirectories()
sourceDirectories.asFileTree.visit {
if (file.toPath().absolute().normalize().startsWith(vanillaJavaDir.path.absolute().normalize())) {
return@visit
}
if (file.isDirectory()) {
workingDir.resolve(path).createDirectories()
} else {
file.toPath().copyTo(workingDir.resolve(path))
}
}
workingDir.resolve(".git").deleteRecursive()
Files.walk(workingDir).use { stream ->
val oldSrc = vanillaJavaDir.path
for (file in stream) {
if (file.isDirectory()) {
continue
}
val relativeFile = file.relativeTo(workingDir)
val relativeFilePath = relativeFile.invariantSeparatorsPathString
val decompFile = oldSrc.resolve(relativeFilePath)
if (decompFile.exists()) {
val patchName = relativeFile.name + ".patch"
val outputFile = output.resolve(relativeFilePath).resolveSibling(patchName)
diffFiles(relativeFilePath, decompFile, file)
?.copyTo(outputFile.createParentDirectories())
} else {
val outputFile = output.resolve(relativeFilePath)
file.copyTo(outputFile.createParentDirectories())
}
}
}
}
private fun diffFiles(fileName: String, original: Path, patched: Path): Path? {
val dir = temporaryDir.toPath().resolve("diff-work")
dir.deleteRecursive()
dir.createDirectories()
val a = dir.resolve("a")
val oldFile = a.resolve(fileName).createParentDirectories()
val b = dir.resolve("b")
val newFile = b.resolve(fileName).createParentDirectories()
val patchOut = dir.resolve("out")
original.copyTo(oldFile)
patched.copyTo(newFile)
val logFile = temporaryDir.toPath().resolve("diff-log/${fileName.replace("/", "_")}.txt")
.createParentDirectories()
PrintStream(logFile.toFile(), Charsets.UTF_8).use { logOut ->
DiffOperation.builder()
.logTo(logOut)
.baseInput(DiffInput.MultiInput.folder(a))
.changedInput(DiffInput.MultiInput.folder(b))
.patchesOutput(DiffOutput.MultiOutput.folder(patchOut))
.autoHeader(true)
.level(LogLevel.ALL)
.lineEnding("\n")
.context(3)
.summary(true)
.build()
.operate()
}
return patchOut.resolve("$fileName.patch").takeIf { it.isRegularFile() }
}
@Suppress("SameParameterValue")
private fun createBundleConfig(dataTargetDir: String, patchTargetDir: String): DevBundleConfig {
return DevBundleConfig(
minecraftVersion = minecraftVersion.get(),
mache = createMacheDep(),
patchDir = patchTargetDir,
mojangMappedPaperclipFile = "$dataTargetDir/$mojangMappedPaperclipFileName",
libraryRepositories = libraryRepositories.get(),
pluginRemapArgs = TinyRemapper.pluginRemapArgs,
)
}
private fun createMacheDep(): MavenDep =
macheUrl.zip(macheDep) { url, dep -> MavenDep(url, listOf(dep)) }.get()
data class DevBundleConfig(
val minecraftVersion: String,
val mache: MavenDep,
val patchDir: String,
val mojangMappedPaperclipFile: String,
val libraryRepositories: List<String>,
val pluginRemapArgs: List<String>,
)
companion object {
const val mojangMappedPaperclipFileName = "paperclip-$DEOBF_NAMESPACE.jar"
// Should be bumped when the dev bundle config/contents changes in a way which will require users to update paperweight
const val currentDataVersion = 8
}
}