From 6cf28cd9496f78fed06b3c0dd10fdd33b3ac9bb5 Mon Sep 17 00:00:00 2001 From: inotia00 Date: Thu, 11 May 2023 19:08:37 +0900 Subject: [PATCH] fix(youtube/default-video-quality): fix default video quality/speed being applied when resuming app --- .../patch/MainstreamVideoIdPatch.kt | 30 +++++++++---------- .../patch/VideoQualityBytecodePatch.kt | 6 ++-- .../bytecode/patch/VideoSpeedBytecodePatch.kt | 7 ++--- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/videoid/mainstream/patch/MainstreamVideoIdPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/videoid/mainstream/patch/MainstreamVideoIdPatch.kt index 5a6d9e2f6..0d4485c55 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/misc/videoid/mainstream/patch/MainstreamVideoIdPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/videoid/mainstream/patch/MainstreamVideoIdPatch.kt @@ -57,7 +57,7 @@ class MainstreamVideoIdPatch : BytecodePatch( playerInitMethod = parentResult.mutableClass.methods.first { MethodUtil.isConstructor(it) } // hook the player controller for use through integrations - onCreateHook(INTEGRATIONS_CLASS_DESCRIPTOR, "playerController_onCreateHook") + onCreateHook(INTEGRATIONS_CLASS_DESCRIPTOR, "initialize") SeekFingerprint.also { it.resolve(context, parentResult.classDef) }.result?.let { val resultMethod = it.method @@ -157,6 +157,9 @@ class MainstreamVideoIdPatch : BytecodePatch( internal var reactReference: String? = null private var offset = 0 + private var playerInitInsertIndex = 4 + private var timeInitInsertIndex = 2 + private var highPrecisionInsertIndex = 0 private var insertIndex: Int = 0 private var videoIdRegister: Int = 0 @@ -178,21 +181,24 @@ class MainstreamVideoIdPatch : BytecodePatch( ) } - private fun MutableMethod.insert(insert: InsertIndex, register: String, descriptor: String) = - addInstruction(insert.index, "invoke-static { $register }, $descriptor") + private fun MutableMethod.insert(insertIndex: Int, register: String, descriptor: String) = + addInstruction(insertIndex, "invoke-static { $register }, $descriptor") - private fun MutableMethod.insertTimeHook(insert: InsertIndex, descriptor: String) = - insert(insert, "p1, p2", descriptor) + private fun MutableMethod.insertTimeHook(insertIndex: Int, descriptor: String) = + insert(insertIndex, "p1, p2", descriptor) /** - * Hook the player controller. + * Hook the player controller. Called when a video is opened or the current video is changed. + * + * Note: This hook is called very early and is called before the video id, video time, video length, + * and many other data fields are set. * * @param targetMethodClass The descriptor for the class to invoke when the player controller is created. * @param targetMethodName The name of the static method to invoke when the player controller is created. */ internal fun onCreateHook(targetMethodClass: String, targetMethodName: String) = playerInitMethod.insert( - InsertIndex.CREATE, + playerInitInsertIndex++, "v0", "$targetMethodClass->$targetMethodName(Ljava/lang/Object;)V" ) @@ -206,7 +212,7 @@ class MainstreamVideoIdPatch : BytecodePatch( */ internal fun videoTimeHook(targetMethodClass: String, targetMethodName: String) = timeMethod.insertTimeHook( - InsertIndex.TIME, + timeInitInsertIndex++, "$targetMethodClass->$targetMethodName(J)V" ) @@ -220,15 +226,9 @@ class MainstreamVideoIdPatch : BytecodePatch( */ internal fun highPrecisionTimeHook(targetMethodClass: String, targetMethodName: String) = highPrecisionTimeMethod.insertTimeHook( - InsertIndex.HIGH_PRECISION_TIME, + highPrecisionInsertIndex++, "$targetMethodClass->$targetMethodName(J)V" ) - - enum class InsertIndex(internal val index: Int) { - CREATE(4), - TIME(2), - HIGH_PRECISION_TIME(0), - } } } diff --git a/src/main/kotlin/app/revanced/patches/youtube/video/quality/bytecode/patch/VideoQualityBytecodePatch.kt b/src/main/kotlin/app/revanced/patches/youtube/video/quality/bytecode/patch/VideoQualityBytecodePatch.kt index a952b245b..205965e9e 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/video/quality/bytecode/patch/VideoQualityBytecodePatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/video/quality/bytecode/patch/VideoQualityBytecodePatch.kt @@ -12,14 +12,14 @@ import app.revanced.patcher.patch.PatchResult import app.revanced.patcher.patch.PatchResultSuccess import app.revanced.patcher.patch.annotations.DependsOn import app.revanced.patches.shared.annotation.YouTubeCompatibility -import app.revanced.patches.youtube.misc.videoid.legacy.patch.LegacyVideoIdPatch +import app.revanced.patches.youtube.misc.videoid.mainstream.patch.MainstreamVideoIdPatch import app.revanced.patches.youtube.video.quality.bytecode.fingerprints.* import app.revanced.util.integrations.Constants.VIDEO_PATH import org.jf.dexlib2.iface.instruction.ReferenceInstruction import org.jf.dexlib2.iface.reference.FieldReference @Name("default-video-quality-bytecode-patch") -@DependsOn([LegacyVideoIdPatch::class]) +@DependsOn([MainstreamVideoIdPatch::class]) @YouTubeCompatibility @Version("0.0.1") class VideoQualityBytecodePatch : BytecodePatch( @@ -62,7 +62,7 @@ class VideoQualityBytecodePatch : BytecodePatch( ) } ?: return VideoQualitySettingsParentFingerprint.toErrorResult() - LegacyVideoIdPatch.injectCall("$INTEGRATIONS_VIDEO_QUALITY_CLASS_DESCRIPTOR->newVideoStarted(Ljava/lang/String;)V") + MainstreamVideoIdPatch.onCreateHook(INTEGRATIONS_VIDEO_QUALITY_CLASS_DESCRIPTOR, "newVideoStarted") return PatchResultSuccess() } diff --git a/src/main/kotlin/app/revanced/patches/youtube/video/speed/bytecode/patch/VideoSpeedBytecodePatch.kt b/src/main/kotlin/app/revanced/patches/youtube/video/speed/bytecode/patch/VideoSpeedBytecodePatch.kt index aada0e655..c29e37528 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/video/speed/bytecode/patch/VideoSpeedBytecodePatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/video/speed/bytecode/patch/VideoSpeedBytecodePatch.kt @@ -13,17 +13,16 @@ import app.revanced.patches.shared.annotation.YouTubeCompatibility import app.revanced.patches.youtube.misc.overridespeed.bytecode.fingerprints.VideoSpeedSettingsFingerprint import app.revanced.patches.youtube.misc.overridespeed.bytecode.patch.OverrideSpeedHookPatch import app.revanced.patches.youtube.misc.resourceid.patch.SharedResourceIdPatch -import app.revanced.patches.youtube.misc.videoid.legacy.patch.LegacyVideoIdPatch +import app.revanced.patches.youtube.misc.videoid.mainstream.patch.MainstreamVideoIdPatch import app.revanced.patches.youtube.video.livestream.patch.LiveStreamPatch -import app.revanced.patches.youtube.video.speed.bytecode.fingerprints.* import app.revanced.util.integrations.Constants.VIDEO_PATH import org.jf.dexlib2.iface.instruction.FiveRegisterInstruction @Name("default-video-speed-bytecode-patch") @DependsOn( [ - LegacyVideoIdPatch::class, LiveStreamPatch::class, + MainstreamVideoIdPatch::class, OverrideSpeedHookPatch::class, SharedResourceIdPatch::class ] @@ -55,7 +54,7 @@ class VideoSpeedBytecodePatch : BytecodePatch( "invoke-static {}, $INTEGRATIONS_VIDEO_SPEED_CLASS_DESCRIPTOR->setDefaultSpeed()V" ) ?: return VideoSpeedSettingsFingerprint.toErrorResult() - LegacyVideoIdPatch.injectCall("$INTEGRATIONS_VIDEO_SPEED_CLASS_DESCRIPTOR->newVideoStarted(Ljava/lang/String;)V") + MainstreamVideoIdPatch.onCreateHook(INTEGRATIONS_VIDEO_SPEED_CLASS_DESCRIPTOR, "newVideoStarted") return PatchResultSuccess() }