refactor(youtube/general-ads): abstract into LithoFilterPatch

This commit is contained in:
oSumAtrIX
2022-11-18 02:36:47 +01:00
parent 1483a9d8bb
commit 2095e34db8
11 changed files with 342 additions and 467 deletions
@@ -0,0 +1,14 @@
package app.revanced.patches.youtube.misc.litho.filter.annotation
import app.revanced.patcher.annotation.Compatibility
import app.revanced.patcher.annotation.Package
@Compatibility(
[Package(
"com.google.android.youtube", arrayOf("17.36.37", "17.41.37", "17.42.35", "17.43.36")
)]
)
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
internal annotation class LithoFilterCompatibility
@@ -0,0 +1,26 @@
package app.revanced.patches.youtube.misc.litho.filter.fingerprints
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patches.youtube.misc.litho.filter.annotation.LithoFilterCompatibility
import org.jf.dexlib2.Opcode
@Name("component-context-parser-fingerprint")
@LithoFilterCompatibility
@Version("0.0.1")
object ComponentContextParserFingerprint : MethodFingerprint(
opcodes = listOf(
Opcode.INVOKE_INTERFACE,
Opcode.MOVE_RESULT_OBJECT,
Opcode.CHECK_CAST,
Opcode.INVOKE_VIRTUAL,
Opcode.GOTO,
Opcode.INVOKE_VIRTUAL,
Opcode.CONST_16,
Opcode.INVOKE_VIRTUAL,
Opcode.IPUT_OBJECT,
Opcode.NEW_INSTANCE
),
strings = listOf("LoggingProperties are not in proto format")
)
@@ -0,0 +1,16 @@
package app.revanced.patches.youtube.misc.litho.filter.fingerprints
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patches.youtube.misc.litho.filter.annotation.LithoFilterCompatibility
import org.jf.dexlib2.Opcode
@Name("empty-component-builder-fingerprint")
@LithoFilterCompatibility
@Version("0.0.1")
object EmptyComponentBuilderFingerprint : MethodFingerprint(
opcodes = listOf(
Opcode.INVOKE_STATIC_RANGE
),
)
@@ -0,0 +1,86 @@
package app.revanced.patches.youtube.misc.litho.filter.patch
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.data.BytecodeContext
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.BytecodePatch
import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultError
import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.patch.annotations.DependsOn
import app.revanced.patcher.util.smali.ExternalLabel
import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch
import app.revanced.patches.youtube.misc.litho.filter.annotation.LithoFilterCompatibility
import app.revanced.patches.youtube.misc.litho.filter.fingerprints.ComponentContextParserFingerprint
import app.revanced.patches.youtube.misc.litho.filter.fingerprints.EmptyComponentBuilderFingerprint
import org.jf.dexlib2.iface.instruction.Instruction
import org.jf.dexlib2.iface.instruction.OneRegisterInstruction
import org.jf.dexlib2.iface.instruction.ReferenceInstruction
import org.jf.dexlib2.iface.reference.FieldReference
import org.jf.dexlib2.iface.reference.MethodReference
@DependsOn([IntegrationsPatch::class])
@Description("Hooks the method which parses the bytes into a ComponentContext to filter components.")
@LithoFilterCompatibility
@Version("0.0.1")
class LithoFilterPatch : BytecodePatch(
listOf(ComponentContextParserFingerprint)
) {
override fun execute(context: BytecodeContext): PatchResult {
ComponentContextParserFingerprint.result?.let { result ->
val builderMethodIndex = EmptyComponentBuilderFingerprint
.also { it.resolve(context, result.mutableMethod, result.mutableClass) }
.let { it.result!!.scanResult.patternScanResult!!.startIndex }
val emptyComponentFieldIndex = builderMethodIndex + 2
with(result.mutableMethod) {
val insertHookIndex = result.scanResult.patternScanResult!!.endIndex
val clobberedRegister = (instruction(insertHookIndex - 3) as OneRegisterInstruction).registerA
val builderMethodDescriptor = instruction(builderMethodIndex).toDescriptor()
val emptyComponentFieldDescriptor = instruction(emptyComponentFieldIndex).toDescriptor()
addInstructions(
insertHookIndex, // right after setting the component.pathBuilder field,
"""
invoke-static {v5, v2}, Lapp/revanced/integrations/patches/LithoFilterPatch;->filter(Ljava/lang/StringBuilder;Ljava/lang/String;)Z
move-result v$clobberedRegister
if-eqz v$clobberedRegister, :not_an_ad
move-object/from16 v2, p1
invoke-static {v2}, $builderMethodDescriptor
move-result-object v0
iget-object v0, v0, $emptyComponentFieldDescriptor
return-object v0
""",
listOf(ExternalLabel("not_an_ad", instruction(insertHookIndex)))
)
}
} ?: return PatchResultError("Could not find the method to hook.")
return PatchResultSuccess()
}
private companion object {
fun Instruction.toDescriptor() = when (val reference = (this as ReferenceInstruction).reference) {
MethodReference::class -> {
val methodReference = reference as MethodReference
"${methodReference.definingClass}->${methodReference.name}(${
methodReference.parameterTypes.joinToString(
""
) { it }
})${methodReference.returnType}"
}
FieldReference::class -> {
val fieldReference = reference as FieldReference
"${fieldReference.definingClass}->${fieldReference.name}:${fieldReference.type}"
}
else -> throw PatchResultError("Unsupported reference type")
}
}
}