From 4540e7a48462cb4ed622e77b5c4bd969e396d437 Mon Sep 17 00:00:00 2001 From: Pun Butrach Date: Mon, 12 Jan 2026 15:20:18 +0700 Subject: [PATCH] refactor(reddit): HideAdsPatch --- .../patches/reddit/ad/general/Fingerprints.kt | 26 ++++++++++++------- .../patches/reddit/ad/general/HideAdsPatch.kt | 20 +++++++------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/patches/src/main/kotlin/app/revanced/patches/reddit/ad/general/Fingerprints.kt b/patches/src/main/kotlin/app/revanced/patches/reddit/ad/general/Fingerprints.kt index 22cf466fc..a637f8b6d 100644 --- a/patches/src/main/kotlin/app/revanced/patches/reddit/ad/general/Fingerprints.kt +++ b/patches/src/main/kotlin/app/revanced/patches/reddit/ad/general/Fingerprints.kt @@ -1,16 +1,24 @@ -package app.revanced.patches.reddit.ad.general +package app.revanced.patches.nunl.ads -import app.revanced.patcher.fingerprint +import app.revanced.patcher.BytecodePatchContextMethodMatching.gettingFirstMutableMethodDeclaratively +import app.revanced.patcher.definingClass +import app.revanced.patcher.instructions +import app.revanced.patcher.invoke +import app.revanced.patcher.patch.BytecodePatchContext +import app.revanced.patcher.returnType import com.android.tools.smali.dexlib2.Opcode -internal val adPostFingerprint = fingerprint { - returns("V") +internal val BytecodePatchContext.adPostMethod by gettingFirstMutableMethodDeclaratively("children") { + returnType("V") // "children" are present throughout multiple versions - strings("children") - custom { _, classDef -> classDef.endsWith("Listing;") } + instructions("children"()) + definingClass { endsWith("Listing;") } } -internal val newAdPostFingerprint = fingerprint { - opcodes(Opcode.INVOKE_VIRTUAL) - strings("feedElement", "com.reddit.cookie") +internal val BytecodePatchContext.newAdPostMethod by gettingFirstMutableMethodDeclaratively( + "feedElement", "com.reddit.cookie" +) { + instructions(Opcode.INVOKE_VIRTUAL()) + instructions("feedElement"()) + instructions("com.reddit.cookie"()) } diff --git a/patches/src/main/kotlin/app/revanced/patches/reddit/ad/general/HideAdsPatch.kt b/patches/src/main/kotlin/app/revanced/patches/reddit/ad/general/HideAdsPatch.kt index 14e53a3d0..f4393c252 100644 --- a/patches/src/main/kotlin/app/revanced/patches/reddit/ad/general/HideAdsPatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/reddit/ad/general/HideAdsPatch.kt @@ -2,8 +2,10 @@ package app.revanced.patches.reddit.ad.general import app.revanced.patcher.extensions.addInstructions import app.revanced.patcher.extensions.removeInstruction -import app.revanced.patcher.patch.bytecodePatch -import app.revanced.patches.reddit.ad.comments.hideCommentAdsPatch +import app.revanced.patcher.patch.creatingBytecodePatch +import app.revanced.patches.nunl.ads.adPostMethod +import app.revanced.patches.nunl.ads.newAdPostMethod +import app.revanced.patches.reddit.ad.comments.`Hide comment ads` import app.revanced.patches.reddit.misc.extension.sharedExtensionPatch import com.android.tools.smali.dexlib2.Opcode import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction @@ -11,11 +13,11 @@ import com.android.tools.smali.dexlib2.iface.instruction.formats.Instruction22c import com.android.tools.smali.dexlib2.iface.reference.FieldReference import com.android.tools.smali.dexlib2.iface.reference.MethodReference -@Suppress("unused") -val hideAdsPatch = bytecodePatch( - name = "Hide ads", +@Suppress("unused", "ObjectPropertyName") +val `Hide ads` by creatingBytecodePatch( + description = "Hide ads" ) { - dependsOn(hideCommentAdsPatch, sharedExtensionPatch) + dependsOn(`Hide comment ads`, sharedExtensionPatch) compatibleWith("com.reddit.frontpage") @@ -26,7 +28,7 @@ val hideAdsPatch = bytecodePatch( "Lapp/revanced/extension/reddit/patches/FilterPromotedLinksPatch;" + "->filterChildren(Ljava/lang/Iterable;)Ljava/util/List;" - adPostFingerprint.method.apply { + adPostMethod.apply { val setPostsListChildren = implementation!!.instructions.first { instruction -> if (instruction.opcode != Opcode.IPUT_OBJECT) return@first false @@ -58,7 +60,7 @@ val hideAdsPatch = bytecodePatch( // AdElementConverter is conveniently responsible for inserting all feed ads. // By removing the appending instruction no ad posts gets appended to the feed. - val index = newAdPostFingerprint.originalMethod.implementation!!.instructions.indexOfFirst { + val index = newAdPostMethod.implementation!!.instructions.indexOfFirst { if (it.opcode != Opcode.INVOKE_VIRTUAL) return@indexOfFirst false val reference = (it as ReferenceInstruction).reference as MethodReference @@ -66,7 +68,7 @@ val hideAdsPatch = bytecodePatch( reference.name == "add" && reference.definingClass == "Ljava/util/ArrayList;" } - newAdPostFingerprint.method.removeInstruction(index) + newAdPostMethod.removeInstruction(index) } // endregion