refactor(hide-shorts-component): rewrite the code

This commit is contained in:
inotia00 2023-04-08 05:23:55 +09:00
parent 37e64b6a1f
commit c505d28c1a
10 changed files with 247 additions and 137 deletions

View File

@ -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
}
)

View File

@ -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
}
)

View File

@ -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
}
)

View File

@ -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(

View File

@ -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 &&

View File

@ -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()
}
}

View File

@ -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()
}
}

View File

@ -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()
}
}

View File

@ -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
}
}

View File

@ -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")