From 2d132125116144657d3beada6bf6fc6a27e239bc Mon Sep 17 00:00:00 2001 From: inotia00 Date: Tue, 20 Jun 2023 17:24:44 +0900 Subject: [PATCH] feat(youtube): add `custom-speed-overlay` patch --- .../SpeedOverlayHookFingerprint.kt | 16 ++++ .../YouTubeTextViewFingerprint.kt | 10 +++ .../patch/CustomSpeedOverlayPatch.kt | 85 +++++++++++++++++++ .../youtube/speed/host/values/arrays.xml | 25 ------ 4 files changed, 111 insertions(+), 25 deletions(-) create mode 100644 src/main/kotlin/app/revanced/patches/youtube/player/customspeedoverlay/fingerprints/SpeedOverlayHookFingerprint.kt create mode 100644 src/main/kotlin/app/revanced/patches/youtube/player/customspeedoverlay/fingerprints/YouTubeTextViewFingerprint.kt create mode 100644 src/main/kotlin/app/revanced/patches/youtube/player/customspeedoverlay/patch/CustomSpeedOverlayPatch.kt delete mode 100644 src/main/resources/youtube/speed/host/values/arrays.xml diff --git a/src/main/kotlin/app/revanced/patches/youtube/player/customspeedoverlay/fingerprints/SpeedOverlayHookFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/player/customspeedoverlay/fingerprints/SpeedOverlayHookFingerprint.kt new file mode 100644 index 000000000..0506c8c2f --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/player/customspeedoverlay/fingerprints/SpeedOverlayHookFingerprint.kt @@ -0,0 +1,16 @@ +package app.revanced.patches.youtube.player.customspeedoverlay.fingerprints + +import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint +import org.jf.dexlib2.Opcode + +object SpeedOverlayHookFingerprint : MethodFingerprint( + returnType = "V", + opcodes = listOf( + Opcode.CMPL_FLOAT, + Opcode.IF_GEZ, + Opcode.IGET_OBJECT, + Opcode.INVOKE_VIRTUAL + ), + strings = listOf("Failed to easy seek haptics vibrate."), + customFingerprint = { it, _ -> it.name == "run" } +) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/youtube/player/customspeedoverlay/fingerprints/YouTubeTextViewFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/player/customspeedoverlay/fingerprints/YouTubeTextViewFingerprint.kt new file mode 100644 index 000000000..04a036f8e --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/player/customspeedoverlay/fingerprints/YouTubeTextViewFingerprint.kt @@ -0,0 +1,10 @@ +package app.revanced.patches.youtube.player.customspeedoverlay.fingerprints + +import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint +import org.jf.dexlib2.Opcode + +object YouTubeTextViewFingerprint : MethodFingerprint( + returnType = "V", + opcodes = listOf(Opcode.INVOKE_SUPER), + customFingerprint = { it, _ -> it.definingClass.endsWith("YouTubeTextView;") && it.name == "setText" } +) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/youtube/player/customspeedoverlay/patch/CustomSpeedOverlayPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/player/customspeedoverlay/patch/CustomSpeedOverlayPatch.kt new file mode 100644 index 000000000..1c34ca9cc --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/player/customspeedoverlay/patch/CustomSpeedOverlayPatch.kt @@ -0,0 +1,85 @@ +package app.revanced.patches.youtube.player.customspeedoverlay.patch + +import app.revanced.extensions.toErrorResult +import app.revanced.patcher.annotation.Description +import app.revanced.patcher.annotation.Name +import app.revanced.patcher.annotation.Version +import app.revanced.patcher.data.BytecodeContext +import app.revanced.patcher.extensions.InstructionExtensions.addInstructions +import app.revanced.patcher.extensions.InstructionExtensions.getInstruction +import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction +import app.revanced.patcher.patch.BytecodePatch +import app.revanced.patcher.patch.PatchResult +import app.revanced.patcher.patch.PatchResultSuccess +import app.revanced.patcher.patch.annotations.DependsOn +import app.revanced.patcher.patch.annotations.Patch +import app.revanced.patches.shared.annotation.YouTubeCompatibility +import app.revanced.patches.youtube.player.customspeedoverlay.fingerprints.SpeedOverlayHookFingerprint +import app.revanced.patches.youtube.player.customspeedoverlay.fingerprints.YouTubeTextViewFingerprint +import app.revanced.patches.youtube.utils.settings.resource.patch.SettingsPatch +import app.revanced.util.integrations.Constants.PLAYER +import org.jf.dexlib2.iface.instruction.ReferenceInstruction +import org.jf.dexlib2.iface.instruction.formats.Instruction35c + +@Patch +@Name("custom-speed-overlay") +@Description("Customize the video speed that changes when pressing and holding the player.") +@DependsOn([SettingsPatch::class]) +@YouTubeCompatibility +@Version("0.0.1") +class CustomSpeedOverlayPatch : BytecodePatch( + listOf( + SpeedOverlayHookFingerprint, + YouTubeTextViewFingerprint + ) +) { + override fun execute(context: BytecodeContext): PatchResult { + + SpeedOverlayHookFingerprint.result?.let { + it.mutableMethod.apply { + val insertIndex = it.scanResult.patternScanResult!!.endIndex + val insertRegister = getInstruction(insertIndex).registerD + + addInstructions( + insertIndex, """ + invoke-static {v$insertRegister}, $PLAYER->customSpeedOverlay(F)F + move-result v$insertRegister + """ + ) + } + } ?: return SpeedOverlayHookFingerprint.toErrorResult() + + YouTubeTextViewFingerprint.result?.let { + it.mutableMethod.apply { + val targetIndex = it.scanResult.patternScanResult!!.startIndex + val targetInstruction = getInstruction(targetIndex) + val targetReference = getInstruction(targetIndex).reference + + replaceInstruction( + targetIndex, + "invoke-static {v${targetInstruction.registerC}, v${targetInstruction.registerD}}, $PLAYER->customSpeedOverlay(Landroid/widget/TextView;Ljava/lang/CharSequence;)Ljava/lang/CharSequence;" + ) + addInstructions( + targetIndex + 1, """ + move-result-object v${targetInstruction.registerD} + invoke-super {v${targetInstruction.registerC}, v${targetInstruction.registerD}, v${targetInstruction.registerE}}, $targetReference + """ + ) + } + } ?: return YouTubeTextViewFingerprint.toErrorResult() + + /** + * Add settings + */ + SettingsPatch.addPreference( + arrayOf( + "PREFERENCE: PLAYER_SETTINGS", + "SETTINGS: CUSTOM_SPEED_OVERLAY" + ) + ) + + SettingsPatch.updatePatchStatus("custom-speed-overlay") + + return PatchResultSuccess() + } +} diff --git a/src/main/resources/youtube/speed/host/values/arrays.xml b/src/main/resources/youtube/speed/host/values/arrays.xml deleted file mode 100644 index d040507d6..000000000 --- a/src/main/resources/youtube/speed/host/values/arrays.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - @string/quality_auto - 0.25x - 0.5x - 0.75x - @string/shorts_speed_control_normal_label - 1.25x - 1.5x - 1.75x - 2.0x - - - -2.0 - 0.25 - 0.5 - 0.75 - 1.0 - 1.25 - 1.5 - 1.75 - 2.0 - -