From c505d28c1a61b360711d7c52e747c74b109ffade Mon Sep 17 00:00:00 2001 From: inotia00 Date: Sat, 8 Apr 2023 05:23:55 +0900 Subject: [PATCH] refactor(hide-shorts-component): rewrite the code --- .../fingerprints/ShortsCommentFingerprint.kt | 15 ++ .../fingerprints/ShortsRemixFingerprint.kt | 15 ++ .../ShortsSubscriptionsFingerprint.kt | 15 ++ ...> ShortsSubscriptionsTabletFingerprint.kt} | 2 +- ...tsSubscriptionsTabletParentFingerprint.kt} | 2 +- .../patch/ShortsCommentButtonPatch.kt | 44 +++++ .../patch/ShortsComponentPatch.kt | 155 +++--------------- .../patch/ShortsRemixButtonPatch.kt | 44 +++++ .../patch/ShortsSubscriptionsButtonPatch.kt | 86 ++++++++++ .../resourceid/patch/SharedResourceIdPatch.kt | 6 + 10 files changed, 247 insertions(+), 137 deletions(-) create mode 100644 src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/ShortsCommentFingerprint.kt create mode 100644 src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/ShortsRemixFingerprint.kt create mode 100644 src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/ShortsSubscriptionsFingerprint.kt rename src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/{SubscriptionsButtonTabletFingerprint.kt => ShortsSubscriptionsTabletFingerprint.kt} (84%) rename src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/{SubscriptionsButtonTabletParentFingerprint.kt => ShortsSubscriptionsTabletParentFingerprint.kt} (90%) create mode 100644 src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/patch/ShortsCommentButtonPatch.kt create mode 100644 src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/patch/ShortsRemixButtonPatch.kt create mode 100644 src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/patch/ShortsSubscriptionsButtonPatch.kt diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/ShortsCommentFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/ShortsCommentFingerprint.kt new file mode 100644 index 000000000..6ea743681 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/ShortsCommentFingerprint.kt @@ -0,0 +1,15 @@ +package app.revanced.patches.youtube.layout.general.shortscomponent.fingerprints + +import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint +import app.revanced.patches.youtube.misc.resourceid.patch.SharedResourceIdPatch +import org.jf.dexlib2.Opcode +import org.jf.dexlib2.iface.instruction.WideLiteralInstruction + +object ShortsCommentFingerprint : MethodFingerprint( + customFingerprint = { methodDef -> + methodDef.implementation?.instructions?.any { + it.opcode.ordinal == Opcode.CONST.ordinal && + (it as? WideLiteralInstruction)?.wideLiteral == SharedResourceIdPatch.rightCommentLabelId + } == true + } +) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/ShortsRemixFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/ShortsRemixFingerprint.kt new file mode 100644 index 000000000..2400eb5db --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/ShortsRemixFingerprint.kt @@ -0,0 +1,15 @@ +package app.revanced.patches.youtube.layout.general.shortscomponent.fingerprints + +import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint +import app.revanced.patches.youtube.misc.resourceid.patch.SharedResourceIdPatch +import org.jf.dexlib2.Opcode +import org.jf.dexlib2.iface.instruction.WideLiteralInstruction + +object ShortsRemixFingerprint : MethodFingerprint( + customFingerprint = { methodDef -> + methodDef.implementation?.instructions?.any { + it.opcode.ordinal == Opcode.CONST.ordinal && + (it as? WideLiteralInstruction)?.wideLiteral == SharedResourceIdPatch.reelRemixLabelId + } == true + } +) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/ShortsSubscriptionsFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/ShortsSubscriptionsFingerprint.kt new file mode 100644 index 000000000..a7a017e04 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/ShortsSubscriptionsFingerprint.kt @@ -0,0 +1,15 @@ +package app.revanced.patches.youtube.layout.general.shortscomponent.fingerprints + +import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint +import app.revanced.patches.youtube.misc.resourceid.patch.SharedResourceIdPatch +import org.jf.dexlib2.Opcode +import org.jf.dexlib2.iface.instruction.WideLiteralInstruction + +object ShortsSubscriptionsFingerprint : MethodFingerprint( + customFingerprint = { methodDef -> + methodDef.implementation?.instructions?.any { + it.opcode.ordinal == Opcode.CONST.ordinal && + (it as? WideLiteralInstruction)?.wideLiteral == SharedResourceIdPatch.reelPlayerPausedLabelId + } == true + } +) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/SubscriptionsButtonTabletFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/ShortsSubscriptionsTabletFingerprint.kt similarity index 84% rename from src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/SubscriptionsButtonTabletFingerprint.kt rename to src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/ShortsSubscriptionsTabletFingerprint.kt index 7d1735383..8c3682add 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/SubscriptionsButtonTabletFingerprint.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/ShortsSubscriptionsTabletFingerprint.kt @@ -3,7 +3,7 @@ package app.revanced.patches.youtube.layout.general.shortscomponent.fingerprints import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint import org.jf.dexlib2.Opcode -object SubscriptionsButtonTabletFingerprint : MethodFingerprint( +object ShortsSubscriptionsTabletFingerprint : MethodFingerprint( returnType = "V", parameters = listOf("L", "L", "Z"), opcodes = listOf( diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/SubscriptionsButtonTabletParentFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/ShortsSubscriptionsTabletParentFingerprint.kt similarity index 90% rename from src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/SubscriptionsButtonTabletParentFingerprint.kt rename to src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/ShortsSubscriptionsTabletParentFingerprint.kt index 311aacc1e..577ac5252 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/SubscriptionsButtonTabletParentFingerprint.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/fingerprints/ShortsSubscriptionsTabletParentFingerprint.kt @@ -5,7 +5,7 @@ import app.revanced.patches.youtube.misc.resourceid.patch.SharedResourceIdPatch import org.jf.dexlib2.Opcode import org.jf.dexlib2.iface.instruction.WideLiteralInstruction -object SubscriptionsButtonTabletParentFingerprint : MethodFingerprint( +object ShortsSubscriptionsTabletParentFingerprint : MethodFingerprint( customFingerprint = { methodDef -> methodDef.implementation?.instructions?.any { it.opcode.ordinal == Opcode.CONST.ordinal && diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/patch/ShortsCommentButtonPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/patch/ShortsCommentButtonPatch.kt new file mode 100644 index 000000000..3fffce202 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/patch/ShortsCommentButtonPatch.kt @@ -0,0 +1,44 @@ +package app.revanced.patches.youtube.layout.general.shortscomponent.patch + +import app.revanced.extensions.toErrorResult +import app.revanced.patcher.annotation.Name +import app.revanced.patcher.annotation.Version +import app.revanced.patcher.data.BytecodeContext +import app.revanced.patcher.extensions.addInstruction +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.patches.shared.annotation.YouTubeCompatibility +import app.revanced.patches.youtube.layout.general.shortscomponent.fingerprints.ShortsCommentFingerprint +import app.revanced.patches.youtube.misc.resourceid.patch.SharedResourceIdPatch +import app.revanced.util.integrations.Constants.GENERAL +import org.jf.dexlib2.iface.instruction.OneRegisterInstruction +import org.jf.dexlib2.iface.instruction.WideLiteralInstruction + +@Name("hide-shorts-comment") +@DependsOn([SharedResourceIdPatch::class]) +@YouTubeCompatibility +@Version("0.0.1") +class ShortsCommentButtonPatch : BytecodePatch( + listOf(ShortsCommentFingerprint) +) { + override fun execute(context: BytecodeContext): PatchResult { + ShortsCommentFingerprint.result?.mutableMethod?.let { method -> + with (method.implementation!!.instructions) { + val insertIndex = this.indexOfFirst { + (it as? WideLiteralInstruction)?.wideLiteral == SharedResourceIdPatch.rightCommentLabelId + } + 3 + + val insertRegister = (elementAt(insertIndex) as OneRegisterInstruction).registerA + + method.addInstruction( + insertIndex + 1, + "invoke-static {v$insertRegister}, $GENERAL->hideShortsPlayerCommentsButton(Landroid/view/View;)V" + ) + } + } ?: return ShortsCommentFingerprint.toErrorResult() + + return PatchResultSuccess() + } +} diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/patch/ShortsComponentPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/patch/ShortsComponentPatch.kt index c179ef7cb..4981082ee 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/patch/ShortsComponentPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/patch/ShortsComponentPatch.kt @@ -1,37 +1,21 @@ package app.revanced.patches.youtube.layout.general.shortscomponent.patch -import app.revanced.extensions.findMutableMethodOf -import app.revanced.extensions.injectHideCall -import app.revanced.extensions.toErrorResult 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.fingerprint.method.impl.MethodFingerprint.Companion.resolve import app.revanced.patcher.patch.BytecodePatch import app.revanced.patcher.patch.PatchResult -import app.revanced.patcher.patch.PatchResultError import app.revanced.patcher.patch.PatchResultSuccess import app.revanced.patcher.patch.annotations.DependsOn import app.revanced.patcher.patch.annotations.Patch import app.revanced.patches.shared.annotation.YouTubeCompatibility import app.revanced.patches.shared.patch.mapping.ResourceMappingPatch import app.revanced.patches.youtube.ads.general.bytecode.patch.GeneralAdsBytecodePatch -import app.revanced.patches.youtube.layout.general.shortscomponent.fingerprints.* import app.revanced.patches.youtube.misc.litho.patch.LithoFilterPatch import app.revanced.patches.youtube.misc.resourceid.patch.SharedResourceIdPatch import app.revanced.patches.youtube.misc.settings.resource.patch.SettingsPatch import app.revanced.util.bytecode.BytecodeHelper.updatePatchStatus -import app.revanced.util.integrations.Constants.GENERAL -import org.jf.dexlib2.Opcode -import org.jf.dexlib2.iface.instruction.OneRegisterInstruction -import org.jf.dexlib2.iface.instruction.ReferenceInstruction -import org.jf.dexlib2.iface.instruction.TwoRegisterInstruction -import org.jf.dexlib2.iface.instruction.WideLiteralInstruction -import org.jf.dexlib2.iface.instruction.formats.Instruction21c -import org.jf.dexlib2.iface.instruction.formats.Instruction31i -import org.jf.dexlib2.iface.reference.FieldReference @Patch @Name("hide-shorts-component") @@ -42,134 +26,35 @@ import org.jf.dexlib2.iface.reference.FieldReference LithoFilterPatch::class, ResourceMappingPatch::class, SettingsPatch::class, - SharedResourceIdPatch::class + SharedResourceIdPatch::class, + ShortsCommentButtonPatch::class, + ShortsRemixButtonPatch::class, + ShortsSubscriptionsButtonPatch::class ] ) @YouTubeCompatibility @Version("0.0.1") -class ShortsComponentPatch : BytecodePatch( - listOf(SubscriptionsButtonTabletParentFingerprint) -) { - - // list of resource names to get the id of - private val resourceIds = arrayOf( - "ic_right_comment_32c", - "reel_dyn_remix", - "reel_player_paused_state_buttons" - ).map { name -> - ResourceMappingPatch.resourceMappings.single { it.name == name }.id - } - private var patchSuccessArray = Array(resourceIds.size) {false} - +class ShortsComponentPatch : BytecodePatch() { override fun execute(context: BytecodeContext): PatchResult { - context.classes.forEach { classDef -> - classDef.methods.forEach { method -> - with(method.implementation) { - this?.instructions?.forEachIndexed { index, instruction -> - when (instruction.opcode) { - Opcode.CONST -> { - when ((instruction as Instruction31i).wideLiteral) { - resourceIds[0] -> { // shorts player comment - val insertIndex = index - 2 - val invokeInstruction = instructions.elementAt(insertIndex) - if (invokeInstruction.opcode != Opcode.CONST_HIGH16) return@forEachIndexed - val mutableMethod = context.proxy(classDef).mutableClass.findMutableMethodOf(method) + context.updatePatchStatus("ShortsComponent") - val viewRegister = (instructions.elementAt(index + 3) as OneRegisterInstruction).registerA - mutableMethod.implementation!!.injectHideCall(index + 4, viewRegister, "layout/GeneralPatch", "hideShortsPlayerCommentsButton") - - patchSuccessArray[0] = true - } - - resourceIds[1] -> { // shorts player remix - val insertIndex = index - 2 - val invokeInstruction = instructions.elementAt(insertIndex) - if (invokeInstruction.opcode != Opcode.CHECK_CAST) return@forEachIndexed - - val mutableMethod = context.proxy(classDef).mutableClass.findMutableMethodOf(method) - - val viewRegister = (invokeInstruction as Instruction21c).registerA - mutableMethod.implementation!!.injectHideCall(index - 1, viewRegister, "layout/GeneralPatch", "hideShortsPlayerRemixButton") - - patchSuccessArray[1] = true - } - - resourceIds[2] -> { // shorts player subscriptions banner - val insertIndex = index + 3 - val invokeInstruction = instructions.elementAt(insertIndex) - if (invokeInstruction.opcode != Opcode.CHECK_CAST) return@forEachIndexed - - val mutableMethod = context.proxy(classDef).mutableClass.findMutableMethodOf(method) - - val viewRegister = (invokeInstruction as Instruction21c).registerA - mutableMethod.implementation!!.injectHideCall(insertIndex, viewRegister, "layout/GeneralPatch", "hideShortsPlayerSubscriptionsButton") - - patchSuccessArray[2] = true - } - } - } - else -> return@forEachIndexed - } - } - } - } - } - - SubscriptionsButtonTabletParentFingerprint.result?.let { parentResult -> - with (parentResult.mutableMethod.implementation!!.instructions) { - val targetIndex = this.indexOfFirst { - (it as? WideLiteralInstruction)?.wideLiteral == SharedResourceIdPatch.reelPlayerFooterLabelId - } - 1 - if (elementAt(targetIndex).opcode.ordinal != Opcode.IPUT.ordinal) return SubscriptionsButtonTabletFingerprint.toErrorResult() - subscriptionFieldReference = (elementAt(targetIndex) as ReferenceInstruction).reference as FieldReference - } - SubscriptionsButtonTabletFingerprint.also { it.resolve(context, parentResult.classDef) }.result?.mutableMethod?.let { - with (it.implementation!!.instructions) { - filter { instruction -> - val fieldReference = (instruction as? ReferenceInstruction)?.reference as? FieldReference - instruction.opcode.ordinal == Opcode.IGET.ordinal && fieldReference == subscriptionFieldReference - }.forEach { instruction -> - val insertIndex = indexOf(instruction) + 1 - val register = (instruction as TwoRegisterInstruction).registerA - - it.addInstructions( - insertIndex,""" - invoke-static {v$register}, $GENERAL->hideShortsPlayerSubscriptionsButton(I)I - move-result v$register - """ - ) - } - } - } ?: return SubscriptionsButtonTabletFingerprint.toErrorResult() - } ?: return SubscriptionsButtonTabletParentFingerprint.toErrorResult() - - val errorIndex: Int = patchSuccessArray.indexOf(false) - - if (errorIndex == -1) { - context.updatePatchStatus("ShortsComponent") - - /* - * Add settings - */ - SettingsPatch.addPreference( - arrayOf( - "PREFERENCE: GENERAL_SETTINGS", - "SETTINGS: SHORTS_COMPONENT.PARENT", - "SETTINGS: SHORTS_COMPONENT_PARENT.A", - "SETTINGS: SHORTS_COMPONENT_PARENT.B", - "SETTINGS: HIDE_SHORTS_COMPONENTS", - "SETTINGS: HIDE_SHORTS_SHELF" - ) + /* + * Add settings + */ + SettingsPatch.addPreference( + arrayOf( + "PREFERENCE: GENERAL_SETTINGS", + "SETTINGS: SHORTS_COMPONENT.PARENT", + "SETTINGS: SHORTS_COMPONENT_PARENT.A", + "SETTINGS: SHORTS_COMPONENT_PARENT.B", + "SETTINGS: HIDE_SHORTS_COMPONENTS", + "SETTINGS: HIDE_SHORTS_SHELF" ) + ) - SettingsPatch.updatePatchStatus("hide-shorts-component") + SettingsPatch.updatePatchStatus("hide-shorts-component") - return PatchResultSuccess() - } else - return PatchResultError("Instruction not found: $errorIndex") - } - private companion object { - private lateinit var subscriptionFieldReference: FieldReference + return PatchResultSuccess() } } diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/patch/ShortsRemixButtonPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/patch/ShortsRemixButtonPatch.kt new file mode 100644 index 000000000..0fd0f81d9 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/patch/ShortsRemixButtonPatch.kt @@ -0,0 +1,44 @@ +package app.revanced.patches.youtube.layout.general.shortscomponent.patch + +import app.revanced.extensions.toErrorResult +import app.revanced.patcher.annotation.Name +import app.revanced.patcher.annotation.Version +import app.revanced.patcher.data.BytecodeContext +import app.revanced.patcher.extensions.addInstruction +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.patches.shared.annotation.YouTubeCompatibility +import app.revanced.patches.youtube.layout.general.shortscomponent.fingerprints.ShortsRemixFingerprint +import app.revanced.patches.youtube.misc.resourceid.patch.SharedResourceIdPatch +import app.revanced.util.integrations.Constants.GENERAL +import org.jf.dexlib2.iface.instruction.OneRegisterInstruction +import org.jf.dexlib2.iface.instruction.WideLiteralInstruction + +@Name("hide-shorts-remix") +@DependsOn([SharedResourceIdPatch::class]) +@YouTubeCompatibility +@Version("0.0.1") +class ShortsRemixButtonPatch : BytecodePatch( + listOf(ShortsRemixFingerprint) +) { + override fun execute(context: BytecodeContext): PatchResult { + ShortsRemixFingerprint.result?.mutableMethod?.let { method -> + with (method.implementation!!.instructions) { + val insertIndex = this.indexOfFirst { + (it as? WideLiteralInstruction)?.wideLiteral == SharedResourceIdPatch.reelRemixLabelId + } - 2 + + val insertRegister = (elementAt(insertIndex) as OneRegisterInstruction).registerA + + method.addInstruction( + insertIndex, + "invoke-static {v$insertRegister}, $GENERAL->hideShortsPlayerRemixButton(Landroid/view/View;)V" + ) + } + } ?: return ShortsRemixFingerprint.toErrorResult() + + return PatchResultSuccess() + } +} diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/patch/ShortsSubscriptionsButtonPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/patch/ShortsSubscriptionsButtonPatch.kt new file mode 100644 index 000000000..73db8833e --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/general/shortscomponent/patch/ShortsSubscriptionsButtonPatch.kt @@ -0,0 +1,86 @@ +package app.revanced.patches.youtube.layout.general.shortscomponent.patch + +import app.revanced.extensions.toErrorResult +import app.revanced.patcher.annotation.Name +import app.revanced.patcher.annotation.Version +import app.revanced.patcher.data.BytecodeContext +import app.revanced.patcher.extensions.addInstruction +import app.revanced.patcher.extensions.addInstructions +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.patches.shared.annotation.YouTubeCompatibility +import app.revanced.patches.youtube.layout.general.shortscomponent.fingerprints.ShortsSubscriptionsFingerprint +import app.revanced.patches.youtube.layout.general.shortscomponent.fingerprints.ShortsSubscriptionsTabletFingerprint +import app.revanced.patches.youtube.layout.general.shortscomponent.fingerprints.ShortsSubscriptionsTabletParentFingerprint +import app.revanced.patches.youtube.misc.resourceid.patch.SharedResourceIdPatch +import app.revanced.util.integrations.Constants.GENERAL +import org.jf.dexlib2.Opcode +import org.jf.dexlib2.iface.instruction.OneRegisterInstruction +import org.jf.dexlib2.iface.instruction.ReferenceInstruction +import org.jf.dexlib2.iface.instruction.TwoRegisterInstruction +import org.jf.dexlib2.iface.instruction.WideLiteralInstruction +import org.jf.dexlib2.iface.reference.FieldReference + +@Name("hide-shorts-subscriptions") +@DependsOn([SharedResourceIdPatch::class]) +@YouTubeCompatibility +@Version("0.0.1") +class ShortsSubscriptionsButtonPatch : BytecodePatch( + listOf( + ShortsSubscriptionsFingerprint, + ShortsSubscriptionsTabletParentFingerprint + ) +) { + override fun execute(context: BytecodeContext): PatchResult { + ShortsSubscriptionsFingerprint.result?.mutableMethod?.let { method -> + with (method.implementation!!.instructions) { + val insertIndex = this.indexOfFirst { + (it as? WideLiteralInstruction)?.wideLiteral == SharedResourceIdPatch.reelPlayerPausedLabelId + } + 2 + + val insertRegister = (elementAt(insertIndex) as OneRegisterInstruction).registerA + + method.addInstruction( + insertIndex + 1, + "invoke-static {v$insertRegister}, $GENERAL->hideShortsPlayerSubscriptionsButton(Landroid/view/View;)V" + ) + } + } ?: return ShortsSubscriptionsFingerprint.toErrorResult() + + ShortsSubscriptionsTabletParentFingerprint.result?.let { parentResult -> + with (parentResult.mutableMethod.implementation!!.instructions) { + val targetIndex = this.indexOfFirst { + (it as? WideLiteralInstruction)?.wideLiteral == SharedResourceIdPatch.reelPlayerFooterLabelId + } - 1 + if (elementAt(targetIndex).opcode.ordinal != Opcode.IPUT.ordinal) return ShortsSubscriptionsTabletFingerprint.toErrorResult() + subscriptionFieldReference = (elementAt(targetIndex) as ReferenceInstruction).reference as FieldReference + } + ShortsSubscriptionsTabletFingerprint.also { it.resolve(context, parentResult.classDef) }.result?.mutableMethod?.let { + with (it.implementation!!.instructions) { + filter { instruction -> + val fieldReference = (instruction as? ReferenceInstruction)?.reference as? FieldReference + instruction.opcode.ordinal == Opcode.IGET.ordinal && fieldReference == subscriptionFieldReference + }.forEach { instruction -> + val insertIndex = indexOf(instruction) + 1 + val register = (instruction as TwoRegisterInstruction).registerA + + it.addInstructions( + insertIndex,""" + invoke-static {v$register}, $GENERAL->hideShortsPlayerSubscriptionsButton(I)I + move-result v$register + """ + ) + } + } + } ?: return ShortsSubscriptionsTabletFingerprint.toErrorResult() + } ?: return ShortsSubscriptionsTabletParentFingerprint.toErrorResult() + + return PatchResultSuccess() + } + private companion object { + private lateinit var subscriptionFieldReference: FieldReference + } +} diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/resourceid/patch/SharedResourceIdPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/resourceid/patch/SharedResourceIdPatch.kt index f4f1fe0c1..9eafe90fd 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/misc/resourceid/patch/SharedResourceIdPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/resourceid/patch/SharedResourceIdPatch.kt @@ -40,7 +40,10 @@ class SharedResourceIdPatch : ResourcePatch { var layoutVideo: Long = -1 var liveChatButtonId: Long = -1 var reelPlayerFooterLabelId: Long = -1 + var reelPlayerPausedLabelId: Long = -1 + var reelRemixLabelId: Long = -1 var relatedChipCloudMarginLabelId: Long = -1 + var rightCommentLabelId: Long = -1 var scrubbingLabelId: Long = -1 var timeStampsContainerLabelId: Long = -1 var tooltipLabelId: Long = -1 @@ -75,7 +78,10 @@ class SharedResourceIdPatch : ResourcePatch { layoutVideo = find(LAYOUT, "endscreen_element_layout_video") liveChatButtonId = find(ID, "live_chat_overlay_button") reelPlayerFooterLabelId = find(LAYOUT, "reel_player_dyn_footer_vert_stories3") + reelPlayerPausedLabelId = find(ID, "reel_player_paused_state_buttons") + reelRemixLabelId = find(ID, "reel_dyn_remix") relatedChipCloudMarginLabelId = find(LAYOUT, "related_chip_cloud_reduced_margins") + rightCommentLabelId = find(DRAWABLE, "ic_right_comment_32c") scrubbingLabelId = find(DIMEN, "vertical_touch_offset_to_enter_fine_scrubbing") timeStampsContainerLabelId = find(ID, "timestamps_container") tooltipLabelId = find(LAYOUT, "tooltip_content_view")