From 0868e38ce614738e7172ee84b1e2a7d274178757 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Tue, 11 Feb 2025 15:18:35 +0200 Subject: [PATCH] refactor: Improve XML performance --- .../patches/spotify/layout/theme/CustomThemePatch.kt | 5 +++-- .../youtube/layout/branding/header/ChangeHeaderPatch.kt | 8 +++++--- .../patches/youtube/layout/seekbar/SeekbarColorPatch.kt | 4 +++- .../youtube/misc/playercontrols/PlayerControlsPatch.kt | 9 ++++++--- .../patches/youtube/misc/settings/SettingsPatch.kt | 4 +++- .../src/main/kotlin/app/revanced/util/ResourceUtils.kt | 5 ++++- 6 files changed, 24 insertions(+), 11 deletions(-) diff --git a/patches/src/main/kotlin/app/revanced/patches/spotify/layout/theme/CustomThemePatch.kt b/patches/src/main/kotlin/app/revanced/patches/spotify/layout/theme/CustomThemePatch.kt index 4c17ebce6..28d48e010 100644 --- a/patches/src/main/kotlin/app/revanced/patches/spotify/layout/theme/CustomThemePatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/spotify/layout/theme/CustomThemePatch.kt @@ -56,8 +56,9 @@ val customThemePatch = resourcePatch( document("res/values/colors.xml").use { document -> val resourcesNode = document.getElementsByTagName("resources").item(0) as Element - for (i in 0 until resourcesNode.childNodes.length) { - val node = resourcesNode.childNodes.item(i) as? Element ?: continue + val childNodes = resourcesNode.childNodes + for (i in 0 until childNodes.length) { + val node = childNodes.item(i) as? Element ?: continue node.textContent = when (node.getAttribute("name")) { diff --git a/patches/src/main/kotlin/app/revanced/patches/youtube/layout/branding/header/ChangeHeaderPatch.kt b/patches/src/main/kotlin/app/revanced/patches/youtube/layout/branding/header/ChangeHeaderPatch.kt index 73c5e820c..69e54cc4b 100644 --- a/patches/src/main/kotlin/app/revanced/patches/youtube/layout/branding/header/ChangeHeaderPatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/youtube/layout/branding/header/ChangeHeaderPatch.kt @@ -172,14 +172,16 @@ val changeHeaderPatch = resourcePatch( // Instead change styles.xml to use the old drawable resources. if (is_19_25_or_greater) { document("res/values/styles.xml").use { document -> + val documentChildNodes = document.childNodes + arrayOf( "CairoLightThemeRingo2Updates" to variants[0], "CairoDarkThemeRingo2Updates" to variants[1] ).forEach { (styleName, theme) -> - val style = document.childNodes.findElementByAttributeValueOrThrow( + val styleNodes = documentChildNodes.findElementByAttributeValueOrThrow( "name", styleName, - ) + ).childNodes val drawable = "@drawable/${HEADER_FILE_NAME}_${theme}" @@ -187,7 +189,7 @@ val changeHeaderPatch = resourcePatch( "ytWordmarkHeader", "ytPremiumWordmarkHeader" ).forEach { itemName -> - style.childNodes.findElementByAttributeValueOrThrow( + styleNodes.findElementByAttributeValueOrThrow( "name", itemName, ).textContent = drawable diff --git a/patches/src/main/kotlin/app/revanced/patches/youtube/layout/seekbar/SeekbarColorPatch.kt b/patches/src/main/kotlin/app/revanced/patches/youtube/layout/seekbar/SeekbarColorPatch.kt index 35567b829..daf11528d 100644 --- a/patches/src/main/kotlin/app/revanced/patches/youtube/layout/seekbar/SeekbarColorPatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/youtube/layout/seekbar/SeekbarColorPatch.kt @@ -125,8 +125,10 @@ private val seekbarColorResourcePatch = resourcePatch { fun setSplashDrawablePathFillColor(xmlFileNames: Iterable, vararg resourceNames: String) { xmlFileNames.forEach { xmlFileName -> document(xmlFileName).use { document -> + val childNodes = document.childNodes + resourceNames.forEach { elementId -> - val element = document.childNodes.findElementByAttributeValueOrThrow( + val element = childNodes.findElementByAttributeValueOrThrow( "android:name", elementId ) diff --git a/patches/src/main/kotlin/app/revanced/patches/youtube/misc/playercontrols/PlayerControlsPatch.kt b/patches/src/main/kotlin/app/revanced/patches/youtube/misc/playercontrols/PlayerControlsPatch.kt index 6beeaf242..6145048ce 100644 --- a/patches/src/main/kotlin/app/revanced/patches/youtube/misc/playercontrols/PlayerControlsPatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/youtube/misc/playercontrols/PlayerControlsPatch.kt @@ -76,10 +76,11 @@ val playerControlsResourcePatch = resourcePatch { "android.support.constraint.ConstraintLayout", ).item(0) - var bottomInsertBeforeNode: Node = bottomTargetDocument.childNodes.findElementByAttributeValue( + val bottomTargetDocumentChildNodes = bottomTargetDocument.childNodes + var bottomInsertBeforeNode: Node = bottomTargetDocumentChildNodes.findElementByAttributeValue( "android:inflatedId", bottomLastLeftOf, - ) ?: bottomTargetDocument.childNodes.findElementByAttributeValueOrThrow( + ) ?: bottomTargetDocumentChildNodes.findElementByAttributeValueOrThrow( "android:id", // Older targets use non-inflated id. bottomLastLeftOf, ) @@ -143,11 +144,13 @@ val playerControlsResourcePatch = resourcePatch { } finalize { + val childNodes = bottomTargetDocument.childNodes + arrayOf( "@id/bottom_end_container", "@id/multiview_button", ).forEach { - bottomTargetDocument.childNodes.findElementByAttributeValue( + childNodes.findElementByAttributeValue( "android:id", it, )?.setAttribute("yt:layout_constraintRight_toLeftOf", bottomLastLeftOf) diff --git a/patches/src/main/kotlin/app/revanced/patches/youtube/misc/settings/SettingsPatch.kt b/patches/src/main/kotlin/app/revanced/patches/youtube/misc/settings/SettingsPatch.kt index 8bf7cfc06..60f9bb742 100644 --- a/patches/src/main/kotlin/app/revanced/patches/youtube/misc/settings/SettingsPatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/youtube/misc/settings/SettingsPatch.kt @@ -97,6 +97,8 @@ private val settingsResourcePatch = resourcePatch { // Remove horizontal divider from the settings Preferences // To better match the appearance of the stock YouTube settings. document("res/values/styles.xml").use { document -> + val childNodes = document.childNodes + arrayOf( "Theme.YouTube.Settings", "Theme.YouTube.Settings.Dark", @@ -105,7 +107,7 @@ private val settingsResourcePatch = resourcePatch { listDividerNode.setAttribute("name", "android:listDivider") listDividerNode.appendChild(document.createTextNode("@null")) - document.childNodes.findElementByAttributeValueOrThrow( + childNodes.findElementByAttributeValueOrThrow( "name", value, ).appendChild(listDividerNode) diff --git a/patches/src/main/kotlin/app/revanced/util/ResourceUtils.kt b/patches/src/main/kotlin/app/revanced/util/ResourceUtils.kt index 7a1496afc..51135beba 100644 --- a/patches/src/main/kotlin/app/revanced/util/ResourceUtils.kt +++ b/patches/src/main/kotlin/app/revanced/util/ResourceUtils.kt @@ -41,7 +41,10 @@ inline fun Node.forEachChildElement(action: (Element) -> Unit) = */ fun Node.doRecursively(action: (Node) -> Unit) { action(this) - for (i in 0 until this.childNodes.length) this.childNodes.item(i).doRecursively(action) + val childNodes = this.childNodes + for (i in 0 until childNodes.length) { + childNodes.item(i).doRecursively(action) + } } fun Node.insertFirst(node: Node) {