From 37d416735a26c95bd8bb5eb19c6ba4eb5fe7a7ab Mon Sep 17 00:00:00 2001 From: inotia00 Date: Mon, 18 Sep 2023 00:44:34 +0900 Subject: [PATCH] feat(music/video-information): integrate `video-id` patch --- .../patch/DownloadButtonHookPatch.kt | 4 +- .../replace/patch/ReplaceDismissQueuePatch.kt | 4 +- .../misc/quality/patch/VideoQualityPatch.kt | 6 +- .../ReturnYouTubeDislikeBytecodePatch.kt | 6 +- .../patch/SponsorBlockBytecodePatch.kt | 6 +- .../music/utils/videoid/patch/VideoIdPatch.kt | 79 --------------- ...erControllerSetTimeReferenceFingerprint.kt | 2 +- .../fingerprints/PlayerInitFingerprint.kt | 2 +- .../fingerprints/SeekFingerprint.kt | 2 +- .../fingerprints}/VideoIdParentFingerprint.kt | 2 +- .../fingerprints/VideoLengthFingerprint.kt | 2 +- .../patch/VideoInformationPatch.kt | 96 +++++++++++++++---- 12 files changed, 94 insertions(+), 117 deletions(-) delete mode 100644 src/main/kotlin/app/revanced/patches/music/utils/videoid/patch/VideoIdPatch.kt rename src/main/kotlin/app/revanced/patches/music/{utils/videoinformation => video/information}/fingerprints/PlayerControllerSetTimeReferenceFingerprint.kt (84%) rename src/main/kotlin/app/revanced/patches/music/{utils/videoinformation => video/information}/fingerprints/PlayerInitFingerprint.kt (74%) rename src/main/kotlin/app/revanced/patches/music/{utils/videoinformation => video/information}/fingerprints/SeekFingerprint.kt (70%) rename src/main/kotlin/app/revanced/patches/music/{utils/videoid/fingerprint => video/information/fingerprints}/VideoIdParentFingerprint.kt (91%) rename src/main/kotlin/app/revanced/patches/music/{utils/videoinformation => video/information}/fingerprints/VideoLengthFingerprint.kt (86%) rename src/main/kotlin/app/revanced/patches/music/{utils/videoinformation => video/information}/patch/VideoInformationPatch.kt (71%) diff --git a/src/main/kotlin/app/revanced/patches/music/buttoncontainer/downloadbuttonhook/patch/DownloadButtonHookPatch.kt b/src/main/kotlin/app/revanced/patches/music/buttoncontainer/downloadbuttonhook/patch/DownloadButtonHookPatch.kt index 2c91082f8..14abb4242 100644 --- a/src/main/kotlin/app/revanced/patches/music/buttoncontainer/downloadbuttonhook/patch/DownloadButtonHookPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/buttoncontainer/downloadbuttonhook/patch/DownloadButtonHookPatch.kt @@ -10,7 +10,7 @@ import app.revanced.patches.music.utils.annotations.MusicCompatibility import app.revanced.patches.music.utils.buttoncontainerhook.patch.ButtonContainerHookPatch import app.revanced.patches.music.utils.intenthook.patch.IntentHookPatch import app.revanced.patches.music.utils.settings.resource.patch.SettingsPatch -import app.revanced.patches.music.utils.videoid.patch.VideoIdPatch +import app.revanced.patches.music.video.information.patch.VideoInformationPatch import app.revanced.util.enum.CategoryType @Patch @@ -21,7 +21,7 @@ import app.revanced.util.enum.CategoryType ButtonContainerHookPatch::class, IntentHookPatch::class, SettingsPatch::class, - VideoIdPatch::class + VideoInformationPatch::class ] ) @MusicCompatibility diff --git a/src/main/kotlin/app/revanced/patches/music/flyoutpanel/replace/patch/ReplaceDismissQueuePatch.kt b/src/main/kotlin/app/revanced/patches/music/flyoutpanel/replace/patch/ReplaceDismissQueuePatch.kt index ab637e015..9c0f0245f 100644 --- a/src/main/kotlin/app/revanced/patches/music/flyoutpanel/replace/patch/ReplaceDismissQueuePatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/flyoutpanel/replace/patch/ReplaceDismissQueuePatch.kt @@ -14,7 +14,7 @@ import app.revanced.patches.music.utils.annotations.MusicCompatibility import app.revanced.patches.music.utils.fingerprints.MenuItemFingerprint import app.revanced.patches.music.utils.flyoutbutton.patch.FlyoutButtonItemResourcePatch import app.revanced.patches.music.utils.settings.resource.patch.SettingsPatch -import app.revanced.patches.music.utils.videoid.patch.VideoIdPatch +import app.revanced.patches.music.video.information.patch.VideoInformationPatch import app.revanced.util.enum.CategoryType import app.revanced.util.integrations.Constants.MUSIC_FLYOUT import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction @@ -26,7 +26,7 @@ import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction [ FlyoutButtonItemResourcePatch::class, SettingsPatch::class, - VideoIdPatch::class + VideoInformationPatch::class ] ) @MusicCompatibility diff --git a/src/main/kotlin/app/revanced/patches/music/misc/quality/patch/VideoQualityPatch.kt b/src/main/kotlin/app/revanced/patches/music/misc/quality/patch/VideoQualityPatch.kt index 803f8f9b2..596077fda 100644 --- a/src/main/kotlin/app/revanced/patches/music/misc/quality/patch/VideoQualityPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/misc/quality/patch/VideoQualityPatch.kt @@ -18,7 +18,7 @@ import app.revanced.patches.music.misc.quality.fingerprints.UserQualityChangeFin import app.revanced.patches.music.utils.annotations.MusicCompatibility import app.revanced.patches.music.utils.resourceid.patch.SharedResourceIdPatch import app.revanced.patches.music.utils.settings.resource.patch.SettingsPatch -import app.revanced.patches.music.utils.videoid.patch.VideoIdPatch +import app.revanced.patches.music.video.information.patch.VideoInformationPatch import app.revanced.util.enum.CategoryType import app.revanced.util.integrations.Constants.MUSIC_MISC_PATH import com.android.tools.smali.dexlib2.Opcode @@ -35,7 +35,7 @@ import com.android.tools.smali.dexlib2.iface.reference.Reference [ SettingsPatch::class, SharedResourceIdPatch::class, - VideoIdPatch::class + VideoInformationPatch::class ] ) @MusicCompatibility @@ -112,7 +112,7 @@ class VideoQualityPatch : BytecodePatch( ) ?: throw MusicVideoQualitySettingsFingerprint.exception } ?: throw MusicVideoQualitySettingsParentFingerprint.exception - VideoIdPatch.injectCall("$INTEGRATIONS_VIDEO_QUALITY_CLASS_DESCRIPTOR->newVideoStarted(Ljava/lang/String;)V") + VideoInformationPatch.injectCall("$INTEGRATIONS_VIDEO_QUALITY_CLASS_DESCRIPTOR->newVideoStarted(Ljava/lang/String;)V") SettingsPatch.addMusicPreference( CategoryType.MISC, "revanced_enable_save_video_quality", diff --git a/src/main/kotlin/app/revanced/patches/music/utils/returnyoutubedislike/bytecode/patch/ReturnYouTubeDislikeBytecodePatch.kt b/src/main/kotlin/app/revanced/patches/music/utils/returnyoutubedislike/bytecode/patch/ReturnYouTubeDislikeBytecodePatch.kt index 084ebbfe4..c3db64650 100644 --- a/src/main/kotlin/app/revanced/patches/music/utils/returnyoutubedislike/bytecode/patch/ReturnYouTubeDislikeBytecodePatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/utils/returnyoutubedislike/bytecode/patch/ReturnYouTubeDislikeBytecodePatch.kt @@ -13,7 +13,7 @@ import app.revanced.patches.music.utils.returnyoutubedislike.bytecode.fingerprin import app.revanced.patches.music.utils.returnyoutubedislike.bytecode.fingerprints.LikeFingerprint import app.revanced.patches.music.utils.returnyoutubedislike.bytecode.fingerprints.RemoveLikeFingerprint import app.revanced.patches.music.utils.returnyoutubedislike.bytecode.fingerprints.TextComponentFingerprint -import app.revanced.patches.music.utils.videoid.patch.VideoIdPatch +import app.revanced.patches.music.video.information.patch.VideoInformationPatch import app.revanced.util.integrations.Constants.MUSIC_UTILS_PATH import com.android.tools.smali.dexlib2.Opcode import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction @@ -22,7 +22,7 @@ import com.android.tools.smali.dexlib2.iface.instruction.formats.Instruction35c @DependsOn( [ SharedResourceIdPatch::class, - VideoIdPatch::class + VideoInformationPatch::class ] ) class ReturnYouTubeDislikeBytecodePatch : BytecodePatch( @@ -77,7 +77,7 @@ class ReturnYouTubeDislikeBytecodePatch : BytecodePatch( } } ?: throw TextComponentFingerprint.exception - VideoIdPatch.injectCall("$INTEGRATIONS_RYD_CLASS_DESCRIPTOR->newVideoLoaded(Ljava/lang/String;)V") + VideoInformationPatch.injectCall("$INTEGRATIONS_RYD_CLASS_DESCRIPTOR->newVideoLoaded(Ljava/lang/String;)V") } diff --git a/src/main/kotlin/app/revanced/patches/music/utils/sponsorblock/bytecode/patch/SponsorBlockBytecodePatch.kt b/src/main/kotlin/app/revanced/patches/music/utils/sponsorblock/bytecode/patch/SponsorBlockBytecodePatch.kt index 64dc0448c..a78f15dff 100644 --- a/src/main/kotlin/app/revanced/patches/music/utils/sponsorblock/bytecode/patch/SponsorBlockBytecodePatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/utils/sponsorblock/bytecode/patch/SponsorBlockBytecodePatch.kt @@ -13,7 +13,7 @@ import app.revanced.patches.music.utils.resourceid.patch.SharedResourceIdPatch import app.revanced.patches.music.utils.sponsorblock.bytecode.fingerprints.MusicPlaybackControlsTimeBarDrawFingerprint import app.revanced.patches.music.utils.sponsorblock.bytecode.fingerprints.MusicPlaybackControlsTimeBarOnMeasureFingerprint import app.revanced.patches.music.utils.sponsorblock.bytecode.fingerprints.SeekbarOnDrawFingerprint -import app.revanced.patches.music.utils.videoid.patch.VideoIdPatch +import app.revanced.patches.music.video.information.patch.VideoInformationPatch import app.revanced.patches.music.utils.videoinformation.patch.VideoInformationPatch import com.android.tools.smali.dexlib2.Opcode import com.android.tools.smali.dexlib2.builder.instruction.BuilderInstruction3rc @@ -26,7 +26,7 @@ import com.android.tools.smali.dexlib2.iface.reference.MethodReference @DependsOn( [ SharedResourceIdPatch::class, - VideoIdPatch::class, + VideoInformationPatch::class, VideoInformationPatch::class ] ) @@ -166,7 +166,7 @@ class SponsorBlockBytecodePatch : BytecodePatch( /** * Set current video id */ - VideoIdPatch.injectCall("$INTEGRATIONS_SEGMENT_PLAYBACK_CONTROLLER_CLASS_DESCRIPTOR->setCurrentVideoId(Ljava/lang/String;)V") + VideoInformationPatch.injectCall("$INTEGRATIONS_SEGMENT_PLAYBACK_CONTROLLER_CLASS_DESCRIPTOR->setCurrentVideoId(Ljava/lang/String;)V") } private companion object { diff --git a/src/main/kotlin/app/revanced/patches/music/utils/videoid/patch/VideoIdPatch.kt b/src/main/kotlin/app/revanced/patches/music/utils/videoid/patch/VideoIdPatch.kt deleted file mode 100644 index c46aa4446..000000000 --- a/src/main/kotlin/app/revanced/patches/music/utils/videoid/patch/VideoIdPatch.kt +++ /dev/null @@ -1,79 +0,0 @@ -package app.revanced.patches.music.utils.videoid.patch - -import app.revanced.extensions.exception -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.util.proxy.mutableTypes.MutableMethod -import app.revanced.patches.music.utils.videoid.fingerprint.VideoIdParentFingerprint -import app.revanced.util.integrations.Constants.MUSIC_UTILS_PATH -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.reference.FieldReference - -class VideoIdPatch : BytecodePatch( - listOf(VideoIdParentFingerprint) -) { - override fun execute(context: BytecodeContext) { - - VideoIdParentFingerprint.result?.let { - it.mutableMethod.apply { - val targetIndex = it.scanResult.patternScanResult!!.endIndex - - val targetReference = getInstruction(targetIndex).reference - val targetClass = (targetReference as FieldReference).type - - insertMethod = context - .findClass(targetClass)!! - .mutableClass.methods.first { method -> - method.name == "handleVideoStageEvent" - } - } - } ?: throw VideoIdParentFingerprint.exception - - insertMethod.apply { - for (index in implementation!!.instructions.size - 1 downTo 0) { - if (getInstruction(index).opcode != Opcode.INVOKE_INTERFACE) continue - - val targetReference = getInstruction(index).reference - - if (!targetReference.toString().endsWith("Ljava/lang/String;")) continue - - insertIndex = index + 1 - videoIdRegister = getInstruction(insertIndex).registerA - - break - } - offset++ // offset so setVideoId is called before any injected call - } - - injectCall("$INTEGRATIONS_CLASS_DESCRIPTOR->setVideoId(Ljava/lang/String;)V") - - } - - companion object { - const val INTEGRATIONS_CLASS_DESCRIPTOR = "$MUSIC_UTILS_PATH/VideoInformation;" - - private var offset = 0 - - private var insertIndex: Int = 0 - private var videoIdRegister: Int = 0 - private lateinit var insertMethod: MutableMethod - - /** - * Adds an invoke-static instruction, called with the new id when the video changes - * @param methodDescriptor which method to call. Params have to be `Ljava/lang/String;` - */ - fun injectCall( - methodDescriptor: String - ) { - insertMethod.addInstructions( - insertIndex + offset, // move-result-object offset - "invoke-static {v$videoIdRegister}, $methodDescriptor" - ) - } - } -} - diff --git a/src/main/kotlin/app/revanced/patches/music/utils/videoinformation/fingerprints/PlayerControllerSetTimeReferenceFingerprint.kt b/src/main/kotlin/app/revanced/patches/music/video/information/fingerprints/PlayerControllerSetTimeReferenceFingerprint.kt similarity index 84% rename from src/main/kotlin/app/revanced/patches/music/utils/videoinformation/fingerprints/PlayerControllerSetTimeReferenceFingerprint.kt rename to src/main/kotlin/app/revanced/patches/music/video/information/fingerprints/PlayerControllerSetTimeReferenceFingerprint.kt index 0df55cdba..4dfd2d6aa 100644 --- a/src/main/kotlin/app/revanced/patches/music/utils/videoinformation/fingerprints/PlayerControllerSetTimeReferenceFingerprint.kt +++ b/src/main/kotlin/app/revanced/patches/music/video/information/fingerprints/PlayerControllerSetTimeReferenceFingerprint.kt @@ -1,4 +1,4 @@ -package app.revanced.patches.music.utils.videoinformation.fingerprints +package app.revanced.patches.music.video.information.fingerprints import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint import com.android.tools.smali.dexlib2.Opcode diff --git a/src/main/kotlin/app/revanced/patches/music/utils/videoinformation/fingerprints/PlayerInitFingerprint.kt b/src/main/kotlin/app/revanced/patches/music/video/information/fingerprints/PlayerInitFingerprint.kt similarity index 74% rename from src/main/kotlin/app/revanced/patches/music/utils/videoinformation/fingerprints/PlayerInitFingerprint.kt rename to src/main/kotlin/app/revanced/patches/music/video/information/fingerprints/PlayerInitFingerprint.kt index a28517523..254b4e899 100644 --- a/src/main/kotlin/app/revanced/patches/music/utils/videoinformation/fingerprints/PlayerInitFingerprint.kt +++ b/src/main/kotlin/app/revanced/patches/music/video/information/fingerprints/PlayerInitFingerprint.kt @@ -1,4 +1,4 @@ -package app.revanced.patches.music.utils.videoinformation.fingerprints +package app.revanced.patches.music.video.information.fingerprints import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint diff --git a/src/main/kotlin/app/revanced/patches/music/utils/videoinformation/fingerprints/SeekFingerprint.kt b/src/main/kotlin/app/revanced/patches/music/video/information/fingerprints/SeekFingerprint.kt similarity index 70% rename from src/main/kotlin/app/revanced/patches/music/utils/videoinformation/fingerprints/SeekFingerprint.kt rename to src/main/kotlin/app/revanced/patches/music/video/information/fingerprints/SeekFingerprint.kt index d1c0856ed..a7974ce20 100644 --- a/src/main/kotlin/app/revanced/patches/music/utils/videoinformation/fingerprints/SeekFingerprint.kt +++ b/src/main/kotlin/app/revanced/patches/music/video/information/fingerprints/SeekFingerprint.kt @@ -1,4 +1,4 @@ -package app.revanced.patches.music.utils.videoinformation.fingerprints +package app.revanced.patches.music.video.information.fingerprints import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint diff --git a/src/main/kotlin/app/revanced/patches/music/utils/videoid/fingerprint/VideoIdParentFingerprint.kt b/src/main/kotlin/app/revanced/patches/music/video/information/fingerprints/VideoIdParentFingerprint.kt similarity index 91% rename from src/main/kotlin/app/revanced/patches/music/utils/videoid/fingerprint/VideoIdParentFingerprint.kt rename to src/main/kotlin/app/revanced/patches/music/video/information/fingerprints/VideoIdParentFingerprint.kt index df08030cd..5ade692b2 100644 --- a/src/main/kotlin/app/revanced/patches/music/utils/videoid/fingerprint/VideoIdParentFingerprint.kt +++ b/src/main/kotlin/app/revanced/patches/music/video/information/fingerprints/VideoIdParentFingerprint.kt @@ -1,4 +1,4 @@ -package app.revanced.patches.music.utils.videoid.fingerprint +package app.revanced.patches.music.video.information.fingerprints import app.revanced.patcher.extensions.or import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint diff --git a/src/main/kotlin/app/revanced/patches/music/utils/videoinformation/fingerprints/VideoLengthFingerprint.kt b/src/main/kotlin/app/revanced/patches/music/video/information/fingerprints/VideoLengthFingerprint.kt similarity index 86% rename from src/main/kotlin/app/revanced/patches/music/utils/videoinformation/fingerprints/VideoLengthFingerprint.kt rename to src/main/kotlin/app/revanced/patches/music/video/information/fingerprints/VideoLengthFingerprint.kt index dc2a322f4..ff89b8705 100644 --- a/src/main/kotlin/app/revanced/patches/music/utils/videoinformation/fingerprints/VideoLengthFingerprint.kt +++ b/src/main/kotlin/app/revanced/patches/music/video/information/fingerprints/VideoLengthFingerprint.kt @@ -1,4 +1,4 @@ -package app.revanced.patches.music.utils.videoinformation.fingerprints +package app.revanced.patches.music.video.information.fingerprints import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint import com.android.tools.smali.dexlib2.Opcode diff --git a/src/main/kotlin/app/revanced/patches/music/utils/videoinformation/patch/VideoInformationPatch.kt b/src/main/kotlin/app/revanced/patches/music/video/information/patch/VideoInformationPatch.kt similarity index 71% rename from src/main/kotlin/app/revanced/patches/music/utils/videoinformation/patch/VideoInformationPatch.kt rename to src/main/kotlin/app/revanced/patches/music/video/information/patch/VideoInformationPatch.kt index 477d17e6a..601821af8 100644 --- a/src/main/kotlin/app/revanced/patches/music/utils/videoinformation/patch/VideoInformationPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/video/information/patch/VideoInformationPatch.kt @@ -1,8 +1,6 @@ -package app.revanced.patches.music.utils.videoinformation.patch +package app.revanced.patches.music.video.information.patch import app.revanced.extensions.exception -import app.revanced.patcher.annotation.Description -import app.revanced.patcher.annotation.Name import app.revanced.patcher.data.BytecodeContext import app.revanced.patcher.extensions.InstructionExtensions.addInstruction import app.revanced.patcher.extensions.InstructionExtensions.addInstructions @@ -13,14 +11,16 @@ import app.revanced.patcher.patch.BytecodePatch import app.revanced.patcher.patch.annotations.DependsOn import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod.Companion.toMutable -import app.revanced.patches.music.utils.annotations.MusicCompatibility import app.revanced.patches.music.utils.fingerprints.SeekBarConstructorFingerprint import app.revanced.patches.music.utils.resourceid.patch.SharedResourceIdPatch -import app.revanced.patches.music.utils.videoinformation.fingerprints.PlayerControllerSetTimeReferenceFingerprint -import app.revanced.patches.music.utils.videoinformation.fingerprints.PlayerInitFingerprint -import app.revanced.patches.music.utils.videoinformation.fingerprints.SeekFingerprint -import app.revanced.patches.music.utils.videoinformation.fingerprints.VideoLengthFingerprint +import app.revanced.patches.music.video.information.fingerprints.PlayerControllerSetTimeReferenceFingerprint +import app.revanced.patches.music.video.information.fingerprints.PlayerInitFingerprint +import app.revanced.patches.music.video.information.fingerprints.SeekFingerprint +import app.revanced.patches.music.video.information.fingerprints.VideoIdParentFingerprint +import app.revanced.patches.music.video.information.fingerprints.VideoLengthFingerprint +import app.revanced.util.integrations.Constants.MUSIC_VIDEO_PATH import com.android.tools.smali.dexlib2.AccessFlags +import com.android.tools.smali.dexlib2.Opcode import com.android.tools.smali.dexlib2.builder.MutableMethodImplementation import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction @@ -29,19 +29,13 @@ import com.android.tools.smali.dexlib2.immutable.ImmutableMethod import com.android.tools.smali.dexlib2.immutable.ImmutableMethodParameter import com.android.tools.smali.dexlib2.util.MethodUtil -@Name("Video information") -@Description("Hooks YouTube to get information about the current playing video.") -@DependsOn( - [ - SharedResourceIdPatch::class - ] -) -@MusicCompatibility +@DependsOn([SharedResourceIdPatch::class]) class VideoInformationPatch : BytecodePatch( listOf( PlayerControllerSetTimeReferenceFingerprint, PlayerInitFingerprint, SeekBarConstructorFingerprint, + VideoIdParentFingerprint ) ) { override fun execute(context: BytecodeContext) { @@ -80,6 +74,7 @@ class VideoInformationPatch : BytecodePatch( } ?: throw SeekFingerprint.exception } ?: throw PlayerInitFingerprint.exception + /** * Set current video length */ @@ -109,6 +104,7 @@ class VideoInformationPatch : BytecodePatch( } ?: throw VideoLengthFingerprint.exception } ?: throw SeekBarConstructorFingerprint.exception + /** * Set the video time method */ @@ -118,24 +114,84 @@ class VideoInformationPatch : BytecodePatch( .getMethod() as MutableMethod } ?: throw PlayerControllerSetTimeReferenceFingerprint.exception + /** * Set current video time */ videoTimeHook(INTEGRATIONS_CLASS_DESCRIPTOR, "setVideoTime") + + + /** + * Inject call for video id + */ + VideoIdParentFingerprint.result?.let { + it.mutableMethod.apply { + val targetIndex = it.scanResult.patternScanResult!!.endIndex + + val targetReference = getInstruction(targetIndex).reference + val targetClass = (targetReference as FieldReference).type + + videoIdMethod = context + .findClass(targetClass)!! + .mutableClass.methods.first { method -> + method.name == "handleVideoStageEvent" + } + } + } ?: throw VideoIdParentFingerprint.exception + + videoIdMethod.apply { + for (index in implementation!!.instructions.size - 1 downTo 0) { + if (getInstruction(index).opcode != Opcode.INVOKE_INTERFACE) continue + + val targetReference = getInstruction(index).reference + + if (!targetReference.toString().endsWith("Ljava/lang/String;")) continue + + videoIdIndex = index + 1 + videoIdRegister = getInstruction(videoIdIndex).registerA + + break + } + offset++ // offset so setVideoId is called before any injected call + } + + + /** + * Set current video id + */ + injectCall("$INTEGRATIONS_CLASS_DESCRIPTOR->setVideoId(Ljava/lang/String;)V") } companion object { private const val INTEGRATIONS_CLASS_DESCRIPTOR = - "Lapp/revanced/music/patches/utils/VideoInformation;" + "$MUSIC_VIDEO_PATH/VideoInformation;" - private lateinit var playerInitMethod: MutableMethod + private var offset = 0 private var playerInitInsertIndex = 4 - - private lateinit var timeMethod: MutableMethod private var timeInitInsertIndex = 2 + private var videoIdIndex = 0 + + private var videoIdRegister: Int = 0 + + private lateinit var videoIdMethod: MutableMethod + private lateinit var playerInitMethod: MutableMethod + private lateinit var timeMethod: MutableMethod lateinit var rectangleFieldName: String + /** + * Adds an invoke-static instruction, called with the new id when the video changes + * @param methodDescriptor which method to call. Params have to be `Ljava/lang/String;` + */ + internal fun injectCall( + methodDescriptor: String + ) { + videoIdMethod.addInstructions( + videoIdIndex + offset, // move-result-object offset + "invoke-static {v$videoIdRegister}, $methodDescriptor" + ) + } + private fun MutableMethod.insert(insertIndex: Int, register: String, descriptor: String) = addInstruction(insertIndex, "invoke-static { $register }, $descriptor")