mirror of
https://github.com/revanced/revanced-patcher.git
synced 2025-04-29 21:04:26 +02:00

This commit allows reading and writing arbitrary files in an APK file. Additionally it allows deleting files from APK files. A `RawResourcePatch` class has been added which has access to `ResourceContext` but ReVanced Patcher will not decode APK resources. A regular `ResourcePatch` can read and write arbitrary files from an APK file, unless they are decoded to `PatcherConfig.apkFiles`. On attempt to get a file from `PatcherConfig.apkFiles` if the second parameter is true, it will read and write the raw resource file from the original APK to `PatcherConfig.apkFiles` if it does not exist. With this commit, many APIs have been deprecated as well, such as `DomFileEditor` and instead a `Document` has been added.
49 lines
1.4 KiB
Kotlin
49 lines
1.4 KiB
Kotlin
package app.revanced.patcher.util
|
|
|
|
import org.w3c.dom.Document
|
|
import java.io.Closeable
|
|
import java.io.File
|
|
import java.io.InputStream
|
|
import javax.xml.parsers.DocumentBuilderFactory
|
|
import javax.xml.transform.TransformerFactory
|
|
import javax.xml.transform.dom.DOMSource
|
|
import javax.xml.transform.stream.StreamResult
|
|
|
|
class Document internal constructor(
|
|
inputStream: InputStream,
|
|
) : Document by DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream), Closeable {
|
|
private var file: File? = null
|
|
|
|
init {
|
|
normalize()
|
|
}
|
|
|
|
internal constructor(file: File) : this(file.inputStream()) {
|
|
this.file = file
|
|
readerCount.merge(file, 1, Int::plus)
|
|
}
|
|
|
|
override fun close() {
|
|
file?.let {
|
|
if (readerCount[it]!! > 1) {
|
|
throw IllegalStateException(
|
|
"Two or more instances are currently reading $it." +
|
|
"To be able to close this instance, no other instances may be reading $it at the same time.",
|
|
)
|
|
} else {
|
|
readerCount.remove(it)
|
|
}
|
|
|
|
it.outputStream().use { stream ->
|
|
TransformerFactory.newInstance()
|
|
.newTransformer()
|
|
.transform(DOMSource(this), StreamResult(stream))
|
|
}
|
|
}
|
|
}
|
|
|
|
private companion object {
|
|
private val readerCount = mutableMapOf<File, Int>()
|
|
}
|
|
}
|