forked from ReVanced/revanced-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Patcher.kt
55 lines (44 loc) 路 1.82 KB
/
Patcher.kt
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
package app.revanced.cli
import app.revanced.utils.filesystem.FileSystemUtils
import app.revanced.utils.patcher.addPatchesFiltered
import app.revanced.utils.patcher.applyPatchesPrint
import app.revanced.utils.patcher.mergeFiles
import app.revanced.utils.signing.Signer
import java.io.File
import java.io.FileFilter
internal class Patcher {
internal companion object {
internal fun start(patcher: app.revanced.patcher.Patcher) {
// merge files like necessary integrations
patcher.mergeFiles()
// add patches, but filter incompatible or excluded patches
patcher.addPatchesFiltered(includeFilter = MainCommand.includedPatches.isNotEmpty())
// apply patches
patcher.applyPatchesPrint()
// write output file
val outFile = File(MainCommand.outputPath)
if (outFile.exists()) outFile.delete()
MainCommand.inputFile.copyTo(outFile)
val zipFileSystem = FileSystemUtils(outFile)
// replace all dex files
for ((name, data) in patcher.save()) {
zipFileSystem.replaceFile(name, data.data)
}
if (MainCommand.patchResources) {
for (file in File(MainCommand.cacheDirectory).resolve("build/").listFiles(FileFilter { it.isDirectory })
?.first()?.listFiles()!!) {
if (!file.isDirectory) {
zipFileSystem.replaceFile(file.name, file.readBytes())
continue
}
zipFileSystem.replaceDirectory(file)
}
}
// finally close the stream
zipFileSystem.close()
// and sign the apk file
Signer.signApk(outFile)
println("[done]")
}
}
}