Skip to content

Commit

Permalink
fix(youtube/custom-branding-youtube-name): takes too long to apply th…
Browse files Browse the repository at this point in the history
…e patch in the RVX Manager
  • Loading branch information
inotia00 committed Jul 23, 2023
1 parent e0e6dad commit 441a9ec
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package app.revanced.patches.youtube.layout.branding.name.patch

import app.revanced.extensions.startsWithAny
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
Expand All @@ -15,41 +14,43 @@ import app.revanced.patcher.patch.annotations.Patch
import app.revanced.patches.youtube.utils.annotations.YouTubeCompatibility
import app.revanced.patches.youtube.utils.settings.resource.patch.SettingsPatch
import app.revanced.util.resources.ResourceHelper.updatePatchStatusLabel
import org.w3c.dom.Element

@Patch
@Name("Custom branding YouTube name")
@Description("Rename the YouTube app to the name specified in options.json.")
@DependsOn([SettingsPatch::class])
@DependsOn(
[
RemoveElementsPatch::class,
SettingsPatch::class
]
)
@YouTubeCompatibility
@Version("0.0.1")
class CustomBrandingNamePatch : ResourcePatch {
override fun execute(context: ResourceContext): PatchResult {

// App name
val resourceFileNames = arrayOf("strings.xml")
val appName = YouTubeAppName
val appName =
if (YouTubeAppName != null)
YouTubeAppName
else
"ReVanced Extended"

context.forEach {
if (!it.name.startsWithAny(*resourceFileNames)) return@forEach
context.xmlEditor["res/values/strings.xml"].use { editor ->
val document = editor.file

// for each file in the "layouts" directory replace all necessary attributes content
context.xmlEditor[it.absolutePath].use { editor ->
val resourcesNode = editor.file.getElementsByTagName("resources").item(0) as Element
mapOf(
"application_name" to appName
).forEach { (k, v) ->
val stringElement = document.createElement("string")

for (i in 0 until resourcesNode.childNodes.length) {
val node = resourcesNode.childNodes.item(i)
if (node !is Element) continue
stringElement.setAttribute("name", k)
stringElement.textContent = v

val element = resourcesNode.childNodes.item(i) as Element
element.textContent = when (element.getAttribute("name")) {
"application_name" -> "$appName"
else -> continue
}
}
document.getElementsByTagName("resources").item(0).appendChild(stringElement)
}
}


context.updatePatchStatusLabel("$appName")

return PatchResultSuccess()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package app.revanced.patches.youtube.layout.branding.name.patch

import app.revanced.patcher.data.ResourceContext
import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.patch.ResourcePatch
import kotlin.io.path.exists

class RemoveElementsPatch : ResourcePatch {
override fun execute(context: ResourceContext): PatchResult {

LANGUAGE_LIST.forEach { path ->
val resDirectory = context["res"]
val targetXmlPath = resDirectory.resolve(path).resolve("strings.xml").toPath()

if (targetXmlPath.exists()) {
val targetXml = context["res/$path/strings.xml"]

targetXml.writeText(
targetXml.readText()
.replace(""".+"application_name".+""".toRegex(), "")
)
}
}

return PatchResultSuccess()
}

companion object {
val LANGUAGE_LIST = arrayOf(
"values",
"values-af",
"values-am",
"values-ar",
"values-as",
"values-az",
"values-b+sr+Latn",
"values-be",
"values-bg",
"values-bn",
"values-bs",
"values-ca",
"values-cs",
"values-da",
"values-de",
"values-el",
"values-en-rGB",
"values-en-rIN",
"values-es",
"values-es-rUS",
"values-et",
"values-eu",
"values-fa",
"values-fi",
"values-fr",
"values-fr-rCA",
"values-gl",
"values-gu",
"values-hi",
"values-hr",
"values-hu",
"values-hy",
"values-in",
"values-is",
"values-it",
"values-iw",
"values-ja",
"values-ka",
"values-kk",
"values-km",
"values-kn",
"values-ko",
"values-ky",
"values-lo",
"values-lt",
"values-lv",
"values-mk",
"values-ml",
"values-mn",
"values-mr",
"values-ms",
"values-my",
"values-nb",
"values-ne",
"values-nl",
"values-or",
"values-pa",
"values-pl",
"values-pt",
"values-pt-rBR",
"values-pt-rPT",
"values-ro",
"values-ru",
"values-si",
"values-sk",
"values-sl",
"values-sq",
"values-sr",
"values-sv",
"values-sw",
"values-ta",
"values-te",
"values-th",
"values-tl",
"values-tr",
"values-uk",
"values-ur",
"values-uz",
"values-vi",
"values-zh-rCN",
"values-zh-rHK",
"values-zh-rTW",
"values-zu"
)
}
}

0 comments on commit 441a9ec

Please sign in to comment.