fix(Reddit - Hide ads): View all comments button hidden if comment ads are hidden https://github.com/inotia00/ReVanced_Extended/issues/2765 (Also close https://github.com/inotia00/ReVanced_Extended/issues/2678)

This commit is contained in:
inotia00 2025-02-07 18:07:50 +09:00
parent 2ec24f95ec
commit 7eb0c06001
3 changed files with 35 additions and 82 deletions

View File

@ -7,9 +7,7 @@ import android.widget.LinearLayout;
import app.revanced.extension.reddit.settings.preference.ReVancedPreferenceFragment;
/**
* @noinspection ALL
*/
@SuppressWarnings("all")
public class ActivityHook {
public static void initialize(Activity activity) {
SettingsStatus.load();

View File

@ -5,80 +5,34 @@ import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWith
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction
import app.revanced.patcher.patch.bytecodePatch
import app.revanced.patcher.patch.resourcePatch
import app.revanced.patcher.util.smali.ExternalLabel
import app.revanced.patches.reddit.utils.compatibility.Constants.COMPATIBLE_PACKAGE
import app.revanced.patches.reddit.utils.extension.Constants.PATCHES_PATH
import app.revanced.patches.reddit.utils.patch.PatchList.HIDE_ADS
import app.revanced.patches.reddit.utils.settings.settingsPatch
import app.revanced.patches.reddit.utils.settings.updatePatchStatus
import app.revanced.util.fingerprint.matchOrThrow
import app.revanced.util.findMutableMethodOf
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.indexOfFirstStringInstruction
import app.revanced.util.or
import com.android.tools.smali.dexlib2.AccessFlags
import com.android.tools.smali.dexlib2.iface.Method
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
private const val RESOURCE_FILE_PATH = "res/layout/merge_listheader_link_detail.xml"
private val bannerAdsPatch = resourcePatch(
description = "bannerAdsPatch",
) {
execute {
document(RESOURCE_FILE_PATH).use { document ->
document.getElementsByTagName("merge").item(0).childNodes.apply {
val attributes = arrayOf("height", "width")
for (i in 1 until length) {
val view = item(i)
if (
view.hasAttributes() &&
view.attributes.getNamedItem("android:id").nodeValue.endsWith("ad_view_stub")
) {
attributes.forEach { attribute ->
view.attributes.getNamedItem("android:layout_$attribute").nodeValue =
"0.0dip"
}
break
}
}
}
}
}
}
private const val EXTENSION_METHOD_DESCRIPTOR =
"$PATCHES_PATH/GeneralAdsPatch;->hideCommentAds()Z"
private val commentAdsPatch = bytecodePatch(
description = "commentAdsPatch",
) {
execute {
commentAdsFingerprint.matchOrThrow().let {
val walkerMethod = it.getWalkerMethod(it.patternMatch!!.startIndex)
walkerMethod.apply {
addInstructionsWithLabels(
0, """
invoke-static {}, $EXTENSION_METHOD_DESCRIPTOR
move-result v0
if-eqz v0, :show
new-instance v0, Ljava/lang/Object;
invoke-direct {v0}, Ljava/lang/Object;-><init>()V
return-object v0
""", ExternalLabel("show", getInstruction(0))
)
}
}
}
}
private const val EXTENSION_CLASS_DESCRIPTOR =
"$PATCHES_PATH/GeneralAdsPatch;"
private val isCommentAdsMethod: Method.() -> Boolean = {
parameterTypes.size == 1 &&
parameterTypes.first().startsWith("Lcom/reddit/ads/conversation/") &&
accessFlags == AccessFlags.PUBLIC or AccessFlags.FINAL &&
returnType == "V" &&
indexOfFirstStringInstruction("ad") >= 0
}
@Suppress("unused")
val adsPatch = bytecodePatch(
HIDE_ADS.title,
@ -86,11 +40,7 @@ val adsPatch = bytecodePatch(
) {
compatibleWith(COMPATIBLE_PACKAGE)
dependsOn(
settingsPatch,
bannerAdsPatch,
commentAdsPatch,
)
dependsOn(settingsPatch)
execute {
// region Filter promoted ads (does not work in popular or latest feed)
@ -127,6 +77,27 @@ val adsPatch = bytecodePatch(
)
}
// region Filter comment ads
classes.forEach { classDef ->
classDef.methods.forEach { method ->
if (method.isCommentAdsMethod()) {
proxy(classDef)
.mutableClass
.findMutableMethodOf(method)
.addInstructionsWithLabels(
0, """
invoke-static {}, $EXTENSION_CLASS_DESCRIPTOR->hideCommentAds()Z
move-result v0
if-eqz v0, :show
return-void
:show
nop
"""
)
}
}
}
updatePatchStatus(
"enableGeneralAds",
HIDE_ADS

View File

@ -9,22 +9,6 @@ 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(
name = "commentAdsFingerprint",
returnType = "L",
accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL,
parameters = listOf("L"),
opcodes = listOf(
Opcode.INVOKE_STATIC,
Opcode.MOVE_RESULT_OBJECT,
Opcode.RETURN_OBJECT
),
customFingerprint = { method, _ ->
method.definingClass.endsWith("/PostDetailPresenter\$loadAd\$1;") &&
method.name == "invokeSuspend"
},
)
internal val adPostFingerprint = legacyFingerprint(
name = "adPostFingerprint",
returnType = "V",