diff --git a/src/main/kotlin/app/revanced/patches/music/utils/litho/LithoFilterPatch.kt b/src/main/kotlin/app/revanced/patches/music/utils/litho/LithoFilterPatch.kt index 201df0009..9edc4549f 100644 --- a/src/main/kotlin/app/revanced/patches/music/utils/litho/LithoFilterPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/utils/litho/LithoFilterPatch.kt @@ -8,7 +8,6 @@ import app.revanced.patcher.patch.annotation.Patch import app.revanced.patches.music.utils.integrations.Constants.COMPONENTS_PATH import app.revanced.patches.music.utils.litho.fingerprints.LithoFilterFingerprint import app.revanced.patches.shared.patch.litho.ComponentParserPatch -import app.revanced.patches.shared.patch.litho.ComponentParserPatch.pathBuilderHook import app.revanced.util.exception import java.io.Closeable @@ -25,7 +24,7 @@ object LithoFilterPatch : BytecodePatch( private var filterCount = 0 override fun execute(context: BytecodeContext) { - pathBuilderHook("$INTEGRATIONS_CLASS_DESCRIPTOR->filter") + ComponentParserPatch.injectCall(INTEGRATIONS_CLASS_DESCRIPTOR) LithoFilterFingerprint.result?.let { it.mutableMethod.apply { diff --git a/src/main/kotlin/app/revanced/patches/music/utils/litho/fingerprints/LithoFilterFingerprint.kt b/src/main/kotlin/app/revanced/patches/music/utils/litho/fingerprints/LithoFilterFingerprint.kt index 9023b91f0..d4358bad6 100644 --- a/src/main/kotlin/app/revanced/patches/music/utils/litho/fingerprints/LithoFilterFingerprint.kt +++ b/src/main/kotlin/app/revanced/patches/music/utils/litho/fingerprints/LithoFilterFingerprint.kt @@ -2,12 +2,13 @@ package app.revanced.patches.music.utils.litho.fingerprints import app.revanced.patcher.extensions.or import app.revanced.patcher.fingerprint.MethodFingerprint +import app.revanced.patches.music.utils.integrations.Constants.COMPONENTS_PATH import com.android.tools.smali.dexlib2.AccessFlags object LithoFilterFingerprint : MethodFingerprint( returnType = "V", accessFlags = AccessFlags.PUBLIC or AccessFlags.STATIC or AccessFlags.CONSTRUCTOR, customFingerprint = { methodDef, _ -> - methodDef.definingClass == "Lapp/revanced/integrations/music/patches/components/LithoFilterPatch;" + methodDef.definingClass == "$COMPONENTS_PATH/LithoFilterPatch;" } ) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/youtube/utils/litho/fingerprints/GeneralByteBufferFingerprint.kt b/src/main/kotlin/app/revanced/patches/shared/fingerprints/litho/GeneralByteBufferFingerprint.kt similarity index 89% rename from src/main/kotlin/app/revanced/patches/youtube/utils/litho/fingerprints/GeneralByteBufferFingerprint.kt rename to src/main/kotlin/app/revanced/patches/shared/fingerprints/litho/GeneralByteBufferFingerprint.kt index 3fccce60e..6471bf128 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/utils/litho/fingerprints/GeneralByteBufferFingerprint.kt +++ b/src/main/kotlin/app/revanced/patches/shared/fingerprints/litho/GeneralByteBufferFingerprint.kt @@ -1,4 +1,4 @@ -package app.revanced.patches.youtube.utils.litho.fingerprints +package app.revanced.patches.shared.fingerprints.litho import app.revanced.patcher.extensions.or import app.revanced.patcher.fingerprint.MethodFingerprint @@ -10,7 +10,7 @@ object GeneralByteBufferFingerprint : MethodFingerprint( accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL, parameters = listOf("I", "Ljava/nio/ByteBuffer;"), opcodes = listOf( - Opcode.IPUT_OBJECT, + null, Opcode.IF_EQZ, Opcode.IPUT, Opcode.INVOKE_VIRTUAL, diff --git a/src/main/kotlin/app/revanced/patches/shared/patch/litho/ComponentParserPatch.kt b/src/main/kotlin/app/revanced/patches/shared/patch/litho/ComponentParserPatch.kt index 77d362778..75da0e027 100644 --- a/src/main/kotlin/app/revanced/patches/shared/patch/litho/ComponentParserPatch.kt +++ b/src/main/kotlin/app/revanced/patches/shared/patch/litho/ComponentParserPatch.kt @@ -1,12 +1,14 @@ package app.revanced.patches.shared.patch.litho import app.revanced.patcher.data.BytecodeContext +import app.revanced.patcher.extensions.InstructionExtensions.addInstruction import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels import app.revanced.patcher.extensions.InstructionExtensions.getInstruction import app.revanced.patcher.patch.BytecodePatch import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod import app.revanced.patcher.util.smali.ExternalLabel import app.revanced.patches.shared.fingerprints.litho.EmptyComponentBuilderFingerprint +import app.revanced.patches.shared.fingerprints.litho.GeneralByteBufferFingerprint import app.revanced.util.exception import app.revanced.util.getEmptyStringInstructionIndex import app.revanced.util.getReference @@ -20,10 +22,14 @@ import com.android.tools.smali.dexlib2.iface.reference.FieldReference import kotlin.properties.Delegates object ComponentParserPatch : BytecodePatch( - setOf(EmptyComponentBuilderFingerprint) + setOf( + EmptyComponentBuilderFingerprint, + GeneralByteBufferFingerprint + ) ) { private lateinit var emptyComponentLabel: String - internal lateinit var insertMethod: MutableMethod + private lateinit var byteBufferMethod: MutableMethod + internal lateinit var pathBuilderMethod: MutableMethod private var emptyComponentIndex by Delegates.notNull() private var insertIndex by Delegates.notNull() @@ -32,24 +38,20 @@ object ComponentParserPatch : BytecodePatch( private var objectRegister by Delegates.notNull() private var stringBuilderRegister by Delegates.notNull() - internal fun generalHook(descriptor: String) { - insertMethod.apply { - addInstructionsWithLabels( - insertIndex, """ - invoke-static {v$stringBuilderRegister, v$identifierRegister, v$objectRegister}, $descriptor(Ljava/lang/StringBuilder;Ljava/lang/String;Ljava/lang/Object;)Z - move-result v$stringBuilderRegister - if-eqz v$stringBuilderRegister, :filter - """ + emptyComponentLabel, - ExternalLabel("filter", getInstruction(insertIndex)) + internal fun injectCall(descriptor: String) { + byteBufferMethod.apply { + val insertIndex = getTargetIndex(0, Opcode.IF_EQZ) + 1 + + addInstruction( + insertIndex, + "invoke-static { p2 }, $descriptor->setProtoBuffer(Ljava/nio/ByteBuffer;)V" ) } - } - internal fun pathBuilderHook(descriptor: String) { - insertMethod.apply { + pathBuilderMethod.apply { addInstructionsWithLabels( insertIndex, """ - invoke-static {v$stringBuilderRegister}, $descriptor(Ljava/lang/StringBuilder;)Z + invoke-static {v$stringBuilderRegister, v$identifierRegister, v$objectRegister}, $descriptor->filter(Ljava/lang/StringBuilder;Ljava/lang/String;Ljava/lang/Object;)Z move-result v$stringBuilderRegister if-eqz v$stringBuilderRegister, :filter """ + emptyComponentLabel, @@ -60,12 +62,12 @@ object ComponentParserPatch : BytecodePatch( override fun execute(context: BytecodeContext) { - /** - * Shared fingerprint - */ + byteBufferMethod = GeneralByteBufferFingerprint.result?.mutableMethod + ?: throw GeneralByteBufferFingerprint.exception + EmptyComponentBuilderFingerprint.result?.let { it.mutableMethod.apply { - insertMethod = this + pathBuilderMethod = this emptyComponentIndex = it.scanResult.patternScanResult!!.startIndex + 1 val builderMethodDescriptor = diff --git a/src/main/kotlin/app/revanced/patches/youtube/utils/browseid/BrowseIdHookPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/utils/browseid/BrowseIdHookPatch.kt index 63273d6e2..6f5657dca 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/utils/browseid/BrowseIdHookPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/utils/browseid/BrowseIdHookPatch.kt @@ -63,7 +63,7 @@ object BrowseIdHookPatch : BytecodePatch( /** * Set BrowseId to integrations. */ - ComponentParserPatch.insertMethod.apply { + ComponentParserPatch.pathBuilderMethod.apply { addInstruction( 0, "invoke-static {}, $INTEGRATIONS_CLASS_DESCRIPTOR->setBrowseIdFromField()V" diff --git a/src/main/kotlin/app/revanced/patches/youtube/utils/litho/LithoFilterPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/utils/litho/LithoFilterPatch.kt index ca1f9e20e..2cd1a7cd1 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/utils/litho/LithoFilterPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/utils/litho/LithoFilterPatch.kt @@ -1,15 +1,12 @@ package app.revanced.patches.youtube.utils.litho import app.revanced.patcher.data.BytecodeContext -import app.revanced.patcher.extensions.InstructionExtensions.addInstruction import app.revanced.patcher.extensions.InstructionExtensions.addInstructions import app.revanced.patcher.extensions.InstructionExtensions.removeInstructions import app.revanced.patcher.patch.BytecodePatch import app.revanced.patcher.patch.annotation.Patch import app.revanced.patches.shared.patch.litho.ComponentParserPatch -import app.revanced.patches.shared.patch.litho.ComponentParserPatch.generalHook import app.revanced.patches.youtube.utils.integrations.Constants.COMPONENTS_PATH -import app.revanced.patches.youtube.utils.litho.fingerprints.GeneralByteBufferFingerprint import app.revanced.patches.youtube.utils.litho.fingerprints.LithoFilterFingerprint import app.revanced.util.exception import java.io.Closeable @@ -17,10 +14,7 @@ import java.io.Closeable @Patch(dependencies = [ComponentParserPatch::class]) @Suppress("unused") object LithoFilterPatch : BytecodePatch( - setOf( - GeneralByteBufferFingerprint, - LithoFilterFingerprint - ) + setOf(LithoFilterFingerprint) ), Closeable { private const val INTEGRATIONS_CLASS_DESCRIPTOR = "$COMPONENTS_PATH/LithoFilterPatch;" @@ -30,18 +24,7 @@ object LithoFilterPatch : BytecodePatch( private var filterCount = 0 override fun execute(context: BytecodeContext) { - - // ByteBuffer is set after checking for non-null - GeneralByteBufferFingerprint.result?.let { - it.mutableMethod.apply { - addInstruction( - 2, - "invoke-static { p2 }, $INTEGRATIONS_CLASS_DESCRIPTOR->setProtoBuffer(Ljava/nio/ByteBuffer;)V" - ) - } - } ?: throw GeneralByteBufferFingerprint.exception - - generalHook("$INTEGRATIONS_CLASS_DESCRIPTOR->filter") + ComponentParserPatch.injectCall(INTEGRATIONS_CLASS_DESCRIPTOR) LithoFilterFingerprint.result?.mutableMethod?.apply { removeInstructions(0, 6) diff --git a/src/main/kotlin/app/revanced/patches/youtube/utils/litho/fingerprints/LithoFilterFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/utils/litho/fingerprints/LithoFilterFingerprint.kt index e8c014fe7..fa37557fb 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/utils/litho/fingerprints/LithoFilterFingerprint.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/utils/litho/fingerprints/LithoFilterFingerprint.kt @@ -2,12 +2,13 @@ package app.revanced.patches.youtube.utils.litho.fingerprints import app.revanced.patcher.extensions.or import app.revanced.patcher.fingerprint.MethodFingerprint +import app.revanced.patches.youtube.utils.integrations.Constants.COMPONENTS_PATH import com.android.tools.smali.dexlib2.AccessFlags object LithoFilterFingerprint : MethodFingerprint( returnType = "V", accessFlags = AccessFlags.PUBLIC or AccessFlags.STATIC or AccessFlags.CONSTRUCTOR, customFingerprint = { methodDef, _ -> - methodDef.definingClass == "Lapp/revanced/integrations/youtube/patches/components/LithoFilterPatch;" + methodDef.definingClass == "$COMPONENTS_PATH/LithoFilterPatch;" } ) \ No newline at end of file