From 0d8d180fbf52866542687def3bfbf74c2f66525a Mon Sep 17 00:00:00 2001 From: OxrxL <108184954+OxrxL@users.noreply.github.com> Date: Sat, 29 Oct 2022 16:01:52 +0200 Subject: [PATCH] feat(youtube/comments): hide shorts comments button (#866) Co-authored-by: oSumAtrIX --- .../ShortsCommentsButtonFingerprint.kt | 23 +++++++ .../comments/bytecode/patch/CommentsPatch.kt | 68 +++++++++++++++++++ .../patch/CommentsResourcePatch.kt} | 31 ++++++--- 3 files changed, 112 insertions(+), 10 deletions(-) create mode 100644 src/main/kotlin/app/revanced/patches/youtube/layout/comments/bytecode/fingerprints/ShortsCommentsButtonFingerprint.kt create mode 100644 src/main/kotlin/app/revanced/patches/youtube/layout/comments/bytecode/patch/CommentsPatch.kt rename src/main/kotlin/app/revanced/patches/youtube/layout/comments/{patch/CommentsPatch.kt => resource/patch/CommentsResourcePatch.kt} (70%) diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/comments/bytecode/fingerprints/ShortsCommentsButtonFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/comments/bytecode/fingerprints/ShortsCommentsButtonFingerprint.kt new file mode 100644 index 000000000..4cc9ef23a --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/comments/bytecode/fingerprints/ShortsCommentsButtonFingerprint.kt @@ -0,0 +1,23 @@ +package app.revanced.patches.youtube.layout.comments.bytecode.fingerprints + +import app.revanced.patcher.annotation.Name +import app.revanced.patcher.annotation.Version +import app.revanced.patcher.extensions.or +import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint +import app.revanced.patches.youtube.layout.comments.annotations.CommentsCompatibility +import app.revanced.patches.youtube.layout.comments.resource.patch.CommentsResourcePatch +import org.jf.dexlib2.AccessFlags +import org.jf.dexlib2.Opcode +import org.jf.dexlib2.iface.instruction.WideLiteralInstruction + +@Name("shorts-comments-button-fingerprint") +@CommentsCompatibility +@Version("0.0.1") +object ShortsCommentsButtonFingerprint : MethodFingerprint( + "V", AccessFlags.PRIVATE or AccessFlags.FINAL, listOf("Z", "Z", "L"), + customFingerprint = { methodDef -> + methodDef.implementation?.instructions?.any { + it.opcode.ordinal == Opcode.CONST.ordinal && (it as WideLiteralInstruction).wideLiteral == CommentsResourcePatch.shortsCommentsButtonId + } == true + } +) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/comments/bytecode/patch/CommentsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/comments/bytecode/patch/CommentsPatch.kt new file mode 100644 index 000000000..b0816b31d --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/comments/bytecode/patch/CommentsPatch.kt @@ -0,0 +1,68 @@ +package app.revanced.patches.youtube.layout.comments.bytecode.patch + +import app.revanced.patcher.annotation.Description +import app.revanced.patcher.annotation.Name +import app.revanced.patcher.annotation.Version +import app.revanced.patcher.data.BytecodeContext +import app.revanced.patcher.extensions.addInstructions +import app.revanced.patcher.extensions.instruction +import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint +import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint.Companion.resolve +import app.revanced.patcher.patch.BytecodePatch +import app.revanced.patcher.patch.PatchResult +import app.revanced.patcher.patch.PatchResultSuccess +import app.revanced.patcher.patch.annotations.DependsOn +import app.revanced.patcher.patch.annotations.Patch +import app.revanced.patches.youtube.ad.general.bytecode.patch.GeneralBytecodeAdsPatch +import app.revanced.patches.youtube.layout.comments.annotations.CommentsCompatibility +import app.revanced.patches.youtube.layout.comments.bytecode.fingerprints.ShortsCommentsButtonFingerprint +import app.revanced.patches.youtube.layout.comments.resource.patch.CommentsResourcePatch +import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch +import app.revanced.patches.youtube.misc.mapping.patch.ResourceMappingResourcePatch +import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch +import app.revanced.patches.youtube.misc.settings.framework.components.impl.PreferenceScreen +import app.revanced.patches.youtube.misc.settings.framework.components.impl.StringResource +import app.revanced.patches.youtube.misc.settings.framework.components.impl.SwitchPreference +import org.jf.dexlib2.Opcode +import org.jf.dexlib2.iface.instruction.OneRegisterInstruction + +@Patch +@DependsOn([IntegrationsPatch::class, CommentsResourcePatch::class]) +@Name("comments") +@Description("Hides components related to comments.") +@CommentsCompatibility +@Version("0.0.1") +class CommentsPatch : BytecodePatch( + listOf( + ShortsCommentsButtonFingerprint + ) +) { + override fun execute(context: BytecodeContext): PatchResult { + val shortsCommentsButtonResult = ShortsCommentsButtonFingerprint.result!! + val shortsCommentsButtonMethod = shortsCommentsButtonResult.mutableMethod + + val checkCastAnchorFingerprint = object : MethodFingerprint( + opcodes = listOf( + Opcode.CONST, + Opcode.CONST_HIGH16, + Opcode.IF_EQZ, + Opcode.CONST, + Opcode.INVOKE_STATIC, + Opcode.MOVE_RESULT_OBJECT, + Opcode.CHECK_CAST, + ) + ) {} + + val checkCastAnchorIndex = checkCastAnchorFingerprint.also { + it.resolve(context, shortsCommentsButtonMethod, shortsCommentsButtonResult.classDef) + }.result!!.scanResult.patternScanResult!!.endIndex + + shortsCommentsButtonMethod.addInstructions( + checkCastAnchorIndex + 1, """ + invoke-static {v${(shortsCommentsButtonMethod.instruction(checkCastAnchorIndex) as OneRegisterInstruction).registerA}}, Lapp/revanced/integrations/patches/HideShortsCommentsButtonPatch;->hideShortsCommentsButton(Landroid/view/View;)V + """ + ) + + return PatchResultSuccess() + } +} diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/comments/patch/CommentsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/comments/resource/patch/CommentsResourcePatch.kt similarity index 70% rename from src/main/kotlin/app/revanced/patches/youtube/layout/comments/patch/CommentsPatch.kt rename to src/main/kotlin/app/revanced/patches/youtube/layout/comments/resource/patch/CommentsResourcePatch.kt index d966b6898..4ce5197aa 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/layout/comments/patch/CommentsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/comments/resource/patch/CommentsResourcePatch.kt @@ -1,6 +1,5 @@ -package app.revanced.patches.youtube.layout.comments.patch +package app.revanced.patches.youtube.layout.comments.resource.patch -import app.revanced.patcher.annotation.Description import app.revanced.patcher.annotation.Name import app.revanced.patcher.annotation.Version import app.revanced.patcher.data.ResourceContext @@ -8,8 +7,6 @@ import app.revanced.patcher.patch.PatchResult import app.revanced.patcher.patch.PatchResultSuccess import app.revanced.patcher.patch.ResourcePatch import app.revanced.patcher.patch.annotations.DependsOn -import app.revanced.patcher.patch.annotations.Patch -import app.revanced.patches.youtube.ad.general.bytecode.patch.GeneralBytecodeAdsPatch import app.revanced.patches.youtube.layout.comments.annotations.CommentsCompatibility import app.revanced.patches.youtube.misc.mapping.patch.ResourceMappingResourcePatch import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch @@ -17,13 +14,15 @@ import app.revanced.patches.youtube.misc.settings.framework.components.impl.Pref import app.revanced.patches.youtube.misc.settings.framework.components.impl.StringResource import app.revanced.patches.youtube.misc.settings.framework.components.impl.SwitchPreference -@Patch -@DependsOn([ResourceMappingResourcePatch::class, GeneralBytecodeAdsPatch::class]) -@Name("comments") -@Description("Hides comments components below the video player.") +@Name("comments-resource-patch") @CommentsCompatibility +@DependsOn([SettingsPatch::class, ResourceMappingResourcePatch::class]) @Version("0.0.1") -class CommentsPatch : ResourcePatch { +class CommentsResourcePatch : ResourcePatch { + companion object { + internal var shortsCommentsButtonId: Long = -1 + } + override fun execute(context: ResourceContext): PatchResult { SettingsPatch.PreferenceScreen.LAYOUT.addPreferences( PreferenceScreen( @@ -44,10 +43,22 @@ class CommentsPatch : ResourcePatch { StringResource("revanced_hide_preview_comment_on", "Preview comment is hidden"), StringResource("revanced_hide_preview_comment_off", "Preview comment is shown") ), + SwitchPreference( + "revanced_hide_shorts_comments_button", + StringResource("revanced_hide_shorts_comments_button_title", "Hide shorts comments button"), + false, + StringResource("revanced_hide_shorts_comments_button_on", "Shorts comments button is hidden"), + StringResource("revanced_hide_shorts_comments_button_off", "Shorts comments button is shown") + ), ), StringResource("revanced_comments_summary", "Manage the visibility of comments section components") ) ) + + shortsCommentsButtonId = ResourceMappingResourcePatch.resourceMappings.single { + it.type == "drawable" && it.name == "ic_right_comment_32c" + }.id + return PatchResultSuccess() } -} +} \ No newline at end of file