|
| 1 | +package app.revanced.patches.spotify.extended |
| 2 | + |
| 3 | +import app.revanced.patcher.extensions.InstructionExtensions.addInstructions |
| 4 | +import app.revanced.patcher.extensions.InstructionExtensions.instructionsOrNull |
| 5 | +import app.revanced.patcher.fingerprint |
| 6 | +import app.revanced.patcher.patch.bytecodePatch |
| 7 | +import app.revanced.util.getReference |
| 8 | +import app.revanced.util.indexOfFirstInstructionOrThrow |
| 9 | +import com.android.tools.smali.dexlib2.AccessFlags |
| 10 | +import com.android.tools.smali.dexlib2.Opcode |
| 11 | +import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction |
| 12 | +import com.android.tools.smali.dexlib2.iface.reference.MethodReference |
| 13 | +import com.android.tools.smali.dexlib2.iface.reference.StringReference |
| 14 | + |
| 15 | +val shareLinkFingerprint = fingerprint { |
| 16 | + accessFlags(AccessFlags.PUBLIC, AccessFlags.CONSTRUCTOR) |
| 17 | + parameters( |
| 18 | + "Ljava/lang/String;", |
| 19 | + "Ljava/lang/String;", |
| 20 | + "Ljava/lang/String;", |
| 21 | + "Ljava/lang/String;" |
| 22 | + ) |
| 23 | + returns("V") |
| 24 | + |
| 25 | + custom { _, classDef -> |
| 26 | + val toStringMethod = classDef.methods.firstOrNull { |
| 27 | + it.name == "toString" && it.parameters.isEmpty() && it.returnType == "Ljava/lang/String;" |
| 28 | + } ?: return@custom false |
| 29 | + |
| 30 | + val toStringInstructions = toStringMethod.instructionsOrNull ?: return@custom false |
| 31 | + toStringInstructions.any { instruction -> |
| 32 | + instruction.opcode == Opcode.CONST_STRING && |
| 33 | + (instruction as? ReferenceInstruction)?.reference?.let { ref -> |
| 34 | + (ref as? StringReference)?.string?.startsWith("ShareUrl(url=") == true |
| 35 | + } == true |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +private const val EXTENSION_CLASS_DESCRIPTOR = "Lapp/revanced/extension/spotify/misc/UrlCleaner;" |
| 41 | + |
| 42 | +@Suppress("unused") |
| 43 | +val removeShareTrackingPatch = bytecodePatch( |
| 44 | + name = "Sanitize sharing links", |
| 45 | + description = "Removes the '?si=' tracking parameter from shared links (e.g., Copy Link, Share to...).", |
| 46 | +) { |
| 47 | + compatibleWith("com.spotify.music") |
| 48 | + |
| 49 | + execute { |
| 50 | + val originalMethod = shareLinkFingerprint.method |
| 51 | + val invokeDirectIndex = originalMethod.indexOfFirstInstructionOrThrow { |
| 52 | + opcode == Opcode.INVOKE_DIRECT && |
| 53 | + (this as ReferenceInstruction).reference is MethodReference && |
| 54 | + (getReference<MethodReference>()?.name == "<init>") && |
| 55 | + (getReference<MethodReference>()?.definingClass == "Ljava/lang/Object;") |
| 56 | + } |
| 57 | + |
| 58 | + val smaliCodeToInsert = """ |
| 59 | + invoke-static {p1}, $EXTENSION_CLASS_DESCRIPTOR->removeSiParameter(Ljava/lang/String;)Ljava/lang/String; |
| 60 | + move-result-object p1 |
| 61 | + """.trimIndent() |
| 62 | + |
| 63 | + originalMethod.addInstructions(invokeDirectIndex + 1, smaliCodeToInsert) |
| 64 | + } |
| 65 | +} |
0 commit comments