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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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")

View File

@ -505,6 +505,9 @@ You tab → View channel → Menu → Settings"</string>
Tap and hold to open YouTube settings."</string>
<string name="revanced_replace_toolbar_create_button_type_summary_off">"Tap to open YouTube settings.
Tap and hold to open RVX settings."</string>
<string name="revanced_hook_playlist_download_button_title">Override playlist download action</string>
<string name="revanced_hook_playlist_download_button_summary_off">Playlist download will behave like the original, and button might not appear.</string>
<string name="revanced_hook_playlist_download_button_summary_on">Playlist download button will be present, and the download will be handled by YTDLnis.</string>
<!-- PreferenceScreen: Player -->
@ -900,6 +903,9 @@ Tap and hold to undo."</string>
<string name="revanced_external_downloader_package_name_title">External downloader package name</string>
<string name="revanced_external_downloader_package_name_summary">Package name of your installed external downloader app, such as NewPipe or YTDLnis.</string>
<string name="revanced_external_downloader_dialog_title">External downloader</string>
<string name="revanced_playlist_external_downloader_package_name_title">Playlist external downloader package name</string>
<string name="revanced_playlist_external_downloader_package_name_summary">Package name of your installed external downloader app used to download from playlists, currently only YTDLnis works.</string>
<string name="revanced_playlist_external_downloader_dialog_title">Playlist external downloader</string>
<string name="revanced_external_downloader_not_installed_dialog_title">Warning</string>
<string name="revanced_external_downloader_not_installed_dialog_message">"%1$s is not installed.
Please download %2$s from the website."</string>

View File

@ -230,6 +230,11 @@
<SwitchPreference android:title="@string/revanced_enable_phone_layout_title" android:key="revanced_enable_phone_layout" android:summary="@string/revanced_enable_phone_layout_summary" />
<SwitchPreference android:title="@string/revanced_enable_tablet_layout_title" android:key="revanced_enable_tablet_layout" android:summary="@string/revanced_enable_tablet_layout_summary" />SETTINGS: LAYOUT_SWITCH -->
<!-- SETTINGS: HOOK_DOWNLOAD_ACTIONS
<SwitchPreference android:title="@string/revanced_hook_playlist_download_button_title" android:key="revanced_hook_playlist_download_button" android:defaultValue="true" android:summaryOn="@string/revanced_hook_playlist_download_button_summary_on" android:summaryOff="@string/revanced_hook_playlist_download_button_summary_off"/>
<SwitchPreference android:title="@string/revanced_external_downloader_action_title" android:key="revanced_external_downloader_action" android:defaultValue="false" android:summaryOn="@string/revanced_external_downloader_action_summary_on" android:summaryOff="@string/revanced_external_downloader_action_summary_off" />
<app.revanced.integrations.youtube.settings.preference.ExternalPlaylistDownloaderPreference android:title="@string/revanced_playlist_external_downloader_package_name_title" android:key="revanced_playlist_external_downloader_package_name" android:summary="@string/revanced_playlist_external_downloader_package_name_summary" />SETTINGS: HOOK_DOWNLOAD_ACTIONS -->
<!-- SETTINGS: SPOOF_APP_VERSION
<SwitchPreference android:title="@string/revanced_spoof_app_version_title" android:key="revanced_spoof_app_version" android:summaryOn="@string/revanced_spoof_app_version_summary_on" android:summaryOff="@string/revanced_spoof_app_version_summary_off" />
<ListPreference android:title="@string/revanced_spoof_app_version_target_entry_title" android:key="revanced_spoof_app_version_target" android:entries="@array/revanced_spoof_app_version_target_entries" android:entryValues="@array/revanced_spoof_app_version_target_entry_values" />
@ -383,8 +388,7 @@
<SwitchPreference android:title="@string/revanced_overlay_button_whitelist_title" android:key="revanced_overlay_button_whitelist" android:summary="@string/revanced_overlay_button_whitelist_summary" />
<app.revanced.integrations.youtube.settings.preference.ExternalDownloaderPreference android:title="@string/revanced_external_downloader_package_name_title" android:key="revanced_external_downloader_package_name" android:summary="@string/revanced_external_downloader_package_name_summary" />
<app.revanced.integrations.youtube.settings.preference.WhitelistedChannelsPreference android:title="@string/revanced_whitelist_settings_title" android:key="revanced_whitelist_settings" android:summary="@string/revanced_whitelist_settings_summary" />
<PreferenceCategory android:title="@string/revanced_preference_category_experimental_flag" android:layout="@layout/revanced_settings_preferences_category" />
<SwitchPreference android:title="@string/revanced_external_downloader_action_title" android:key="revanced_external_downloader_action" android:summaryOn="@string/revanced_external_downloader_action_summary_on" android:summaryOff="@string/revanced_external_downloader_action_summary_off" />SETTINGS: OVERLAY_BUTTONS -->
<PreferenceCategory android:title="@string/revanced_preference_category_experimental_flag" android:layout="@layout/revanced_settings_preferences_category" />SETTINGS: OVERLAY_BUTTONS -->
<!-- PREFERENCE_SCREENS: PLAYER_BUTTONS
</PreferenceScreen>PREFERENCE_SCREENS: PLAYER_BUTTONS -->