revanced-cli/src/main/kotlin/app/revanced/cli/patcher/Patcher.kt
2022-06-23 02:10:11 +02:00

48 lines
1.7 KiB
Kotlin

package app.revanced.cli.patcher
import app.revanced.cli.command.MainCommand.args
import app.revanced.cli.command.MainCommand.logger
import app.revanced.utils.filesystem.ZipFileSystemUtils
import app.revanced.utils.patcher.addPatchesFiltered
import app.revanced.utils.patcher.applyPatchesVerbose
import app.revanced.utils.patcher.mergeFiles
import java.io.File
import java.nio.file.Files
internal object Patcher {
internal fun start(patcher: app.revanced.patcher.Patcher, output: File) {
val args = args.pArgs!!
// merge files like necessary integrations
patcher.mergeFiles()
// add patches, but filter incompatible or excluded patches
patcher.addPatchesFiltered(excludePatches = args.excludedPatches.isNotEmpty())
// apply patches
patcher.applyPatchesVerbose()
// write output file
if (output.exists()) Files.delete(output.toPath())
args.inputFile.copyTo(output)
val result = patcher.save()
val inputFile = if (!args.disableResourcePatching && result.resourceFile != null) {
result.resourceFile
} else null
ZipFileSystemUtils(inputFile, output).use { fileSystem ->
// replace all dex files
result.dexFiles.forEach {
logger.info("Writing dex file ${it.name}")
fileSystem.write(it.name, it.dexFileInputStream.readAllBytes())
}
// inputFile being null implies resource patching being disabled
if (inputFile != null) {
// write resources
logger.info("Writing resources")
fileSystem.writeInput()
fileSystem.uncompress(*result.doNotCompress!!.toTypedArray())
}
}
}
}