feat(YouTube): add Hook download actions patch (#70)

* feat(YouTube/Download Playlist Button): add playlist download button

Co-authored-by: Hoàng Gia Bảo <70064328+YT-Advanced@users.noreply.github.com>
Co-authored-by: inotia00 <108592928+inotia00@users.noreply.github.com>

* feat(YouTube/Download Playlist Button): add playlist external downloader field

* refactor(YouTube/Download Playlist Button): remove duplicate resources

---------

Co-authored-by: Hoàng Gia Bảo <70064328+YT-Advanced@users.noreply.github.com>
Co-authored-by: inotia00 <108592928+inotia00@users.noreply.github.com>
This commit is contained in:
Francesco Marastoni
2024-08-06 02:48:40 +00:00
committed by GitHub
parent e2a9e5b556
commit d647061d94
10 changed files with 247 additions and 49 deletions

View File

@ -0,0 +1,174 @@
package app.revanced.patches.youtube.misc.downloadactions
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.InstructionExtensions.addInstructions
import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.util.smali.ExternalLabel
import app.revanced.patches.youtube.misc.downloadactions.fingerprints.*
import app.revanced.patches.youtube.utils.compatibility.Constants
import app.revanced.patches.youtube.utils.integrations.Constants.INTEGRATIONS_PATH
import app.revanced.patches.youtube.utils.integrations.Constants.MISC_PATH
import app.revanced.patches.youtube.utils.mainactivity.MainActivityResolvePatch
import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch
import app.revanced.patches.youtube.utils.settings.SettingsPatch
import app.revanced.util.getReference
import app.revanced.util.indexOfFirstInstructionOrThrow
import app.revanced.util.patch.BaseBytecodePatch
import app.revanced.util.resultOrThrow
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction
import com.android.tools.smali.dexlib2.iface.instruction.formats.Instruction35c
import com.android.tools.smali.dexlib2.iface.reference.MethodReference
@Suppress("unused")
object HookDownloadActionsPatch : BaseBytecodePatch(
name = "Hook download actions",
description = "Adds options to show the download playlist button and hook the download actions.",
dependencies = setOf(
MainActivityResolvePatch::class,
SharedResourceIdPatch::class,
SettingsPatch::class
),
compatiblePackages = Constants.COMPATIBLE_PACKAGE,
fingerprints = setOf(
OfflineVideoEndpointFingerprint,
PiPPlaybackFingerprint,
AccessibilityOfflineButtonSyncFingerprint,
DownloadPlaylistButtonOnClickFingerprint
)
) {
private const val INTEGRATIONS_DOWNLOAD_PLAYLIST_BUTTON_CLASS_DESCRIPTOR =
"$MISC_PATH/HookDownloadAction;"
private const val INTEGRATIONS_VIDEO_UTILS_CLASS_DESCRIPTOR =
"$INTEGRATIONS_PATH/utils/VideoUtils;"
override fun execute(context: BytecodeContext) {
// region Patch for hook download actions
OfflineVideoEndpointFingerprint.resultOrThrow().mutableMethod.apply {
addInstructionsWithLabels(
0, """
invoke-static/range {p3 .. p3}, $INTEGRATIONS_VIDEO_UTILS_CLASS_DESCRIPTOR->inAppDownloadButtonOnClick(Ljava/lang/String;)Z
move-result v0
if-eqz v0, :show_native_downloader
return-void
""", ExternalLabel("show_native_downloader", getInstruction(0))
)
}
PiPPlaybackFingerprint.resultOrThrow().let {
it.mutableMethod.apply {
val insertIndex = it.scanResult.patternScanResult!!.endIndex
val insertRegister = getInstruction<OneRegisterInstruction>(insertIndex).registerA
addInstructions(
insertIndex, """
invoke-static {v$insertRegister}, $INTEGRATIONS_VIDEO_UTILS_CLASS_DESCRIPTOR->getExternalDownloaderLaunchedState(Z)Z
move-result v$insertRegister
"""
)
}
}
// endregion
// region Force show the playlist download button
AccessibilityOfflineButtonSyncFingerprint.resultOrThrow().let { parentResult ->
SetPlaylistDownloadButtonVisibilityFingerprint.also {
it.resolve(
context,
parentResult.classDef
)
}.resultOrThrow().let { setVisibilityMethod ->
setVisibilityMethod.mutableMethod.apply {
// Find the index of if-nez
val insertIndex = setVisibilityMethod.scanResult.patternScanResult!!.startIndex + 2
// Get register values used in if-nez
val insertRegister = getInstruction<OneRegisterInstruction>(insertIndex).registerA
// Add instructions just above the index of if-nez
addInstructions(
insertIndex,
"""
invoke-static {}, $INTEGRATIONS_DOWNLOAD_PLAYLIST_BUTTON_CLASS_DESCRIPTOR->isPlaylistDownloadButtonHooked()Z
move-result v$insertRegister
"""
)
}
}
}
// endregion
// region Hook Download Playlist Button OnClick method
DownloadPlaylistButtonOnClickFingerprint.resultOrThrow().let {
it.mutableMethod.apply {
// region Get the index of the instruction that initializes the onClickListener
val onClickListenerInitializeIndex = indexOfFirstInstructionOrThrow {
val reference = ((this as? ReferenceInstruction)?.reference as? MethodReference)
opcode == Opcode.INVOKE_VIRTUAL_RANGE
&& reference?.parameterTypes?.first() == "Ljava/lang/String;"
}
// endregion
// region Get the class that contains the onClick method
val onClickListenerInitializeReference =
getInstruction<ReferenceInstruction>(onClickListenerInitializeIndex).reference
val onClickClass = context.findClass(
(onClickListenerInitializeReference as MethodReference).returnType
)!!.mutableClass
// endregion
onClickClass.methods.find { method -> method.name == "onClick" }?.apply {
// region Get the index of playlist id
val insertIndex = implementation!!.instructions.indexOfFirst { instruction ->
instruction.opcode == Opcode.INVOKE_STATIC
&& instruction.getReference<MethodReference>()?.name == "isEmpty"
}
val insertRegister = getInstruction<Instruction35c>(insertIndex).registerC
// endregion
addInstructions(
insertIndex,
"""
invoke-static {v$insertRegister}, $INTEGRATIONS_DOWNLOAD_PLAYLIST_BUTTON_CLASS_DESCRIPTOR->startPlaylistDownloadActivity(Ljava/lang/String;)Ljava/lang/String;
move-result-object v$insertRegister
""".trimIndent()
)
}
}
}
// endregion
/**
* Add settings
*/
SettingsPatch.addPreference(
arrayOf(
"PREFERENCE_SCREEN: GENERAL",
"SETTINGS: HOOK_DOWNLOAD_ACTIONS"
)
)
SettingsPatch.updatePatchStatus(this)
}
}

View File

@ -0,0 +1,12 @@
package app.revanced.patches.youtube.misc.downloadactions.fingerprints
import app.revanced.patcher.extensions.or
import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.AccessibilityOfflineButtonSync
import app.revanced.util.fingerprint.LiteralValueFingerprint
import com.android.tools.smali.dexlib2.AccessFlags
object AccessibilityOfflineButtonSyncFingerprint : LiteralValueFingerprint(
accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL,
returnType = "V",
literalSupplier = { AccessibilityOfflineButtonSync }
)

View File

@ -0,0 +1,29 @@
package app.revanced.patches.youtube.misc.downloadactions.fingerprints
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.MethodFingerprint
import app.revanced.patches.youtube.misc.downloadactions.fingerprints.DownloadPlaylistButtonOnClickFingerprint.PLAYLIST_ON_CLICK_INITIALIZE_PAREMETER
import app.revanced.util.getReference
import app.revanced.util.indexOfFirstInstruction
import com.android.tools.smali.dexlib2.AccessFlags
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.reference.MethodReference
object DownloadPlaylistButtonOnClickFingerprint : MethodFingerprint(
accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL,
returnType = "V",
customFingerprint = { methodDef, _ ->
methodDef.indexOfFirstInstruction {
opcode == Opcode.INVOKE_VIRTUAL &&
getReference<MethodReference>()?.parameterTypes == PLAYLIST_ON_CLICK_INITIALIZE_PAREMETER
} >= 0
}
) {
val PLAYLIST_ON_CLICK_INITIALIZE_PAREMETER = listOf(
"Ljava/lang/String;",
"Lcom/google/android/apps/youtube/app/offline/ui/OfflineArrowView;",
"I",
"Landroid/view/View${'$'}OnClickListener;"
)
}

View File

@ -1,4 +1,4 @@
package app.revanced.patches.youtube.player.overlaybuttons.fingerprints
package app.revanced.patches.youtube.misc.downloadactions.fingerprints
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.MethodFingerprint

View File

@ -1,4 +1,4 @@
package app.revanced.patches.youtube.player.overlaybuttons.fingerprints
package app.revanced.patches.youtube.misc.downloadactions.fingerprints
import app.revanced.patcher.fingerprint.MethodFingerprint
import com.android.tools.smali.dexlib2.Opcode

View File

@ -0,0 +1,15 @@
package app.revanced.patches.youtube.misc.downloadactions.fingerprints
import app.revanced.patcher.fingerprint.MethodFingerprint
import com.android.tools.smali.dexlib2.Opcode
object SetPlaylistDownloadButtonVisibilityFingerprint : MethodFingerprint(
returnType = "V",
opcodes = listOf(
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT,
Opcode.IF_NEZ,
Opcode.IGET,
Opcode.CONST_4
)
)

View File

@ -1,29 +1,19 @@
package app.revanced.patches.youtube.player.overlaybuttons
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.InstructionExtensions.addInstructions
import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.PatchException
import app.revanced.patcher.patch.annotation.Patch
import app.revanced.patcher.util.smali.ExternalLabel
import app.revanced.patches.youtube.player.overlaybuttons.fingerprints.OfflineVideoEndpointFingerprint
import app.revanced.patches.youtube.player.overlaybuttons.fingerprints.PiPPlaybackFingerprint
import app.revanced.patches.youtube.player.overlaybuttons.fingerprints.PlayerButtonConstructorFingerprint
import app.revanced.patches.youtube.utils.integrations.Constants.INTEGRATIONS_PATH
import app.revanced.patches.youtube.utils.integrations.Constants.UTILS_PATH
import app.revanced.patches.youtube.utils.mainactivity.MainActivityResolvePatch
import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch
import app.revanced.patches.youtube.video.information.VideoInformationPatch
import app.revanced.util.addFieldAndInstructions
import app.revanced.util.getReference
import app.revanced.util.getTargetIndexWithReferenceOrThrow
import app.revanced.util.indexOfFirstInstruction
import app.revanced.util.indexOfFirstInstructionOrThrow
import app.revanced.util.resultOrThrow
import app.revanced.util.*
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction
import com.android.tools.smali.dexlib2.iface.instruction.TwoRegisterInstruction
import com.android.tools.smali.dexlib2.iface.reference.FieldReference
@ -38,48 +28,14 @@ import com.android.tools.smali.dexlib2.iface.reference.MethodReference
)
object OverlayButtonsBytecodePatch : BytecodePatch(
setOf(
OfflineVideoEndpointFingerprint,
PiPPlaybackFingerprint,
PlayerButtonConstructorFingerprint
)
) {
private const val INTEGRATIONS_ALWAYS_REPEAT_CLASS_DESCRIPTOR =
"$UTILS_PATH/AlwaysRepeatPatch;"
private const val INTEGRATIONS_VIDEO_UTILS_CLASS_DESCRIPTOR =
"$INTEGRATIONS_PATH/utils/VideoUtils;"
override fun execute(context: BytecodeContext) {
// region patch for hook download button
OfflineVideoEndpointFingerprint.resultOrThrow().mutableMethod.apply {
addInstructionsWithLabels(
0, """
invoke-static/range {p3 .. p3}, $INTEGRATIONS_VIDEO_UTILS_CLASS_DESCRIPTOR->inAppDownloadButtonOnClick(Ljava/lang/String;)Z
move-result v0
if-eqz v0, :show_native_downloader
return-void
""", ExternalLabel("show_native_downloader", getInstruction(0))
)
}
PiPPlaybackFingerprint.resultOrThrow().let {
it.mutableMethod.apply {
val insertIndex = it.scanResult.patternScanResult!!.endIndex
val insertRegister = getInstruction<OneRegisterInstruction>(insertIndex).registerA
addInstructions(
insertIndex, """
invoke-static {v$insertRegister}, $INTEGRATIONS_VIDEO_UTILS_CLASS_DESCRIPTOR->getExternalDownloaderLaunchedState(Z)Z
move-result v$insertRegister
"""
)
}
}
// endregion
// region patch for always repeat and pause
PlayerButtonConstructorFingerprint.resultOrThrow().mutableMethod.apply {

View File

@ -17,6 +17,7 @@ import app.revanced.patches.shared.mapping.ResourceType.STYLE
@Patch(dependencies = [ResourceMappingPatch::class])
object SharedResourceIdPatch : ResourcePatch() {
var AccessibilityOfflineButtonSync = -1L
var AccountSwitcherAccessibility = -1L
var ActionBarRingo = -1L
var ActionBarRingoBackground = -1L
@ -121,6 +122,7 @@ object SharedResourceIdPatch : ResourcePatch() {
override fun execute(context: ResourceContext) {
AccessibilityOfflineButtonSync = getId(STRING, "accessibility_offline_button_sync")
AccountSwitcherAccessibility = getId(STRING, "account_switcher_accessibility_label")
ActionBarRingo = getId(LAYOUT, "action_bar_ringo")
ActionBarRingoBackground = getId(LAYOUT, "action_bar_ringo_background")