diff --git a/src/main/kotlin/app/revanced/patches/music/ads/video/patch/MusicVideoAdsPatch.kt b/src/main/kotlin/app/revanced/patches/music/ads/video/patch/MusicVideoAdsPatch.kt index 58f0f8d6b..b5266297d 100644 --- a/src/main/kotlin/app/revanced/patches/music/ads/video/patch/MusicVideoAdsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/ads/video/patch/MusicVideoAdsPatch.kt @@ -4,15 +4,14 @@ 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.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.patches.music.misc.litho.patch.MusicLithoFilterPatch +import app.revanced.patches.music.utils.litho.patch.MusicLithoFilterPatch import app.revanced.patches.music.utils.settings.resource.patch.MusicSettingsPatch import app.revanced.patches.shared.annotation.YouTubeMusicCompatibility -import app.revanced.patches.shared.patch.videoads.GeneralVideoAdsPatch +import app.revanced.patches.shared.patch.videoads.AbstractVideoAdsPatch import app.revanced.util.enum.CategoryType import app.revanced.util.integrations.Constants.MUSIC_ADS_PATH @@ -21,25 +20,20 @@ import app.revanced.util.integrations.Constants.MUSIC_ADS_PATH @Description("Removes ads in the music player.") @DependsOn( [ - GeneralVideoAdsPatch::class, MusicLithoFilterPatch::class, MusicSettingsPatch::class ] ) @YouTubeMusicCompatibility @Version("0.0.1") -class MusicVideoAdsPatch : BytecodePatch() { +class MusicVideoAdsPatch : AbstractVideoAdsPatch( + "$MUSIC_ADS_PATH/HideMusicAdsPatch;->hideMusicAds()Z" +) { override fun execute(context: BytecodeContext): PatchResult { - - GeneralVideoAdsPatch.injectLegacyAds(INTEGRATIONS_CLASS_DESCRIPTOR) - - GeneralVideoAdsPatch.injectMainstreamAds(INTEGRATIONS_CLASS_DESCRIPTOR) + super.execute(context) MusicSettingsPatch.addMusicPreference(CategoryType.ADS, "revanced_hide_music_ads", "true") return PatchResultSuccess() } - private companion object { - const val INTEGRATIONS_CLASS_DESCRIPTOR = "$MUSIC_ADS_PATH/HideMusicAdsPatch;->hideMusicAds()Z" - } } diff --git a/src/main/kotlin/app/revanced/patches/shared/patch/videoads/AbstractVideoAdsPatch.kt b/src/main/kotlin/app/revanced/patches/shared/patch/videoads/AbstractVideoAdsPatch.kt new file mode 100644 index 000000000..9ff2e00d5 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/shared/patch/videoads/AbstractVideoAdsPatch.kt @@ -0,0 +1,58 @@ +package app.revanced.patches.shared.patch.videoads + +import app.revanced.extensions.toErrorResult +import app.revanced.patcher.annotation.Name +import app.revanced.patcher.annotation.Version +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.shared.fingerprints.LegacyVideoAdsFingerprint +import app.revanced.patches.shared.fingerprints.MainstreamVideoAdsFingerprint + +@Name("abstract-video-ads-patch") +@Version("0.0.1") +abstract class AbstractVideoAdsPatch( + private val descriptor: String +) : BytecodePatch( + listOf( + LegacyVideoAdsFingerprint, + MainstreamVideoAdsFingerprint + ) +) { + override fun execute(context: BytecodeContext): PatchResult { + LegacyVideoAdsFingerprint.result?.let { + (context.toMethodWalker(it.method) + .nextMethod(13, true) + .getMethod() as MutableMethod).apply { + addInstructions( + 0, """ + invoke-static {}, $descriptor + move-result v1 + """ + ) + } + } ?: return LegacyVideoAdsFingerprint.toErrorResult() + + MainstreamVideoAdsFingerprint.result?.let { + it.mutableMethod.apply { + addInstructionsWithLabels( + 0, """ + invoke-static {}, $descriptor + move-result v0 + if-nez v0, :show_video_ads + return-void + """, ExternalLabel("show_video_ads", getInstruction(0)) + ) + } + } + + return PatchResultSuccess() + } +} \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/shared/patch/videoads/GeneralVideoAdsPatch.kt b/src/main/kotlin/app/revanced/patches/shared/patch/videoads/GeneralVideoAdsPatch.kt deleted file mode 100644 index 8c01abdca..000000000 --- a/src/main/kotlin/app/revanced/patches/shared/patch/videoads/GeneralVideoAdsPatch.kt +++ /dev/null @@ -1,70 +0,0 @@ -package app.revanced.patches.shared.patch.videoads - -import app.revanced.extensions.toErrorResult -import app.revanced.patcher.annotation.Name -import app.revanced.patcher.annotation.Version -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.shared.fingerprints.LegacyVideoAdsFingerprint -import app.revanced.patches.shared.fingerprints.MainstreamVideoAdsFingerprint - -@Name("general-video-ads-patch") -@Version("0.0.1") -class GeneralVideoAdsPatch : BytecodePatch( - listOf( - LegacyVideoAdsFingerprint, - MainstreamVideoAdsFingerprint, - ) -) { - override fun execute(context: BytecodeContext): PatchResult { - val legacyVideoAdsResult = LegacyVideoAdsFingerprint.result ?: return LegacyVideoAdsFingerprint.toErrorResult() - val mainstreamVideoAdsResult = MainstreamVideoAdsFingerprint.result ?: return MainstreamVideoAdsFingerprint.toErrorResult() - - legacyVideoAdsMethod = - context.toMethodWalker(legacyVideoAdsResult.method) - .nextMethod(13, true) - .getMethod() as MutableMethod - - mainstreamVideoAdsMethod = mainstreamVideoAdsResult.mutableMethod - - return PatchResultSuccess() - } - - internal companion object { - lateinit var legacyVideoAdsMethod: MutableMethod - lateinit var mainstreamVideoAdsMethod: MutableMethod - - fun injectLegacyAds( - descriptor: String - ) { - legacyVideoAdsMethod.addInstructions( - 0, """ - invoke-static {}, $descriptor - move-result v1 - """ - ) - } - - fun injectMainstreamAds( - descriptor: String - ) { - mainstreamVideoAdsMethod.addInstructionsWithLabels( - 0, """ - invoke-static {}, $descriptor - move-result v0 - if-nez v0, :show_video_ads - return-void - """, ExternalLabel("show_video_ads", mainstreamVideoAdsMethod.getInstruction(0)) - ) - } - - } -} \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/youtube/ads/video/patch/VideoAdsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/ads/video/patch/VideoAdsPatch.kt index e5a793026..0511cdbad 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/ads/video/patch/VideoAdsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/ads/video/patch/VideoAdsPatch.kt @@ -4,37 +4,30 @@ 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.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.patches.shared.annotation.YouTubeCompatibility -import app.revanced.patches.shared.patch.videoads.GeneralVideoAdsPatch +import app.revanced.patches.shared.patch.videoads.AbstractVideoAdsPatch import app.revanced.patches.youtube.utils.settings.resource.patch.SettingsPatch import app.revanced.util.integrations.Constants.ADS_PATH @Patch @Name("hide-video-ads") @Description("Removes ads in the video player.") -@DependsOn( - [ - GeneralVideoAdsPatch::class, - SettingsPatch::class - ] -) +@DependsOn([SettingsPatch::class]) @YouTubeCompatibility @Version("0.0.1") -class VideoAdsPatch : BytecodePatch() { +class VideoAdsPatch : AbstractVideoAdsPatch( + "$ADS_PATH/HideVideoAdsPatch;->hideVideoAds()Z" +) { override fun execute(context: BytecodeContext): PatchResult { - - GeneralVideoAdsPatch.injectLegacyAds(INTEGRATIONS_CLASS_DESCRIPTOR) - GeneralVideoAdsPatch.injectMainstreamAds(INTEGRATIONS_CLASS_DESCRIPTOR) + super.execute(context) /** * Add settings */ - SettingsPatch.addPreference( arrayOf( "PREFERENCE: ADS_SETTINGS", @@ -46,8 +39,4 @@ class VideoAdsPatch : BytecodePatch() { return PatchResultSuccess() } - - private companion object { - const val INTEGRATIONS_CLASS_DESCRIPTOR = "$ADS_PATH/HideVideoAdsPatch;->hideVideoAds()Z" - } }