From 6e667c81737fdd26cb4a5b94b2faa871d20a8894 Mon Sep 17 00:00:00 2001 From: inotia00 Date: Sun, 16 Apr 2023 13:11:17 +0900 Subject: [PATCH] refactor(hide-new-playlist): apply a much safer patching method --- .../fingerprints/FloatingButtonFingerprint.kt | 18 +---------- .../FloatingButtonParentFingerprint.kt | 20 ++++++++++++ .../patch/NewPlaylistButtonPatch.kt | 31 ++++++++----------- .../resourceid/patch/SharedResourceIdPatch.kt | 2 -- .../music/settings/host/values/strings.xml | 3 +- 5 files changed, 35 insertions(+), 39 deletions(-) create mode 100644 src/main/kotlin/app/revanced/patches/music/layout/floatingbutton/fingerprints/FloatingButtonParentFingerprint.kt diff --git a/src/main/kotlin/app/revanced/patches/music/layout/floatingbutton/fingerprints/FloatingButtonFingerprint.kt b/src/main/kotlin/app/revanced/patches/music/layout/floatingbutton/fingerprints/FloatingButtonFingerprint.kt index 4bd0f162e..033c349bc 100644 --- a/src/main/kotlin/app/revanced/patches/music/layout/floatingbutton/fingerprints/FloatingButtonFingerprint.kt +++ b/src/main/kotlin/app/revanced/patches/music/layout/floatingbutton/fingerprints/FloatingButtonFingerprint.kt @@ -2,8 +2,6 @@ package app.revanced.patches.music.layout.floatingbutton.fingerprints import app.revanced.patcher.extensions.or import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint -import app.revanced.patches.music.misc.resourceid.patch.SharedResourceIdPatch -import org.jf.dexlib2.iface.instruction.WideLiteralInstruction import org.jf.dexlib2.AccessFlags import org.jf.dexlib2.Opcode @@ -11,20 +9,6 @@ object FloatingButtonFingerprint : MethodFingerprint( returnType = "V", access = AccessFlags.PRIVATE or AccessFlags.FINAL, parameters = listOf("L"), - opcodes = listOf( - Opcode.CONST, - Opcode.INVOKE_VIRTUAL, - Opcode.MOVE_RESULT_OBJECT, - Opcode.MOVE_OBJECT, - Opcode.CHECK_CAST, - Opcode.CONST_4, - Opcode.INVOKE_VIRTUAL - ), - customFingerprint = { methodDef -> - methodDef.implementation?.instructions?.any { - it.opcode.ordinal == Opcode.CONST.ordinal && - (it as? WideLiteralInstruction)?.wideLiteral == SharedResourceIdPatch.floatingActionButtonLabelId - } == true - } + opcodes = listOf(Opcode.AND_INT_LIT16) ) diff --git a/src/main/kotlin/app/revanced/patches/music/layout/floatingbutton/fingerprints/FloatingButtonParentFingerprint.kt b/src/main/kotlin/app/revanced/patches/music/layout/floatingbutton/fingerprints/FloatingButtonParentFingerprint.kt new file mode 100644 index 000000000..b19f0cd25 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/music/layout/floatingbutton/fingerprints/FloatingButtonParentFingerprint.kt @@ -0,0 +1,20 @@ +package app.revanced.patches.music.layout.floatingbutton.fingerprints + +import app.revanced.patcher.extensions.or +import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint +import org.jf.dexlib2.AccessFlags +import org.jf.dexlib2.Opcode +import org.jf.dexlib2.iface.instruction.NarrowLiteralInstruction + +object FloatingButtonParentFingerprint : MethodFingerprint( + returnType = "V", + access = AccessFlags.PROTECTED or AccessFlags.FINAL, + parameters = listOf("L"), + opcodes = listOf(Opcode.INVOKE_DIRECT), + customFingerprint = { methodDef -> + methodDef.implementation!!.instructions.any { + ((it as? NarrowLiteralInstruction)?.narrowLiteral == 259982244) + } + } +) + diff --git a/src/main/kotlin/app/revanced/patches/music/layout/floatingbutton/patch/NewPlaylistButtonPatch.kt b/src/main/kotlin/app/revanced/patches/music/layout/floatingbutton/patch/NewPlaylistButtonPatch.kt index 885a1e02d..ff03da746 100644 --- a/src/main/kotlin/app/revanced/patches/music/layout/floatingbutton/patch/NewPlaylistButtonPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/layout/floatingbutton/patch/NewPlaylistButtonPatch.kt @@ -7,18 +7,18 @@ import app.revanced.patcher.annotation.Version import app.revanced.patcher.data.BytecodeContext import app.revanced.patcher.extensions.addInstructions import app.revanced.patcher.extensions.instruction +import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint.Companion.resolve import app.revanced.patcher.patch.BytecodePatch import app.revanced.patcher.patch.PatchResult import app.revanced.patcher.patch.PatchResultSuccess import app.revanced.patcher.patch.annotations.DependsOn import app.revanced.patcher.patch.annotations.Patch import app.revanced.patcher.util.smali.ExternalLabel -import app.revanced.patches.music.layout.floatingbutton.fingerprints.FloatingButtonFingerprint +import app.revanced.patches.music.layout.floatingbutton.fingerprints.* import app.revanced.patches.music.misc.resourceid.patch.SharedResourceIdPatch import app.revanced.patches.music.misc.settings.patch.MusicSettingsPatch import app.revanced.patches.shared.annotation.YouTubeMusicCompatibility import app.revanced.util.integrations.Constants.MUSIC_SETTINGS_PATH -import org.jf.dexlib2.iface.instruction.formats.Instruction35c @Patch @Name("hide-new-playlist") @@ -33,28 +33,23 @@ import org.jf.dexlib2.iface.instruction.formats.Instruction35c @Version("0.0.1") class NewPlaylistButtonPatch : BytecodePatch( listOf( - FloatingButtonFingerprint + FloatingButtonParentFingerprint ) ) { override fun execute(context: BytecodeContext): PatchResult { - FloatingButtonFingerprint.result?.let { - with (it.mutableMethod) { - val targetIndex = it.scanResult.patternScanResult!!.endIndex - - val targetRegister = (instruction(targetIndex) as Instruction35c).registerC - val dummyRegister = (instruction(targetIndex) as Instruction35c).registerD - - addInstructions( - targetIndex + 1, """ + FloatingButtonParentFingerprint.result?.let { parentResult -> + FloatingButtonFingerprint.also { it.resolve(context, parentResult.classDef) }.result?.mutableMethod?.let { + it.addInstructions( + 1, """ invoke-static {}, $MUSIC_SETTINGS_PATH->hideNewPlaylistButton()Z - move-result v$dummyRegister - if-eqz v$dummyRegister, :show - const/4 v$targetRegister, 0x0 - """, listOf(ExternalLabel("show", instruction(targetIndex + 1))) + move-result v0 + if-eqz v0, :show + return-void + """, listOf(ExternalLabel("show", it.instruction(1))) ) - } - } ?: return FloatingButtonFingerprint.toErrorResult() + } ?: return FloatingButtonFingerprint.toErrorResult() + } ?: return FloatingButtonParentFingerprint.toErrorResult() MusicSettingsPatch.addMusicPreference("navigation", "revanced_hide_new_playlist_button", "false") diff --git a/src/main/kotlin/app/revanced/patches/music/misc/resourceid/patch/SharedResourceIdPatch.kt b/src/main/kotlin/app/revanced/patches/music/misc/resourceid/patch/SharedResourceIdPatch.kt index 3ba920dc3..979cc59f3 100644 --- a/src/main/kotlin/app/revanced/patches/music/misc/resourceid/patch/SharedResourceIdPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/misc/resourceid/patch/SharedResourceIdPatch.kt @@ -21,7 +21,6 @@ class SharedResourceIdPatch : ResourcePatch { var colorGreyLabelId: Long = -1 var dialogSolidLabelId: Long = -1 var disabledIconLabelId: Long = -1 - var floatingActionButtonLabelId: Long = -1 var isTabletLabelId: Long = -1 } @@ -34,7 +33,6 @@ class SharedResourceIdPatch : ResourcePatch { colorGreyLabelId = find(COLOR, "ytm_color_grey_12") dialogSolidLabelId = find(STYLE, "Theme.YouTubeMusic.Dialog.Solid") disabledIconLabelId = find(DIMEN, "disabled_icon_alpha") - floatingActionButtonLabelId = find(ID, "floating_action_button") isTabletLabelId = find(BOOL, "is_tablet") return PatchResultSuccess() diff --git a/src/main/resources/music/settings/host/values/strings.xml b/src/main/resources/music/settings/host/values/strings.xml index 84b42e609..49d048c7a 100644 --- a/src/main/resources/music/settings/host/values/strings.xml +++ b/src/main/resources/music/settings/host/values/strings.xml @@ -36,8 +36,7 @@ "Hide ads before playing a track. (requires an app restart)" Hide music ads - "Hide the New Playlist button in the Library tab. -(requires an app restart)" + Hide the New Playlist button in the Library tab. Hide new playlist button "Hides the playlist card from homepage. (requires an app restart)"