generated from FabricMC/fabric-example-mod
-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
stonecutter.gradle.kts
269 lines (219 loc) · 8.5 KB
/
stonecutter.gradle.kts
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
import java.io.FileInputStream
import java.io.FileOutputStream
import java.util.concurrent.CompletableFuture
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
import java.util.zip.ZipOutputStream
plugins {
id("dev.kikugie.stonecutter")
}
stonecutter active "1.21.1-fabric" /* [SC] DO NOT EDIT */
stonecutter registerChiseled tasks.register("chiseledBuild", stonecutter.chiseled) {
group = "project"
ofTask("build")
finalizedBy("mergeJars")
}
stonecutter configureEach {
val current = project.property("loom.platform")
val platforms = listOf("fabric", "forge", "neoforge").map { it to (it == current) }
consts(platforms)
}
// Non stonecutter stuff
val mergedDir = File("${rootProject.projectDir}/merged")
class MinecraftVersionData(private val name: String) {
fun greaterThan(other: String) : Boolean {
return stonecutter.compare(name, other.lowercase()) > 0
}
fun lessThan(other: String) : Boolean {
return stonecutter.compare(name, other.lowercase()) < 0
}
fun greaterOrEqual(other: String) : Boolean {
return stonecutter.compare(name, other.lowercase()) >= 0
}
fun lessOrEqual(other: String) : Boolean {
return stonecutter.compare(name, other.lowercase()) <= 0
}
override fun equals(other: Any?) : Boolean {
return name == other
}
override fun toString(): String {
return name
}
override fun hashCode(): Int {
return name.hashCode()
}
}
val coreModules = getProperty("core_modules")!!.split(',').map { it.trim() }
fun getProperty(key: String): String? {
return project.findProperty(key) as? String
}
// TODO find better way to do it
tasks.register("mergeJars") {
coreModules.forEach { module ->
dependsOn(":loader-$module:build")
}
doLast {
mergedDir.mkdirs()
val jarsToMerge = File("$rootDir/versions").listFiles()
?.flatMap {
File("$it/build/libs").listFiles()
?.filter { file -> file.isFile && !file.name.endsWith("-sources.jar") && file.name.endsWith(".jar") }
?: error("Couldn't find any mod jar!")
}
?: emptyList()
val tasks = mutableListOf<CompletableFuture<Void>>()
val time = System.currentTimeMillis()
val size = jarsToMerge.size
var current = 0;
for (jarToMerge in jarsToMerge) {
val task = CompletableFuture.runAsync {
val minecraftVersionStr = jarToMerge.name.substringAfterLast("-mc").substringBefore("-")
val minecraftVersion = MinecraftVersionData(minecraftVersionStr)
var loaderModule = ""
if (jarToMerge.name.contains("fabric")) {
loaderModule = "fabric/core"
} else if (jarToMerge.name.contains("neoforge")) {
loaderModule = if (minecraftVersion.greaterOrEqual("1.20.6")) {
"neoforge/fml4"
} else {
"neoforge/fml2"
}
} else if (jarToMerge.name.contains("forge")) {
loaderModule = if (minecraftVersion.lessOrEqual("1.18.2")) {
"forge/fml40"
} else {
"forge/fml47"
}
}
val loaderFile = File("${rootProject.projectDir}/loader/$loaderModule/build/libs").listFiles()
?.single { it.isFile && !it.name.endsWith("-sources.jar") && it.name.endsWith(".jar") } ?: return@runAsync
val finalJar = File("$mergedDir/${jarToMerge.name}")
loaderFile.copyTo(finalJar, overwrite = true)
appendFileToZip(finalJar, jarToMerge, "automodpack-mod.jar")
println("${++current}/$size - Merged: ${jarToMerge.name} into: ${finalJar.name} from: ${loaderFile.name}")
}
tasks.add(task)
}
tasks.forEach { it.join() }
if (size == 0) {
error("No jars to merge!")
} else if (size != current) {
error("Not all jars were merged!")
} else {
println("All jars were merged! Took: ${System.currentTimeMillis() - time}ms")
}
}
}
fun appendFileToZip(zipFile: File, fileToAppend: File, entryName: String) {
val entries = ZipInputStream(FileInputStream(zipFile)).use { zipStream ->
generateSequence { zipStream.nextEntry }
.toList()
}
val graph = mutableMapOf<String, MutableList<String>>()
entries.forEach { entry ->
val children = entry.name.split("/")
var currentParent = ""
children.forEach { child ->
if (child.isNotEmpty()) {
currentParent = "$currentParent$child/"
val parent = graph.getOrPut(currentParent) { mutableListOf() }
parent.add(child)
}
}
}
// graph.forEach { (parent, children) ->
// println("$parent -> $children")
// }
val filteredGraph = filterEntries(entries, graph, zipFile)
// Doing with temp file since for some reason just adding the file breaks the zip/jar file
val tempFile = File("$zipFile.temp")
tempFile.createNewFile()
ZipOutputStream(FileOutputStream(tempFile)).use { zipStream ->
ZipInputStream(FileInputStream(zipFile)).use { existingZipStream ->
while (true) {
val entry = existingZipStream.nextEntry ?: break
if (filteredGraph.containsKey("${entry.name}/")) {
try {
val zipEntry = ZipEntry(entry.name)
zipStream.putNextEntry(zipEntry)
existingZipStream.copyTo(zipStream)
zipStream.closeEntry()
} catch (e: Exception) {
println("Error while copying entry: ${entry.name}")
}
}
}
}
// Add the new entry
zipStream.putNextEntry(ZipEntry(entryName))
FileInputStream(fileToAppend).use { fileInputStream ->
fileInputStream.copyTo(zipStream)
}
zipStream.closeEntry()
}
// Replace the original zip file with the one containing the new entry
zipFile.delete()
tempFile.renameTo(zipFile)
}
val dupesDir = File("${rootProject.projectDir}/dupes")
fun filterEntries(entries: List<ZipEntry>, graph: Map<String, MutableList<String>>, zipFile: File): Map<String, MutableList<String>> {
val emptyDirs = mutableSetOf<String>()
// find empty directories
graph.forEach { (parent, children) ->
if (children.size == 0) {
emptyDirs.add(parent)
}
}
// dump duplicate entries
dupesDir.deleteRecursively()
dupesDir.mkdirs()
dumpDupeEntries(zipFile, entries)
// filter empty directories
// filter single duplicate files (leave only one)
val dupes = mutableSetOf<String>()
val filteredGraph = graph.filter { (parent, children) ->
if (emptyDirs.contains(parent)) {
println("Filtering empty dir: $parent -> $children")
return@filter false
}
val count = graph.count { parent == it.key }
if (count > 1 && !dupes.add(children[0])) {
return@filter false
}
return@filter true
}
return filteredGraph
}
fun dumpDupeEntries(zipFile: File, entries: List<ZipEntry>) {
// check for duplicates
val entryNames = mutableSetOf<String>()
entries.forEach { duplicate ->
if (!entryNames.add(duplicate.name)) {
println("Duplicate entry: $duplicate")
// write the entry to the file
ZipInputStream(FileInputStream(zipFile)).use { zipStream ->
var i = 0
generateSequence { zipStream.nextEntry }
.filter { it.name == duplicate.name }
.forEach { _ ->
i++
val dupeFile = File("$dupesDir/$i-$duplicate.dupe")
println("Extracting to: $dupeFile")
dupeFile.parentFile.mkdirs()
dupeFile.createNewFile()
FileOutputStream(dupeFile).use { fileOutputStream ->
zipStream.copyTo(fileOutputStream)
}
}
}
}
}
}
tasks.register("clean") {
dependsOn("cleanMerged")
}
tasks.register("cleanMerged") {
doLast {
mergedDir.deleteRecursively()
}
}