From 4eac4dd5a99b987d7c3f23e3a61868cc20118e39 Mon Sep 17 00:00:00 2001 From: inotia00 Date: Sun, 5 Feb 2023 12:39:50 +0900 Subject: [PATCH] refactor: `always-autorepeat` patch --- .../AutoNavInformerFingerprint.kt | 14 ++---- .../fingerprints/AutoRepeatFingerprint.kt | 6 +-- .../AutoRepeatParentFingerprint.kt | 4 +- .../autorepeat/patch/AutoRepeatPatch.kt | 46 ++++++++++--------- 4 files changed, 33 insertions(+), 37 deletions(-) diff --git a/src/main/kotlin/app/revanced/patches/youtube/button/autorepeat/fingerprints/AutoNavInformerFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/button/autorepeat/fingerprints/AutoNavInformerFingerprint.kt index d2cdbcadb..1d273978f 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/button/autorepeat/fingerprints/AutoNavInformerFingerprint.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/button/autorepeat/fingerprints/AutoNavInformerFingerprint.kt @@ -3,18 +3,10 @@ package app.revanced.patches.youtube.button.autorepeat.fingerprints 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 AutoNavInformerFingerprint : MethodFingerprint( - "Z", - AccessFlags.PUBLIC or AccessFlags.FINAL, - opcodes = listOf( - Opcode.IGET_OBJECT, - Opcode.INVOKE_INTERFACE, - Opcode.MOVE_RESULT_OBJECT, - Opcode.CHECK_CAST, - Opcode.INVOKE_VIRTUAL, - Opcode.MOVE_RESULT, - ), + returnType = "Z", + access = AccessFlags.PUBLIC or AccessFlags.FINAL, + parameters = listOf(), customFingerprint = { it.definingClass.endsWith("WillAutonavInformer;") } ) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/youtube/button/autorepeat/fingerprints/AutoRepeatFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/button/autorepeat/fingerprints/AutoRepeatFingerprint.kt index 105932384..a0a2617b2 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/button/autorepeat/fingerprints/AutoRepeatFingerprint.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/button/autorepeat/fingerprints/AutoRepeatFingerprint.kt @@ -5,8 +5,8 @@ import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint import org.jf.dexlib2.AccessFlags object AutoRepeatFingerprint : MethodFingerprint( - "V", - AccessFlags.PUBLIC or AccessFlags.FINAL, - listOf(), + returnType = "V", + access = AccessFlags.PUBLIC or AccessFlags.FINAL, + parameters = listOf(), customFingerprint = { methodDef -> methodDef.implementation!!.instructions.count() == 3 && methodDef.annotations.isEmpty()} ) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/youtube/button/autorepeat/fingerprints/AutoRepeatParentFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/button/autorepeat/fingerprints/AutoRepeatParentFingerprint.kt index 80f230031..a7093eab7 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/button/autorepeat/fingerprints/AutoRepeatParentFingerprint.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/button/autorepeat/fingerprints/AutoRepeatParentFingerprint.kt @@ -5,8 +5,8 @@ import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint import org.jf.dexlib2.AccessFlags object AutoRepeatParentFingerprint : MethodFingerprint( - "V", - AccessFlags.PUBLIC or AccessFlags.FINAL, + returnType = "V", + access = AccessFlags.PUBLIC or AccessFlags.FINAL, strings = listOf( "play() called when the player wasn't loaded.", "play() blocked because Background Playability failed" diff --git a/src/main/kotlin/app/revanced/patches/youtube/button/autorepeat/patch/AutoRepeatPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/button/autorepeat/patch/AutoRepeatPatch.kt index ee5f2aec0..1cc8a75b3 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/button/autorepeat/patch/AutoRepeatPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/button/autorepeat/patch/AutoRepeatPatch.kt @@ -10,12 +10,12 @@ import app.revanced.patcher.patch.BytecodePatch import app.revanced.patcher.patch.PatchResult import app.revanced.patcher.patch.PatchResultSuccess import app.revanced.patcher.util.smali.ExternalLabel -import app.revanced.patches.youtube.button.autorepeat.fingerprints.AutoNavInformerFingerprint -import app.revanced.patches.youtube.button.autorepeat.fingerprints.AutoRepeatFingerprint -import app.revanced.patches.youtube.button.autorepeat.fingerprints.AutoRepeatParentFingerprint +import app.revanced.patches.youtube.button.autorepeat.fingerprints.* import app.revanced.shared.annotation.YouTubeCompatibility +import app.revanced.shared.extensions.toErrorResult import app.revanced.shared.util.integrations.Constants.UTILS_PATH import app.revanced.shared.util.integrations.Constants.VIDEO_PATH +import org.jf.dexlib2.iface.instruction.OneRegisterInstruction @Name("always-autorepeat") @YouTubeCompatibility @@ -27,30 +27,34 @@ class AutoRepeatPatch : BytecodePatch( ) ) { override fun execute(context: BytecodeContext): PatchResult { - with(AutoRepeatFingerprint.also { - it.resolve(context, AutoRepeatParentFingerprint.result!!.classDef) - }.result!!.mutableMethod) { - addInstructions( - 0, """ + AutoRepeatParentFingerprint.result?.classDef?.let { classDef -> + AutoRepeatFingerprint.also { + it.resolve(context, classDef) + }.result?.mutableMethod?.let { + it.addInstructions( + 0, """ invoke-static {}, $VIDEO_PATH/VideoInformation;->videoEnded()Z move-result v0 if-eqz v0, :noautorepeat return-void - """, listOf(ExternalLabel("noautorepeat", instruction(0))) - ) - } + """, listOf(ExternalLabel("noautorepeat", it.instruction(0))) + ) + } ?: return AutoRepeatFingerprint.toErrorResult() + } ?: return AutoRepeatParentFingerprint.toErrorResult() - with(AutoNavInformerFingerprint.result!!.mutableMethod) { - addInstructions( - 0, """ - invoke-static {}, $UTILS_PATH/EnableAutoRepeatPatch;->enableAutoRepeat()Z + AutoNavInformerFingerprint.result?.mutableMethod?.let { + with (it.implementation!!.instructions) { + val index = this.size - 1 - 1 + val register = (this[index] as OneRegisterInstruction).registerA + it.addInstructions( + index + 1, """ + invoke-static {v$register}, $UTILS_PATH/EnableAutoRepeatPatch;->enableAutoRepeat(Z)Z move-result v0 - if-eqz v0, :hidden - const/4 v0, 0x0 - return v0 - """, listOf(ExternalLabel("hidden", instruction(0))) - ) - } + """ + ) + + } + } ?: return AutoNavInformerFingerprint.toErrorResult() return PatchResultSuccess() }