diff options
author | Linnea Gräf <nea@nea.moe> | 2024-09-12 13:34:11 +0200 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-09-12 13:34:11 +0200 |
commit | 52623b715d395f65f96e09f4492285935b797b4b (patch) | |
tree | 1b96097a17dd9c81671478b11d127ce3278ddada /buildSrc/src/InnerJarsUnpacker.kt | |
parent | ab809e197f3e8c9a51745ee9510bc65c952c0cc8 (diff) | |
download | Firmament-52623b715d395f65f96e09f4492285935b797b4b.tar.gz Firmament-52623b715d395f65f96e09f4492285935b797b4b.tar.bz2 Firmament-52623b715d395f65f96e09f4492285935b797b4b.zip |
Isolate CIT resewn compatfix/citresewn
Diffstat (limited to 'buildSrc/src/InnerJarsUnpacker.kt')
-rw-r--r-- | buildSrc/src/InnerJarsUnpacker.kt | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/buildSrc/src/InnerJarsUnpacker.kt b/buildSrc/src/InnerJarsUnpacker.kt new file mode 100644 index 0000000..de06467 --- /dev/null +++ b/buildSrc/src/InnerJarsUnpacker.kt @@ -0,0 +1,70 @@ +import com.google.gson.Gson +import com.google.gson.JsonArray +import com.google.gson.JsonObject +import com.google.gson.JsonPrimitive +import java.io.File +import java.util.zip.ZipInputStream +import org.gradle.api.DefaultTask +import org.gradle.api.file.ConfigurableFileCollection +import org.gradle.api.file.DirectoryProperty +import org.gradle.api.tasks.InputFile +import org.gradle.api.tasks.InputFiles +import org.gradle.api.tasks.OutputDirectory +import org.gradle.api.tasks.OutputFiles +import org.gradle.api.tasks.TaskAction +import kotlin.io.path.createDirectories +import kotlin.io.path.outputStream + +abstract class InnerJarsUnpacker : DefaultTask() { + @get:InputFiles + abstract val inputJars: ConfigurableFileCollection + + @get:OutputDirectory + abstract val outputDir: DirectoryProperty + + private fun getFabricModJson(inputFile: File): JsonObject { + inputFile.inputStream().use { + val zis = ZipInputStream(it) + while (true) { + val entry = zis.nextEntry ?: error("Failed to find fabric.mod.json") + if (entry.name == "fabric.mod.json") { + return Gson().fromJson(zis.reader(), JsonObject::class.java) + } + } + } + } + + @TaskAction + fun unpack() { + inputJars.forEach { inputFile -> + val fabricModObject = getFabricModJson(inputFile) + val jars = fabricModObject["jars"] as? JsonArray ?: error("No jars to unpack in $inputFile") + val jarPaths = jars.map { + ((it as? JsonObject)?.get("file") as? JsonPrimitive)?.asString + ?: error("Invalid Jar $it in $inputFile") + } + extractJars(inputFile, jarPaths) + } + } + + private fun extractJars(inputFile: File, jarPaths: List<String>) { + val outputFile = outputDir.get().asFile.toPath() + val jarPathSet = jarPaths.toMutableSet() + inputFile.inputStream().use { + val zis = ZipInputStream(it) + while (true) { + val entry = zis.nextEntry ?: break + if (jarPathSet.remove(entry.name)) { + val resolvedPath = outputFile.resolve(entry.name) + resolvedPath.parent.createDirectories() + resolvedPath.outputStream().use { os -> + zis.copyTo(os) + } + } + } + } + if (jarPathSet.isNotEmpty()) { + error("Could not extract all jars: $jarPathSet") + } + } +} |