feat(music): add repace-cast-button patch

This commit is contained in:
inotia00 2023-09-29 21:17:27 +09:00
parent 2bd01e0307
commit 08d236bfb4
10 changed files with 258 additions and 0 deletions

View File

@ -0,0 +1,13 @@
package app.revanced.patches.music.player.replace.fingerprints
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patches.music.utils.resourceid.patch.SharedResourceIdPatch.Companion.PlayerCastMediaRouteButton
import app.revanced.util.bytecode.isWideLiteralExists
object CastButtonContainerFingerprint : MethodFingerprint(
returnType = "V",
customFingerprint = { methodDef, _ ->
methodDef.isWideLiteralExists(PlayerCastMediaRouteButton)
&& methodDef.definingClass.endsWith("/MusicActivity;")
}
)

View File

@ -0,0 +1,31 @@
package app.revanced.patches.music.player.replace.fingerprints
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import com.android.tools.smali.dexlib2.AccessFlags
import com.android.tools.smali.dexlib2.Opcode
object PlaybackStartDescriptorFingerprint : MethodFingerprint(
accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL,
returnType = "L",
parameters = listOf(
"Ljava/lang/String;",
"[B",
"Ljava/lang/String;",
"Ljava/lang/String;",
"I",
"I",
"Ljava/util/Set;",
"Ljava/lang/String;",
"Ljava/lang/String;",
"L",
"Z",
"Z"
),
opcodes = listOf(
Opcode.INVOKE_INTERFACE,
Opcode.MOVE_RESULT_OBJECT,
Opcode.CHECK_CAST,
Opcode.INVOKE_INTERFACE
)
)

View File

@ -0,0 +1,121 @@
package app.revanced.patches.music.player.replace.patch
import app.revanced.extensions.exception
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.InstructionExtensions.addInstruction
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.extensions.InstructionExtensions.removeInstruction
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.annotations.DependsOn
import app.revanced.patcher.patch.annotations.Patch
import app.revanced.patches.music.player.replace.fingerprints.CastButtonContainerFingerprint
import app.revanced.patches.music.player.replace.fingerprints.PlaybackStartDescriptorFingerprint
import app.revanced.patches.music.utils.annotations.MusicCompatibility
import app.revanced.patches.music.utils.resourceid.patch.SharedResourceIdPatch
import app.revanced.patches.music.utils.resourceid.patch.SharedResourceIdPatch.Companion.PlayerCastMediaRouteButton
import app.revanced.patches.music.utils.settings.resource.patch.SettingsPatch
import app.revanced.patches.music.utils.settings.resource.patch.SettingsPatch.Companion.contexts
import app.revanced.patches.music.utils.videotype.patch.VideoTypeHookPatch
import app.revanced.util.bytecode.getWideLiteralIndex
import app.revanced.util.enum.CategoryType
import app.revanced.util.integrations.Constants.MUSIC_PLAYER
import app.revanced.util.integrations.Constants.MUSIC_UTILS_PATH
import app.revanced.util.resources.ResourceUtils
import app.revanced.util.resources.ResourceUtils.copyResources
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.instruction.formats.Instruction35c
import com.android.tools.smali.dexlib2.iface.reference.MethodReference
@Patch(false)
@Name("Replace cast button")
@Description("Replace the cast button in the player with the open music button.")
@DependsOn(
[
SettingsPatch::class,
SharedResourceIdPatch::class,
VideoTypeHookPatch::class
]
)
@MusicCompatibility
class ReplaceCastButtonPatch : BytecodePatch(
listOf(
CastButtonContainerFingerprint,
PlaybackStartDescriptorFingerprint
)
) {
override fun execute(context: BytecodeContext) {
CastButtonContainerFingerprint.result?.let {
it.mutableMethod.apply {
val freeIndex = getWideLiteralIndex(PlayerCastMediaRouteButton) + 1
val freeRegister = getInstruction<OneRegisterInstruction>(freeIndex).registerA
val getActivityIndex = freeIndex - 4
val getActivityRegister = getInstruction<TwoRegisterInstruction>(getActivityIndex).registerB
val getActivityReference = getInstruction<ReferenceInstruction>(getActivityIndex).reference
for (index in freeIndex + 20 downTo freeIndex) {
if (getInstruction(index).opcode != Opcode.INVOKE_VIRTUAL)
continue
if ((getInstruction<ReferenceInstruction>(index).reference as MethodReference).name != "addView")
continue
val viewGroupInstruction = getInstruction<Instruction35c>(index)
addInstruction(
index + 1,
"invoke-static {v$freeRegister, v${viewGroupInstruction.registerC}, v${viewGroupInstruction.registerD}}, " +
MUSIC_PLAYER +
"->" +
"replaceCastButton(Landroid/app/Activity;Landroid/view/ViewGroup;Landroid/view/View;)V"
)
addInstruction(
index + 1,
"iget-object v$freeRegister, v$getActivityRegister, $getActivityReference"
)
removeInstruction(index)
break
}
}
} ?: throw CastButtonContainerFingerprint.exception
PlaybackStartDescriptorFingerprint.result?.let {
it.mutableMethod.apply {
val videoIdRegister = 1
val playlistIdRegister = 4
val playlistIndexRegister = 5
addInstruction(
0,
"invoke-static {p$videoIdRegister, p$playlistIdRegister, p$playlistIndexRegister}, " +
"$MUSIC_UTILS_PATH/CheckMusicVideoPatch;" +
"->" +
"playbackStart(Ljava/lang/String;Ljava/lang/String;I)V"
)
}
} ?: throw PlaybackStartDescriptorFingerprint.exception
arrayOf(
ResourceUtils.ResourceGroup(
"layout",
"open_music_button.xml"
)
).forEach { resourceGroup ->
contexts.copyResources("music/cast", resourceGroup)
}
SettingsPatch.addMusicPreference(
CategoryType.PLAYER,
"revanced_replace_player_cast_button",
"false"
)
}
}

View File

@ -27,6 +27,7 @@ class SharedResourceIdPatch : ResourcePatch {
var MenuEntry: Long = -1
var MusicMenuLikeButtons: Long = -1
var NamesInactiveAccountThumbnailSize: Long = -1
var PlayerCastMediaRouteButton: Long = -1
var PlayerOverlayChip: Long = -1
var PrivacyTosFooter: Long = -1
var QualityAuto: Long = -1
@ -53,6 +54,7 @@ class SharedResourceIdPatch : ResourcePatch {
MenuEntry = find(LAYOUT, "menu_entry")
MusicMenuLikeButtons = find(LAYOUT, "music_menu_like_buttons")
NamesInactiveAccountThumbnailSize = find(DIMEN, "names_inactive_account_thumbnail_size")
PlayerCastMediaRouteButton = find(LAYOUT, "player_cast_media_route_button")
PlayerOverlayChip = find(ID, "player_overlay_chip")
PrivacyTosFooter = find(ID, "privacy_tos_footer")
QualityAuto = find(STRING, "quality_auto")

View File

@ -48,6 +48,7 @@ class SettingsBytecodePatch : BytecodePatch(
}
} ?: throw PreferenceFingerprint.exception
context.injectInit("InitializationPatch", "setDeviceInformation", false)
context.injectInit("InitializationPatch", "initializeReVancedSettings", false)
}

View File

@ -0,0 +1,22 @@
package app.revanced.patches.music.utils.videotype.fingerprint
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import com.android.tools.smali.dexlib2.AccessFlags
import com.android.tools.smali.dexlib2.Opcode
object VideoTypeFingerprint : MethodFingerprint(
returnType = "L",
accessFlags = AccessFlags.PUBLIC or AccessFlags.STATIC,
parameters = listOf("L"),
opcodes = listOf(
Opcode.IGET,
Opcode.INVOKE_STATIC,
Opcode.MOVE_RESULT_OBJECT,
Opcode.IF_NEZ,
Opcode.SGET_OBJECT,
Opcode.GOTO,
Opcode.SGET_OBJECT,
Opcode.RETURN_OBJECT
)
)

View File

@ -0,0 +1,12 @@
package app.revanced.patches.music.utils.videotype.fingerprint
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import com.android.tools.smali.dexlib2.AccessFlags
object VideoTypeParentFingerprint : MethodFingerprint(
returnType = "Z",
accessFlags = AccessFlags.PUBLIC or AccessFlags.STATIC,
parameters = listOf("L", "L"),
strings = listOf("RQ")
)

View File

@ -0,0 +1,47 @@
package app.revanced.patches.music.utils.videotype.patch
import app.revanced.extensions.exception
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.removeInstruction
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint.Companion.resolve
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patches.music.utils.videotype.fingerprint.VideoTypeFingerprint
import app.revanced.patches.music.utils.videotype.fingerprint.VideoTypeParentFingerprint
import app.revanced.util.integrations.Constants.MUSIC_UTILS_PATH
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
class VideoTypeHookPatch : BytecodePatch(
listOf(VideoTypeParentFingerprint)
) {
override fun execute(context: BytecodeContext) {
VideoTypeParentFingerprint.result?.let { parentResult ->
VideoTypeFingerprint.also {
it.resolve(
context,
parentResult.classDef
)
}.result?.let {
it.mutableMethod.apply {
val videoTypeIndex = it.scanResult.patternScanResult!!.endIndex
val videoTypeRegister = getInstruction<OneRegisterInstruction>(videoTypeIndex).registerA
addInstructions(
videoTypeIndex + 1, """
invoke-static {v$videoTypeRegister}, $INTEGRATIONS_CLASS_DESCRIPTOR->setVideoType(Ljava/lang/Enum;)V
return-object v$videoTypeRegister
"""
)
removeInstruction(videoTypeIndex)
}
} ?: throw VideoTypeFingerprint.exception
} ?: throw VideoTypeParentFingerprint.exception
}
companion object {
private const val INTEGRATIONS_CLASS_DESCRIPTOR =
"$MUSIC_UTILS_PATH/VideoTypeHookPatch;"
}
}

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<com.google.android.libraries.youtube.common.ui.TouchImageView android:id="@+id/open_music_button" android:padding="@dimen/remix_overlay_player_control_button_padding" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/yt_fill_youtube_music_white_24" android:tint="@color/ytm_icon_color_active" android:contentDescription="@string/logo_button_accessibility" style="@style/MusicPlayerButton" />
</LinearLayout>

View File

@ -125,12 +125,16 @@
<string name="revanced_hide_upgrade_button_title">Hide upgrade button</string>
<string name="revanced_hook_action_bar_download_summary">Replaces the offline download button with an external download button.</string>
<string name="revanced_hook_action_bar_download_title">Hook download button</string>
<string name="revanced_playlist_dismiss">Already playing official music source</string>
<string name="revanced_playlist_error">Official music source not available</string>
<string name="revanced_reboot_first_run">Restart to load the layout normally</string>
<string name="revanced_reboot_message">Refresh and restart</string>
<string name="revanced_replace_flyout_panel_dismiss_queue_continue_watch_summary">When watching on YouTube, continue watching from the current time.</string>
<string name="revanced_replace_flyout_panel_dismiss_queue_continue_watch_title">Continue watching</string>
<string name="revanced_replace_flyout_panel_dismiss_queue_summary">Replaces dismiss queue menu to watch on YouTube.</string>
<string name="revanced_replace_flyout_panel_dismiss_queue_title">Replace dismiss queue</string>
<string name="revanced_replace_player_cast_button_summary">Replace the cast button in the player with the open music button. (Experimental)</string>
<string name="revanced_replace_player_cast_button_title">Replace cast button</string>
<string name="revanced_remember_repeat_state_summary">Remembers the state of the repeat.</string>
<string name="revanced_remember_repeat_state_title">Remember repeat state</string>
<string name="revanced_remember_shuffle_state_summary">Remembers the state of the shuffle.</string>