generated from ReVanced/revanced-patches-template
-
-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(twitch/block-embedded-ads):
block-embedded-ads
patch support (#…
- Loading branch information
1 parent
cd51c71
commit ae7bdfd
Showing
9 changed files
with
224 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...in/kotlin/app/revanced/patches/twitch/ad/embedded/annotations/EmbeddedAdsCompatibility.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package app.revanced.patches.twitch.ad.embedded.annotations | ||
|
||
import app.revanced.patcher.annotation.Compatibility | ||
import app.revanced.patcher.annotation.Package | ||
|
||
@Compatibility([Package("tv.twitch.android.app")]) | ||
@Target(AnnotationTarget.CLASS) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
internal annotation class EmbeddedAdsCompatibility | ||
|
9 changes: 9 additions & 0 deletions
9
...tlin/app/revanced/patches/twitch/ad/embedded/fingerprints/CreateUsherClientFingerprint.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package app.revanced.patches.twitch.ad.embedded.fingerprints | ||
|
||
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint | ||
|
||
object CreateUsherClientFingerprint : MethodFingerprint( | ||
customFingerprint = { method -> | ||
method.definingClass.endsWith("Ltv/twitch/android/network/OkHttpClientFactory;") && method.name == "buildOkHttpClient" | ||
} | ||
) |
78 changes: 78 additions & 0 deletions
78
src/main/kotlin/app/revanced/patches/twitch/ad/embedded/patch/EmbeddedAdsPatch.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package app.revanced.patches.twitch.ad.embedded.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.MethodFingerprintExtensions.name | ||
import app.revanced.patcher.extensions.addInstructions | ||
import app.revanced.patcher.patch.BytecodePatch | ||
import app.revanced.patcher.patch.PatchResult | ||
import app.revanced.patcher.patch.PatchResultError | ||
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.settings.preference.impl.ArrayResource | ||
import app.revanced.patches.shared.settings.preference.impl.ListPreference | ||
import app.revanced.patches.shared.settings.preference.impl.StringResource | ||
import app.revanced.patches.twitch.ad.embedded.annotations.EmbeddedAdsCompatibility | ||
import app.revanced.patches.twitch.ad.embedded.fingerprints.CreateUsherClientFingerprint | ||
import app.revanced.patches.twitch.ad.video.patch.VideoAdsPatch | ||
import app.revanced.patches.twitch.misc.integrations.patch.IntegrationsPatch | ||
import app.revanced.patches.twitch.misc.settings.bytecode.patch.SettingsPatch | ||
|
||
@Patch | ||
@DependsOn([VideoAdsPatch::class, IntegrationsPatch::class, SettingsPatch::class]) | ||
@Name("block-embedded-ads") | ||
@Description("Blocks embedded steam ads using services like TTV.lol or PurpleAdBlocker.") | ||
@EmbeddedAdsCompatibility | ||
@Version("0.0.1") | ||
class EmbeddedAdsPatch : BytecodePatch( | ||
listOf(CreateUsherClientFingerprint) | ||
) { | ||
override fun execute(context: BytecodeContext): PatchResult { | ||
val result = CreateUsherClientFingerprint.result ?: return PatchResultError("${CreateUsherClientFingerprint.name} not found") | ||
|
||
// Inject OkHttp3 application interceptor | ||
result.mutableMethod.addInstructions( | ||
3, | ||
""" | ||
invoke-static {}, Lapp/revanced/twitch/patches/EmbeddedAdsPatch;->createRequestInterceptor()Lapp/revanced/twitch/api/RequestInterceptor; | ||
move-result-object v2 | ||
invoke-virtual {v0, v2}, Lokhttp3/OkHttpClient${"$"}Builder;->addInterceptor(Lokhttp3/Interceptor;)Lokhttp3/OkHttpClient${"$"}Builder; | ||
""" | ||
) | ||
|
||
SettingsPatch.PreferenceScreen.ADS.SURESTREAM.addPreferences( | ||
ListPreference( | ||
"revanced_block_embedded_ads", | ||
StringResource( | ||
"revanced_block_embedded_ads", | ||
"Block embedded video ads" | ||
), | ||
ArrayResource( | ||
"revanced_hls_proxies", | ||
listOf( | ||
StringResource("revanced_proxy_disabled", "Disabled"), | ||
StringResource("revanced_proxy_ttv_lol", "TTV LOL proxy"), | ||
StringResource("revanced_proxy_purpleadblock", "PurpleAdBlock proxy"), | ||
) | ||
), | ||
ArrayResource( | ||
"revanced_hls_proxies_values", | ||
listOf( | ||
StringResource("key_revanced_proxy_disabled", "disabled"), | ||
StringResource("key_revanced_proxy_ttv_lol", "ttv-lol"), | ||
StringResource("key_revanced_proxy_purpleadblock", "purpleadblock") | ||
) | ||
), | ||
"ttv-lol" | ||
) | ||
) | ||
|
||
SettingsPatch.addString("revanced_embedded_ads_service_unavailable", "%s is unavailable. Ads may show. Try switching to another ad block service in settings.") | ||
SettingsPatch.addString("revanced_embedded_ads_service_failed", "%s server returned an error. Ads may show. Try switching to another ad block service in settings.") | ||
|
||
return PatchResultSuccess() | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/kotlin/app/revanced/patches/twitch/ad/shared/util/AbstractAdPatch.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package app.revanced.patches.twitch.ad.shared.util | ||
|
||
import app.revanced.patcher.data.BytecodeContext | ||
import app.revanced.patcher.extensions.addInstructions | ||
import app.revanced.patcher.extensions.instruction | ||
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint | ||
import app.revanced.patcher.patch.BytecodePatch | ||
import app.revanced.patcher.util.smali.ExternalLabel | ||
|
||
abstract class AbstractAdPatch( | ||
val conditionCall: String, | ||
val skipLabelName: String, | ||
internal val fingerprints: Iterable<MethodFingerprint>? = null, | ||
) : BytecodePatch(fingerprints) { | ||
|
||
protected fun createConditionInstructions(register: String = "v0") = """ | ||
invoke-static { }, $conditionCall | ||
move-result $register | ||
if-eqz $register, :$skipLabelName | ||
""" | ||
|
||
protected data class ReturnMethod(val returnType: Char = 'V', val value: String = "") | ||
|
||
protected fun BytecodeContext.blockMethods(clazz: String, vararg methodNames: String, returnMethod: ReturnMethod = ReturnMethod()): Boolean { | ||
|
||
return with(findClass(clazz)?.mutableClass) { | ||
this ?: return false | ||
|
||
this.methods.filter { methodNames.contains(it.name) }.forEach { | ||
val retIntructions = when(returnMethod.returnType) { | ||
'V' -> "return-void" | ||
'Z' -> """ | ||
const/4 v0, ${returnMethod.value} | ||
return v0 | ||
""" | ||
else -> throw NotImplementedError() | ||
} | ||
it.addInstructions( | ||
0, | ||
""" | ||
${createConditionInstructions("v0")} | ||
$retIntructions | ||
""", | ||
listOf(ExternalLabel(skipLabelName, it.instruction(0))) | ||
) | ||
} | ||
true | ||
} | ||
} | ||
|
||
} |
5 changes: 2 additions & 3 deletions
5
...deo/fingerprints/AdsManagerFingerprint.kt → ...ngerprints/GetReadyToShowAdFingerprint.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
package app.revanced.patches.twitch.ad.video.fingerprints | ||
|
||
|
||
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint | ||
|
||
object AdsManagerFingerprint : MethodFingerprint( | ||
object GetReadyToShowAdFingerprint : MethodFingerprint( | ||
customFingerprint = { method -> | ||
method.definingClass.endsWith("AdsManagerImpl;") && method.name == "playAds" | ||
method.definingClass.endsWith("/StreamDisplayAdsPresenter;") && method.name == "getReadyToShowAdOrAbort" | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters