|
| 1 | +package app.revanced.patches.spotify.misc.lyrics |
| 2 | + |
| 3 | +import app.revanced.patcher.extensions.InstructionExtensions.addInstruction |
| 4 | +import app.revanced.patcher.extensions.InstructionExtensions.getInstruction |
| 5 | +import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction |
| 6 | +import app.revanced.patcher.patch.bytecodePatch |
| 7 | +import app.revanced.patcher.patch.stringOption |
| 8 | +import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod |
| 9 | +import app.revanced.patches.spotify.shared.IS_SPOTIFY_LEGACY_APP_TARGET |
| 10 | +import app.revanced.util.getReference |
| 11 | +import app.revanced.util.indexOfFirstInstructionOrThrow |
| 12 | +import app.revanced.util.indexOfFirstInstructionReversedOrThrow |
| 13 | +import com.android.tools.smali.dexlib2.builder.instruction.BuilderInstruction35c |
| 14 | +import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction |
| 15 | +import com.android.tools.smali.dexlib2.iface.reference.MethodReference |
| 16 | +import com.android.tools.smali.dexlib2.immutable.reference.ImmutableMethodReference |
| 17 | +import java.net.InetAddress |
| 18 | +import java.net.URI |
| 19 | +import java.net.URISyntaxException |
| 20 | +import java.net.UnknownHostException |
| 21 | +import java.util.logging.Logger |
| 22 | + |
| 23 | +@Suppress("unused") |
| 24 | +val changeLyricsProviderPatch = bytecodePatch( |
| 25 | + name = "Change lyrics provider", |
| 26 | + description = "Changes the lyrics provider to a custom one.", |
| 27 | + use = false, |
| 28 | +) { |
| 29 | + compatibleWith("com.spotify.music") |
| 30 | + |
| 31 | + val lyricsProviderHost by stringOption( |
| 32 | + key = "lyricsProviderHost", |
| 33 | + default = "lyrics.natanchiodi.fr", |
| 34 | + title = "Lyrics provider host", |
| 35 | + description = "The domain name or IP address of a custom lyrics provider.", |
| 36 | + required = false, |
| 37 | + ) { |
| 38 | + // Fix bad data if the user enters a URL (https://whatever.com/path). |
| 39 | + val host = try { |
| 40 | + URI(it!!).host ?: it |
| 41 | + } catch (e: URISyntaxException) { |
| 42 | + return@stringOption false |
| 43 | + } |
| 44 | + |
| 45 | + // Do a courtesy check if the host can be resolved. |
| 46 | + // If it does not resolve, then print a warning but use the host anyway. |
| 47 | + // Unresolvable hosts should not be rejected, since the patching environment |
| 48 | + // may not allow network connections or the network may be down. |
| 49 | + try { |
| 50 | + InetAddress.getByName(host) |
| 51 | + } catch (e: UnknownHostException) { |
| 52 | + Logger.getLogger(this::class.java.name).warning( |
| 53 | + "Host \"$host\" did not resolve to any domain." |
| 54 | + ) |
| 55 | + } |
| 56 | + true |
| 57 | + } |
| 58 | + |
| 59 | + execute { |
| 60 | + if (IS_SPOTIFY_LEGACY_APP_TARGET) { |
| 61 | + Logger.getLogger(this::class.java.name).severe( |
| 62 | + "Change lyrics provider patch is not supported for this target version." |
| 63 | + ) |
| 64 | + return@execute |
| 65 | + } |
| 66 | + |
| 67 | + val httpClientBuilderMethod = httpClientBuilderFingerprint.originalMethod |
| 68 | + |
| 69 | + // region Create a modified copy of the HTTP client builder method with the custom lyrics provider host. |
| 70 | + val patchedHttpClientBuilderMethod = with(httpClientBuilderMethod) { |
| 71 | + val invokeBuildUrlIndex = indexOfFirstInstructionOrThrow { |
| 72 | + getReference<MethodReference>()?.returnType == "Lokhttp3/HttpUrl;" |
| 73 | + } |
| 74 | + val setUrlBuilderHostIndex = indexOfFirstInstructionReversedOrThrow(invokeBuildUrlIndex) { |
| 75 | + val reference = getReference<MethodReference>() |
| 76 | + reference?.definingClass == "Lokhttp3/HttpUrl${"$"}Builder;" && |
| 77 | + reference.parameterTypes.firstOrNull() == "Ljava/lang/String;" |
| 78 | + } |
| 79 | + val hostRegister = getInstruction<FiveRegisterInstruction>(setUrlBuilderHostIndex).registerD |
| 80 | + |
| 81 | + MutableMethod(this).apply { |
| 82 | + name = "rv_getCustomLyricsProviderHttpClient" |
| 83 | + addInstruction( |
| 84 | + setUrlBuilderHostIndex, |
| 85 | + "const-string v$hostRegister, \"$lyricsProviderHost\"" |
| 86 | + ) |
| 87 | + |
| 88 | + // Add the patched method to the class. |
| 89 | + httpClientBuilderFingerprint.classDef.methods.add(this) |
| 90 | + } |
| 91 | + } |
| 92 | + //endregion |
| 93 | + |
| 94 | + // region Replace the call to the HTTP client builder method used exclusively for lyrics by the modified one. |
| 95 | + getLyricsHttpClientFingerprint(httpClientBuilderMethod).method.apply { |
| 96 | + val getLyricsHttpClientIndex = indexOfFirstInstructionOrThrow { |
| 97 | + getReference<MethodReference>() == httpClientBuilderMethod |
| 98 | + } |
| 99 | + val getLyricsHttpClientInstruction = getInstruction<BuilderInstruction35c>(getLyricsHttpClientIndex) |
| 100 | + |
| 101 | + // Replace the original method call with a call to our patched method. |
| 102 | + replaceInstruction( |
| 103 | + getLyricsHttpClientIndex, |
| 104 | + BuilderInstruction35c( |
| 105 | + getLyricsHttpClientInstruction.opcode, |
| 106 | + getLyricsHttpClientInstruction.registerCount, |
| 107 | + getLyricsHttpClientInstruction.registerC, |
| 108 | + getLyricsHttpClientInstruction.registerD, |
| 109 | + getLyricsHttpClientInstruction.registerE, |
| 110 | + getLyricsHttpClientInstruction.registerF, |
| 111 | + getLyricsHttpClientInstruction.registerG, |
| 112 | + ImmutableMethodReference( |
| 113 | + patchedHttpClientBuilderMethod.definingClass, |
| 114 | + patchedHttpClientBuilderMethod.name, // Only difference from the original method. |
| 115 | + patchedHttpClientBuilderMethod.parameters, |
| 116 | + patchedHttpClientBuilderMethod.returnType |
| 117 | + ) |
| 118 | + ) |
| 119 | + ) |
| 120 | + } |
| 121 | + //endregion |
| 122 | + } |
| 123 | +} |
0 commit comments