refactor(primevideo): SkipAdsPatch

This commit is contained in:
Pun Butrach 2026-01-12 17:10:00 +07:00
parent ad783451ed
commit 198cb93ef2
2 changed files with 42 additions and 30 deletions

View file

@ -1,33 +1,39 @@
package app.revanced.patches.primevideo.ads
import app.revanced.patcher.fingerprint
import app.revanced.patcher.BytecodePatchContextMethodMatching.gettingFirstMutableMethodDeclaratively
import app.revanced.patcher.accessFlags
import app.revanced.patcher.definingClass
import app.revanced.patcher.instructions
import app.revanced.patcher.invoke
import app.revanced.patcher.name
import app.revanced.patcher.parameterTypes
import app.revanced.patcher.patch.BytecodePatchContext
import app.revanced.patcher.returnType
import com.android.tools.smali.dexlib2.AccessFlags
import com.android.tools.smali.dexlib2.Opcode
internal val enterServerInsertedAdBreakStateFingerprint = fingerprint {
internal val BytecodePatchContext.enterServerInsertedAdBreakStateMethod by gettingFirstMutableMethodDeclaratively {
accessFlags(AccessFlags.PUBLIC)
parameters("Lcom/amazon/avod/fsm/Trigger;")
returns("V")
opcodes(
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT_OBJECT,
Opcode.CONST_4,
Opcode.CONST_4
parameterTypes("Lcom/amazon/avod/fsm/Trigger;")
returnType("V")
instructions(
Opcode.INVOKE_VIRTUAL(),
Opcode.MOVE_RESULT_OBJECT(),
Opcode.CONST_4(),
Opcode.CONST_4()
)
custom { method, classDef ->
method.name == "enter" && classDef.type == "Lcom/amazon/avod/media/ads/internal/state/ServerInsertedAdBreakState;"
}
name("enter")
definingClass("Lcom/amazon/avod/media/ads/internal/state/ServerInsertedAdBreakState;")
}
internal val doTriggerFingerprint = fingerprint {
internal val BytecodePatchContext.doTriggerMethod by gettingFirstMutableMethodDeclaratively {
accessFlags(AccessFlags.PROTECTED)
returns("V")
opcodes(
Opcode.IGET_OBJECT,
Opcode.INVOKE_INTERFACE,
Opcode.RETURN_VOID
returnType("V")
instructions(
Opcode.IGET_OBJECT(),
Opcode.INVOKE_INTERFACE(),
Opcode.RETURN_VOID()
)
custom { method, classDef ->
method.name == "doTrigger" && classDef.type == "Lcom/amazon/avod/fsm/StateBase;"
}
}
name("doTrigger")
definingClass("Lcom/amazon/avod/fsm/StateBase;")
}

View file

@ -2,14 +2,17 @@ package app.revanced.patches.primevideo.ads
import app.revanced.patcher.extensions.addInstructions
import app.revanced.patcher.extensions.getInstruction
import app.revanced.patcher.patch.bytecodePatch
import app.revanced.patcher.patch.creatingBytecodePatch
import app.revanced.patches.primevideo.misc.extension.sharedExtensionPatch
import app.revanced.util.getReference
import app.revanced.util.indexOfFirstInstructionOrThrow
import com.android.tools.smali.dexlib2.AccessFlags
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
import com.android.tools.smali.dexlib2.iface.reference.MethodReference
@Suppress("unused")
val skipAdsPatch = bytecodePatch(
name = "Skip ads",
@Suppress("unused", "ObjectPropertyName")
val `Skip ads` by creatingBytecodePatch(
description = "Automatically skips video stream ads.",
) {
compatibleWith("com.amazon.avod.thirdpartyclient"("3.0.412.2947"))
@ -20,13 +23,17 @@ val skipAdsPatch = bytecodePatch(
// ad break. Instead, force the video player to seek over the entire break and reset the state machine.
apply {
// Force doTrigger() access to public so we can call it from our extension.
doTriggerFingerprint.method.accessFlags = AccessFlags.PUBLIC.value;
doTriggerMethod.accessFlags = AccessFlags.PUBLIC.value
val getPlayerIndex = enterServerInsertedAdBreakStateFingerprint.patternMatch.startIndex
enterServerInsertedAdBreakStateFingerprint.method.apply {
enterServerInsertedAdBreakStateMethod.apply {
// Get register that stores VideoPlayer:
// invoke-virtual ->getPrimaryPlayer()
// move-result-object { playerRegister }
val getPlayerIndex = indexOfFirstInstructionOrThrow {
opcode == Opcode.INVOKE_VIRTUAL &&
getReference<MethodReference>()?.name == "getPrimaryPlayer"
}
val playerRegister = getInstruction<OneRegisterInstruction>(getPlayerIndex + 1).registerA
// Reuse the params from the original method:
@ -42,4 +49,3 @@ val skipAdsPatch = bytecodePatch(
}
}
}