Skip to content

Commit 29105ba

Browse files
committed
fix: CLI not working
1 parent 31853fe commit 29105ba

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

src/main/kotlin/app/revanced/cli/patcher/Patcher.kt

+9-5
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,20 @@ internal object Patcher {
2323
if (output.exists()) Files.delete(output.toPath())
2424
args.inputFile.copyTo(output)
2525

26-
ZipFileSystemUtils(output).use { fileSystem ->
26+
val result = patcher.save()
27+
val inputFile = if (!args.disableResourcePatching && result.resourceFile != null) {
28+
result.resourceFile
29+
} else null
30+
ZipFileSystemUtils(inputFile, output).use { fileSystem ->
2731
// replace all dex files
28-
val result = patcher.save()
2932
result.dexFiles.forEach {
3033
fileSystem.write(it.name, it.memoryDataStore.data)
3134
}
3235

33-
// write resources
34-
if (!args.disableResourcePatching) {
35-
fileSystem.writePathRecursively(File(args.cacheDirectory).resolve("build").toPath())
36+
// inputFile being null implies resource patching being disabled
37+
if (inputFile != null) {
38+
// write resources
39+
fileSystem.writeInput()
3640
fileSystem.uncompress(*result.doNotCompress!!.toTypedArray())
3741
}
3842
}

src/main/kotlin/app/revanced/utils/filesystem/ZipFileSystemUtils.kt

+26-20
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import java.nio.file.Files
77
import java.nio.file.Path
88
import java.util.zip.ZipEntry
99

10-
internal class ZipFileSystemUtils(
11-
file: File
12-
) : Closeable {
13-
private var zipFileSystem = FileSystems.newFileSystem(file.toPath(), mapOf("noCompression" to true))
10+
internal class ZipFileSystemUtils(input: File?, output: File) : Closeable {
11+
private val inFileSystem = if (input != null) {
12+
FileSystems.newFileSystem(input.toPath())
13+
} else null
14+
private val outFileSystem = FileSystems.newFileSystem(output.toPath(), mapOf("noCompression" to true))
1415

1516
private fun Path.deleteRecursively() {
1617
if (!Files.exists(this)) {
17-
throw IllegalStateException("File exists in real folder but not in zip file system")
18+
throw IllegalStateException("File exists in input but not in output, cannot delete")
1819
}
1920

2021
if (Files.isDirectory(this)) {
@@ -26,21 +27,25 @@ internal class ZipFileSystemUtils(
2627
Files.delete(this)
2728
}
2829

29-
internal fun writePathRecursively(path: Path) {
30-
Files.list(path).let { fileStream ->
30+
internal fun writeInput() {
31+
if (inFileSystem == null) {
32+
throw IllegalArgumentException("Input file not set")
33+
}
34+
val root = inFileSystem.getPath(inFileSystem.separator)
35+
36+
Files.list(root).close()
37+
38+
Files.list(root).also { fileStream ->
3139
fileStream.forEach { filePath ->
32-
val fileSystemPath = filePath.getRelativePath(path)
40+
val fileSystemPath = filePath.getRelativePath(root)
3341
fileSystemPath.deleteRecursively()
3442
}
35-
36-
fileStream
3743
}.close()
3844

39-
Files.walk(path).let { fileStream ->
40-
// don't include build directory
41-
// by skipping the root node.
45+
Files.walk(root).also { fileStream ->
46+
// don't include build directory by skipping the root node.
4247
fileStream.skip(1).forEach { filePath ->
43-
val relativePath = filePath.getRelativePath(path)
48+
val relativePath = filePath.getRelativePath(root)
4449

4550
if (Files.isDirectory(filePath)) {
4651
Files.createDirectory(relativePath)
@@ -49,17 +54,18 @@ internal class ZipFileSystemUtils(
4954

5055
Files.copy(filePath, relativePath)
5156
}
52-
53-
fileStream
5457
}.close()
5558
}
5659

57-
internal fun write(path: String, content: ByteArray) = Files.write(zipFileSystem.getPath(path), content)
60+
internal fun write(path: String, content: ByteArray) = Files.write(outFileSystem.getPath(path), content)
5861

59-
private fun Path.getRelativePath(path: Path): Path = zipFileSystem.getPath(path.relativize(this).toString())
62+
private fun Path.getRelativePath(path: Path): Path = outFileSystem.getPath(path.relativize(this).toString())
6063

6164
internal fun uncompress(vararg paths: String) =
62-
paths.forEach { Files.setAttribute(zipFileSystem.getPath(it), "zip:method", ZipEntry.STORED) }
65+
paths.forEach { Files.setAttribute(outFileSystem.getPath(it), "zip:method", ZipEntry.STORED) }
6366

64-
override fun close() = zipFileSystem.close()
67+
override fun close() {
68+
inFileSystem?.close()
69+
outFileSystem.close()
70+
}
6571
}

0 commit comments

Comments
 (0)