diff --git a/patches/src/main/kotlin/app/revanced/patches/youtube/misc/fix/backtoexitgesture/Fingerprints.kt b/patches/src/main/kotlin/app/revanced/patches/youtube/misc/fix/backtoexitgesture/Fingerprints.kt index fed0199aa..fe8f4684a 100644 --- a/patches/src/main/kotlin/app/revanced/patches/youtube/misc/fix/backtoexitgesture/Fingerprints.kt +++ b/patches/src/main/kotlin/app/revanced/patches/youtube/misc/fix/backtoexitgesture/Fingerprints.kt @@ -15,22 +15,16 @@ internal val onBackPressedFingerprint = fingerprint { } } -internal val recyclerViewScrollingFingerprint = fingerprint { - accessFlags(AccessFlags.PRIVATE, AccessFlags.FINAL) +internal val scrollPositionFingerprint = fingerprint { + accessFlags(AccessFlags.PROTECTED, AccessFlags.FINAL) returns("V") - parameters() + parameters("L") opcodes( - Opcode.IGET_OBJECT, - Opcode.IGET_OBJECT, - Opcode.IF_EQZ, - Opcode.IGET_OBJECT, - Opcode.CHECK_CAST, - Opcode.INVOKE_VIRTUAL, - Opcode.MOVE_RESULT, - Opcode.IF_LEZ, - Opcode.IGET_OBJECT, - Opcode.CONST_4, + Opcode.IF_NEZ, + Opcode.INVOKE_DIRECT, + Opcode.RETURN_VOID ) + strings("scroll_position") } internal val recyclerViewTopScrollingFingerprint = fingerprint { diff --git a/patches/src/main/kotlin/app/revanced/patches/youtube/misc/fix/backtoexitgesture/FixBackToExitGesturePatch.kt b/patches/src/main/kotlin/app/revanced/patches/youtube/misc/fix/backtoexitgesture/FixBackToExitGesturePatch.kt index 0778e814f..3154b3668 100644 --- a/patches/src/main/kotlin/app/revanced/patches/youtube/misc/fix/backtoexitgesture/FixBackToExitGesturePatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/youtube/misc/fix/backtoexitgesture/FixBackToExitGesturePatch.kt @@ -1,54 +1,49 @@ package app.revanced.patches.youtube.misc.fix.backtoexitgesture -import app.revanced.patcher.Fingerprint import app.revanced.patcher.extensions.InstructionExtensions.addInstruction import app.revanced.patcher.patch.bytecodePatch +import app.revanced.util.getReference +import app.revanced.util.indexOfFirstInstructionOrThrow +import com.android.tools.smali.dexlib2.Opcode +import com.android.tools.smali.dexlib2.iface.reference.MethodReference + +private const val EXTENSION_CLASS_DESCRIPTOR = "Lapp/revanced/extension/youtube/patches/FixBackToExitGesturePatch;" internal val fixBackToExitGesturePatch = bytecodePatch( description = "Fixes the swipe back to exit gesture.", ) { execute { - /** - * Inject a call to a method from the extension. - * - * @param targetMethod The target method to call. - */ - fun Fingerprint.injectCall(targetMethod: ExtensionMethod) = method.addInstruction( - patternMatch!!.endIndex, - targetMethod.toString(), - ) + recyclerViewTopScrollingFingerprint.match(recyclerViewTopScrollingParentFingerprint.originalClassDef) + .let { + it.method.addInstruction( + it.patternMatch!!.endIndex, + "invoke-static { }, $EXTENSION_CLASS_DESCRIPTOR->onTopView()V" + ) + } - mapOf( - recyclerViewTopScrollingFingerprint.also { - it.match(recyclerViewTopScrollingParentFingerprint.originalClassDef) - } to ExtensionMethod( - methodName = "onTopView", - ), - recyclerViewScrollingFingerprint to ExtensionMethod( - methodName = "onScrollingViews", - ), - onBackPressedFingerprint to ExtensionMethod( - "p0", - "onBackPressed", - "Landroid/app/Activity;", - ), - ).forEach { (fingerprint, target) -> fingerprint.injectCall(target) } + scrollPositionFingerprint.let { + navigate(it.originalMethod) + .to(it.patternMatch!!.startIndex + 1) + .stop().apply { + val index = indexOfFirstInstructionOrThrow { + opcode == Opcode.INVOKE_VIRTUAL && getReference()?.definingClass == + "Landroid/support/v7/widget/RecyclerView;" + } + + addInstruction( + index, + "invoke-static { }, $EXTENSION_CLASS_DESCRIPTOR->onScrollingViews()V" + ) + } + + } + + onBackPressedFingerprint.let { + it.method.addInstruction( + it.patternMatch!!.endIndex, + "invoke-static { p0 }, $EXTENSION_CLASS_DESCRIPTOR->onBackPressed(Landroid/app/Activity;)V" + ) + } } } - -/** - * A reference to a method from the extension for [fixBackToExitGesturePatch]. - * - * @param register The method registers. - * @param methodName The method name. - * @param parameterTypes The parameters of the method. - */ -private class ExtensionMethod( - val register: String = "", - val methodName: String, - val parameterTypes: String = "", -) { - override fun toString() = - "invoke-static {$register}, Lapp/revanced/extension/youtube/patches/FixBackToExitGesturePatch;->$methodName($parameterTypes)V" -}