From 43982a7dfdeb7477690171a008547ae6d630654f Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Tue, 26 Nov 2024 15:46:30 +0400 Subject: [PATCH] refactor: Cleanup, internalize fields that should not have been api --- .../youtube/patches/VideoInformation.java | 20 ++------- .../speed/RememberPlaybackSpeedPatch.java | 8 +--- patches/api/patches.api | 5 +-- .../information/VideoInformationPatch.kt | 44 ++++++++----------- 4 files changed, 26 insertions(+), 51 deletions(-) diff --git a/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/VideoInformation.java b/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/VideoInformation.java index 62377337b2..6df9a90951 100644 --- a/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/VideoInformation.java +++ b/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/VideoInformation.java @@ -1,14 +1,14 @@ package app.revanced.extension.youtube.patches; import androidx.annotation.NonNull; -import app.revanced.extension.youtube.patches.playback.speed.RememberPlaybackSpeedPatch; -import app.revanced.extension.youtube.shared.VideoState; -import app.revanced.extension.shared.Logger; -import app.revanced.extension.shared.Utils; import java.lang.ref.WeakReference; import java.util.Objects; +import app.revanced.extension.shared.Logger; +import app.revanced.extension.shared.Utils; +import app.revanced.extension.youtube.shared.VideoState; + /** * Hooking class for the current playing video. * @noinspection unused @@ -141,18 +141,6 @@ public static void userSelectedPlaybackSpeed(float userSelectedPlaybackSpeed) { playbackSpeed = userSelectedPlaybackSpeed; } - /** - * Overrides the current playback speed. - *

- * Used exclusively by {@link RememberPlaybackSpeedPatch} - */ - public static void overridePlaybackSpeed(float speedOverride) { - if (speedOverride <= 0) throw new IllegalArgumentException("Invalid speed override: " + speedOverride); - - Logger.printDebug(() -> "Overriding playback speed to: " + speedOverride); - playbackSpeed = speedOverride; - } - /** * Injection point. * diff --git a/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/playback/speed/RememberPlaybackSpeedPatch.java b/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/playback/speed/RememberPlaybackSpeedPatch.java index 28623e72b5..a6c86477c1 100644 --- a/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/playback/speed/RememberPlaybackSpeedPatch.java +++ b/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/playback/speed/RememberPlaybackSpeedPatch.java @@ -20,12 +20,8 @@ public final class RememberPlaybackSpeedPatch { * Injection point. */ public static void newVideoStarted(VideoInformation.PlaybackController ignoredPlayerController) { - try { - Logger.printDebug(() -> "newVideoStarted"); - newVideoStarted = true; - } catch (Exception ex) { - Logger.printException(() -> "newVideoStarted failure", ex); - } + Logger.printDebug(() -> "newVideoStarted"); + newVideoStarted = true; } /** diff --git a/patches/api/patches.api b/patches/api/patches.api index 2546145df4..2ba4d9a4e0 100644 --- a/patches/api/patches.api +++ b/patches/api/patches.api @@ -1370,13 +1370,10 @@ public final class app/revanced/patches/youtube/shared/FingerprintsKt { } public final class app/revanced/patches/youtube/video/information/VideoInformationPatchKt { - public static final fun getSetPlaybackSpeedClassFieldReference ()Ljava/lang/String; - public static final fun getSetPlaybackSpeedContainerClassFieldReference ()Ljava/lang/String; - public static final fun getSetPlaybackSpeedMethodReference ()Ljava/lang/String; public static final fun getVideoInformationPatch ()Lapp/revanced/patcher/patch/BytecodePatch; public static final fun userSelectedPlaybackSpeedHook (Ljava/lang/String;Ljava/lang/String;)V + public static final fun videoSpeedChangedHook (Ljava/lang/String;Ljava/lang/String;)V public static final fun videoTimeHook (Ljava/lang/String;Ljava/lang/String;)V - public static final fun videoVideoSpeedChanged (Ljava/lang/String;Ljava/lang/String;)V } public abstract class app/revanced/patches/youtube/video/playerresponse/Hook { diff --git a/patches/src/main/kotlin/app/revanced/patches/youtube/video/information/VideoInformationPatch.kt b/patches/src/main/kotlin/app/revanced/patches/youtube/video/information/VideoInformationPatch.kt index a7b5cfc865..a22934d1ca 100644 --- a/patches/src/main/kotlin/app/revanced/patches/youtube/video/information/VideoInformationPatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/youtube/video/information/VideoInformationPatch.kt @@ -20,13 +20,13 @@ import app.revanced.util.getReference import app.revanced.util.indexOfFirstInstructionOrThrow import com.android.tools.smali.dexlib2.AccessFlags import com.android.tools.smali.dexlib2.Opcode -import com.android.tools.smali.dexlib2.builder.BuilderInstruction import com.android.tools.smali.dexlib2.builder.MutableMethodImplementation import com.android.tools.smali.dexlib2.iface.Method 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.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 import com.android.tools.smali.dexlib2.immutable.ImmutableMethod import com.android.tools.smali.dexlib2.immutable.ImmutableMethodParameter @@ -62,11 +62,11 @@ private lateinit var setPlaybackSpeedMethod: MutableMethod private var setPlaybackSpeedMethodIndex = -1 // Used by other patches. -lateinit var setPlaybackSpeedContainerClassFieldReference: String +internal lateinit var setPlaybackSpeedContainerClassFieldReference: FieldReference private set -lateinit var setPlaybackSpeedClassFieldReference: String +internal lateinit var setPlaybackSpeedClassFieldReference: FieldReference private set -lateinit var setPlaybackSpeedMethodReference: String +internal lateinit var setPlaybackSpeedMethodReference: MethodReference private set val videoInformationPatch = bytecodePatch( @@ -168,29 +168,27 @@ val videoInformationPatch = bytecodePatch( videoTimeHook(EXTENSION_CLASS_DESCRIPTOR, "setVideoTime") /* - * Hook the user playback speed selection + * Hook the user playback speed selection. */ onPlaybackSpeedItemClickFingerprint.method.apply { - val speedSelectionMethodInstructions = implementation!!.instructions - val speedSelectionValueInstructionIndex = speedSelectionMethodInstructions.indexOfFirst { - it.opcode == Opcode.IGET - } + val speedSelectionValueInstructionIndex = indexOfFirstInstructionOrThrow(Opcode.IGET) + legacySpeedSelectionInsertMethod = this legacySpeedSelectionInsertIndex = speedSelectionValueInstructionIndex + 1 legacySpeedSelectionValueRegister = getInstruction(speedSelectionValueInstructionIndex).registerA - val setPlaybackSpeedClassReference = + setPlaybackSpeedMethodReference = getInstruction(speedSelectionValueInstructionIndex + 2).reference as MethodReference - setPlaybackSpeedMethod = proxy(classes.first { it.type == setPlaybackSpeedClassReference.definingClass }) - .mutableClass.methods.first { it.name == setPlaybackSpeedClassReference.name } - setPlaybackSpeedMethodIndex = 0 - - setPlaybackSpeedMethodReference = setPlaybackSpeedClassReference.toString() setPlaybackSpeedClassFieldReference = - getInstruction(speedSelectionValueInstructionIndex + 1).reference.toString() + getInstruction(speedSelectionValueInstructionIndex + 1).reference as FieldReference setPlaybackSpeedContainerClassFieldReference = - getReference(speedSelectionMethodInstructions, -1, Opcode.IF_EQZ) + getInstruction(indexOfFirstInstructionOrThrow(Opcode.IF_EQZ) - 1).reference as FieldReference + + setPlaybackSpeedMethod = + proxy(classes.first { it.type == setPlaybackSpeedMethodReference.definingClass }) + .mutableClass.methods.first { it.name == setPlaybackSpeedMethodReference.name } + setPlaybackSpeedMethodIndex = 0 } // Handle new playback speed menu. @@ -204,7 +202,7 @@ val videoInformationPatch = bytecodePatch( speedSelectionValueRegister = getInstruction(index).registerA } - videoVideoSpeedChanged(EXTENSION_CLASS_DESCRIPTOR, "videoSpeedChanged") + videoSpeedChangedHook(EXTENSION_CLASS_DESCRIPTOR, "videoSpeedChanged") userSelectedPlaybackSpeedHook(EXTENSION_CLASS_DESCRIPTOR, "userSelectedPlaybackSpeed") } } @@ -306,9 +304,9 @@ fun videoTimeHook(targetMethodClass: String, targetMethodName: String) = ) /** - * Hook when the video speed is changed, either by the user or any other reason. + * Hook when the video speed is changed for any reason _except when the user manually selects a new speed_. */ -fun videoVideoSpeedChanged(targetMethodClass: String, targetMethodName: String) = +fun videoSpeedChangedHook(targetMethodClass: String, targetMethodName: String) = setPlaybackSpeedMethod.addInstruction( setPlaybackSpeedMethodIndex++, "invoke-static { p1 }, $targetMethodClass->$targetMethodName(F)V" @@ -327,8 +325,4 @@ fun userSelectedPlaybackSpeedHook(targetMethodClass: String, targetMethodName: S speedSelectionInsertIndex++, "invoke-static { v$speedSelectionValueRegister }, $targetMethodClass->$targetMethodName(F)V", ) -} - -private fun getReference(instructions: List, offset: Int, opcode: Opcode) = - (instructions[instructions.indexOfFirst { it.opcode == opcode } + offset] as ReferenceInstruction) - .reference.toString() +} \ No newline at end of file