fix: Use deprecated members to ensure backwards compatibility

By migrating to early to new APIs of ReVanced Patcher, if you were to use old versions of ReVanced Patcher, you would get compatibility issues. By using deprecated members until most have updated ReVanced Patcher, we can ensure seamless migration.
This commit is contained in:
oSumAtrIX
2024-02-22 01:10:09 +01:00
parent 1e3356808d
commit a6d8e4210f
24 changed files with 174 additions and 108 deletions
@@ -1,7 +1,6 @@
package app.revanced.util
import app.revanced.patcher.data.ResourceContext
import app.revanced.patcher.util.Document
import app.revanced.patcher.util.DomFileEditor
import app.revanced.util.resource.BaseResource
import org.w3c.dom.Node
@@ -50,7 +49,7 @@ fun ResourceContext.copyResources(
sourceResourceDirectory: String,
vararg resources: ResourceGroup,
) {
val targetResourceDirectory = this.get("res", false)
val targetResourceDirectory = this.get("res")
for (resourceGroup in resources) {
resourceGroup.resources.forEach { resource ->
@@ -86,28 +85,23 @@ fun ResourceContext.iterateXmlNodeChildren(
resource: String,
targetTag: String,
callback: (node: Node) -> Unit,
) = document[classLoader.getResourceAsStream(resource)!!].use {
val stringsNode = it.getElementsByTagName(targetTag).item(0).childNodes
) = xmlEditor[classLoader.getResourceAsStream(resource)!!].use { editor ->
val document = editor.file
val stringsNode = document.getElementsByTagName(targetTag).item(0).childNodes
for (i in 1 until stringsNode.length - 1) callback(stringsNode.item(i))
}
/**
* Copies the specified node of the source [Document] to the target [Document].
* @param source the source [Document].
* @param target the target [Document]-
* @return AutoCloseable that closes the [Document]s.
*/
fun String.copyXmlNode(
source: Document,
target: Document,
): AutoCloseable {
val hostNodes = source.getElementsByTagName(this).item(0).childNodes
// TODO: After the migration to the new patcher, remove the following code and replace it with the commented code below.
fun String.copyXmlNode(source: DomFileEditor, target: DomFileEditor): AutoCloseable {
val hostNodes = source.file.getElementsByTagName(this).item(0).childNodes
val destinationNode = target.getElementsByTagName(this).item(0)
val destinationResourceFile = target.file
val destinationNode = destinationResourceFile.getElementsByTagName(this).item(0)
for (index in 0 until hostNodes.length) {
val node = hostNodes.item(index).cloneNode(true)
target.adoptNode(node)
destinationResourceFile.adoptNode(node)
destinationNode.appendChild(node)
}
@@ -117,18 +111,44 @@ fun String.copyXmlNode(
}
}
@Deprecated(
"Use copyXmlNode(Document, Document) instead.",
ReplaceWith(
"this.copyXmlNode(source.file as Document, target.file as Document)",
"app.revanced.patcher.util.Document",
"app.revanced.patcher.util.Document",
),
)
fun String.copyXmlNode(
source: DomFileEditor,
target: DomFileEditor,
) = this.copyXmlNode(source.file as Document, target.file as Document)
// /**
// * Copies the specified node of the source [Document] to the target [Document].
// * @param source the source [Document].
// * @param target the target [Document]-
// * @return AutoCloseable that closes the [Document]s.
// */
// fun String.copyXmlNode(
// source: Document,
// target: Document,
// ): AutoCloseable {
// val hostNodes = source.getElementsByTagName(this).item(0).childNodes
//
// val destinationNode = target.getElementsByTagName(this).item(0)
//
// for (index in 0 until hostNodes.length) {
// val node = hostNodes.item(index).cloneNode(true)
// target.adoptNode(node)
// destinationNode.appendChild(node)
// }
//
// return AutoCloseable {
// source.close()
// target.close()
// }
// }
// @Deprecated(
// "Use copyXmlNode(Document, Document) instead.",
// ReplaceWith(
// "this.copyXmlNode(source.file as Document, target.file as Document)",
// "app.revanced.patcher.util.Document",
// "app.revanced.patcher.util.Document",
// ),
// )
// fun String.copyXmlNode(
// source: DomFileEditor,
// target: DomFileEditor,
// ) = this.copyXmlNode(source.file as Document, target.file as Document)
/**
* Add a resource node child.
@@ -143,4 +163,4 @@ internal fun Node.addResource(
appendChild(resource.serialize(ownerDocument, resourceCallback))
}
internal fun Document?.getNode(tagName: String) = this!!.getElementsByTagName(tagName).item(0)
internal fun org.w3c.dom.Document.getNode(tagName: String) = this.getElementsByTagName(tagName).item(0)