From f711d21b2f037598a762be780ef4ab6f0a014c85 Mon Sep 17 00:00:00 2001 From: inotia00 <108592928+inotia00@users.noreply.github.com> Date: Thu, 28 Sep 2023 23:08:49 +0900 Subject: [PATCH] fix(youtube/hide-shorts-components): `Hide pivot button` makes empty space --- .../fingerprints/ShortsPivotFingerprint.kt | 14 ++++ .../patch/ShortsComponentPatch.kt | 1 + .../patch/ShortsPivotButtonPatch.kt | 71 +++++++++++++++++++ .../resourceid/patch/SharedResourceIdPatch.kt | 2 + 4 files changed, 88 insertions(+) create mode 100644 src/main/kotlin/app/revanced/patches/youtube/shorts/shortscomponent/fingerprints/ShortsPivotFingerprint.kt create mode 100644 src/main/kotlin/app/revanced/patches/youtube/shorts/shortscomponent/patch/ShortsPivotButtonPatch.kt diff --git a/src/main/kotlin/app/revanced/patches/youtube/shorts/shortscomponent/fingerprints/ShortsPivotFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/shorts/shortscomponent/fingerprints/ShortsPivotFingerprint.kt new file mode 100644 index 000000000..fc02db005 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/shorts/shortscomponent/fingerprints/ShortsPivotFingerprint.kt @@ -0,0 +1,14 @@ +package app.revanced.patches.youtube.shorts.shortscomponent.fingerprints + +import app.revanced.patcher.extensions.or +import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint +import app.revanced.patches.youtube.utils.resourceid.patch.SharedResourceIdPatch.Companion.ReelForcedMuteButton +import app.revanced.util.bytecode.isWideLiteralExists +import com.android.tools.smali.dexlib2.AccessFlags + +object ShortsPivotFingerprint : MethodFingerprint( + returnType = "V", + accessFlags = AccessFlags.PRIVATE or AccessFlags.FINAL, + parameters = listOf("Z", "Z", "L"), + customFingerprint = { methodDef, _ -> methodDef.isWideLiteralExists(ReelForcedMuteButton) } +) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/youtube/shorts/shortscomponent/patch/ShortsComponentPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/shorts/shortscomponent/patch/ShortsComponentPatch.kt index bf3ab3f21..c426b487f 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/shorts/shortscomponent/patch/ShortsComponentPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/shorts/shortscomponent/patch/ShortsComponentPatch.kt @@ -29,6 +29,7 @@ import app.revanced.util.integrations.Constants.PATCHES_PATH ShortsLikeButtonPatch::class, ShortsNavigationBarPatch::class, ShortsPaidPromotionBannerPatch::class, + ShortsPivotButtonPatch::class, ShortsRemixButtonPatch::class, ShortsShareButtonPatch::class, ShortsSubscriptionsButtonPatch::class, diff --git a/src/main/kotlin/app/revanced/patches/youtube/shorts/shortscomponent/patch/ShortsPivotButtonPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/shorts/shortscomponent/patch/ShortsPivotButtonPatch.kt new file mode 100644 index 000000000..99812d8e0 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/shorts/shortscomponent/patch/ShortsPivotButtonPatch.kt @@ -0,0 +1,71 @@ +package app.revanced.patches.youtube.shorts.shortscomponent.patch + +import app.revanced.extensions.exception +import app.revanced.patcher.data.BytecodeContext +import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels +import app.revanced.patcher.extensions.InstructionExtensions.getInstruction +import app.revanced.patcher.patch.BytecodePatch +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.youtube.shorts.shortscomponent.fingerprints.ShortsPivotFingerprint +import app.revanced.patches.youtube.utils.resourceid.patch.SharedResourceIdPatch.Companion.ReelForcedMuteButton +import app.revanced.util.bytecode.getWideLiteralIndex +import app.revanced.util.integrations.Constants.SHORTS +import com.android.tools.smali.dexlib2.Opcode +import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction + +class ShortsPivotButtonPatch : BytecodePatch( + listOf(ShortsPivotFingerprint) +) { + override fun execute(context: BytecodeContext) { + ShortsPivotFingerprint.result?.let { + it.mutableMethod.apply { + val targetIndex = getWideLiteralIndex(ReelForcedMuteButton) + val targetRegister = getInstruction(targetIndex).registerA + + val insertIndex = getTargetIndexDownTo(targetIndex, Opcode.IF_EQZ) + val jumpIndex = getTargetIndexUpTo(targetIndex, Opcode.GOTO) + + if (insertIndex == -1 || jumpIndex == -1) + throw PatchException("Failed to find hook method") + + addInstructionsWithLabels( + insertIndex, """ + invoke-static {}, $SHORTS->hideShortsPlayerPivotButton()Z + move-result v$targetRegister + if-nez v$targetRegister, :hide + """, ExternalLabel("hide", getInstruction(jumpIndex)) + ) + } + } ?: throw ShortsPivotFingerprint.exception + + } + private companion object { + fun MutableMethod.getTargetIndexDownTo( + startIndex: Int, + opcode: Opcode + ): Int { + for (index in startIndex downTo 0) { + if (getInstruction(index).opcode != opcode) + continue + + return index + } + return -1 + } + + fun MutableMethod.getTargetIndexUpTo( + startIndex: Int, + opcode: Opcode + ): Int { + for (index in startIndex until implementation!!.instructions.size) { + if (getInstruction(index).opcode != opcode) + continue + + return index + } + return -1 + } + } +} diff --git a/src/main/kotlin/app/revanced/patches/youtube/utils/resourceid/patch/SharedResourceIdPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/utils/resourceid/patch/SharedResourceIdPatch.kt index e12e6fddc..491722630 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/utils/resourceid/patch/SharedResourceIdPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/utils/resourceid/patch/SharedResourceIdPatch.kt @@ -58,6 +58,7 @@ class SharedResourceIdPatch : ResourcePatch { var QuickActionsElementContainer: Long = -1 var ReelDynRemix: Long = -1 var ReelDynShare: Long = -1 + var ReelForcedMuteButton: Long = -1 var ReelPlayerBadge: Long = -1 var ReelPlayerBadge2: Long = -1 var ReelPlayerFooter: Long = -1 @@ -137,6 +138,7 @@ class SharedResourceIdPatch : ResourcePatch { QuickActionsElementContainer = find(ID, "quick_actions_element_container") ReelDynRemix = find(ID, "reel_dyn_remix") ReelDynShare = find(ID, "reel_dyn_share") + ReelForcedMuteButton = find(ID, "reel_player_forced_mute_button") ReelPlayerBadge = find(ID, "reel_player_badge") ReelPlayerBadge2 = find(ID, "reel_player_badge2") ReelPlayerFooter = find(LAYOUT, "reel_player_dyn_footer_vert_stories3")