From 22479585bbf65a64cbc0a5d1b9cd53acd72915bd Mon Sep 17 00:00:00 2001 From: TheNoFace Date: Tue, 11 Oct 2022 21:36:39 +0900 Subject: [PATCH] fix(youtube/general-ads): hide ads on wide screens (#765) --- .../bytecode/patch/GeneralBytecodeAdsPatch.kt | 5 +- .../resource/patch/GeneralResourceAdsPatch.kt | 55 +++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/app/revanced/patches/youtube/ad/general/resource/patch/GeneralResourceAdsPatch.kt diff --git a/src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/patch/GeneralBytecodeAdsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/patch/GeneralBytecodeAdsPatch.kt index d3c94812..57ee1f6d 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/patch/GeneralBytecodeAdsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/patch/GeneralBytecodeAdsPatch.kt @@ -21,6 +21,7 @@ import app.revanced.patcher.util.smali.ExternalLabel import app.revanced.patches.youtube.ad.general.annotation.GeneralAdsCompatibility import app.revanced.patches.youtube.ad.general.bytecode.extensions.MethodExtensions.findMutableMethodOf import app.revanced.patches.youtube.ad.general.bytecode.extensions.MethodExtensions.toDescriptor +import app.revanced.patches.youtube.ad.general.resource.patch.GeneralResourceAdsPatch import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch import app.revanced.patches.youtube.misc.mapping.patch.ResourceMappingResourcePatch import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch @@ -38,7 +39,7 @@ import org.jf.dexlib2.iface.reference.MethodReference import org.jf.dexlib2.iface.reference.StringReference @Patch -@DependsOn([ResourceMappingResourcePatch::class, IntegrationsPatch::class, SettingsPatch::class]) +@DependsOn([ResourceMappingResourcePatch::class, IntegrationsPatch::class, SettingsPatch::class, GeneralResourceAdsPatch::class]) @Name("general-ads") @Description("Removes general ads.") @GeneralAdsCompatibility @@ -386,4 +387,4 @@ class GeneralBytecodeAdsPatch : BytecodePatch() { instruction.opcode == Opcode.CONST && (instruction as Instruction31i).narrowLiteral == lithoConstant } ?: false } -} +} \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/youtube/ad/general/resource/patch/GeneralResourceAdsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/ad/general/resource/patch/GeneralResourceAdsPatch.kt new file mode 100644 index 00000000..37b2711e --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/ad/general/resource/patch/GeneralResourceAdsPatch.kt @@ -0,0 +1,55 @@ +package app.revanced.patches.youtube.ad.general.resource.patch + +import app.revanced.extensions.doRecursively +import app.revanced.extensions.startsWithAny +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.annotations.DependsOn +import app.revanced.patcher.patch.ResourcePatch +import app.revanced.patches.youtube.ad.general.annotation.GeneralAdsCompatibility +import app.revanced.patches.youtube.misc.manifest.patch.FixLocaleConfigErrorPatch +import org.w3c.dom.Element + +@DependsOn(dependencies = [FixLocaleConfigErrorPatch::class]) +@Name("general-resource-ads") +@Description("Patch to remove general ads in resources.") +@GeneralAdsCompatibility +@Version("0.0.1") +class GeneralResourceAdsPatch : ResourcePatch { + // list of resource file names which need to be hidden + private val resourceFileNames = arrayOf( + "compact_promoted_", + "promoted_video_", + ) + + // the attributes to change the value of + private val replacements = arrayOf( + "height", + "width", + "marginTop", + ) + + override fun execute(context: ResourceContext): PatchResult { + context.forEach { + if (!it.name.startsWithAny(*resourceFileNames)) return@forEach + + // for each file in the "layouts" directory replace all necessary attributes content + context.xmlEditor[it.absolutePath].use { editor -> + editor.file.doRecursively { node -> + replacements.forEach replacement@{ replacement -> + if (node !is Element) return@replacement + + node.getAttributeNode("android:layout_$replacement")?.let { attribute -> + attribute.textContent = "1.0dip" + } + } + } + } + } + return PatchResultSuccess() + } +}