feat(music): add return-youtube-dislike patch

This commit is contained in:
inotia00 2023-09-05 12:15:25 +09:00
parent 0a27088c8d
commit f97d6de1c5
11 changed files with 262 additions and 0 deletions

View File

@ -18,6 +18,7 @@ import app.revanced.util.enum.ResourceType.STYLE
class SharedResourceIdPatch : ResourcePatch {
internal companion object {
var ActionsContainer: Long = -1
var ButtonIconPaddingMedium: Long = -1
var ChipCloud: Long = -1
var ColorGrey: Long = -1
var DialogSolid: Long = -1
@ -39,6 +40,7 @@ class SharedResourceIdPatch : ResourcePatch {
?: throw PatchException("Failed to find resource id : $resourceName")
ActionsContainer = find(ID, "actions_container")
ButtonIconPaddingMedium = find(DIMEN, "button_icon_padding_medium")
ChipCloud = find(LAYOUT, "chip_cloud")
ColorGrey = find(COLOR, "ytm_color_grey_12")
DialogSolid = find(STYLE, "Theme.YouTubeMusic.Dialog.Solid")

View File

@ -0,0 +1,8 @@
package app.revanced.patches.music.utils.returnyoutubedislike.bytecode.fingerprints
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
object DislikeFingerprint : MethodFingerprint(
returnType = "V",
strings = listOf("like/dislike")
)

View File

@ -0,0 +1,8 @@
package app.revanced.patches.music.utils.returnyoutubedislike.bytecode.fingerprints
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
object LikeFingerprint : MethodFingerprint(
returnType = "V",
strings = listOf("like/like")
)

View File

@ -0,0 +1,8 @@
package app.revanced.patches.music.utils.returnyoutubedislike.bytecode.fingerprints
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
object RemoveLikeFingerprint : MethodFingerprint(
returnType = "V",
strings = listOf("like/removelike")
)

View File

@ -0,0 +1,12 @@
package app.revanced.patches.music.utils.returnyoutubedislike.bytecode.fingerprints
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patches.music.utils.resourceid.patch.SharedResourceIdPatch.Companion.ButtonIconPaddingMedium
import app.revanced.util.bytecode.isWideLiteralExists
import com.android.tools.smali.dexlib2.Opcode
object TextComponentFingerprint : MethodFingerprint(
returnType = "V",
opcodes = listOf(Opcode.CONST_HIGH16),
customFingerprint = { methodDef, _ -> methodDef.isWideLiteralExists(ButtonIconPaddingMedium) }
)

View File

@ -0,0 +1,98 @@
package app.revanced.patches.music.utils.returnyoutubedislike.bytecode.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.fingerprint.method.impl.MethodFingerprint
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.PatchException
import app.revanced.patcher.patch.annotations.DependsOn
import app.revanced.patches.music.utils.resourceid.patch.SharedResourceIdPatch
import app.revanced.patches.music.utils.returnyoutubedislike.bytecode.fingerprints.DislikeFingerprint
import app.revanced.patches.music.utils.returnyoutubedislike.bytecode.fingerprints.LikeFingerprint
import app.revanced.patches.music.utils.returnyoutubedislike.bytecode.fingerprints.RemoveLikeFingerprint
import app.revanced.patches.music.utils.returnyoutubedislike.bytecode.fingerprints.TextComponentFingerprint
import app.revanced.patches.music.utils.videoid.patch.VideoIdPatch
import app.revanced.util.integrations.Constants.MUSIC_UTILS_PATH
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
import com.android.tools.smali.dexlib2.iface.instruction.formats.Instruction35c
@DependsOn(
[
SharedResourceIdPatch::class,
VideoIdPatch::class
]
)
class ReturnYouTubeDislikeBytecodePatch : BytecodePatch(
listOf(
DislikeFingerprint,
LikeFingerprint,
RemoveLikeFingerprint,
TextComponentFingerprint
)
) {
override fun execute(context: BytecodeContext) {
listOf(
LikeFingerprint.toPatch(Vote.LIKE),
DislikeFingerprint.toPatch(Vote.DISLIKE),
RemoveLikeFingerprint.toPatch(Vote.REMOVE_LIKE)
).forEach { (fingerprint, vote) ->
with(fingerprint.result ?: throw fingerprint.exception) {
mutableMethod.addInstructions(
0,
"""
const/4 v0, ${vote.value}
invoke-static {v0}, $INTEGRATIONS_RYD_CLASS_DESCRIPTOR->sendVote(I)V
"""
)
}
}
TextComponentFingerprint.result?.let {
it.mutableMethod.apply {
var insertIndex = -1
for ((index, instruction) in implementation!!.instructions.withIndex()) {
if (instruction.opcode != Opcode.INVOKE_STATIC) continue
val reference = getInstruction<Instruction35c>(index).reference.toString()
if (!reference.endsWith("Ljava/lang/CharSequence;") && !reference.endsWith("Landroid/text/Spanned;")) continue
val insertRegister = getInstruction<OneRegisterInstruction>(index + 1).registerA
insertIndex = index + 2
addInstructions(
insertIndex, """
invoke-static {v$insertRegister}, $INTEGRATIONS_RYD_CLASS_DESCRIPTOR->onComponentCreated(Landroid/text/Spanned;)Landroid/text/Spanned;
move-result-object v$insertRegister
"""
)
break
}
if (insertIndex == -1)
throw PatchException("target Instruction not found!")
}
} ?: throw TextComponentFingerprint.exception
VideoIdPatch.injectCall("$INTEGRATIONS_RYD_CLASS_DESCRIPTOR->newVideoLoaded(Ljava/lang/String;)V")
}
private companion object {
const val INTEGRATIONS_RYD_CLASS_DESCRIPTOR =
"$MUSIC_UTILS_PATH/ReturnYouTubeDislikePatch;"
}
private fun MethodFingerprint.toPatch(voteKind: Vote) = VotePatch(this, voteKind)
private data class VotePatch(val fingerprint: MethodFingerprint, val voteKind: Vote)
private enum class Vote(val value: Int) {
LIKE(1),
DISLIKE(-1),
REMOVE_LIKE(0)
}
}

View File

@ -0,0 +1,60 @@
package app.revanced.patches.music.utils.returnyoutubedislike.resource.patch
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.data.ResourceContext
import app.revanced.patcher.patch.ResourcePatch
import app.revanced.patcher.patch.annotations.DependsOn
import app.revanced.patcher.patch.annotations.Patch
import app.revanced.patches.music.utils.annotations.MusicCompatibility
import app.revanced.patches.music.utils.returnyoutubedislike.bytecode.patch.ReturnYouTubeDislikeBytecodePatch
import app.revanced.patches.music.utils.settings.resource.patch.SettingsPatch
import app.revanced.util.resources.MusicResourceHelper.hookPreference
import app.revanced.util.resources.ResourceUtils
import app.revanced.util.resources.ResourceUtils.copyResources
@Patch
@Name("Return YouTube Dislike")
@Description("Shows the dislike count of videos using the Return YouTube Dislike API.")
@DependsOn(
[
ReturnYouTubeDislikeBytecodePatch::class,
SettingsPatch::class
]
)
@MusicCompatibility
class ReturnYouTubeDislikePatch : ResourcePatch {
override fun execute(context: ResourceContext) {
/**
* Copy preference
*/
arrayOf(
ResourceUtils.ResourceGroup(
"xml",
"returnyoutubedislike_prefs.xml"
)
).forEach { resourceGroup ->
context.copyResources("music/returnyoutubedislike", resourceGroup)
}
/**
* Hook RYD preference
*/
context.hookPreference(
"revanced_ryd_settings",
"com.google.android.apps.youtube.music.settings.fragment.AdvancedPrefsFragmentCompat"
)
val publicFile = context["res/values/public.xml"]
publicFile.writeText(
publicFile.readText()
.replace(
"\"advanced_prefs_compat\"",
"\"returnyoutubedislike_prefs\""
)
)
}
}

View File

@ -212,4 +212,46 @@ internal object MusicResourceHelper {
}
}
internal fun ResourceContext.hookPreference(
key: String,
fragment: String
) {
this.xmlEditor[YOUTUBE_MUSIC_SETTINGS_PATH].use { editor ->
with(editor.file) {
doRecursively loop@{
if (it !is Element) return@loop
it.getAttributeNode("android:key")?.let { attribute ->
if (attribute.textContent == "settings_header_about_youtube_music" && it.getAttributeNode(
"app:allowDividerBelow"
).textContent == "false"
) {
it.insertNode("Preference", it) {
setAttribute("android:persistent", "false")
setAttribute(
"android:title",
"@string/" + key + "_title"
)
setAttribute("android:key", key)
setAttribute("android:fragment", fragment)
setAttribute("app:allowDividerAbove", "false")
setAttribute("app:allowDividerAbove", "false")
}
it.getAttributeNode("app:allowDividerBelow").textContent = "true"
return@loop
}
}
}
doRecursively loop@{
if (it !is Element) return@loop
it.getAttributeNode("app:allowDividerBelow")?.let { attribute ->
if (attribute.textContent == "true") {
attribute.textContent = "false"
}
}
}
}
}
}
}

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:yt="http://schemas.android.com/apk/res-auto">
<com.google.android.apps.youtube.music.ui.preference.SwitchCompatPreference android:title="@string/revanced_ryd_enabled_title" android:key="revanced_ryd_enabled" android:summary="@string/revanced_ryd_enabled_summary" android:defaultValue="true" />
<com.google.android.apps.youtube.music.ui.preference.SwitchCompatPreference android:title="@string/revanced_ryd_dislike_percentage_title" android:key="revanced_ryd_dislike_percentage" android:summary="@string/revanced_ryd_dislike_percentage_summary" android:dependency="revanced_ryd_enabled" android:defaultValue="false" />
<com.google.android.apps.youtube.music.ui.preference.SwitchCompatPreference android:title="@string/revanced_ryd_compact_layout_title" android:key="revanced_ryd_compact_layout" android:summary="@string/revanced_ryd_compact_layout_summary" android:dependency="revanced_ryd_enabled" android:defaultValue="false" />
<com.google.android.apps.youtube.music.ui.preference.PreferenceCategoryCompat android:title="@string/revanced_ryd_about" android:key="about">
<Preference android:title="@string/revanced_ryd_attribution_title" android:key="revanced_ryd_attribution" android:summary="@string/revanced_ryd_attribution_summary">
<intent android:action="android.intent.action.VIEW" android:data="https://returnyoutubedislike.com" />
</Preference>
</com.google.android.apps.youtube.music.ui.preference.PreferenceCategoryCompat>
</PreferenceScreen>

View File

@ -93,6 +93,16 @@
<string name="revanced_hook_button_container_download_title">Hook download button</string>
<string name="revanced_reboot_message">Refresh and restart</string>
<string name="revanced_reset">Reset</string>
<string name="revanced_ryd_about">About</string>
<string name="revanced_ryd_attribution_summary">Data is provided by the Return YouTube Dislike API. Tap here to learn more.</string>
<string name="revanced_ryd_attribution_title">ReturnYouTubeDislike.com</string>
<string name="revanced_ryd_compact_layout_summary">Hides the separator of the like button.</string>
<string name="revanced_ryd_compact_layout_title">Compact like button</string>
<string name="revanced_ryd_dislike_percentage_summary">Instead of the number of dislikes, the percentage of dislikes is shown.</string>
<string name="revanced_ryd_dislike_percentage_title">Dislikes as percentage</string>
<string name="revanced_ryd_enabled_summary">Shows the dislike count of videos.</string>
<string name="revanced_ryd_failure_client_rate_limit_requested">Dislikes not available (client API limit reached)</string>
<string name="revanced_ryd_video_likes_hidden_by_video_owner">Hidden</string>
<string name="revanced_save_playback_speed">Changing default speed to:</string>
<string name="revanced_save_video_quality_mobile">Changing default mobile data quality to:</string>
<string name="revanced_save_video_quality_none">Failed to set quality</string>

View File

@ -2,4 +2,6 @@
<resources>
<!-- Translation Exception -->
<string name="revanced_extended_settings_title">ReVanced Extended</string>
<string name="revanced_ryd_enabled_title">@string/revanced_ryd_settings_title</string>
<string name="revanced_ryd_settings_title">Return YouTube Dislike</string>
</resources>