@@ -5,92 +5,42 @@ import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWith
5
5
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
6
6
import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction
7
7
import app.revanced.patcher.patch.bytecodePatch
8
- import app.revanced.patcher.patch.resourcePatch
9
- import app.revanced.patcher.util.smali.ExternalLabel
10
8
import app.revanced.patches.reddit.utils.compatibility.Constants.COMPATIBLE_PACKAGE
11
9
import app.revanced.patches.reddit.utils.extension.Constants.PATCHES_PATH
12
10
import app.revanced.patches.reddit.utils.patch.PatchList.HIDE_ADS
13
11
import app.revanced.patches.reddit.utils.settings.settingsPatch
14
12
import app.revanced.patches.reddit.utils.settings.updatePatchStatus
15
- import app.revanced.util.fingerprint.matchOrThrow
13
+ import app.revanced.util.findMutableMethodOf
16
14
import app.revanced.util.fingerprint.methodOrThrow
17
15
import app.revanced.util.getReference
18
- import app.revanced.util.getWalkerMethod
19
16
import app.revanced.util.indexOfFirstInstructionOrThrow
20
17
import app.revanced.util.indexOfFirstStringInstruction
18
+ import app.revanced.util.or
19
+ import com.android.tools.smali.dexlib2.AccessFlags
20
+ import com.android.tools.smali.dexlib2.iface.Method
21
21
import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction
22
22
import com.android.tools.smali.dexlib2.iface.instruction.TwoRegisterInstruction
23
23
import com.android.tools.smali.dexlib2.iface.reference.FieldReference
24
24
25
- private const val RESOURCE_FILE_PATH = " res/layout/merge_listheader_link_detail.xml"
26
-
27
- private val bannerAdsPatch = resourcePatch(
28
- description = " bannerAdsPatch" ,
29
- ) {
30
- execute {
31
- document(RESOURCE_FILE_PATH ).use { document ->
32
- document.getElementsByTagName(" merge" ).item(0 ).childNodes.apply {
33
- val attributes = arrayOf(" height" , " width" )
34
-
35
- for (i in 1 until length) {
36
- val view = item(i)
37
- if (
38
- view.hasAttributes() &&
39
- view.attributes.getNamedItem(" android:id" ).nodeValue.endsWith(" ad_view_stub" )
40
- ) {
41
- attributes.forEach { attribute ->
42
- view.attributes.getNamedItem(" android:layout_$attribute " ).nodeValue =
43
- " 0.0dip"
44
- }
45
-
46
- break
47
- }
48
- }
49
- }
50
- }
51
- }
52
- }
53
-
54
- private const val EXTENSION_METHOD_DESCRIPTOR =
55
- " $PATCHES_PATH /GeneralAdsPatch;->hideCommentAds()Z"
56
-
57
- private val commentAdsPatch = bytecodePatch(
58
- description = " commentAdsPatch" ,
59
- ) {
60
- execute {
61
- commentAdsFingerprint.matchOrThrow().let {
62
- val walkerMethod = it.getWalkerMethod(it.patternMatch!! .startIndex)
63
- walkerMethod.apply {
64
- addInstructionsWithLabels(
65
- 0 , """
66
- invoke-static {}, $EXTENSION_METHOD_DESCRIPTOR
67
- move-result v0
68
- if-eqz v0, :show
69
- new-instance v0, Ljava/lang/Object;
70
- invoke-direct {v0}, Ljava/lang/Object;-><init>()V
71
- return-object v0
72
- """ , ExternalLabel (" show" , getInstruction(0 ))
73
- )
74
- }
75
- }
76
- }
77
- }
78
-
79
25
private const val EXTENSION_CLASS_DESCRIPTOR =
80
26
" $PATCHES_PATH /GeneralAdsPatch;"
81
27
28
+ private val isCommentAdsMethod: Method .() -> Boolean = {
29
+ parameterTypes.size == 1 &&
30
+ parameterTypes.first().startsWith(" Lcom/reddit/ads/conversation/" ) &&
31
+ accessFlags == AccessFlags .PUBLIC or AccessFlags .FINAL &&
32
+ returnType == " V" &&
33
+ indexOfFirstStringInstruction(" ad" ) >= 0
34
+ }
35
+
82
36
@Suppress(" unused" )
83
37
val adsPatch = bytecodePatch(
84
38
HIDE_ADS .title,
85
39
HIDE_ADS .summary,
86
40
) {
87
41
compatibleWith(COMPATIBLE_PACKAGE )
88
42
89
- dependsOn(
90
- settingsPatch,
91
- bannerAdsPatch,
92
- commentAdsPatch,
93
- )
43
+ dependsOn(settingsPatch)
94
44
95
45
execute {
96
46
// region Filter promoted ads (does not work in popular or latest feed)
@@ -127,6 +77,27 @@ val adsPatch = bytecodePatch(
127
77
)
128
78
}
129
79
80
+ // region Filter comment ads
81
+ classes.forEach { classDef ->
82
+ classDef.methods.forEach { method ->
83
+ if (method.isCommentAdsMethod()) {
84
+ proxy(classDef)
85
+ .mutableClass
86
+ .findMutableMethodOf(method)
87
+ .addInstructionsWithLabels(
88
+ 0 , """
89
+ invoke-static {}, $EXTENSION_CLASS_DESCRIPTOR ->hideCommentAds()Z
90
+ move-result v0
91
+ if-eqz v0, :show
92
+ return-void
93
+ :show
94
+ nop
95
+ """
96
+ )
97
+ }
98
+ }
99
+ }
100
+
130
101
updatePatchStatus(
131
102
" enableGeneralAds" ,
132
103
HIDE_ADS
0 commit comments