feat: settings patch framework (#266)

This commit is contained in:
oSumAtrIX
2022-08-22 01:59:43 +02:00
committed by GitHub
parent 059913c239
commit 8af8cdbfd6
62 changed files with 1219 additions and 728 deletions

View File

@ -2,7 +2,9 @@ package app.revanced.util.resources
import app.revanced.patcher.data.impl.DomFileEditor
import app.revanced.patcher.data.impl.ResourceData
import org.w3c.dom.Node
import java.nio.file.Files
import java.nio.file.StandardCopyOption
internal object ResourceUtils {
/**
@ -19,7 +21,7 @@ internal object ResourceUtils {
val resourceFile = "${resourceGroup.resourceDirectoryName}/$resource"
Files.copy(
classLoader.getResourceAsStream("$sourceResourceDirectory/$resourceFile")!!,
targetResourceDirectory.resolve(resourceFile).toPath()
targetResourceDirectory.resolve(resourceFile).toPath(), StandardCopyOption.REPLACE_EXISTING
)
}
}
@ -33,20 +35,17 @@ internal object ResourceUtils {
internal class ResourceGroup(val resourceDirectoryName: String, vararg val resources: String)
/**
* Copy resources from the current class loader to the resource directory.
* @param resourceDirectory The directory of the resource.
* @param targetResource The target resource.
* @param elementTag The element to copy.
* Iterate through the children of a node by its tag.
* @param resource The xml resource.
* @param targetTag The target xml node.
* @param callback The callback to call when iterating over the nodes.
*/
internal fun ResourceData.copyXmlNode(resourceDirectory: String, targetResource: String, elementTag: String) {
val stringsResourceInputStream = ResourceUtils.javaClass.classLoader.getResourceAsStream("$resourceDirectory/$targetResource")!!
internal fun ResourceData.iterateXmlNodeChildren(resource: String, targetTag: String, callback: (node: Node) -> Unit) =
xmlEditor[ResourceUtils.javaClass.classLoader.getResourceAsStream(resource)!!].use {
val stringsNode = it.file.getElementsByTagName(targetTag).item(0).childNodes
for (i in 1 until stringsNode.length - 1) callback(stringsNode.item(i))
}
// Copy nodes from the resources node to the real resource node
elementTag.copyXmlNode(
this.xmlEditor[stringsResourceInputStream],
this.xmlEditor["res/$targetResource"]
).close()
}
/**
* Copies the specified node of the source [DomFileEditor] to the target [DomFileEditor].
@ -54,7 +53,7 @@ internal object ResourceUtils {
* @param target the target [DomFileEditor]-
* @return AutoCloseable that closes the target [DomFileEditor]s.
*/
internal fun String.copyXmlNode(source: DomFileEditor, target: DomFileEditor): AutoCloseable {
fun String.copyXmlNode(source: DomFileEditor, target: DomFileEditor): AutoCloseable {
val hostNodes = source.file.getElementsByTagName(this).item(0).childNodes
val destinationResourceFile = target.file