From 263a3c5c8c4faa2a09fc799631422a96d02eddd8 Mon Sep 17 00:00:00 2001 From: inotia00 Date: Thu, 27 Jul 2023 00:53:32 +0900 Subject: [PATCH] feat(reddit/hide-ads): implementing with settings --- .../ad/comments/patch/HideCommentAdsPatch.kt | 18 +++++++--- .../reddit/ad/general/patch/HideAdsPatch.kt | 36 +++++++++++++------ 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/app/revanced/patches/reddit/ad/comments/patch/HideCommentAdsPatch.kt b/src/main/kotlin/app/revanced/patches/reddit/ad/comments/patch/HideCommentAdsPatch.kt index 988ca35f7..e39012d39 100644 --- a/src/main/kotlin/app/revanced/patches/reddit/ad/comments/patch/HideCommentAdsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/reddit/ad/comments/patch/HideCommentAdsPatch.kt @@ -3,11 +3,13 @@ package app.revanced.patches.reddit.ad.comments.patch import app.revanced.extensions.toErrorResult import app.revanced.patcher.data.BytecodeContext import app.revanced.patcher.data.toMethodWalker -import app.revanced.patcher.extensions.InstructionExtensions.addInstructions +import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels +import app.revanced.patcher.extensions.InstructionExtensions.getInstruction import app.revanced.patcher.patch.BytecodePatch import app.revanced.patcher.patch.PatchResult import app.revanced.patcher.patch.PatchResultSuccess import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod +import app.revanced.patcher.util.smali.ExternalLabel import app.revanced.patches.reddit.ad.comments.fingerprints.HideCommentAdsFingerprint class HideCommentAdsPatch : BytecodePatch( @@ -21,17 +23,25 @@ class HideCommentAdsPatch : BytecodePatch( .nextMethod(it.scanResult.patternScanResult!!.startIndex, true) .getMethod() as MutableMethod ) { - - addInstructions( + addInstructionsWithLabels( 0, """ + invoke-static {}, $INTEGRATION_METHOD_DESCRIPTOR + move-result v0 + if-eqz v0, :show new-instance v0, Ljava/lang/Object; invoke-direct {v0}, Ljava/lang/Object;->()V return-object v0 - """ + """, ExternalLabel("show", getInstruction(0)) ) } } ?: return HideCommentAdsFingerprint.toErrorResult() return PatchResultSuccess() } + + private companion object { + private const val INTEGRATION_METHOD_DESCRIPTOR = + "Lapp/revanced/reddit/patches/GeneralAdsPatch;" + + "->hideCommentAds()Z" + } } diff --git a/src/main/kotlin/app/revanced/patches/reddit/ad/general/patch/HideAdsPatch.kt b/src/main/kotlin/app/revanced/patches/reddit/ad/general/patch/HideAdsPatch.kt index a343236bc..88d05d710 100644 --- a/src/main/kotlin/app/revanced/patches/reddit/ad/general/patch/HideAdsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/reddit/ad/general/patch/HideAdsPatch.kt @@ -6,8 +6,8 @@ import app.revanced.patcher.annotation.Name import app.revanced.patcher.annotation.Version import app.revanced.patcher.data.BytecodeContext import app.revanced.patcher.extensions.InstructionExtensions.addInstructions +import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels import app.revanced.patcher.extensions.InstructionExtensions.getInstruction -import app.revanced.patcher.extensions.InstructionExtensions.removeInstruction import app.revanced.patcher.patch.BytecodePatch import app.revanced.patcher.patch.PatchResult import app.revanced.patcher.patch.PatchResultError @@ -15,11 +15,14 @@ import app.revanced.patcher.patch.PatchResultSuccess import app.revanced.patcher.patch.annotations.DependsOn import app.revanced.patcher.patch.annotations.Patch import app.revanced.patcher.patch.annotations.RequiresIntegrations +import app.revanced.patcher.util.smali.ExternalLabel import app.revanced.patches.reddit.ad.banner.patch.HideBannerPatch import app.revanced.patches.reddit.ad.comments.patch.HideCommentAdsPatch import app.revanced.patches.reddit.ad.general.fingerprints.AdPostFingerprint import app.revanced.patches.reddit.ad.general.fingerprints.NewAdPostFingerprint import app.revanced.patches.reddit.utils.annotations.RedditCompatibility +import app.revanced.patches.reddit.utils.settings.bytecode.patch.SettingsBytecodePatch.Companion.updateSettingsStatus +import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction import com.android.tools.smali.dexlib2.iface.instruction.formats.Instruction22c import com.android.tools.smali.dexlib2.iface.reference.FieldReference @@ -54,14 +57,12 @@ class HideAdsPatch : BytecodePatch( if (targetReferenceName != "children") throw PatchResultError("Method signature reference name did not match: $targetReferenceName") - val castedInstruction = getInstruction(targetIndex) + val targetRegister = getInstruction(targetIndex).registerA - removeInstruction(targetIndex) addInstructions( targetIndex, """ - invoke-static {v${castedInstruction.registerA}}, $FILTER_METHOD_DESCRIPTOR - move-result-object v0 - iput-object v0, v${castedInstruction.registerB}, ${castedInstruction.reference} + invoke-static {v$targetRegister}, $INTEGRATIONS_OLD_METHOD_DESCRIPTOR + move-result-object v$targetRegister """ ) } @@ -79,16 +80,31 @@ class HideAdsPatch : BytecodePatch( if (!targetParameter.endsWith("Ljava/util/ArrayList;->add(Ljava/lang/Object;)Z")) throw PatchResultError("Method signature parameter did not match: $targetParameter") - removeInstruction(targetIndex) + val targetRegister = + getInstruction(targetIndex).registerD + 1 + + addInstructionsWithLabels( + targetIndex, """ + invoke-static {}, $INTEGRATIONS_NEW_METHOD_DESCRIPTOR + move-result v$targetRegister + if-nez v$targetRegister, :show + """, ExternalLabel("show", getInstruction(targetIndex + 1)) + ) } } ?: return NewAdPostFingerprint.toErrorResult() + updateSettingsStatus("GeneralAds") + return PatchResultSuccess() } private companion object { - private const val FILTER_METHOD_DESCRIPTOR = - "Lapp/revanced/reddit/patches/FilterPromotedLinksPatch;" + - "->filterChildren(Ljava/lang/Iterable;)Ljava/util/List;" + private const val INTEGRATIONS_OLD_METHOD_DESCRIPTOR = + "Lapp/revanced/reddit/patches/GeneralAdsPatch;" + + "->hideOldPostAds(Ljava/util/List;)Ljava/util/List;" + + private const val INTEGRATIONS_NEW_METHOD_DESCRIPTOR = + "Lapp/revanced/reddit/patches/GeneralAdsPatch;" + + "->hideNewPostAds()Z" } }