From 749900f6f8736a4a1c7d1289f01216119c3973a0 Mon Sep 17 00:00:00 2001 From: inotia00 <108592928+inotia00@users.noreply.github.com> Date: Sat, 21 Dec 2024 14:02:28 +0900 Subject: [PATCH] fix(YouTube - Custom Shorts action buttons): Patch not working on YouTube 19.38.41+ --- .../actionbuttons/ShortsActionButtonsPatch.kt | 129 +++++++++++++----- 1 file changed, 94 insertions(+), 35 deletions(-) diff --git a/patches/src/main/kotlin/app/revanced/patches/youtube/layout/actionbuttons/ShortsActionButtonsPatch.kt b/patches/src/main/kotlin/app/revanced/patches/youtube/layout/actionbuttons/ShortsActionButtonsPatch.kt index 1f1dac491..eea71b603 100644 --- a/patches/src/main/kotlin/app/revanced/patches/youtube/layout/actionbuttons/ShortsActionButtonsPatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/youtube/layout/actionbuttons/ShortsActionButtonsPatch.kt @@ -4,15 +4,30 @@ import app.revanced.patcher.patch.resourcePatch import app.revanced.patcher.patch.stringOption import app.revanced.patches.youtube.utils.compatibility.Constants.COMPATIBLE_PACKAGE import app.revanced.patches.youtube.utils.patch.PatchList.CUSTOM_SHORTS_ACTION_BUTTONS +import app.revanced.patches.youtube.utils.playservice.is_19_36_or_greater +import app.revanced.patches.youtube.utils.playservice.versionCheckPatch import app.revanced.patches.youtube.utils.settings.ResourceUtils.addPreference import app.revanced.patches.youtube.utils.settings.settingsPatch import app.revanced.util.ResourceGroup import app.revanced.util.copyResources +import app.revanced.util.inputStreamFromBundledResourceOrThrow import app.revanced.util.lowerCaseOrThrow +import java.nio.file.Files +import java.nio.file.StandardCopyOption private const val DEFAULT_ICON = "cairo" private const val YOUTUBE_ICON = "youtube" +private val sizeArray = arrayOf( + "xxxhdpi", + "xxhdpi", + "xhdpi", + "hdpi", + "mdpi" +) + +private val drawableDirectories = sizeArray.map { "drawable-$it" } + @Suppress("unused") val shortsActionButtonsPatch = resourcePatch( CUSTOM_SHORTS_ACTION_BUTTONS.title, @@ -20,9 +35,12 @@ val shortsActionButtonsPatch = resourcePatch( ) { compatibleWith(COMPATIBLE_PACKAGE) - dependsOn(settingsPatch) + dependsOn( + settingsPatch, + versionCheckPatch + ) - val iconType = stringOption( + val iconTypeOption = stringOption( key = "iconType", default = DEFAULT_ICON, values = mapOf( @@ -41,7 +59,7 @@ val shortsActionButtonsPatch = resourcePatch( execute { // Check patch options first. - val iconType = iconType + val iconType = iconTypeOption .lowerCaseOrThrow() if (iconType == YOUTUBE_ICON) { @@ -50,40 +68,41 @@ val shortsActionButtonsPatch = resourcePatch( return@execute } - arrayOf( - "xxxhdpi", - "xxhdpi", - "xhdpi", - "hdpi", - "mdpi" - ).forEach { dpi -> - copyResources( - "youtube/shorts/actionbuttons/$iconType", - ResourceGroup( - "drawable-$dpi", - "ic_remix_filled_white_shadowed.webp", - "ic_right_comment_shadowed.webp", - "ic_right_dislike_off_shadowed.webp", - "ic_right_dislike_on_shadowed.webp", - "ic_right_like_off_shadowed.webp", - "ic_right_like_on_shadowed.webp", - "ic_right_share_shadowed.webp", + val sourceResourceDirectory = "youtube/shorts/actionbuttons/$iconType" - // for older versions only - "ic_remix_filled_white_24.webp", - "ic_right_dislike_on_32c.webp", - "ic_right_like_on_32c.webp" - ), - ResourceGroup( - "drawable", - "ic_right_comment_32c.xml", - "ic_right_dislike_off_32c.xml", - "ic_right_like_off_32c.xml", - "ic_right_share_32c.xml" - ) - ) + val resourceMap = ShortsActionButtons.entries.map { it.newResource to it.resources } + val res = get("res") + + for ((toFileName, fromResourceArray) in resourceMap) { + fromResourceArray.forEach { fromFileName -> + drawableDirectories.forEach { drawableDirectory -> + val fromFile = "$drawableDirectory/$fromFileName.webp" + val fromPath = res.resolve(fromFile).toPath() + val toFile = "$drawableDirectory/$toFileName.webp" + val toPath = res.resolve(toFile).toPath() + val inputStreamForLegacy = inputStreamFromBundledResourceOrThrow(sourceResourceDirectory, fromFile) + val inputStreamForNew = inputStreamFromBundledResourceOrThrow(sourceResourceDirectory, fromFile) + + Files.copy(inputStreamForLegacy, fromPath, StandardCopyOption.REPLACE_EXISTING) + + if (is_19_36_or_greater) { + Files.copy(inputStreamForNew, toPath, StandardCopyOption.REPLACE_EXISTING) + } + } + } } + copyResources( + sourceResourceDirectory, + ResourceGroup( + "drawable", + "ic_right_comment_32c.xml", + "ic_right_dislike_off_32c.xml", + "ic_right_like_off_32c.xml", + "ic_right_share_32c.xml" + ) + ) + addPreference(CUSTOM_SHORTS_ACTION_BUTTONS) if (iconType == DEFAULT_ICON) { @@ -99,6 +118,46 @@ val shortsActionButtonsPatch = resourcePatch( "reel_search_bold_24dp.xml" ) ) - } } + +internal enum class ShortsActionButtons(val newResource: String, vararg val resources: String) { + LIKE( + "youtube_shorts_like_outline_32dp", + // This replaces the new icon. + "ic_right_like_off_shadowed", + ), + LIKE_FILLED( + "youtube_shorts_like_fill_32dp", + "ic_right_like_on_32c", + // This replaces the new icon. + "ic_right_like_on_shadowed", + ), + DISLIKE( + "youtube_shorts_dislike_outline_32dp", + // This replaces the new icon. + "ic_right_dislike_off_shadowed", + ), + DISLIKE_FILLED( + "youtube_shorts_dislike_fill_32dp", + "ic_right_dislike_on_32c", + // This replaces the new icon. + "ic_right_dislike_on_shadowed", + ), + COMMENT( + "youtube_shorts_comment_outline_32dp", + // This replaces the new icon. + "ic_right_comment_shadowed", + ), + SHARE( + "youtube_shorts_share_outline_32dp", + // This replaces the new icon. + "ic_right_share_shadowed", + ), + REMIX( + "youtube_shorts_remix_outline_32dp", + "ic_remix_filled_white_24", + // This replaces the new icon. + "ic_remix_filled_white_shadowed", + ), +}