From 441a9ecd586ca0bf6f372e38d43d9c6ae57aa230 Mon Sep 17 00:00:00 2001 From: inotia00 Date: Sun, 23 Jul 2023 21:51:49 +0900 Subject: [PATCH] fix(youtube/custom-branding-youtube-name): takes too long to apply the patch in the RVX Manager --- .../name/patch/CustomBrandingNamePatch.kt | 41 ++++--- .../name/patch/RemoveElementsPatch.kt | 116 ++++++++++++++++++ 2 files changed, 137 insertions(+), 20 deletions(-) create mode 100644 src/main/kotlin/app/revanced/patches/youtube/layout/branding/name/patch/RemoveElementsPatch.kt diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/branding/name/patch/CustomBrandingNamePatch.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/branding/name/patch/CustomBrandingNamePatch.kt index a320d99dc5..6d97956e62 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/layout/branding/name/patch/CustomBrandingNamePatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/branding/name/patch/CustomBrandingNamePatch.kt @@ -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 @@ -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() diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/branding/name/patch/RemoveElementsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/branding/name/patch/RemoveElementsPatch.kt new file mode 100644 index 0000000000..04386ceadc --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/branding/name/patch/RemoveElementsPatch.kt @@ -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" + ) + } +}