From 1e13c4e83ed280acd75c9323613e4367fff54ec6 Mon Sep 17 00:00:00 2001 From: inotia00 <108592928+inotia00@users.noreply.github.com> Date: Sun, 22 Dec 2024 16:38:26 +0900 Subject: [PATCH] fix(Reddit - Hide ads): `Hide ads` patch fails on `2024.17.0` https://github.com/inotia00/ReVanced_Extended/issues/2592 --- .../revanced/patches/reddit/ad/AdsPatch.kt | 18 ++++++------- .../patches/reddit/ad/Fingerprints.kt | 27 ++++++++++++++++--- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/patches/src/main/kotlin/app/revanced/patches/reddit/ad/AdsPatch.kt b/patches/src/main/kotlin/app/revanced/patches/reddit/ad/AdsPatch.kt index aff6e4335..d01e0bc6e 100644 --- a/patches/src/main/kotlin/app/revanced/patches/reddit/ad/AdsPatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/reddit/ad/AdsPatch.kt @@ -17,12 +17,10 @@ import app.revanced.util.fingerprint.methodOrThrow import app.revanced.util.getReference import app.revanced.util.getWalkerMethod import app.revanced.util.indexOfFirstInstructionOrThrow -import app.revanced.util.indexOfFirstStringInstructionOrThrow -import com.android.tools.smali.dexlib2.Opcode +import app.revanced.util.indexOfFirstStringInstruction import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction import com.android.tools.smali.dexlib2.iface.instruction.TwoRegisterInstruction import com.android.tools.smali.dexlib2.iface.reference.FieldReference -import com.android.tools.smali.dexlib2.iface.reference.MethodReference private const val RESOURCE_FILE_PATH = "res/layout/merge_listheader_link_detail.xml" @@ -113,13 +111,13 @@ val adsPatch = bytecodePatch( // The new feeds work by inserting posts into lists. // AdElementConverter is conveniently responsible for inserting all feed ads. // By removing the appending instruction no ad posts gets appended to the feed. - newAdPostFingerprint.methodOrThrow().apply { - val stringIndex = - indexOfFirstStringInstructionOrThrow("android_feed_freeform_render_variant") - val targetIndex = indexOfFirstInstructionOrThrow(stringIndex) { - opcode == Opcode.INVOKE_VIRTUAL - && getReference()?.toString() == "Ljava/util/ArrayList;->add(Ljava/lang/Object;)Z" - } + val newAdPostMethod = newAdPostFingerprint.second.methodOrNull + ?: newAdPostLegacyFingerprint.methodOrThrow() + + newAdPostMethod.apply { + val startIndex = + 0.coerceAtLeast(indexOfFirstStringInstruction("android_feed_freeform_render_variant")) + val targetIndex = indexOfAddArrayListInstruction(this, startIndex) val targetInstruction = getInstruction(targetIndex) replaceInstruction( diff --git a/patches/src/main/kotlin/app/revanced/patches/reddit/ad/Fingerprints.kt b/patches/src/main/kotlin/app/revanced/patches/reddit/ad/Fingerprints.kt index 726e76933..ad7bd5971 100644 --- a/patches/src/main/kotlin/app/revanced/patches/reddit/ad/Fingerprints.kt +++ b/patches/src/main/kotlin/app/revanced/patches/reddit/ad/Fingerprints.kt @@ -6,6 +6,7 @@ import app.revanced.util.indexOfFirstInstruction import app.revanced.util.or import com.android.tools.smali.dexlib2.AccessFlags import com.android.tools.smali.dexlib2.Opcode +import com.android.tools.smali.dexlib2.iface.Method import com.android.tools.smali.dexlib2.iface.reference.MethodReference internal val commentAdsFingerprint = legacyFingerprint( @@ -37,7 +38,7 @@ internal val adPostFingerprint = legacyFingerprint( "children", "uxExperiences" ), - customFingerprint = { method, classDef -> + customFingerprint = { _, classDef -> classDef.type.endsWith("/Listing;") }, ) @@ -51,9 +52,27 @@ internal val newAdPostFingerprint = legacyFingerprint( "android_feed_freeform_render_variant", ), customFingerprint = { method, _ -> - method.indexOfFirstInstruction { - getReference()?.toString() == "Ljava/util/ArrayList;->add(Ljava/lang/Object;)Z" - } >= 0 + indexOfAddArrayListInstruction(method) >= 0 }, ) +internal val newAdPostLegacyFingerprint = legacyFingerprint( + name = "newAdPostLegacyFingerprint", + returnType = "L", + accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL, + opcodes = listOf(Opcode.INVOKE_VIRTUAL), + strings = listOf( + "chain", + "feedElement" + ), + customFingerprint = { method, classDef -> + classDef.sourceFile == "AdElementConverter.kt" && + indexOfAddArrayListInstruction(method) >= 0 + }, +) + +internal fun indexOfAddArrayListInstruction(method: Method, index: Int = 0) = + method.indexOfFirstInstruction(index) { + getReference()?.toString() == "Ljava/util/ArrayList;->add(Ljava/lang/Object;)Z" + } +