feat(youtube): support version 18.08.37

This commit is contained in:
oSumAtrIX
2023-04-16 19:21:25 +02:00
parent 6f66ccaaac
commit f1c9793a3e
39 changed files with 357 additions and 279 deletions
@@ -6,14 +6,15 @@ import app.revanced.patcher.annotation.Package
@Compatibility(
[Package(
"com.google.android.youtube", arrayOf(
"17.49.37",
"17.49.37",
"18.03.36",
"18.03.42",
"18.04.35",
"18.04.41",
"18.05.32",
"18.05.35",
"18.05.40"
"18.05.40",
"18.08.37"
)
)]
)
@@ -107,7 +107,7 @@ class SpoofSignatureVerificationPatch : BytecodePatch(
return PatchResultSuccess()
}
companion object {
private companion object {
const val INTEGRATIONS_CLASS_DESCRIPTOR = "Lapp/revanced/integrations/patches/SpoofSignatureVerificationPatch;"
}
}
@@ -6,14 +6,15 @@ import app.revanced.patcher.annotation.Package
@Compatibility(
[Package(
"com.google.android.youtube", arrayOf(
"17.49.37",
"17.49.37",
"18.03.36",
"18.03.42",
"18.04.35",
"18.04.41",
"18.05.32",
"18.05.35",
"18.05.40"
"18.05.40",
"18.08.37"
)
)]
)
@@ -3,5 +3,5 @@ package app.revanced.patches.youtube.misc.integrations.fingerprints
import app.revanced.patches.shared.integrations.patch.AbstractIntegrationsPatch.IntegrationsFingerprint
object InitFingerprint : IntegrationsFingerprint(
strings = listOf("Application creation"),
strings = listOf("Application creation", "Application.onCreate"),
)
@@ -5,13 +5,6 @@ import org.jf.dexlib2.Opcode
object ComponentContextParserFingerprint : MethodFingerprint(
opcodes = listOf(
Opcode.INVOKE_INTERFACE,
Opcode.MOVE_RESULT_OBJECT,
Opcode.CHECK_CAST,
Opcode.INVOKE_VIRTUAL,
Opcode.GOTO,
Opcode.INVOKE_VIRTUAL,
Opcode.CONST_16,
Opcode.INVOKE_VIRTUAL,
Opcode.IPUT_OBJECT,
Opcode.NEW_INSTANCE
@@ -5,6 +5,7 @@ import org.jf.dexlib2.Opcode
object EmptyComponentBuilderFingerprint : MethodFingerprint(
opcodes = listOf(
Opcode.INVOKE_INTERFACE,
Opcode.INVOKE_STATIC_RANGE
),
)
@@ -0,0 +1,12 @@
package app.revanced.patches.youtube.misc.litho.filter.fingerprints
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.Opcode
object ReadComponentIdentifierFingerprint : MethodFingerprint(
opcodes = listOf(
Opcode.IF_NEZ,
null,
Opcode.MOVE_RESULT_OBJECT // Register stores the component identifier string
)
)
@@ -1,10 +1,12 @@
package app.revanced.patches.youtube.misc.litho.filter.patch
import app.revanced.extensions.toErrorResult
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Version
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.fingerprint.method.impl.MethodFingerprint.Companion.resolve
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.PatchResult
@@ -16,12 +18,10 @@ import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch
import app.revanced.patches.youtube.misc.litho.filter.annotation.LithoFilterCompatibility
import app.revanced.patches.youtube.misc.litho.filter.fingerprints.ComponentContextParserFingerprint
import app.revanced.patches.youtube.misc.litho.filter.fingerprints.EmptyComponentBuilderFingerprint
import app.revanced.patches.youtube.misc.litho.filter.fingerprints.ReadComponentIdentifierFingerprint
import org.jf.dexlib2.iface.instruction.Instruction
import org.jf.dexlib2.iface.instruction.OneRegisterInstruction
import org.jf.dexlib2.iface.instruction.ReferenceInstruction
import org.jf.dexlib2.iface.instruction.TwoRegisterInstruction
import org.jf.dexlib2.iface.reference.FieldReference
import org.jf.dexlib2.iface.reference.MethodReference
@DependsOn([IntegrationsPatch::class])
@Description("Hooks the method which parses the bytes into a ComponentContext to filter components.")
@@ -31,33 +31,40 @@ class LithoFilterPatch : BytecodePatch(
listOf(ComponentContextParserFingerprint)
) {
override fun execute(context: BytecodeContext): PatchResult {
ComponentContextParserFingerprint.result?.let { result ->
val builderMethodIndex = EmptyComponentBuilderFingerprint
.also { it.resolve(context, result.mutableMethod, result.mutableClass) }
.let { it.result!!.scanResult.patternScanResult!!.startIndex }
ComponentContextParserFingerprint.result?.also {
arrayOf(EmptyComponentBuilderFingerprint, ReadComponentIdentifierFingerprint).forEach { fingerprint ->
if (!fingerprint.resolve(context, it.mutableMethod, it.mutableClass))
return fingerprint.toErrorResult()
}
}?.let { result ->
val builderMethodIndex = EmptyComponentBuilderFingerprint.patternScanEndIndex
val emptyComponentFieldIndex = builderMethodIndex + 2
with(result.mutableMethod) {
result.mutableMethod.apply {
val insertHookIndex = result.scanResult.patternScanResult!!.endIndex
val clobberedRegister = (instruction(insertHookIndex - 3) as OneRegisterInstruction).registerA
val builderMethodDescriptor = instruction(builderMethodIndex).descriptor
val emptyComponentFieldDescriptor = instruction(emptyComponentFieldIndex).descriptor
// Register is overwritten right after it is used for this patch, therefore free to clobber.
val clobberedRegister = instruction(insertHookIndex).oneRegister
val builderMethodDescriptor = instruction(builderMethodIndex).toDescriptor()
val emptyComponentFieldDescriptor = instruction(emptyComponentFieldIndex).toDescriptor()
@Suppress("UnnecessaryVariable")
// The register, this patch clobbers, is previously used for the StringBuilder,
// later on a new StringBuilder is instantiated on it.
val stringBuilderRegister = clobberedRegister
val stringBuilderRegister = (instruction(insertHookIndex - 1) as TwoRegisterInstruction).registerA
val identifierRegister = instruction(ReadComponentIdentifierFingerprint.patternScanEndIndex).oneRegister
addInstructions(
insertHookIndex, // right after setting the component.pathBuilder field,
"""
invoke-static {v$stringBuilderRegister, v0}, Lapp/revanced/integrations/patches/LithoFilterPatch;->filter(Ljava/lang/StringBuilder;Ljava/lang/String;)Z
invoke-static {v$stringBuilderRegister, v$identifierRegister}, Lapp/revanced/integrations/patches/LithoFilterPatch;->filter(Ljava/lang/StringBuilder;Ljava/lang/String;)Z
move-result v$clobberedRegister
if-eqz v$clobberedRegister, :not_an_ad
move-object/from16 v0, p1
invoke-static {v0}, $builderMethodDescriptor
move-result-object v0
iget-object v0, v0, $emptyComponentFieldDescriptor
return-object v0
move-object/from16 v$clobberedRegister, p1
invoke-static {v$clobberedRegister}, $builderMethodDescriptor
move-result-object v$clobberedRegister
iget-object v$clobberedRegister, v$clobberedRegister, $emptyComponentFieldDescriptor
return-object v$clobberedRegister
""",
listOf(ExternalLabel("not_an_ad", instruction(insertHookIndex)))
)
@@ -68,14 +75,14 @@ class LithoFilterPatch : BytecodePatch(
}
private companion object {
fun Instruction.toDescriptor() = when (val reference = (this as? ReferenceInstruction)?.reference) {
is MethodReference -> "${reference.definingClass}->${reference.name}(${
reference.parameterTypes.joinToString(
""
) { it }
})${reference.returnType}"
is FieldReference -> "${reference.definingClass}->${reference.name}:${reference.type}"
else -> throw PatchResultError("Unsupported reference type")
}
val MethodFingerprint.patternScanEndIndex
get() = result!!.scanResult.patternScanResult!!.endIndex
val Instruction.descriptor
get() = (this as ReferenceInstruction).reference.toString()
val Instruction.oneRegister
get() = (this as OneRegisterInstruction).registerA
}
}
}
@@ -1,10 +0,0 @@
package app.revanced.patches.youtube.misc.microg.fingerprints
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags
object IntegrityCheckFingerprint : MethodFingerprint(
"L", AccessFlags.PUBLIC or AccessFlags.STATIC, listOf("L", "L"),
strings = listOf("This should never happen.", "GooglePlayServicesUtil", "Google Play Store signature invalid.")
)
@@ -10,8 +10,8 @@ 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.fingerprints.WatchWhileActivityFingerprint
import app.revanced.patches.youtube.layout.buttons.cast.patch.HideCastButtonPatch
import app.revanced.patches.shared.misc.fix.spoof.patch.ClientSpoofPatch
import app.revanced.patches.youtube.layout.buttons.cast.patch.HideCastButtonPatch
import app.revanced.patches.youtube.misc.fix.playback.patch.SpoofSignatureVerificationPatch
import app.revanced.patches.youtube.misc.microg.annotations.MicroGPatchCompatibility
import app.revanced.patches.youtube.misc.microg.fingerprints.*
@@ -34,7 +34,6 @@ import app.revanced.util.microg.MicroGBytecodeHelper
@Version("0.0.1")
class MicroGBytecodePatch : BytecodePatch(
listOf(
IntegrityCheckFingerprint,
ServiceCheckFingerprint,
GooglePlayUtilityFingerprint,
CastDynamiteModuleFingerprint,
@@ -59,7 +58,6 @@ class MicroGBytecodePatch : BytecodePatch(
REVANCED_PACKAGE_NAME
),
listOf(
IntegrityCheckFingerprint,
ServiceCheckFingerprint,
GooglePlayUtilityFingerprint,
CastDynamiteModuleFingerprint,
@@ -10,16 +10,16 @@ object ThemeSetterAppFingerprint : MethodFingerprint(
AccessFlags.PUBLIC or AccessFlags.STATIC,
parameters = listOf("L", "L", "L", "L"),
opcodes = listOf(
Opcode.CONST, //target reference
Opcode.CONST, // target reference
Opcode.GOTO,
Opcode.CONST, //target reference
Opcode.CONST, // target reference
Opcode.INVOKE_DIRECT,
Opcode.RETURN_OBJECT,
Opcode.NEW_INSTANCE,
Opcode.INVOKE_INTERFACE,
null, // changed from invoke interface to invoke virtual
Opcode.MOVE_RESULT_OBJECT,
Opcode.SGET_OBJECT,
Opcode.IF_NE,
Opcode.CONST, //target reference
Opcode.CONST, // target reference
)
)
@@ -0,0 +1,14 @@
package app.revanced.patches.youtube.misc.video.quality.fingerprints
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.Opcode
object SetQualityByIndexMethodClassFieldReferenceFingerprint : MethodFingerprint(
returnType = "V",
parameters = listOf("L"),
opcodes = listOf(
Opcode.IGET_OBJECT,
Opcode.INVOKE_INTERFACE,
Opcode.RETURN_VOID
)
)
@@ -0,0 +1,9 @@
package app.revanced.patches.youtube.misc.video.quality.fingerprints
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
object VideoQualityItemOnClickParentFingerprint : MethodFingerprint(
"V",
strings = listOf("VIDEO_QUALITIES_MENU_BOTTOM_SHEET_FRAGMENT")
)
@@ -1,12 +0,0 @@
package app.revanced.patches.youtube.misc.video.quality.fingerprints
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode
object VideoQualityReferenceFingerprint : MethodFingerprint(
"V", AccessFlags.PUBLIC or AccessFlags.FINAL, listOf("L"), listOf(
Opcode.IPUT_OBJECT, Opcode.RETURN_VOID
)
)
@@ -9,12 +9,13 @@ import org.jf.dexlib2.Opcode
object VideoQualitySetterFingerprint : MethodFingerprint(
"V",
AccessFlags.PUBLIC or AccessFlags.FINAL,
listOf("[L", "I", "I", "Z", "I"),
listOf("[L", "I", "Z"),
listOf(
Opcode.IF_EQZ,
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT_OBJECT,
Opcode.INVOKE_VIRTUAL,
Opcode.IPUT_BOOLEAN,
)
),
strings = listOf("menu_item_video_quality"),
)
@@ -1,20 +0,0 @@
package app.revanced.patches.youtube.misc.video.quality.fingerprints
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode
object VideoUserQualityChangeFingerprint : MethodFingerprint(
"V",
AccessFlags.PUBLIC or AccessFlags.FINAL,
listOf("L","L","I","J"),
listOf(
Opcode.MOVE,
Opcode.MOVE_WIDE,
Opcode.INVOKE_INTERFACE_RANGE,
Opcode.RETURN_VOID
)
)
@@ -1,5 +1,6 @@
package app.revanced.patches.youtube.misc.video.quality.patch
import app.revanced.extensions.toErrorResult
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
@@ -9,6 +10,7 @@ import app.revanced.patcher.extensions.addInstructions
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint.Companion.resolve
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
@@ -17,9 +19,9 @@ import app.revanced.patches.shared.settings.preference.impl.SwitchPreference
import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch
import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch
import app.revanced.patches.youtube.misc.video.quality.annotations.RememberVideoQualityCompatibility
import app.revanced.patches.youtube.misc.video.quality.fingerprints.VideoQualityReferenceFingerprint
import app.revanced.patches.youtube.misc.video.quality.fingerprints.SetQualityByIndexMethodClassFieldReferenceFingerprint
import app.revanced.patches.youtube.misc.video.quality.fingerprints.VideoQualityItemOnClickParentFingerprint
import app.revanced.patches.youtube.misc.video.quality.fingerprints.VideoQualitySetterFingerprint
import app.revanced.patches.youtube.misc.video.quality.fingerprints.VideoUserQualityChangeFingerprint
import app.revanced.patches.youtube.misc.video.videoid.patch.VideoIdPatch
import org.jf.dexlib2.iface.instruction.ReferenceInstruction
import org.jf.dexlib2.iface.reference.FieldReference
@@ -32,14 +34,18 @@ import org.jf.dexlib2.iface.reference.FieldReference
@Version("0.0.1")
class RememberVideoQualityPatch : BytecodePatch(
listOf(
VideoQualitySetterFingerprint
VideoQualitySetterFingerprint,
VideoQualityItemOnClickParentFingerprint
)
) {
override fun execute(context: BytecodeContext): PatchResult {
SettingsPatch.PreferenceScreen.MISC.addPreferences(
SwitchPreference(
"revanced_remember_video_quality_last_selected",
StringResource("revanced_remember_video_quality_last_selected_title", "Remember video quality changes"),
StringResource(
"revanced_remember_video_quality_last_selected_title",
"Remember video quality changes"
),
true,
StringResource(
"revanced_remember_video_quality_last_selected_summary_on",
@@ -52,37 +58,80 @@ class RememberVideoQualityPatch : BytecodePatch(
)
)
val setterMethod = VideoQualitySetterFingerprint.result!!
/*
* The following code works by hooking the method which is called when the user selects a video quality
* to remember the last selected video quality.
*
* It also hooks the method which is called when the video quality to set is determined.
* Conveniently, at this point the video quality is overridden to the remembered playback speed.
*/
VideoUserQualityChangeFingerprint.resolve(context, setterMethod.classDef)
val userQualityMethod = VideoUserQualityChangeFingerprint.result!!
VideoIdPatch.injectCall("$INTEGRATIONS_CLASS_DESCRIPTOR->newVideoStarted(Ljava/lang/String;)V")
VideoQualityReferenceFingerprint.resolve(context, setterMethod.classDef)
val qualityFieldReference =
VideoQualityReferenceFingerprint.result!!.method.let { method ->
(method.implementation!!.instructions.elementAt(0) as ReferenceInstruction).reference as FieldReference
}
// Inject a call to set the remembered quality once a video loads.
VideoQualitySetterFingerprint.result?.also {
if (!SetQualityByIndexMethodClassFieldReferenceFingerprint.resolve(context, it.classDef))
return PatchResultError("Could not resolve fingerprint to find setQualityByIndex method")
}?.let {
// This instruction refers to the field with the type that contains the setQualityByIndex method.
val instructions = SetQualityByIndexMethodClassFieldReferenceFingerprint.result!!
.method.implementation!!.instructions
VideoIdPatch.injectCall("Lapp/revanced/integrations/patches/playback/quality/RememberVideoQualityPatch;->newVideoStarted(Ljava/lang/String;)V")
val getOnItemClickListenerClassReference =
(instructions.elementAt(0) as ReferenceInstruction).reference
val getSetQualityByIndexMethodClassFieldReference =
(instructions.elementAt(1) as ReferenceInstruction).reference
val qIndexMethodName =
context.classes.single { it.type == qualityFieldReference.type }.methods.single { it.parameterTypes.first() == "I" }.name
val setQualityByIndexMethodClassFieldReference =
getSetQualityByIndexMethodClassFieldReference as FieldReference
setterMethod.mutableMethod.addInstructions(
0,
"""
iget-object v0, p0, ${setterMethod.classDef.type}->${qualityFieldReference.name}:${qualityFieldReference.type}
const-string v1, "$qIndexMethodName"
invoke-static {p1, p2, v0, v1}, Lapp/revanced/integrations/patches/playback/quality/RememberVideoQualityPatch;->setVideoQuality([Ljava/lang/Object;ILjava/lang/Object;Ljava/lang/String;)I
move-result p2
""",
)
val setQualityByIndexMethodClass = context.classes
.find { classDef -> classDef.type == setQualityByIndexMethodClassFieldReference.type }!!
userQualityMethod.mutableMethod.addInstruction(
0,
"invoke-static {p3}, Lapp/revanced/integrations/patches/playback/quality/RememberVideoQualityPatch;->userChangedQuality(I)V"
)
// Get the name of the setQualityByIndex method.
val setQualityByIndexMethod = setQualityByIndexMethodClass.methods
.find { method -> method.parameterTypes.first() == "I" }
?: return PatchResultError("Could not find setQualityByIndex method")
it.mutableMethod.addInstructions(
0,
"""
# Get the object instance to invoke the setQualityByIndex method on.
iget-object v0, p0, $getOnItemClickListenerClassReference
iget-object v0, v0, $getSetQualityByIndexMethodClassFieldReference
# Get the method name.
const-string v1, "${setQualityByIndexMethod.name}"
# Set the quality.
# The first parameter is the array list of video qualities.
# The second parameter is the index of the selected quality.
# The register v0 stores the object instance to invoke the setQualityByIndex method on.
# The register v1 stores the name of the setQualityByIndex method.
invoke-static {p1, p2, v0, v1}, $INTEGRATIONS_CLASS_DESCRIPTOR->setVideoQuality([Ljava/lang/Object;ILjava/lang/Object;Ljava/lang/String;)I
move-result p2
""",
)
} ?: return VideoQualitySetterFingerprint.toErrorResult()
// Inject a call to remember the selected quality.
VideoQualityItemOnClickParentFingerprint.result?.let {
val onItemClickMethod = it.mutableClass.methods.find { method -> method.name == "onItemClick" }
onItemClickMethod?.apply {
val listItemIndexParameter = 3
addInstruction(
0,
"invoke-static {p$listItemIndexParameter}, $INTEGRATIONS_CLASS_DESCRIPTOR->userChangedQuality(I)V"
)
} ?: return PatchResultError("Failed to find onItemClick method")
} ?: return VideoQualityItemOnClickParentFingerprint.toErrorResult()
return PatchResultSuccess()
}
private companion object {
const val INTEGRATIONS_CLASS_DESCRIPTOR =
"Lapp/revanced/integrations/patches/playback/quality/RememberVideoQualityPatch;"
}
}
@@ -1,8 +0,0 @@
package app.revanced.patches.youtube.misc.video.speed.remember.fingerprint
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
object ChangePlaybackSpeedFragmentStateFingerprint : MethodFingerprint(
"V",
strings = listOf("PLAYBACK_RATE_MENU_BOTTOM_SHEET_FRAGMENT")
)
@@ -3,5 +3,6 @@ package app.revanced.patches.youtube.misc.video.speed.remember.fingerprint
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
object InitializePlaybackSpeedValuesFingerprint : MethodFingerprint(
parameters = listOf("[L", "I")
parameters = listOf("[L", "I"),
strings = listOf("menu_item_playback_speed")
)
@@ -6,7 +6,9 @@ import org.jf.dexlib2.Opcode
object OnPlaybackSpeedItemClickFingerprint : MethodFingerprint(
customFingerprint = { it.name == "onItemClick" },
opcodes = listOf(
Opcode.IGET_OBJECT,
Opcode.INVOKE_VIRTUAL
Opcode.MOVE_RESULT_OBJECT,
Opcode.INVOKE_VIRTUAL,
Opcode.INVOKE_VIRTUAL,
Opcode.RETURN_VOID
)
)
@@ -8,8 +8,6 @@ import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.addInstruction
import app.revanced.patcher.extensions.addInstructions
import app.revanced.patcher.extensions.instruction
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint.Companion.resolve
import app.revanced.patcher.patch.*
import app.revanced.patcher.patch.annotations.DependsOn
import app.revanced.patcher.patch.annotations.Patch
@@ -19,12 +17,12 @@ import app.revanced.patches.shared.settings.preference.impl.SwitchPreference
import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch
import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch
import app.revanced.patches.youtube.misc.video.speed.remember.annotation.RememberPlaybackSpeedCompatibility
import app.revanced.patches.youtube.misc.video.speed.remember.fingerprint.ChangePlaybackSpeedFragmentStateFingerprint
import app.revanced.patches.youtube.misc.video.speed.remember.fingerprint.InitializePlaybackSpeedValuesFingerprint
import app.revanced.patches.youtube.misc.video.speed.remember.fingerprint.OnPlaybackSpeedItemClickFingerprint
import app.revanced.patches.youtube.misc.video.videoid.patch.VideoIdPatch
import org.jf.dexlib2.Opcode
import org.jf.dexlib2.iface.instruction.FiveRegisterInstruction
import org.jf.dexlib2.iface.instruction.Instruction
import org.jf.dexlib2.iface.instruction.ReferenceInstruction
@Patch
@@ -34,7 +32,10 @@ import org.jf.dexlib2.iface.instruction.ReferenceInstruction
@RememberPlaybackSpeedCompatibility
@Version("0.0.1")
class RememberPlaybackSpeedPatch : BytecodePatch(
listOf(ChangePlaybackSpeedFragmentStateFingerprint)
listOf(
OnPlaybackSpeedItemClickFingerprint,
InitializePlaybackSpeedValuesFingerprint
)
) {
override fun execute(context: BytecodeContext): PatchResult {
SettingsPatch.PreferenceScreen.MISC.addPreferences(
@@ -56,54 +57,67 @@ class RememberPlaybackSpeedPatch : BytecodePatch(
)
)
context.resolveFingerprints()
VideoIdPatch.injectCall("${INTEGRATIONS_CLASS_DESCRIPTOR}->newVideoLoaded(Ljava/lang/String;)V")
/*
* The following code works by hooking the method which is called when the user selects a playback speed
* to remember the last selected playback speed.
*
* It also hooks the method which is called when the playback speeds are initialized.
* Conveniently, at this point the playback speed is set to the remembered playback speed.
*/
// Set the remembered playback speed.
InitializePlaybackSpeedValuesFingerprint.result!!.apply {
// Infer everything necessary for setPlaybackSate()
InitializePlaybackSpeedValuesFingerprint.result?.apply {
// Infer everything necessary for calling the method setPlaybackSpeed().
val instructions = OnPlaybackSpeedItemClickFingerprint.result!!.mutableMethod.implementation!!.instructions
fun getReference(offset: Int = 0, opcode: Opcode) =
instructions[instructions.indexOfFirst { it.opcode == opcode } + offset].reference
val playbackHandlerWrapperFieldReference =
(object : MethodFingerprint(opcodes = listOf(Opcode.IF_EQZ)) {}).apply {
OnPlaybackSpeedItemClickFingerprint.result!!.apply {
resolve(
context,
method,
classDef
)
}
}.getReference(-1)
val playbackHandlerWrapperImplementorClassReference = OnPlaybackSpeedItemClickFingerprint
.getReference(-1)
val playbackHandlerFieldReference = OnPlaybackSpeedItemClickFingerprint
.getReference()
val setPlaybackSpeedMethodReference = OnPlaybackSpeedItemClickFingerprint
.getReference(1)
val setPlaybackSpeedContainerClassFieldReference =
getReference(-1, Opcode.IF_EQZ)
val setPlaybackSpeedClassFieldReference =
getReference(1, Opcode.IGET)
val setPlaybackSpeedMethodReference =
getReference(2, Opcode.IGET)
val onItemClickListenerClassFieldReference = mutableMethod.instruction(0).reference
// Registers are not used at index 0, so they can be freely used.
mutableMethod.addInstructions(
0,
"""
invoke-static { }, $INTEGRATIONS_CLASS_DESCRIPTOR->getCurrentPlaybackSpeed()F
move-result v0
# check if the playback speed is not 1.0x
# Check if the playback speed is not 1.0x.
const/high16 v1, 0x3f800000 # 1.0f
cmpg-float v1, v0, v1
if-eqz v1, :do_not_override
# Get the instance of the class which has the container class field below.
iget-object v1, p0, $onItemClickListenerClassFieldReference
# invoke setPlaybackSpeed
iget-object v1, p0, $playbackHandlerWrapperFieldReference
check-cast v1, $playbackHandlerWrapperImplementorClassReference
iget-object v2, v1, $playbackHandlerFieldReference
# Get the container class field.
iget-object v1, v1, $setPlaybackSpeedContainerClassFieldReference
# Get the field from its class.
iget-object v2, v1, $setPlaybackSpeedClassFieldReference
# Invoke setPlaybackSpeed on that class.
invoke-virtual {v2, v0}, $setPlaybackSpeedMethodReference
""".trimIndent(),
listOf(ExternalLabel("do_not_override", mutableMethod.instruction(0)))
)
}
} ?: return InitializePlaybackSpeedValuesFingerprint.toErrorResult()
// Remember the selected playback speed.
OnPlaybackSpeedItemClickFingerprint.result!!.apply {
val setPlaybackSpeedIndex = scanResult.patternScanResult!!.endIndex
OnPlaybackSpeedItemClickFingerprint.result?.apply {
val setPlaybackSpeedIndex = scanResult.patternScanResult!!.startIndex - 3
val selectedPlaybackSpeedRegister =
(mutableMethod.instruction(setPlaybackSpeedIndex) as FiveRegisterInstruction).registerD
@@ -111,8 +125,7 @@ class RememberPlaybackSpeedPatch : BytecodePatch(
setPlaybackSpeedIndex,
"invoke-static { v$selectedPlaybackSpeedRegister }, $INTEGRATIONS_CLASS_DESCRIPTOR->setPlaybackSpeed(F)V"
)
}
} ?: return OnPlaybackSpeedItemClickFingerprint.toErrorResult()
return PatchResultSuccess()
}
@@ -121,20 +134,6 @@ class RememberPlaybackSpeedPatch : BytecodePatch(
const val INTEGRATIONS_CLASS_DESCRIPTOR =
"Lapp/revanced/integrations/patches/playback/speed/RememberPlaybackSpeedPatch;"
fun MethodFingerprint.getReference(offsetFromPatternScanResultStartIndex: Int = 0) = this.result!!.let {
val referenceInstruction = it.mutableMethod
.instruction(it.scanResult.patternScanResult!!.startIndex + offsetFromPatternScanResultStartIndex) as ReferenceInstruction
referenceInstruction.reference.toString()
}
fun BytecodeContext.resolveFingerprints() {
ChangePlaybackSpeedFragmentStateFingerprint.result?.also {
fun MethodFingerprint.resolve() = resolve(this@resolveFingerprints, it.classDef)
OnPlaybackSpeedItemClickFingerprint.resolve()
InitializePlaybackSpeedValuesFingerprint.resolve()
} ?: throw ChangePlaybackSpeedFragmentStateFingerprint.toErrorResult()
}
val Instruction.reference get() = (this as ReferenceInstruction).reference.toString()
}
}
}