From 7a49a850badcce6ce333f9cbe5bdd367cdbeef18 Mon Sep 17 00:00:00 2001 From: inotia00 Date: Thu, 29 Jun 2023 18:15:08 +0900 Subject: [PATCH] fix(music/video-id-hook): patches are broken in the latest versions --- .../videoid/fingerprint/VideoIdFingerprint.kt | 16 -------- .../fingerprint/VideoIdParentFingerprint.kt | 20 ++++++++++ .../music/utils/videoid/patch/VideoIdPatch.kt | 37 ++++++++++++++++--- 3 files changed, 51 insertions(+), 22 deletions(-) delete mode 100644 src/main/kotlin/app/revanced/patches/music/utils/videoid/fingerprint/VideoIdFingerprint.kt create mode 100644 src/main/kotlin/app/revanced/patches/music/utils/videoid/fingerprint/VideoIdParentFingerprint.kt diff --git a/src/main/kotlin/app/revanced/patches/music/utils/videoid/fingerprint/VideoIdFingerprint.kt b/src/main/kotlin/app/revanced/patches/music/utils/videoid/fingerprint/VideoIdFingerprint.kt deleted file mode 100644 index 9335b2be3..000000000 --- a/src/main/kotlin/app/revanced/patches/music/utils/videoid/fingerprint/VideoIdFingerprint.kt +++ /dev/null @@ -1,16 +0,0 @@ -package app.revanced.patches.music.utils.videoid.fingerprint - -import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint -import org.jf.dexlib2.Opcode - -object VideoIdFingerprint : MethodFingerprint( - returnType = "V", - parameters = listOf("L"), - opcodes = listOf( - Opcode.INVOKE_INTERFACE, - Opcode.MOVE_RESULT_OBJECT, - Opcode.INVOKE_INTERFACE, - Opcode.MOVE_RESULT_OBJECT - ), - customFingerprint = { methodDef, _ -> methodDef.definingClass.endsWith("SubtitlesOverlayPresenter;") && methodDef.name == "handleVideoStageEvent" } -) diff --git a/src/main/kotlin/app/revanced/patches/music/utils/videoid/fingerprint/VideoIdParentFingerprint.kt b/src/main/kotlin/app/revanced/patches/music/utils/videoid/fingerprint/VideoIdParentFingerprint.kt new file mode 100644 index 000000000..5709259c0 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/music/utils/videoid/fingerprint/VideoIdParentFingerprint.kt @@ -0,0 +1,20 @@ +package app.revanced.patches.music.utils.videoid.fingerprint + +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 VideoIdParentFingerprint : MethodFingerprint( + returnType = "V", + parameters = listOf(), + accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL, + opcodes = listOf( + Opcode.INVOKE_SUPER, + Opcode.IGET_OBJECT, + Opcode.CONST_4, + Opcode.IPUT_OBJECT, + Opcode.IGET_OBJECT + ), + customFingerprint = { methodDef, _ -> methodDef.definingClass.endsWith("WatchFragment;") && methodDef.name == "onDestroyView" } +) 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 index cedda977b..c2020e013 100644 --- 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 @@ -12,27 +12,52 @@ import app.revanced.patcher.patch.PatchResult import app.revanced.patcher.patch.PatchResultSuccess import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod import app.revanced.patches.music.utils.annotations.MusicCompatibility -import app.revanced.patches.music.utils.videoid.fingerprint.VideoIdFingerprint +import app.revanced.patches.music.utils.videoid.fingerprint.VideoIdParentFingerprint import app.revanced.util.integrations.Constants.MUSIC_UTILS_PATH +import org.jf.dexlib2.Opcode import org.jf.dexlib2.iface.instruction.OneRegisterInstruction +import org.jf.dexlib2.iface.instruction.ReferenceInstruction +import org.jf.dexlib2.iface.reference.FieldReference @Name("video-id-hook") @Description("Hook to detect when the video id changes.") @MusicCompatibility @Version("0.0.1") class VideoIdPatch : BytecodePatch( - listOf(VideoIdFingerprint) + listOf(VideoIdParentFingerprint) ) { override fun execute(context: BytecodeContext): PatchResult { - VideoIdFingerprint.result?.let { + VideoIdParentFingerprint.result?.let { it.mutableMethod.apply { - insertIndex = it.scanResult.patternScanResult!!.endIndex - insertMethod = this + 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" + } + } + } ?: return VideoIdParentFingerprint.toErrorResult() + + 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 - } ?: return VideoIdFingerprint.toErrorResult() + } injectCall("$INTEGRATIONS_CLASS_DESCRIPTOR->setVideoId(Ljava/lang/String;)V")