forked from inotia00/revanced-patches
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(YouTube): add
Hook YouTube Music actions
patch
- Loading branch information
Showing
8 changed files
with
199 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/main/kotlin/app/revanced/patches/youtube/general/music/YouTubeMusicActionsPatch.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package app.revanced.patches.youtube.general.music | ||
|
||
import app.revanced.patcher.data.BytecodeContext | ||
import app.revanced.patcher.extensions.InstructionExtensions.addInstructions | ||
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction | ||
import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction | ||
import app.revanced.patches.youtube.general.music.fingerprints.AppDeepLinkFingerprint | ||
import app.revanced.patches.youtube.utils.compatibility.Constants.COMPATIBLE_PACKAGE | ||
import app.revanced.patches.youtube.utils.gms.GmsCoreSupportResourcePatch.PackageNameYouTubeMusic | ||
import app.revanced.patches.youtube.utils.integrations.Constants.GENERAL_PATH | ||
import app.revanced.patches.youtube.utils.settings.ResourceUtils.addEntryValues | ||
import app.revanced.patches.youtube.utils.settings.SettingsBytecodePatch | ||
import app.revanced.patches.youtube.utils.settings.SettingsPatch | ||
import app.revanced.util.getReference | ||
import app.revanced.util.indexOfFirstInstructionOrThrow | ||
import app.revanced.util.patch.BaseBytecodePatch | ||
import app.revanced.util.resultOrThrow | ||
import app.revanced.util.valueOrThrow | ||
import com.android.tools.smali.dexlib2.Opcode | ||
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction | ||
import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction | ||
import com.android.tools.smali.dexlib2.iface.instruction.TwoRegisterInstruction | ||
import com.android.tools.smali.dexlib2.iface.reference.FieldReference | ||
import java.io.Closeable | ||
|
||
@Suppress("unused") | ||
object YouTubeMusicActionsPatch : BaseBytecodePatch( | ||
name = "Hook YouTube Music actions", | ||
description = "Adds support for opening music in RVX Music using the in-app YouTube Music button.", | ||
dependencies = setOf(SettingsPatch::class), | ||
compatiblePackages = COMPATIBLE_PACKAGE, | ||
fingerprints = setOf(AppDeepLinkFingerprint) | ||
), Closeable { | ||
private const val INTEGRATIONS_CLASS_DESCRIPTOR = | ||
"$GENERAL_PATH/YouTubeMusicActionsPatch;" | ||
|
||
override fun execute(context: BytecodeContext) { | ||
|
||
AppDeepLinkFingerprint.resultOrThrow().let { | ||
it.mutableMethod.apply { | ||
val packageNameIndex = it.scanResult.patternScanResult!!.startIndex | ||
val packageNameField = getInstruction<ReferenceInstruction>(packageNameIndex).reference.toString() | ||
|
||
implementation!!.instructions | ||
.withIndex() | ||
.filter { (_, instruction) -> | ||
instruction.opcode == Opcode.IGET_OBJECT && | ||
instruction.getReference<FieldReference>()?.toString() == packageNameField | ||
} | ||
.map { (index, _) -> index } | ||
.reversed() | ||
.forEach { index -> | ||
val register = getInstruction<TwoRegisterInstruction>(index).registerA | ||
|
||
addInstructions( | ||
index + 1, """ | ||
invoke-static {v$register}, $INTEGRATIONS_CLASS_DESCRIPTOR->overridePackageName(Ljava/lang/String;)Ljava/lang/String; | ||
move-result-object v$register | ||
""" | ||
) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Add settings | ||
*/ | ||
SettingsPatch.addPreference( | ||
arrayOf( | ||
"PREFERENCE_SCREEN: GENERAL", | ||
"SETTINGS: HOOK_BUTTONS", | ||
"SETTINGS: HOOK_YOUTUBE_MUSIC_ACTIONS" | ||
) | ||
) | ||
|
||
SettingsPatch.updatePatchStatus(this) | ||
} | ||
|
||
override fun close() { | ||
if (SettingsPatch.containsPatch("GmsCore support")) { | ||
SettingsPatch.contexts.addEntryValues( | ||
"revanced_third_party_youtube_music_label", | ||
"RVX Music" | ||
) | ||
SettingsPatch.contexts.addEntryValues( | ||
"revanced_third_party_youtube_music_package_name", | ||
PackageNameYouTubeMusic.valueOrThrow() | ||
) | ||
|
||
SettingsBytecodePatch.contexts | ||
.findClass { classDef -> classDef.type == INTEGRATIONS_CLASS_DESCRIPTOR } | ||
?.mutableClass | ||
?.methods | ||
?.first { method -> method.name == "getRVXMusicPackageName" } | ||
?.apply { | ||
val replaceIndex = indexOfFirstInstructionOrThrow(Opcode.CONST_STRING) | ||
val replaceRegister = | ||
getInstruction<OneRegisterInstruction>(replaceIndex).registerA | ||
|
||
replaceInstruction( | ||
replaceIndex, | ||
"const-string v$replaceRegister, \"${PackageNameYouTubeMusic.valueOrThrow()}\"" | ||
) | ||
} | ||
} | ||
|
||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
.../kotlin/app/revanced/patches/youtube/general/music/fingerprints/AppDeepLinkFingerprint.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package app.revanced.patches.youtube.general.music.fingerprints | ||
|
||
import app.revanced.patcher.extensions.or | ||
import app.revanced.patcher.fingerprint.MethodFingerprint | ||
import app.revanced.util.getReference | ||
import app.revanced.util.indexOfFirstInstruction | ||
import com.android.tools.smali.dexlib2.AccessFlags | ||
import com.android.tools.smali.dexlib2.Opcode | ||
import com.android.tools.smali.dexlib2.iface.reference.FieldReference | ||
|
||
internal object AppDeepLinkFingerprint : MethodFingerprint( | ||
returnType = "V", | ||
accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL, | ||
parameters = listOf("L", "Ljava/util/Map;"), | ||
opcodes = listOf( | ||
Opcode.IGET_OBJECT, | ||
Opcode.INVOKE_VIRTUAL, | ||
Opcode.MOVE_RESULT, | ||
Opcode.CONST_STRING, | ||
), | ||
strings = listOf("android.intent.action.VIEW"), | ||
customFingerprint = { methodDef, _ -> | ||
methodDef.indexOfFirstInstruction { | ||
getReference<FieldReference>()?.name == "appDeepLinkEndpoint" | ||
} >= 0 | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.