feat(hide-video-ads): change to abstract patch

This commit is contained in:
inotia00 2023-06-18 23:14:42 +09:00
parent ef03b3afbd
commit e83fa54c87
4 changed files with 70 additions and 99 deletions

View File

@ -4,15 +4,14 @@ import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version import app.revanced.patcher.annotation.Version
import app.revanced.patcher.data.BytecodeContext import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.PatchResult import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultSuccess import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.patch.annotations.DependsOn import app.revanced.patcher.patch.annotations.DependsOn
import app.revanced.patcher.patch.annotations.Patch 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.music.utils.settings.resource.patch.MusicSettingsPatch
import app.revanced.patches.shared.annotation.YouTubeMusicCompatibility 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.enum.CategoryType
import app.revanced.util.integrations.Constants.MUSIC_ADS_PATH 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.") @Description("Removes ads in the music player.")
@DependsOn( @DependsOn(
[ [
GeneralVideoAdsPatch::class,
MusicLithoFilterPatch::class, MusicLithoFilterPatch::class,
MusicSettingsPatch::class MusicSettingsPatch::class
] ]
) )
@YouTubeMusicCompatibility @YouTubeMusicCompatibility
@Version("0.0.1") @Version("0.0.1")
class MusicVideoAdsPatch : BytecodePatch() { class MusicVideoAdsPatch : AbstractVideoAdsPatch(
"$MUSIC_ADS_PATH/HideMusicAdsPatch;->hideMusicAds()Z"
) {
override fun execute(context: BytecodeContext): PatchResult { override fun execute(context: BytecodeContext): PatchResult {
super.execute(context)
GeneralVideoAdsPatch.injectLegacyAds(INTEGRATIONS_CLASS_DESCRIPTOR)
GeneralVideoAdsPatch.injectMainstreamAds(INTEGRATIONS_CLASS_DESCRIPTOR)
MusicSettingsPatch.addMusicPreference(CategoryType.ADS, "revanced_hide_music_ads", "true") MusicSettingsPatch.addMusicPreference(CategoryType.ADS, "revanced_hide_music_ads", "true")
return PatchResultSuccess() return PatchResultSuccess()
} }
private companion object {
const val INTEGRATIONS_CLASS_DESCRIPTOR = "$MUSIC_ADS_PATH/HideMusicAdsPatch;->hideMusicAds()Z"
}
} }

View File

@ -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()
}
}

View File

@ -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))
)
}
}
}

View File

@ -4,37 +4,30 @@ import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version import app.revanced.patcher.annotation.Version
import app.revanced.patcher.data.BytecodeContext import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.PatchResult import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultSuccess import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.patch.annotations.DependsOn import app.revanced.patcher.patch.annotations.DependsOn
import app.revanced.patcher.patch.annotations.Patch import app.revanced.patcher.patch.annotations.Patch
import app.revanced.patches.shared.annotation.YouTubeCompatibility 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.patches.youtube.utils.settings.resource.patch.SettingsPatch
import app.revanced.util.integrations.Constants.ADS_PATH import app.revanced.util.integrations.Constants.ADS_PATH
@Patch @Patch
@Name("hide-video-ads") @Name("hide-video-ads")
@Description("Removes ads in the video player.") @Description("Removes ads in the video player.")
@DependsOn( @DependsOn([SettingsPatch::class])
[
GeneralVideoAdsPatch::class,
SettingsPatch::class
]
)
@YouTubeCompatibility @YouTubeCompatibility
@Version("0.0.1") @Version("0.0.1")
class VideoAdsPatch : BytecodePatch() { class VideoAdsPatch : AbstractVideoAdsPatch(
"$ADS_PATH/HideVideoAdsPatch;->hideVideoAds()Z"
) {
override fun execute(context: BytecodeContext): PatchResult { override fun execute(context: BytecodeContext): PatchResult {
super.execute(context)
GeneralVideoAdsPatch.injectLegacyAds(INTEGRATIONS_CLASS_DESCRIPTOR)
GeneralVideoAdsPatch.injectMainstreamAds(INTEGRATIONS_CLASS_DESCRIPTOR)
/** /**
* Add settings * Add settings
*/ */
SettingsPatch.addPreference( SettingsPatch.addPreference(
arrayOf( arrayOf(
"PREFERENCE: ADS_SETTINGS", "PREFERENCE: ADS_SETTINGS",
@ -46,8 +39,4 @@ class VideoAdsPatch : BytecodePatch() {
return PatchResultSuccess() return PatchResultSuccess()
} }
private companion object {
const val INTEGRATIONS_CLASS_DESCRIPTOR = "$ADS_PATH/HideVideoAdsPatch;->hideVideoAds()Z"
}
} }