Skip to content

Commit

Permalink
fix(YouTube - SponsorBlock): Improve create segment manual seek accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
inotia00 authored and Francesco146 committed Aug 7, 2024
1 parent 9417f19 commit f0cefa8
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import app.revanced.patcher.util.proxy.mutableTypes.MutableClass
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod.Companion.toMutable
import app.revanced.patcher.util.smali.toInstructions
import app.revanced.patches.youtube.utils.fingerprints.OrganicPlaybackContextModelFingerprint
import app.revanced.patches.youtube.utils.fingerprints.VideoEndFingerprint
import app.revanced.patches.youtube.utils.integrations.Constants.SHARED_PATH
import app.revanced.patches.youtube.utils.playertype.PlayerTypeHookPatch
Expand All @@ -26,6 +25,7 @@ import app.revanced.patches.youtube.video.information.fingerprints.OnPlaybackSpe
import app.revanced.patches.youtube.video.information.fingerprints.PlaybackInitializationFingerprint
import app.revanced.patches.youtube.video.information.fingerprints.PlaybackSpeedClassFingerprint
import app.revanced.patches.youtube.video.information.fingerprints.PlayerControllerSetTimeReferenceFingerprint
import app.revanced.patches.youtube.video.information.fingerprints.SeekRelativeFingerprint
import app.revanced.patches.youtube.video.information.fingerprints.VideoIdFingerprint
import app.revanced.patches.youtube.video.information.fingerprints.VideoIdFingerprintBackgroundPlay
import app.revanced.patches.youtube.video.information.fingerprints.VideoIdFingerprintShorts
Expand All @@ -36,6 +36,7 @@ import app.revanced.patches.youtube.video.information.fingerprints.VideoTitleFin
import app.revanced.patches.youtube.video.playerresponse.PlayerResponseMethodHookPatch
import app.revanced.patches.youtube.video.videoid.VideoIdPatch
import app.revanced.util.addFieldAndInstructions
import app.revanced.util.alsoResolve
import app.revanced.util.getReference
import app.revanced.util.getTargetIndexOrThrow
import app.revanced.util.getTargetIndexReversedOrThrow
Expand Down Expand Up @@ -71,7 +72,6 @@ object VideoInformationPatch : BytecodePatch(
ChannelNameFingerprint,
MdxPlayerDirectorSetVideoStageFingerprint,
OnPlaybackSpeedItemClickFingerprint,
OrganicPlaybackContextModelFingerprint,
PlaybackInitializationFingerprint,
PlaybackSpeedClassFingerprint,
PlayerControllerSetTimeReferenceFingerprint,
Expand Down Expand Up @@ -117,10 +117,11 @@ object VideoInformationPatch : BytecodePatch(
/**
* Used in [VideoEndFingerprint] and [MdxPlayerDirectorSetVideoStageFingerprint].
* Since both classes are inherited from the same class,
* [VideoEndFingerprint] and [MdxPlayerDirectorSetVideoStageFingerprint] always have the same [seekSourceEnumType] and [seekSourceMethodName].
* [VideoEndFingerprint] and [MdxPlayerDirectorSetVideoStageFingerprint] always have the same [seekSourceEnumType], [seekSourceMethodName] and [seekRelativeSourceMethodName].
*/
private var seekSourceEnumType = ""
private var seekSourceMethodName = ""
private var seekRelativeSourceMethodName = ""

private lateinit var videoInformationMutableClass: MutableClass
private lateinit var context: BytecodeContext
Expand All @@ -138,37 +139,28 @@ object VideoInformationPatch : BytecodePatch(
internal lateinit var speedSelectionInsertMethod: MutableMethod
internal lateinit var videoEndMethod: MutableMethod

private fun getSeekToConstructorMethod(
private fun addSeekInterfaceMethods(
result: MethodFingerprintResult,
seekMethodName: String,
methodName: String,
fieldMethodName: String,
fieldName: String
): Pair<MutableMethod, Int> {
) {
result.mutableMethod.apply {
val constructorMethod =
result.mutableClass.methods.first { method -> MethodUtil.isConstructor(method) }

val constructorInsertIndex = indexOfFirstInstructionOrThrow {
opcode == Opcode.INVOKE_DIRECT && getReference<MethodReference>()?.name == "<init>"
} + 1

if (seekSourceEnumType.isEmpty() && seekSourceMethodName.isEmpty()) {
seekSourceEnumType = parameterTypes[1].toString()
seekSourceMethodName = name
}

result.mutableClass.methods.add(
ImmutableMethod(
definingClass,
"seekTo",
fieldMethodName,
listOf(ImmutableMethodParameter("J", annotations, "time")),
"Z",
AccessFlags.PUBLIC or AccessFlags.FINAL,
annotations,
null,
ImmutableMethodImplementation(
4, """
# first enum (field a) is SEEK_SOURCE_UNKNOWN
sget-object v0, $seekSourceEnumType->a:$seekSourceEnumType
invoke-virtual {p0, p1, p2, v0}, ${definingClass}->$seekSourceMethodName(J$seekSourceEnumType)Z
invoke-virtual {p0, p1, p2, v0}, ${definingClass}->$seekMethodName(J$seekSourceEnumType)Z
move-result p1
return p1
""".toInstructions(),
Expand All @@ -181,7 +173,7 @@ object VideoInformationPatch : BytecodePatch(
val smaliInstructions =
"""
if-eqz v0, :ignore
invoke-virtual {v0, p0, p1}, $definingClass->seekTo(J)Z
invoke-virtual {v0, p0, p1}, $definingClass->$fieldMethodName(J)Z
move-result v0
return v0
:ignore
Expand All @@ -197,8 +189,6 @@ object VideoInformationPatch : BytecodePatch(
smaliInstructions,
true
)

return Pair(constructorMethod, constructorInsertIndex)
}
}

Expand All @@ -208,16 +198,37 @@ object VideoInformationPatch : BytecodePatch(
context.findClass(INTEGRATIONS_CLASS_DESCRIPTOR)!!.mutableClass

VideoEndFingerprint.resultOrThrow().let {
val (playerConstructorMethod, playerConstructorInsertIndex) =
getSeekToConstructorMethod(it, "overrideVideoTime", "videoInformationClass")
it.mutableMethod.apply {
playerConstructorMethod =
it.mutableClass.methods.first { method -> MethodUtil.isConstructor(method) }

this.playerConstructorMethod = playerConstructorMethod
this.playerConstructorInsertIndex = playerConstructorInsertIndex
playerConstructorInsertIndex = playerConstructorMethod.indexOfFirstInstructionOrThrow {
opcode == Opcode.INVOKE_DIRECT && getReference<MethodReference>()?.name == "<init>"
} + 1

// hook the player controller for use through integrations
onCreateHook(INTEGRATIONS_CLASS_DESCRIPTOR, "initialize")
// hook the player controller for use through integrations
onCreateHook(INTEGRATIONS_CLASS_DESCRIPTOR, "initialize")

seekSourceEnumType = parameterTypes[1].toString()
seekSourceMethodName = name
seekRelativeSourceMethodName = SeekRelativeFingerprint.alsoResolve(context, VideoEndFingerprint).mutableMethod.name

// Create integrations interface methods.
addSeekInterfaceMethods(
it,
seekSourceMethodName,
"overrideVideoTime",
"seekTo",
"videoInformationClass"
)
addSeekInterfaceMethods(
it,
seekRelativeSourceMethodName,
"overrideVideoTimeRelative",
"seekToRelative",
"videoInformationClass"
)

it.mutableMethod.apply {
val literalIndex = getWideLiteralInstructionIndex(45368273)
val walkerIndex =
getTargetIndexReversedOrThrow(literalIndex, Opcode.INVOKE_VIRTUAL_RANGE)
Expand All @@ -228,14 +239,33 @@ object VideoInformationPatch : BytecodePatch(
}

MdxPlayerDirectorSetVideoStageFingerprint.resultOrThrow().let {
val (mdxConstructorMethod, mdxConstructorInsertIndex) =
getSeekToConstructorMethod(it, "overrideMDXVideoTime", "videoInformationMDXClass")
it.mutableMethod.apply {
mdxConstructorMethod =
it.mutableClass.methods.first { method -> MethodUtil.isConstructor(method) }

this.mdxConstructorMethod = mdxConstructorMethod
this.mdxConstructorInsertIndex = mdxConstructorInsertIndex
mdxConstructorInsertIndex = mdxConstructorMethod.indexOfFirstInstructionOrThrow {
opcode == Opcode.INVOKE_DIRECT && getReference<MethodReference>()?.name == "<init>"
} + 1

// hook the MDX director for use through integrations
onCreateHookMdx(INTEGRATIONS_CLASS_DESCRIPTOR, "initializeMdx")

// hook the MDX director for use through integrations
onCreateHookMdx(INTEGRATIONS_CLASS_DESCRIPTOR, "initialize")
// Create integrations interface methods.
addSeekInterfaceMethods(
it,
seekSourceMethodName,
"overrideMDXVideoTime",
"seekTo",
"videoInformationMDXClass"
)
addSeekInterfaceMethods(
it,
seekRelativeSourceMethodName,
"overrideMDXVideoTimeRelative",
"seekToRelative",
"videoInformationMDXClass"
)
}
}

/**
Expand Down Expand Up @@ -495,7 +525,7 @@ object VideoInformationPatch : BytecodePatch(
internal fun onCreateHook(targetMethodClass: String, targetMethodName: String) =
playerConstructorMethod.addInstruction(
playerConstructorInsertIndex++,
"invoke-static/range { p0 .. p0 }, $targetMethodClass->$targetMethodName(Ljava/lang/Object;)V"
"invoke-static { }, $targetMethodClass->$targetMethodName()V"
)

/**
Expand All @@ -507,7 +537,7 @@ object VideoInformationPatch : BytecodePatch(
internal fun onCreateHookMdx(targetMethodClass: String, targetMethodName: String) =
mdxConstructorMethod.addInstruction(
mdxConstructorInsertIndex++,
"invoke-static/range { p0 .. p0 }, $targetMethodClass->$targetMethodName(Ljava/lang/Object;)V"
"invoke-static { }, $targetMethodClass->$targetMethodName(Ljava/lang/Object;)V"
)

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package app.revanced.patches.youtube.video.information.fingerprints

import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.MethodFingerprint
import app.revanced.patches.youtube.utils.fingerprints.VideoEndFingerprint
import com.android.tools.smali.dexlib2.AccessFlags
import com.android.tools.smali.dexlib2.Opcode

/**
* Resolves using class found in [VideoEndFingerprint].
*/
internal object SeekRelativeFingerprint : MethodFingerprint(
accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL,
returnType = "Z",
parameters = listOf("J", "L"),
opcodes = listOf(
Opcode.ADD_LONG_2ADDR,
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT,
Opcode.RETURN
)
)
12 changes: 10 additions & 2 deletions src/main/kotlin/app/revanced/util/BytecodeUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
import com.android.tools.smali.dexlib2.AccessFlags
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.Method
import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction
import com.android.tools.smali.dexlib2.iface.instruction.Instruction
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction
Expand Down Expand Up @@ -647,15 +648,22 @@ fun MutableClass.addFieldAndInstructions(
.filter { method -> method.name == "<init>" }
.forEach { mutableMethod ->
mutableMethod.apply {
val initializeIndex = getTargetIndexWithMethodReferenceName("<init>")
val initializeIndex = indexOfFirstInstructionOrThrow {
opcode == Opcode.INVOKE_DIRECT && getReference<MethodReference>()?.name == "<init>"
}
val insertIndex = if (initializeIndex == -1)
1
else
initializeIndex + 1

val initializeRegister = if (initializeIndex == -1)
"p0"
else
"v${getInstruction<FiveRegisterInstruction>(initializeIndex).registerC}"

addInstruction(
insertIndex,
"sput-object p0, $objectCall"
"sput-object $initializeRegister, $objectCall"
)
}
}
Expand Down

0 comments on commit f0cefa8

Please sign in to comment.