From a438a47927fa8d273040f3bdff7d145c1d7ffaaa Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Mon, 21 Nov 2022 00:04:46 +0100 Subject: [PATCH] fix(youtube/general-ads): hide bytecode home ad view --- .../bytecode/patch/GeneralAdsBytecodePatch.kt | 19 ----- .../general/bytecode/patch/GeneralAdsPatch.kt | 80 +++++++++++++++++++ ...AdsPatch.kt => GeneralAdsResourcePatch.kt} | 22 ++--- 3 files changed, 93 insertions(+), 28 deletions(-) delete mode 100644 src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/patch/GeneralAdsBytecodePatch.kt create mode 100644 src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/patch/GeneralAdsPatch.kt rename src/main/kotlin/app/revanced/patches/youtube/ad/general/resource/patch/{GeneralAdsPatch.kt => GeneralAdsResourcePatch.kt} (96%) diff --git a/src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/patch/GeneralAdsBytecodePatch.kt b/src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/patch/GeneralAdsBytecodePatch.kt deleted file mode 100644 index ae3d70aa4..000000000 --- a/src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/patch/GeneralAdsBytecodePatch.kt +++ /dev/null @@ -1,19 +0,0 @@ -package app.revanced.patches.youtube.ad.general.bytecode.patch - -import app.revanced.patcher.annotation.Version -import app.revanced.patcher.data.BytecodeContext -import app.revanced.patcher.patch.BytecodePatch -import app.revanced.patcher.patch.PatchResult -import app.revanced.patcher.patch.annotations.DependsOn -import app.revanced.patches.youtube.ad.general.annotation.GeneralAdsCompatibility -import app.revanced.patches.youtube.misc.settings.framework.components.impl.* - -@DependsOn() -@GeneralAdsCompatibility -@Version("0.0.1") -class GeneralAdsBytecodePatch : BytecodePatch() { - override fun execute(context: BytecodeContext): PatchResult { - TODO("Not yet implemented") - } - -} diff --git a/src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/patch/GeneralAdsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/patch/GeneralAdsPatch.kt new file mode 100644 index 000000000..4ac34569b --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/patch/GeneralAdsPatch.kt @@ -0,0 +1,80 @@ +package app.revanced.patches.youtube.ad.general.bytecode.patch + +import app.revanced.patcher.annotation.Description +import app.revanced.patcher.annotation.Name +import app.revanced.patcher.annotation.Version +import app.revanced.patcher.data.BytecodeContext +import app.revanced.patcher.extensions.addInstruction +import app.revanced.patcher.extensions.softCompareTo +import app.revanced.patcher.patch.BytecodePatch +import app.revanced.patcher.patch.PatchResult +import app.revanced.patcher.patch.PatchResultSuccess +import app.revanced.patcher.patch.annotations.DependsOn +import app.revanced.patcher.patch.annotations.Patch +import app.revanced.patcher.util.proxy.mutableTypes.MutableClass +import app.revanced.patches.youtube.ad.general.annotation.GeneralAdsCompatibility +import app.revanced.patches.youtube.ad.general.resource.patch.GeneralAdsResourcePatch +import app.revanced.patches.youtube.misc.settings.framework.components.impl.* +import org.jf.dexlib2.iface.Method +import org.jf.dexlib2.iface.instruction.formats.Instruction31i +import org.jf.dexlib2.iface.instruction.formats.Instruction35c +import org.jf.dexlib2.immutable.reference.ImmutableMethodReference +import java.util.* + + +@Patch +@DependsOn([GeneralAdsResourcePatch::class]) +@Name("general-ads") +@Description("Removes general ads.") +@GeneralAdsCompatibility +@Version("0.0.1") +class GeneralAdsPatch : BytecodePatch() { + internal companion object { + private fun MutableClass.findMutableMethodOf( + method: Method + ) = this.methods.first { + it.softCompareTo( + ImmutableMethodReference( + method.definingClass, method.name, method.parameters, method.returnType + ) + ) + } + } + + override fun execute(context: BytecodeContext): PatchResult { + context.classes.forEach { classDef -> + classDef.methods.forEach { method -> + with(method.implementation) { + this?.instructions?.forEachIndexed { index, instruction -> + if (instruction.opcode != org.jf.dexlib2.Opcode.CONST) + return@forEachIndexed + // Instruction to store the id adAttribution into a register + if ((instruction as Instruction31i).wideLiteral != app.revanced.patches.youtube.ad.general.resource.patch.GeneralAdsResourcePatch.adAttributionId) + return@forEachIndexed + + val insertIndex = index + 1 + + // Call to get the view with the id adAttribution + with(instructions.elementAt(insertIndex)) { + if (opcode != org.jf.dexlib2.Opcode.INVOKE_VIRTUAL) + return@forEachIndexed + + // Hide the view + val viewRegister = (this as Instruction35c).registerC + context.proxy(classDef).mutableClass.findMutableMethodOf(method).addInstruction( + insertIndex, + "invoke-static { v$viewRegister }, " + + "Lapp/revanced/integrations/patches/GeneralAdsPatch;" + + "->" + + "hideAdAttributionView(Landroid/view/View;)V" + ) + } + } + } + } + } + + return PatchResultSuccess() + } + +} diff --git a/src/main/kotlin/app/revanced/patches/youtube/ad/general/resource/patch/GeneralAdsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/ad/general/resource/patch/GeneralAdsResourcePatch.kt similarity index 96% rename from src/main/kotlin/app/revanced/patches/youtube/ad/general/resource/patch/GeneralAdsPatch.kt rename to src/main/kotlin/app/revanced/patches/youtube/ad/general/resource/patch/GeneralAdsResourcePatch.kt index 417c76385..01a1c4988 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/ad/general/resource/patch/GeneralAdsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/ad/general/resource/patch/GeneralAdsResourcePatch.kt @@ -1,14 +1,11 @@ -package app.revanced.patches.youtube.ad.general.patch +package app.revanced.patches.youtube.ad.general.resource.patch -import app.revanced.patcher.annotation.Description -import app.revanced.patcher.annotation.Name import app.revanced.patcher.annotation.Version import app.revanced.patcher.data.ResourceContext import app.revanced.patcher.patch.PatchResult import app.revanced.patcher.patch.PatchResultSuccess import app.revanced.patcher.patch.ResourcePatch import app.revanced.patcher.patch.annotations.DependsOn -import app.revanced.patcher.patch.annotations.Patch import app.revanced.patches.youtube.ad.general.annotation.GeneralAdsCompatibility import app.revanced.patches.youtube.misc.litho.filter.patch.LithoFilterPatch import app.revanced.patches.youtube.misc.manifest.patch.FixLocaleConfigErrorPatch @@ -17,13 +14,19 @@ import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch.PreferenceScreen import app.revanced.patches.youtube.misc.settings.framework.components.impl.* -@Patch -@DependsOn(dependencies = [FixLocaleConfigErrorPatch::class, LithoFilterPatch::class, SettingsPatch::class, ResourceMappingPatch::class]) -@Name("general-ads") -@Description("Removes general ads.") +@DependsOn(dependencies = [ + FixLocaleConfigErrorPatch::class, + LithoFilterPatch::class, + SettingsPatch::class, + ResourceMappingPatch::class +]) @GeneralAdsCompatibility @Version("0.0.1") -class GeneralAdsPatch : ResourcePatch { +class GeneralAdsResourcePatch : ResourcePatch { + internal companion object { + var adAttributionId: Long = -1 + } + override fun execute(context: ResourceContext): PatchResult { PreferenceScreen.ADS.addPreferences( SwitchPreference( @@ -222,6 +225,7 @@ class GeneralAdsPatch : ResourcePatch { ) ) + adAttributionId = ResourceMappingPatch.resourceMappings.single { it.name == "ad_attribution" }.id return PatchResultSuccess() } }