From 9d5998b5d21ddbab8a04907d35b16fce6ab16f9c Mon Sep 17 00:00:00 2001 From: inotia00 <108592928+inotia00@users.noreply.github.com> Date: Thu, 5 Sep 2024 15:51:49 +0900 Subject: [PATCH] fix(YouTube/Disable force auto captions): patch doesn't work with Shorts https://github.com/inotia00/ReVanced_Extended/issues/2346 --- .../autocaptions/AutoCaptionsBytecodePatch.kt | 6 -- .../general/autocaptions/AutoCaptionsPatch.kt | 34 +++++++--- .../shared/captions/BaseAutoCaptionsPatch.kt | 65 ------------------- .../SubtitleButtonControllerFingerprint.kt | 23 ------- .../fingerprints/SubtitleTrackFingerprint.kt | 2 +- .../autocaptions/AutoCaptionsBytecodePatch.kt | 6 -- .../general/autocaptions/AutoCaptionsPatch.kt | 47 ++++++++++++-- ...dererDecoderRecommendedLevelFingerprint.kt | 12 ++++ .../components/PlayerComponentsPatch.kt | 2 +- .../StartVideoInformerFingerprint.kt | 2 +- .../youtube/settings/host/values/strings.xml | 4 +- 11 files changed, 81 insertions(+), 122 deletions(-) delete mode 100644 src/main/kotlin/app/revanced/patches/music/general/autocaptions/AutoCaptionsBytecodePatch.kt delete mode 100644 src/main/kotlin/app/revanced/patches/shared/captions/BaseAutoCaptionsPatch.kt delete mode 100644 src/main/kotlin/app/revanced/patches/shared/captions/fingerprints/SubtitleButtonControllerFingerprint.kt rename src/main/kotlin/app/revanced/patches/shared/{captions => }/fingerprints/SubtitleTrackFingerprint.kt (86%) delete mode 100644 src/main/kotlin/app/revanced/patches/youtube/general/autocaptions/AutoCaptionsBytecodePatch.kt create mode 100644 src/main/kotlin/app/revanced/patches/youtube/general/autocaptions/fingerprints/StoryboardRendererDecoderRecommendedLevelFingerprint.kt rename src/main/kotlin/app/revanced/patches/{shared => youtube/utils}/fingerprints/StartVideoInformerFingerprint.kt (89%) diff --git a/src/main/kotlin/app/revanced/patches/music/general/autocaptions/AutoCaptionsBytecodePatch.kt b/src/main/kotlin/app/revanced/patches/music/general/autocaptions/AutoCaptionsBytecodePatch.kt deleted file mode 100644 index 8deda0978..000000000 --- a/src/main/kotlin/app/revanced/patches/music/general/autocaptions/AutoCaptionsBytecodePatch.kt +++ /dev/null @@ -1,6 +0,0 @@ -package app.revanced.patches.music.general.autocaptions - -import app.revanced.patches.music.utils.integrations.Constants.GENERAL_CLASS_DESCRIPTOR -import app.revanced.patches.shared.captions.BaseAutoCaptionsPatch - -object AutoCaptionsBytecodePatch : BaseAutoCaptionsPatch(GENERAL_CLASS_DESCRIPTOR, false) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/music/general/autocaptions/AutoCaptionsPatch.kt b/src/main/kotlin/app/revanced/patches/music/general/autocaptions/AutoCaptionsPatch.kt index c50428704..22c0d9b64 100644 --- a/src/main/kotlin/app/revanced/patches/music/general/autocaptions/AutoCaptionsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/general/autocaptions/AutoCaptionsPatch.kt @@ -1,22 +1,38 @@ package app.revanced.patches.music.general.autocaptions -import app.revanced.patcher.data.ResourceContext +import app.revanced.patcher.data.BytecodeContext +import app.revanced.patcher.extensions.InstructionExtensions.addInstructions +import app.revanced.patcher.extensions.InstructionExtensions.getInstruction import app.revanced.patches.music.utils.compatibility.Constants.COMPATIBLE_PACKAGE +import app.revanced.patches.music.utils.integrations.Constants.GENERAL_CLASS_DESCRIPTOR import app.revanced.patches.music.utils.settings.CategoryType import app.revanced.patches.music.utils.settings.SettingsPatch -import app.revanced.util.patch.BaseResourcePatch +import app.revanced.patches.shared.fingerprints.SubtitleTrackFingerprint +import app.revanced.util.patch.BaseBytecodePatch +import app.revanced.util.resultOrThrow +import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction @Suppress("unused") -object AutoCaptionsPatch : BaseResourcePatch( +object AutoCaptionsPatch : BaseBytecodePatch( name = "Disable auto captions", description = "Adds an option to disable captions from being automatically enabled.", - dependencies = setOf( - AutoCaptionsBytecodePatch::class, - SettingsPatch::class, - ), - compatiblePackages = COMPATIBLE_PACKAGE + dependencies = setOf(SettingsPatch::class), + compatiblePackages = COMPATIBLE_PACKAGE, + fingerprints = setOf(SubtitleTrackFingerprint), ) { - override fun execute(context: ResourceContext) { + override fun execute(context: BytecodeContext) { + + SubtitleTrackFingerprint.resultOrThrow().mutableMethod.apply { + val index = implementation!!.instructions.lastIndex + val register = getInstruction(index).registerA + + addInstructions( + index, """ + invoke-static {v$register}, $GENERAL_CLASS_DESCRIPTOR->disableAutoCaptions(Z)Z + move-result v$register + """ + ) + } SettingsPatch.addSwitchPreference( CategoryType.GENERAL, diff --git a/src/main/kotlin/app/revanced/patches/shared/captions/BaseAutoCaptionsPatch.kt b/src/main/kotlin/app/revanced/patches/shared/captions/BaseAutoCaptionsPatch.kt deleted file mode 100644 index cd819bf5f..000000000 --- a/src/main/kotlin/app/revanced/patches/shared/captions/BaseAutoCaptionsPatch.kt +++ /dev/null @@ -1,65 +0,0 @@ -package app.revanced.patches.shared.captions - -import app.revanced.patcher.data.BytecodeContext -import app.revanced.patcher.extensions.InstructionExtensions.addInstructions -import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels -import app.revanced.patcher.extensions.InstructionExtensions.getInstruction -import app.revanced.patcher.patch.BytecodePatch -import app.revanced.patcher.util.smali.ExternalLabel -import app.revanced.patches.shared.captions.fingerprints.SubtitleButtonControllerFingerprint -import app.revanced.patches.shared.captions.fingerprints.SubtitleTrackFingerprint -import app.revanced.patches.shared.fingerprints.StartVideoInformerFingerprint -import app.revanced.util.resultOrThrow -import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction - -abstract class BaseAutoCaptionsPatch( - private val classDescriptor: String, - private val captionsButtonStatus: Boolean -) : BytecodePatch( - setOf( - StartVideoInformerFingerprint, - SubtitleButtonControllerFingerprint, - SubtitleTrackFingerprint - ) -) { - override fun execute(context: BytecodeContext) { - - SubtitleTrackFingerprint.resultOrThrow().mutableMethod.apply { - if (captionsButtonStatus) { - addInstructionsWithLabels( - 0, """ - invoke-static {}, $classDescriptor->disableAutoCaptions()Z - move-result v0 - if-eqz v0, :disabled - const/4 v0, 0x1 - return v0 - """, ExternalLabel("disabled", getInstruction(0)) - ) - } else { - val index = implementation!!.instructions.lastIndex - val register = getInstruction(index).registerA - - addInstructions( - index, """ - invoke-static {v$register}, $classDescriptor->disableAutoCaptions(Z)Z - move-result v$register - """ - ) - } - } - - if (!captionsButtonStatus) return - - mapOf( - StartVideoInformerFingerprint to 0, - SubtitleButtonControllerFingerprint to 1 - ).forEach { (fingerprint, enabled) -> - fingerprint.resultOrThrow().mutableMethod.addInstructions( - 0, """ - const/4 v0, 0x$enabled - invoke-static {v0}, $classDescriptor->setCaptionsButtonStatus(Z)V - """ - ) - } - } -} \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/shared/captions/fingerprints/SubtitleButtonControllerFingerprint.kt b/src/main/kotlin/app/revanced/patches/shared/captions/fingerprints/SubtitleButtonControllerFingerprint.kt deleted file mode 100644 index 4041509b5..000000000 --- a/src/main/kotlin/app/revanced/patches/shared/captions/fingerprints/SubtitleButtonControllerFingerprint.kt +++ /dev/null @@ -1,23 +0,0 @@ -package app.revanced.patches.shared.captions.fingerprints - -import app.revanced.patcher.extensions.or -import app.revanced.patcher.fingerprint.MethodFingerprint -import com.android.tools.smali.dexlib2.AccessFlags -import com.android.tools.smali.dexlib2.Opcode - -internal object SubtitleButtonControllerFingerprint : MethodFingerprint( - returnType = "V", - accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL, - parameters = listOf("Lcom/google/android/libraries/youtube/player/subtitles/model/SubtitleTrack;"), - opcodes = listOf( - Opcode.IGET_OBJECT, - Opcode.IF_NEZ, - Opcode.RETURN_VOID, - Opcode.IGET_BOOLEAN, - Opcode.CONST_4, - Opcode.IF_NEZ, - Opcode.CONST, - Opcode.INVOKE_VIRTUAL, - Opcode.IGET_OBJECT, - ) -) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/shared/captions/fingerprints/SubtitleTrackFingerprint.kt b/src/main/kotlin/app/revanced/patches/shared/fingerprints/SubtitleTrackFingerprint.kt similarity index 86% rename from src/main/kotlin/app/revanced/patches/shared/captions/fingerprints/SubtitleTrackFingerprint.kt rename to src/main/kotlin/app/revanced/patches/shared/fingerprints/SubtitleTrackFingerprint.kt index b1a6012a7..b97457386 100644 --- a/src/main/kotlin/app/revanced/patches/shared/captions/fingerprints/SubtitleTrackFingerprint.kt +++ b/src/main/kotlin/app/revanced/patches/shared/fingerprints/SubtitleTrackFingerprint.kt @@ -1,4 +1,4 @@ -package app.revanced.patches.shared.captions.fingerprints +package app.revanced.patches.shared.fingerprints import app.revanced.patcher.extensions.or import app.revanced.patcher.fingerprint.MethodFingerprint diff --git a/src/main/kotlin/app/revanced/patches/youtube/general/autocaptions/AutoCaptionsBytecodePatch.kt b/src/main/kotlin/app/revanced/patches/youtube/general/autocaptions/AutoCaptionsBytecodePatch.kt deleted file mode 100644 index 914f4db85..000000000 --- a/src/main/kotlin/app/revanced/patches/youtube/general/autocaptions/AutoCaptionsBytecodePatch.kt +++ /dev/null @@ -1,6 +0,0 @@ -package app.revanced.patches.youtube.general.autocaptions - -import app.revanced.patches.shared.captions.BaseAutoCaptionsPatch -import app.revanced.patches.youtube.utils.integrations.Constants.GENERAL_CLASS_DESCRIPTOR - -object AutoCaptionsBytecodePatch : BaseAutoCaptionsPatch(GENERAL_CLASS_DESCRIPTOR, true) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/youtube/general/autocaptions/AutoCaptionsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/general/autocaptions/AutoCaptionsPatch.kt index 52a9b95a0..4ed2f9a3d 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/general/autocaptions/AutoCaptionsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/general/autocaptions/AutoCaptionsPatch.kt @@ -1,24 +1,57 @@ package app.revanced.patches.youtube.general.autocaptions import app.revanced.patcher.data.BytecodeContext +import app.revanced.patcher.extensions.InstructionExtensions.addInstructions +import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels +import app.revanced.patcher.extensions.InstructionExtensions.getInstruction +import app.revanced.patcher.util.smali.ExternalLabel +import app.revanced.patches.shared.fingerprints.SubtitleTrackFingerprint +import app.revanced.patches.youtube.general.autocaptions.fingerprints.StoryboardRendererDecoderRecommendedLevelFingerprint import app.revanced.patches.youtube.utils.compatibility.Constants.COMPATIBLE_PACKAGE -import app.revanced.patches.youtube.utils.playertype.PlayerTypeHookPatch +import app.revanced.patches.youtube.utils.fingerprints.StartVideoInformerFingerprint +import app.revanced.patches.youtube.utils.integrations.Constants.GENERAL_CLASS_DESCRIPTOR import app.revanced.patches.youtube.utils.settings.SettingsPatch import app.revanced.util.patch.BaseBytecodePatch +import app.revanced.util.resultOrThrow @Suppress("unused") object AutoCaptionsPatch : BaseBytecodePatch( name = "Disable auto captions", description = "Adds an option to disable captions from being automatically enabled.", - dependencies = setOf( - AutoCaptionsBytecodePatch::class, - PlayerTypeHookPatch::class, - SettingsPatch::class - ), - compatiblePackages = COMPATIBLE_PACKAGE + dependencies = setOf(SettingsPatch::class), + compatiblePackages = COMPATIBLE_PACKAGE, + fingerprints = setOf( + SubtitleTrackFingerprint, + StartVideoInformerFingerprint, + StoryboardRendererDecoderRecommendedLevelFingerprint, + ) ) { override fun execute(context: BytecodeContext) { + SubtitleTrackFingerprint.resultOrThrow().mutableMethod.apply { + addInstructionsWithLabels( + 0, """ + invoke-static {}, $GENERAL_CLASS_DESCRIPTOR->disableAutoCaptions()Z + move-result v0 + if-eqz v0, :disabled + const/4 v0, 0x1 + return v0 + """, ExternalLabel("disabled", getInstruction(0)) + ) + } + + mapOf( + StartVideoInformerFingerprint to 0, + StoryboardRendererDecoderRecommendedLevelFingerprint to 1 + ).forEach { (fingerprint, enabled) -> + fingerprint.resultOrThrow().mutableMethod.addInstructions( + 0, """ + const/4 v0, 0x$enabled + invoke-static {v0}, $GENERAL_CLASS_DESCRIPTOR->setCaptionsButtonStatus(Z)V + """ + ) + } + /** * Add settings */ diff --git a/src/main/kotlin/app/revanced/patches/youtube/general/autocaptions/fingerprints/StoryboardRendererDecoderRecommendedLevelFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/general/autocaptions/fingerprints/StoryboardRendererDecoderRecommendedLevelFingerprint.kt new file mode 100644 index 000000000..c20b3d2b9 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/general/autocaptions/fingerprints/StoryboardRendererDecoderRecommendedLevelFingerprint.kt @@ -0,0 +1,12 @@ +package app.revanced.patches.youtube.general.autocaptions.fingerprints + +import app.revanced.patcher.extensions.or +import app.revanced.patcher.fingerprint.MethodFingerprint +import com.android.tools.smali.dexlib2.AccessFlags + +internal object StoryboardRendererDecoderRecommendedLevelFingerprint : MethodFingerprint( + returnType = "V", + accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL, + parameters = listOf("Lcom/google/android/libraries/youtube/innertube/model/player/PlayerResponseModel;"), + strings = listOf("#-1#") +) diff --git a/src/main/kotlin/app/revanced/patches/youtube/player/components/PlayerComponentsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/player/components/PlayerComponentsPatch.kt index cf863f8cc..8b3bca28e 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/player/components/PlayerComponentsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/player/components/PlayerComponentsPatch.kt @@ -9,7 +9,6 @@ import app.revanced.patcher.extensions.InstructionExtensions.removeInstruction import app.revanced.patcher.patch.PatchException import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod import app.revanced.patcher.util.smali.ExternalLabel -import app.revanced.patches.shared.fingerprints.StartVideoInformerFingerprint import app.revanced.patches.shared.litho.LithoFilterPatch import app.revanced.patches.youtube.player.components.fingerprints.CrowdfundingBoxFingerprint import app.revanced.patches.youtube.player.components.fingerprints.EngagementPanelControllerFingerprint @@ -34,6 +33,7 @@ import app.revanced.patches.youtube.player.components.fingerprints.WatermarkPare import app.revanced.patches.youtube.player.speedoverlay.SpeedOverlayPatch import app.revanced.patches.youtube.utils.compatibility.Constants.COMPATIBLE_PACKAGE import app.revanced.patches.youtube.utils.controlsoverlay.ControlsOverlayConfigPatch +import app.revanced.patches.youtube.utils.fingerprints.StartVideoInformerFingerprint import app.revanced.patches.youtube.utils.fingerprints.YouTubeControlsOverlayFingerprint import app.revanced.patches.youtube.utils.fix.suggestedvideoendscreen.SuggestedVideoEndScreenPatch import app.revanced.patches.youtube.utils.integrations.Constants.COMPONENTS_PATH diff --git a/src/main/kotlin/app/revanced/patches/shared/fingerprints/StartVideoInformerFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/utils/fingerprints/StartVideoInformerFingerprint.kt similarity index 89% rename from src/main/kotlin/app/revanced/patches/shared/fingerprints/StartVideoInformerFingerprint.kt rename to src/main/kotlin/app/revanced/patches/youtube/utils/fingerprints/StartVideoInformerFingerprint.kt index dec566143..0609b6a3a 100644 --- a/src/main/kotlin/app/revanced/patches/shared/fingerprints/StartVideoInformerFingerprint.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/utils/fingerprints/StartVideoInformerFingerprint.kt @@ -1,4 +1,4 @@ -package app.revanced.patches.shared.fingerprints +package app.revanced.patches.youtube.utils.fingerprints import app.revanced.patcher.extensions.or import app.revanced.patcher.fingerprint.MethodFingerprint diff --git a/src/main/resources/youtube/settings/host/values/strings.xml b/src/main/resources/youtube/settings/host/values/strings.xml index c3a753278..bdc346108 100644 --- a/src/main/resources/youtube/settings/host/values/strings.xml +++ b/src/main/resources/youtube/settings/host/values/strings.xml @@ -319,9 +319,7 @@ Limitations: Watch later Invalid start page, resetting to default. Disable forced auto audio tracks - "Forced auto audio tracks are disabled. - -Limitation: This setting does not apply to Shorts." + Forced auto audio tracks are disabled. Forced auto audio tracks are enabled. Disable forced auto captions Forced auto captions are disabled.