From 9690d8bd0748f4a86b5c62f175c8ddc3961472f1 Mon Sep 17 00:00:00 2001 From: inotia00 Date: Fri, 6 Jan 2023 16:55:25 +0900 Subject: [PATCH] fix: broken patch --- .../fingerprints/AllCodecsFingerprint.kt | 18 ++++++ .../AllCodecsParentFingerprint.kt | 16 +++++ .../AllCodecsReferenceFingerprint.kt | 55 ---------------- .../fingerprints/CodecsLockFingerprint.kt | 18 +----- .../audio/codecs/patch/CodecsUnlockPatch.kt | 64 ++++++++++--------- 5 files changed, 71 insertions(+), 100 deletions(-) create mode 100644 src/main/kotlin/app/revanced/patches/music/audio/codecs/fingerprints/AllCodecsFingerprint.kt create mode 100644 src/main/kotlin/app/revanced/patches/music/audio/codecs/fingerprints/AllCodecsParentFingerprint.kt delete mode 100644 src/main/kotlin/app/revanced/patches/music/audio/codecs/fingerprints/AllCodecsReferenceFingerprint.kt diff --git a/src/main/kotlin/app/revanced/patches/music/audio/codecs/fingerprints/AllCodecsFingerprint.kt b/src/main/kotlin/app/revanced/patches/music/audio/codecs/fingerprints/AllCodecsFingerprint.kt new file mode 100644 index 000000000..55af3d3e0 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/music/audio/codecs/fingerprints/AllCodecsFingerprint.kt @@ -0,0 +1,18 @@ +package app.revanced.patches.music.audio.codecs.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 AllCodecsFingerprint : MethodFingerprint( + returnType = "J", + access = AccessFlags.PUBLIC or AccessFlags.FINAL, + opcodes = listOf( + Opcode.IF_NEZ, + Opcode.GOTO, + Opcode.INVOKE_STATIC, + Opcode.MOVE_RESULT, + Opcode.INVOKE_STATIC + ) +) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/music/audio/codecs/fingerprints/AllCodecsParentFingerprint.kt b/src/main/kotlin/app/revanced/patches/music/audio/codecs/fingerprints/AllCodecsParentFingerprint.kt new file mode 100644 index 000000000..e7d14a9d2 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/music/audio/codecs/fingerprints/AllCodecsParentFingerprint.kt @@ -0,0 +1,16 @@ +package app.revanced.patches.music.audio.codecs.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 AllCodecsParentFingerprint : MethodFingerprint( + returnType = "J", + access = AccessFlags.PUBLIC or AccessFlags.FINAL, + parameters = listOf("L"), + opcodes = listOf( + Opcode.INVOKE_SUPER + ), + strings = listOf("itag") +) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/music/audio/codecs/fingerprints/AllCodecsReferenceFingerprint.kt b/src/main/kotlin/app/revanced/patches/music/audio/codecs/fingerprints/AllCodecsReferenceFingerprint.kt deleted file mode 100644 index d6c2b7445..000000000 --- a/src/main/kotlin/app/revanced/patches/music/audio/codecs/fingerprints/AllCodecsReferenceFingerprint.kt +++ /dev/null @@ -1,55 +0,0 @@ -package app.revanced.patches.music.audio.codecs.fingerprints - -import app.revanced.patcher.extensions.or -import app.revanced.patcher.fingerprint.method.annotation.FuzzyPatternScanMethod -import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint -import org.jf.dexlib2.AccessFlags -import org.jf.dexlib2.Opcode - -@FuzzyPatternScanMethod(2) // FIXME: Test this threshold and find the best value. -object AllCodecsReferenceFingerprint : MethodFingerprint( - returnType = "J", - access = AccessFlags.PUBLIC or AccessFlags.FINAL, - parameters = listOf("L"), - opcodes = listOf( - Opcode.INVOKE_STATIC, - Opcode.MOVE_RESULT_OBJECT, - Opcode.INVOKE_STATIC, - Opcode.MOVE_RESULT_OBJECT, - Opcode.INVOKE_INTERFACE, - Opcode.MOVE_RESULT, - Opcode.CONST_4, - Opcode.IF_EQZ, - Opcode.IGET_BOOLEAN, - Opcode.IF_NEZ, - Opcode.IGET_OBJECT, - Opcode.INVOKE_INTERFACE, - Opcode.IPUT_BOOLEAN, - Opcode.IGET_OBJECT, - Opcode.INVOKE_INTERFACE, - Opcode.GOTO, - Opcode.INVOKE_STATIC, - Opcode.MOVE_RESULT_OBJECT, - Opcode.INVOKE_INTERFACE, - Opcode.MOVE_RESULT, - Opcode.IF_NEZ, - Opcode.INVOKE_STATIC, - Opcode.MOVE_RESULT_OBJECT, - Opcode.INVOKE_INTERFACE, - Opcode.MOVE_RESULT, - Opcode.IF_EQZ, - Opcode.IGET_BOOLEAN, - Opcode.IF_NEZ, - Opcode.IGET_OBJECT, - Opcode.INVOKE_INTERFACE, - Opcode.IPUT_BOOLEAN, - Opcode.IGET_OBJECT, - Opcode.INVOKE_INTERFACE, - Opcode.GOTO, - Opcode.MOVE_EXCEPTION, - Opcode.INVOKE_SUPER, - Opcode.MOVE_RESULT_WIDE, - Opcode.RETURN_WIDE - ), - strings = listOf("itag") -) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/music/audio/codecs/fingerprints/CodecsLockFingerprint.kt b/src/main/kotlin/app/revanced/patches/music/audio/codecs/fingerprints/CodecsLockFingerprint.kt index 2a8a1c931..90cac0dcc 100644 --- a/src/main/kotlin/app/revanced/patches/music/audio/codecs/fingerprints/CodecsLockFingerprint.kt +++ b/src/main/kotlin/app/revanced/patches/music/audio/codecs/fingerprints/CodecsLockFingerprint.kt @@ -1,30 +1,18 @@ package app.revanced.patches.music.audio.codecs.fingerprints import app.revanced.patcher.extensions.or -import app.revanced.patcher.fingerprint.method.annotation.FuzzyPatternScanMethod import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint import org.jf.dexlib2.AccessFlags import org.jf.dexlib2.Opcode -@FuzzyPatternScanMethod(2) // FIXME: Test this threshold and find the best value. object CodecsLockFingerprint : MethodFingerprint( returnType = "L", access = AccessFlags.PUBLIC or AccessFlags.STATIC, opcodes = listOf( - Opcode.INVOKE_DIRECT, - Opcode.INVOKE_VIRTUAL, - Opcode.MOVE_RESULT, - Opcode.IF_NEZ, - Opcode.INVOKE_VIRTUAL, - Opcode.MOVE_RESULT, - Opcode.IF_NEZ, - Opcode.SGET_OBJECT, - Opcode.IGET_OBJECT, + Opcode.NEW_INSTANCE, + Opcode.NEW_INSTANCE, Opcode.INVOKE_STATIC, - Opcode.MOVE_RESULT_OBJECT, - Opcode.INVOKE_INTERFACE, - Opcode.INVOKE_DIRECT, - Opcode.RETURN_OBJECT + Opcode.MOVE_RESULT_OBJECT ), strings = listOf("eac3_supported") ) diff --git a/src/main/kotlin/app/revanced/patches/music/audio/codecs/patch/CodecsUnlockPatch.kt b/src/main/kotlin/app/revanced/patches/music/audio/codecs/patch/CodecsUnlockPatch.kt index bc073515a..e832e5fd0 100644 --- a/src/main/kotlin/app/revanced/patches/music/audio/codecs/patch/CodecsUnlockPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/audio/codecs/patch/CodecsUnlockPatch.kt @@ -7,19 +7,21 @@ import app.revanced.patcher.data.BytecodeContext import app.revanced.patcher.data.toMethodWalker import app.revanced.patcher.extensions.addInstructions import app.revanced.patcher.extensions.instruction +import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint.Companion.resolve import app.revanced.patcher.patch.annotations.DependsOn import app.revanced.patcher.patch.annotations.Patch 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.music.audio.codecs.fingerprints.AllCodecsReferenceFingerprint -import app.revanced.patches.music.audio.codecs.fingerprints.CodecsLockFingerprint +import app.revanced.patches.music.audio.codecs.fingerprints.* import app.revanced.patches.music.misc.integrations.patch.MusicIntegrationsPatch import app.revanced.patches.music.misc.settings.patch.MusicSettingsPatch import app.revanced.shared.annotation.YouTubeMusicCompatibility +import app.revanced.shared.extensions.toErrorResult import app.revanced.shared.util.integrations.Constants.MUSIC_SETTINGS_PATH -import org.jf.dexlib2.Opcode +import org.jf.dexlib2.iface.instruction.OneRegisterInstruction +import org.jf.dexlib2.iface.Method @Patch @DependsOn([MusicIntegrationsPatch::class, MusicSettingsPatch::class]) @@ -29,41 +31,43 @@ import org.jf.dexlib2.Opcode @Version("0.0.1") class CodecsUnlockPatch : BytecodePatch( listOf( - CodecsLockFingerprint, AllCodecsReferenceFingerprint + AllCodecsParentFingerprint, + CodecsLockFingerprint ) ) { override fun execute(context: BytecodeContext): PatchResult { - val codecsLockResult = CodecsLockFingerprint.result!! - val codecsLockMethod = codecsLockResult.mutableMethod - val implementation = codecsLockMethod.implementation!! + AllCodecsParentFingerprint.result?.let { parentResult -> + AllCodecsFingerprint.also { it.resolve(context, parentResult.classDef) }.result?.let { result -> + allCodecsMethod = + context.toMethodWalker(result.method) + .nextMethod(result.scanResult.patternScanResult!!.endIndex) + .getMethod() - val scanResultStartIndex = codecsLockResult.scanResult.patternScanResult!!.startIndex - val instructionIndex = scanResultStartIndex + - if (implementation.instructions[scanResultStartIndex - 1].opcode == Opcode.CHECK_CAST) { - // for 5.16.xx and lower - -3 - } else { - // since 5.17.xx - -2 - } + } ?: return AllCodecsFingerprint.toErrorResult() + } ?: return AllCodecsParentFingerprint.toErrorResult() - val allCodecsResult = AllCodecsReferenceFingerprint.result!! - val allCodecsMethod = - context.toMethodWalker(allCodecsResult.method) - .nextMethod(allCodecsResult.scanResult.patternScanResult!!.startIndex) - .getMethod() + CodecsLockFingerprint.result?.let { result -> + val endIndex = result.scanResult.patternScanResult!!.endIndex - codecsLockMethod.addInstructions( - instructionIndex + 2, """ - invoke-static {}, $MUSIC_SETTINGS_PATH->enableOpusCodec()Z - move-result v7 - if-eqz v7, :mp4a - invoke-static {}, ${allCodecsMethod.definingClass}->${allCodecsMethod.name}()Ljava/util/Set; - move-result-object p4 - """, listOf(ExternalLabel("mp4a", codecsLockMethod.instruction(instructionIndex + 2))) - ) + with(result.mutableMethod) { + val register = (instruction(endIndex) as OneRegisterInstruction).registerA + addInstructions( + endIndex + 1, """ + invoke-static {}, $MUSIC_SETTINGS_PATH->enableOpusCodec()Z + move-result v7 + if-eqz v7, :mp4a + invoke-static {}, ${allCodecsMethod.definingClass}->${allCodecsMethod.name}()Ljava/util/Set; + move-result-object v$register + """, listOf(ExternalLabel("mp4a", instruction(endIndex + 1))) + ) + } + } ?: return CodecsLockFingerprint.toErrorResult() return PatchResultSuccess() } + + private companion object { + lateinit var allCodecsMethod: Method + } }