Skip to content

Instantly share code, notes, and snippets.

@Sculas
Last active August 7, 2022 21:07
Show Gist options
  • Save Sculas/a52cee8389f300f22b102a04ba1150a8 to your computer and use it in GitHub Desktop.
Save Sculas/a52cee8389f300f22b102a04ba1150a8 to your computer and use it in GitHub Desktop.
ReVanced DSL Refactor
object VideoAdsPatch : PatchTest<BytecodeData> {
override val metadata = metadata {
name = "video-ads"
friendlyName = "Disable Video Ads"
description = "Removes ads in the video player."
version = "0.0.1"
compatibility = SharedCompatibility.`video-ads`
dependsOn(IntegrationsPatch::class, SettingsPatch::class)
dependsOn(ShowVideoAdsConstructorFingerprint)
options {
stringOption {
key = "a"
default = "b"
title = "c"
description = "d"
required = true
validator {
true // always valid
}
}
stringListOption {
key = "a"
default = "b"
title = "c"
description = "d"
required = true
validator {
true // always valid
}
options("a", "b", "c")
}
}
preferences(PreferenceScreen.ADS) {
addPreferences( // TODO: make this DSL too?
SwitchPreference(
"revanced_video_ads_enabled",
StringResource("revanced_video_ads_enabled_title", "Hide video ads"),
true,
StringResource("revanced_video_ads_enabled_summary_on", "Video ads are hidden."),
StringResource("revanced_video_ads_enabled_summary_off", "Video ads are shown.")
)
)
}
}
override fun execute(data: BytecodeData) {
ShowVideoAdsFingerprint.resolve(data, ShowVideoAdsConstructorFingerprint.result!!.classDef)
ShowVideoAdsFingerprint.result!!.mutableMethod.addInstructions(
0, """
invoke-static { }, Lapp/revanced/integrations/patches/VideoAdsPatch;->shouldShowAds()Z
move-result v1
"""
)
}
// override fun finish() {
// // ...
// }
}
// -> somewhere in SettingsFramework.kt
// other patches can add extensions to the PatchScope,
// making it very easy to interact with other patches/dependencies.
internal fun PatchMetadata.preferences(screen: PreferenceScreen, block: PreferenceScreen.() -> Unit) {
// ...
}
@Patch
@DependsOn([IntegrationsPatch::class, SettingsPatch::class])
@Name("video-ads")
@Description("Removes ads in the video player.")
@VideoAdsCompatibility
@Version("0.0.1")
class VideoAdsPatch : BytecodePatch(
listOf(
ShowVideoAdsConstructorFingerprint
)
) {
override fun execute(data: BytecodeData): PatchResult {
SettingsPatch.PreferenceScreen.ADS.addPreferences(
SwitchPreference(
"revanced_video_ads_enabled",
StringResource("revanced_video_ads_enabled_title", "Hide video ads"),
true,
StringResource("revanced_video_ads_enabled_summary_on", "Video ads are hidden."),
StringResource("revanced_video_ads_enabled_summary_off", "Video ads are shown.")
)
)
ShowVideoAdsFingerprint.resolve(
data, ShowVideoAdsConstructorFingerprint.result!!.classDef
)
ShowVideoAdsFingerprint.result!!.mutableMethod.addInstructions(
0, """
invoke-static { }, Lapp/revanced/integrations/patches/VideoAdsPatch;->shouldShowAds()Z
move-result v1
"""
)
return PatchResultSuccess()
}
override val options = PatchOptions(
PatchOption.StringOption(
key = "a",
default = "b",
title = "c",
description = "d",
required = true,
validator = { true }
),
PatchOption.StringListOption(
key = "a",
default = "b",
title = "c",
options = listOf("a", "b", "c")
description = "d",
required = true,
validator = { true }
)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment