feat: apply code review suggestions

This commit is contained in:
inotia00
2024-08-06 11:50:56 +09:00
parent d647061d94
commit ec85abc1c7
14 changed files with 265 additions and 255 deletions

View File

@ -0,0 +1,136 @@
package app.revanced.patches.youtube.general.downloads
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.extensions.InstructionExtensions.getInstructions
import app.revanced.patcher.patch.PatchException
import app.revanced.patcher.util.smali.ExternalLabel
import app.revanced.patches.youtube.general.downloads.fingerprints.AccessibilityOfflineButtonSyncFingerprint
import app.revanced.patches.youtube.general.downloads.fingerprints.DownloadPlaylistButtonOnClickFingerprint
import app.revanced.patches.youtube.general.downloads.fingerprints.DownloadPlaylistButtonOnClickFingerprint.indexOfPlaylistDownloadActionInvokeInstruction
import app.revanced.patches.youtube.general.downloads.fingerprints.OfflineVideoEndpointFingerprint
import app.revanced.patches.youtube.general.downloads.fingerprints.SetPlaylistDownloadButtonVisibilityFingerprint
import app.revanced.patches.youtube.utils.compatibility.Constants
import app.revanced.patches.youtube.utils.integrations.Constants.GENERAL_PATH
import app.revanced.patches.youtube.utils.pip.PiPStateHookPatch
import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch
import app.revanced.patches.youtube.utils.settings.SettingsPatch
import app.revanced.util.alsoResolve
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.FiveRegisterInstruction
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
import com.android.tools.smali.dexlib2.iface.reference.MethodReference
@Suppress("unused")
object DownloadActionsPatch : BaseBytecodePatch(
name = "Hook download actions",
description = "Adds support to download videos with an external downloader app using the in-app download button.",
dependencies = setOf(
PiPStateHookPatch::class,
SharedResourceIdPatch::class,
SettingsPatch::class
),
compatiblePackages = Constants.COMPATIBLE_PACKAGE,
fingerprints = setOf(
AccessibilityOfflineButtonSyncFingerprint,
DownloadPlaylistButtonOnClickFingerprint,
OfflineVideoEndpointFingerprint,
)
) {
private const val INTEGRATIONS_CLASS_DESCRIPTOR =
"$GENERAL_PATH/DownloadActionsPatch;"
override fun execute(context: BytecodeContext) {
// region patch for hook download actions (video action bar and flyout panel)
OfflineVideoEndpointFingerprint.resultOrThrow().mutableMethod.apply {
addInstructionsWithLabels(
0, """
invoke-static/range {p3 .. p3}, $INTEGRATIONS_CLASS_DESCRIPTOR->inAppVideoDownloadButtonOnClick(Ljava/lang/String;)Z
move-result v0
if-eqz v0, :show_native_downloader
return-void
""", ExternalLabel("show_native_downloader", getInstruction(0))
)
}
// endregion
// region patch for hook download actions (playlist)
val onClickListenerClass =
DownloadPlaylistButtonOnClickFingerprint.resultOrThrow().mutableMethod.let {
val playlistDownloadActionInvokeIndex =
indexOfPlaylistDownloadActionInvokeInstruction(it)
it.getInstructions().subList(
playlistDownloadActionInvokeIndex - 10,
playlistDownloadActionInvokeIndex,
).find { instruction ->
instruction.opcode == Opcode.INVOKE_VIRTUAL_RANGE
&& instruction.getReference<MethodReference>()?.parameterTypes?.first() == "Ljava/lang/String;"
}?.getReference<MethodReference>()?.returnType
?: throw PatchException("Could not find onClickListenerClass")
}
context.findClass(onClickListenerClass)
?.mutableClass
?.methods
?.first { method -> method.name == "onClick" }?.apply {
val insertIndex = indexOfFirstInstructionOrThrow {
opcode == Opcode.INVOKE_STATIC
&& getReference<MethodReference>()?.name == "isEmpty"
}
val insertRegister = getInstruction<FiveRegisterInstruction>(insertIndex).registerC
addInstructions(
insertIndex, """
invoke-static {v$insertRegister}, $INTEGRATIONS_CLASS_DESCRIPTOR->inAppPlaylistDownloadButtonOnClick(Ljava/lang/String;)Ljava/lang/String;
move-result-object v$insertRegister
"""
)
} ?: throw PatchException("Could not find class $onClickListenerClass")
// endregion
// region patch for show the playlist download button
SetPlaylistDownloadButtonVisibilityFingerprint
.alsoResolve(context, AccessibilityOfflineButtonSyncFingerprint).let {
it.mutableMethod.apply {
val insertIndex = it.scanResult.patternScanResult!!.startIndex + 2
val insertRegister =
getInstruction<OneRegisterInstruction>(insertIndex).registerA
addInstructions(
insertIndex, """
invoke-static {}, $INTEGRATIONS_CLASS_DESCRIPTOR->overridePlaylistDownloadButtonVisibility()Z
move-result v$insertRegister
"""
)
}
}
// endregion
/**
* Add settings
*/
SettingsPatch.addPreference(
arrayOf(
"PREFERENCE_SCREEN: GENERAL",
"PREFERENCE_CATEGORY: GENERAL_EXPERIMENTAL_FLAGS",
"SETTINGS: HOOK_DOWNLOAD_ACTIONS"
)
)
SettingsPatch.updatePatchStatus(this)
}
}

View File

@ -1,12 +1,12 @@
package app.revanced.patches.youtube.misc.downloadactions.fingerprints
package app.revanced.patches.youtube.general.downloads.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(
internal object AccessibilityOfflineButtonSyncFingerprint : LiteralValueFingerprint(
accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL,
returnType = "V",
literalSupplier = { AccessibilityOfflineButtonSync }
)
)

View File

@ -0,0 +1,32 @@
package app.revanced.patches.youtube.general.downloads.fingerprints
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.MethodFingerprint
import app.revanced.patches.youtube.general.downloads.fingerprints.DownloadPlaylistButtonOnClickFingerprint.indexOfPlaylistDownloadActionInvokeInstruction
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.Method
import com.android.tools.smali.dexlib2.iface.reference.MethodReference
internal object DownloadPlaylistButtonOnClickFingerprint : MethodFingerprint(
accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL,
returnType = "V",
opcodes = listOf(Opcode.INVOKE_VIRTUAL_RANGE),
customFingerprint = { methodDef, _ ->
indexOfPlaylistDownloadActionInvokeInstruction(methodDef) >= 0
}
) {
fun indexOfPlaylistDownloadActionInvokeInstruction(methodDef: Method) =
methodDef.indexOfFirstInstruction {
opcode == Opcode.INVOKE_VIRTUAL &&
getReference<MethodReference>()?.parameterTypes ==
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.misc.downloadactions.fingerprints
package app.revanced.patches.youtube.general.downloads.fingerprints
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.MethodFingerprint

View File

@ -1,9 +1,12 @@
package app.revanced.patches.youtube.misc.downloadactions.fingerprints
package app.revanced.patches.youtube.general.downloads.fingerprints
import app.revanced.patcher.fingerprint.MethodFingerprint
import com.android.tools.smali.dexlib2.Opcode
object SetPlaylistDownloadButtonVisibilityFingerprint : MethodFingerprint(
/**
* Resolves using class found in [AccessibilityOfflineButtonSyncFingerprint].
*/
internal object SetPlaylistDownloadButtonVisibilityFingerprint : MethodFingerprint(
returnType = "V",
opcodes = listOf(
Opcode.INVOKE_VIRTUAL,

View File

@ -1,174 +0,0 @@
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

@ -1,29 +0,0 @@
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

@ -5,31 +5,24 @@ import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWith
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.PlayerButtonConstructorFingerprint
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.*
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 com.android.tools.smali.dexlib2.Opcode
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
import com.android.tools.smali.dexlib2.iface.reference.MethodReference
@Patch(
dependencies = [
MainActivityResolvePatch::class,
SharedResourceIdPatch::class,
VideoInformationPatch::class
]
)
object OverlayButtonsBytecodePatch : BytecodePatch(
setOf(
PlayerButtonConstructorFingerprint
)
setOf(PlayerButtonConstructorFingerprint)
) {
private const val INTEGRATIONS_ALWAYS_REPEAT_CLASS_DESCRIPTOR =
"$UTILS_PATH/AlwaysRepeatPatch;"

View File

@ -6,6 +6,7 @@ import app.revanced.patcher.patch.options.PatchOption.PatchExtensions.stringPatc
import app.revanced.patches.youtube.utils.compatibility.Constants.COMPATIBLE_PACKAGE
import app.revanced.patches.youtube.utils.fix.bottomui.CfBottomUIPatch
import app.revanced.patches.youtube.utils.integrations.Constants.OVERLAY_BUTTONS_PATH
import app.revanced.patches.youtube.utils.pip.PiPStateHookPatch
import app.revanced.patches.youtube.utils.playercontrols.PlayerControlsPatch
import app.revanced.patches.youtube.utils.settings.SettingsPatch
import app.revanced.patches.youtube.video.information.VideoInformationPatch
@ -29,6 +30,7 @@ object OverlayButtonsPatch : BaseResourcePatch(
description = "Adds options to display overlay buttons in the video player.",
dependencies = setOf(
CfBottomUIPatch::class,
PiPStateHookPatch::class,
PlayerControlsPatch::class,
SettingsPatch::class,
OverlayButtonsBytecodePatch::class,

View File

@ -0,0 +1,40 @@
package app.revanced.patches.youtube.utils.pip
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.InstructionExtensions.addInstructions
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.annotation.Patch
import app.revanced.patches.youtube.utils.integrations.Constants.INTEGRATIONS_PATH
import app.revanced.patches.youtube.utils.mainactivity.MainActivityResolvePatch
import app.revanced.patches.youtube.utils.pip.fingerprints.PiPPlaybackFingerprint
import app.revanced.util.resultOrThrow
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
@Patch(
description = "Hooks YouTube to prevent it from switching to PiP when an external downloader is launched.",
dependencies = [MainActivityResolvePatch::class]
)
object PiPStateHookPatch : BytecodePatch(
setOf(PiPPlaybackFingerprint)
) {
private const val INTEGRATIONS_VIDEO_UTILS_CLASS_DESCRIPTOR =
"$INTEGRATIONS_PATH/utils/VideoUtils;"
override fun execute(context: BytecodeContext) {
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
"""
)
}
}
}
}

View File

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