|
| 1 | +package app.revanced.patches.soundcloud.offlinesync |
| 2 | + |
| 3 | +import app.revanced.patcher.data.BytecodeContext |
| 4 | +import app.revanced.patcher.extensions.InstructionExtensions.addInstruction |
| 5 | +import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels |
| 6 | +import app.revanced.patcher.extensions.InstructionExtensions.getInstruction |
| 7 | +import app.revanced.patcher.extensions.InstructionExtensions.getInstructions |
| 8 | +import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction |
| 9 | +import app.revanced.patcher.patch.BytecodePatch |
| 10 | +import app.revanced.patcher.patch.annotation.CompatiblePackage |
| 11 | +import app.revanced.patcher.patch.annotation.Patch |
| 12 | +import app.revanced.patcher.util.smali.ExternalLabel |
| 13 | +import app.revanced.patches.soundcloud.offlinesync.fingerprints.DownloadOperationsHeaderVerificationFingerprint |
| 14 | +import app.revanced.patches.soundcloud.offlinesync.fingerprints.DownloadOperationsURLBuilderFingerprint |
| 15 | +import app.revanced.patches.soundcloud.shared.fingerprints.FeatureConstructorFingerprint |
| 16 | +import app.revanced.util.getReference |
| 17 | +import app.revanced.util.resultOrThrow |
| 18 | +import com.android.tools.smali.dexlib2.Opcode |
| 19 | +import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction |
| 20 | +import com.android.tools.smali.dexlib2.iface.reference.FieldReference |
| 21 | + |
| 22 | +@Patch( |
| 23 | + name = "Enable offline sync", |
| 24 | + compatiblePackages = [CompatiblePackage("com.soundcloud.android")], |
| 25 | +) |
| 26 | +@Suppress("unused") |
| 27 | +object EnableOfflineSyncPatch : BytecodePatch( |
| 28 | + setOf( |
| 29 | + FeatureConstructorFingerprint, DownloadOperationsURLBuilderFingerprint, |
| 30 | + DownloadOperationsHeaderVerificationFingerprint |
| 31 | + ), |
| 32 | +) { |
| 33 | + override fun execute(context: BytecodeContext) { |
| 34 | + // Enable the feature to allow offline track syncing by modifying the JSON server response. |
| 35 | + // This method is the constructor of a class representing a "Feature" object parsed from JSON data. |
| 36 | + // p1 is the name of the feature. |
| 37 | + // p2 is true if the feature is enabled, false otherwise. |
| 38 | + FeatureConstructorFingerprint.resultOrThrow().mutableMethod.apply { |
| 39 | + val afterCheckNotNullIndex = 2 |
| 40 | + |
| 41 | + addInstructionsWithLabels( |
| 42 | + afterCheckNotNullIndex, |
| 43 | + """ |
| 44 | + const-string v0, "offline_sync" |
| 45 | + invoke-virtual { p1, v0 }, Ljava/lang/String;->equals(Ljava/lang/Object;)Z |
| 46 | + move-result v0 |
| 47 | + if-eqz v0, :skip |
| 48 | + const/4 p2, 0x1 |
| 49 | + """, |
| 50 | + ExternalLabel("skip", getInstruction(afterCheckNotNullIndex)), |
| 51 | + ) |
| 52 | + } |
| 53 | + |
| 54 | + // Patch the URL builder to use the HTTPS_STREAM endpoint |
| 55 | + // instead of the offline sync endpoint to downloading the track. |
| 56 | + DownloadOperationsURLBuilderFingerprint.resultOrThrow().mutableMethod.apply { |
| 57 | + val getEndpointsEnumFieldIndex = 1 |
| 58 | + val getEndpointsEnumFieldInstruction = getInstruction<OneRegisterInstruction>(getEndpointsEnumFieldIndex) |
| 59 | + |
| 60 | + val targetRegister = getEndpointsEnumFieldInstruction.registerA |
| 61 | + val endpointsType = getEndpointsEnumFieldInstruction.getReference<FieldReference>()!!.type |
| 62 | + |
| 63 | + replaceInstruction( |
| 64 | + getEndpointsEnumFieldIndex, |
| 65 | + "sget-object v$targetRegister, $endpointsType->HTTPS_STREAM:$endpointsType" |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | + // The HTTPS_STREAM endpoint does not return the necessary headers for offline sync. |
| 70 | + // Mock the headers to prevent the app from crashing by setting them to empty strings. |
| 71 | + // The headers are all cosmetic and do not affect the functionality of the app. |
| 72 | + DownloadOperationsHeaderVerificationFingerprint.resultOrThrow().mutableMethod.apply { |
| 73 | + // The first three null checks need to be patched. |
| 74 | + getInstructions().asSequence().filter { |
| 75 | + it.opcode == Opcode.IF_EQZ |
| 76 | + }.take(3).map { it.location.index }.forEach { nullCheckIndex -> |
| 77 | + val headerStringRegister = getInstruction<OneRegisterInstruction>(nullCheckIndex).registerA |
| 78 | + |
| 79 | + addInstruction(nullCheckIndex, "const-string v$headerStringRegister, \"\"") |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments