From 27b9ba8328317b1acca7a14b265ab86431204b8f Mon Sep 17 00:00:00 2001 From: inotia00 Date: Wed, 13 Sep 2023 12:03:51 +0900 Subject: [PATCH] feat(music/hide-flyout-panel): code refactoring --- .../hide/patch/FlyoutPanelPatch.kt | 15 +++++----- .../playbackspeed/patch/PlaybackSpeedPatch.kt | 4 +-- .../music/flyoutpanel/utils/EnumUtils.kt | 28 +++++++++++++++++++ .../fingerprints/MenuItemFingerprint.kt | 13 +++------ .../FlyoutButtonContainerResourcePatch.kt} | 10 +++++-- 5 files changed, 49 insertions(+), 21 deletions(-) create mode 100644 src/main/kotlin/app/revanced/patches/music/flyoutpanel/utils/EnumUtils.kt rename src/main/kotlin/app/revanced/patches/music/{flyoutpanel/hide => utils}/fingerprints/MenuItemFingerprint.kt (61%) rename src/main/kotlin/app/revanced/patches/music/utils/{flyoutbuttonhook/patch/FlyoutButtonHookPatch.kt => flyoutbutton/patch/FlyoutButtonContainerResourcePatch.kt} (78%) diff --git a/src/main/kotlin/app/revanced/patches/music/flyoutpanel/hide/patch/FlyoutPanelPatch.kt b/src/main/kotlin/app/revanced/patches/music/flyoutpanel/hide/patch/FlyoutPanelPatch.kt index f51d905f1..9ae71d9a6 100644 --- a/src/main/kotlin/app/revanced/patches/music/flyoutpanel/hide/patch/FlyoutPanelPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/flyoutpanel/hide/patch/FlyoutPanelPatch.kt @@ -10,9 +10,10 @@ import app.revanced.patcher.patch.BytecodePatch 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.flyoutpanel.hide.fingerprints.MenuItemFingerprint +import app.revanced.patches.music.utils.fingerprints.MenuItemFingerprint +import app.revanced.patches.music.flyoutpanel.utils.EnumUtils.getEnumIndex import app.revanced.patches.music.utils.annotations.MusicCompatibility -import app.revanced.patches.music.utils.flyoutbuttonhook.patch.FlyoutButtonHookPatch +import app.revanced.patches.music.utils.flyoutbutton.patch.FlyoutButtonContainerResourcePatch import app.revanced.patches.music.utils.settings.resource.patch.SettingsPatch import app.revanced.util.enum.CategoryType import app.revanced.util.integrations.Constants.MUSIC_FLYOUT @@ -26,7 +27,7 @@ import com.android.tools.smali.dexlib2.iface.instruction.TwoRegisterInstruction @Description("Hides flyout panel components.") @DependsOn( [ - FlyoutButtonHookPatch::class, + FlyoutButtonContainerResourcePatch::class, SettingsPatch::class ] ) @@ -42,15 +43,15 @@ class FlyoutPanelPatch : BytecodePatch( } val freeRegister = getInstruction(freeIndex).registerA - val targetIndex = it.scanResult.patternScanResult!!.startIndex + 3 - val targetRegister = getInstruction(targetIndex).registerA + val enumIndex = getEnumIndex() + val enumRegister = getInstruction(enumIndex).registerA val jumpInstruction = getInstruction(implementation!!.instructions.size - 1) addInstructionsWithLabels( - targetIndex, """ - invoke-static {v$targetRegister}, $MUSIC_FLYOUT->hideFlyoutPanels(Ljava/lang/Enum;)Z + enumIndex + 1, """ + invoke-static {v$enumRegister}, $MUSIC_FLYOUT->hideFlyoutPanels(Ljava/lang/Enum;)Z move-result v$freeRegister if-nez v$freeRegister, :hide """, ExternalLabel("hide", jumpInstruction) diff --git a/src/main/kotlin/app/revanced/patches/music/flyoutpanel/playbackspeed/patch/PlaybackSpeedPatch.kt b/src/main/kotlin/app/revanced/patches/music/flyoutpanel/playbackspeed/patch/PlaybackSpeedPatch.kt index 28d1d70ad..2e6a7f8c3 100644 --- a/src/main/kotlin/app/revanced/patches/music/flyoutpanel/playbackspeed/patch/PlaybackSpeedPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/flyoutpanel/playbackspeed/patch/PlaybackSpeedPatch.kt @@ -12,7 +12,7 @@ import app.revanced.patcher.patch.annotations.DependsOn import app.revanced.patcher.patch.annotations.Patch import app.revanced.patches.music.flyoutpanel.playbackspeed.fingerprints.FlyoutPanelLikeButtonFingerprint import app.revanced.patches.music.utils.annotations.MusicCompatibility -import app.revanced.patches.music.utils.flyoutbuttonhook.patch.FlyoutButtonHookPatch +import app.revanced.patches.music.utils.flyoutbutton.patch.FlyoutButtonContainerResourcePatch import app.revanced.patches.music.utils.overridespeed.patch.OverrideSpeedHookPatch import app.revanced.patches.music.utils.resourceid.patch.SharedResourceIdPatch import app.revanced.patches.music.utils.settings.resource.patch.SettingsPatch @@ -27,7 +27,7 @@ import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction @Description("Add playback speed button to the flyout panel.") @DependsOn( [ - FlyoutButtonHookPatch::class, + FlyoutButtonContainerResourcePatch::class, OverrideSpeedHookPatch::class, SettingsPatch::class, SharedResourceIdPatch::class diff --git a/src/main/kotlin/app/revanced/patches/music/flyoutpanel/utils/EnumUtils.kt b/src/main/kotlin/app/revanced/patches/music/flyoutpanel/utils/EnumUtils.kt new file mode 100644 index 000000000..662c93b3b --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/music/flyoutpanel/utils/EnumUtils.kt @@ -0,0 +1,28 @@ +package app.revanced.patches.music.flyoutpanel.utils + +import app.revanced.patcher.extensions.InstructionExtensions.getInstruction +import app.revanced.patcher.patch.PatchException +import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod +import com.android.tools.smali.dexlib2.Opcode +import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction + +internal object EnumUtils { + + fun MutableMethod.getEnumIndex(): Int { + var targetIndex = 0 + for ((index, instruction) in implementation!!.instructions.withIndex()) { + if (instruction.opcode != Opcode.INVOKE_STATIC) continue + + val targetParameter = getInstruction(index).reference + + if (!targetParameter.toString().contains("(I)")) continue + + targetIndex = index + 1 + break + } + if (targetIndex == 0) + throw PatchException("Target reference not found!") + + return targetIndex + } +} \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/music/flyoutpanel/hide/fingerprints/MenuItemFingerprint.kt b/src/main/kotlin/app/revanced/patches/music/utils/fingerprints/MenuItemFingerprint.kt similarity index 61% rename from src/main/kotlin/app/revanced/patches/music/flyoutpanel/hide/fingerprints/MenuItemFingerprint.kt rename to src/main/kotlin/app/revanced/patches/music/utils/fingerprints/MenuItemFingerprint.kt index 63cd0fa93..97e54ac62 100644 --- a/src/main/kotlin/app/revanced/patches/music/flyoutpanel/hide/fingerprints/MenuItemFingerprint.kt +++ b/src/main/kotlin/app/revanced/patches/music/utils/fingerprints/MenuItemFingerprint.kt @@ -1,4 +1,4 @@ -package app.revanced.patches.music.flyoutpanel.hide.fingerprints +package app.revanced.patches.music.utils.fingerprints import app.revanced.patcher.extensions.or import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint @@ -10,14 +10,9 @@ object MenuItemFingerprint : MethodFingerprint( accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL, parameters = emptyList(), opcodes = listOf( - Opcode.IGET, - Opcode.INVOKE_STATIC, - Opcode.MOVE_RESULT_OBJECT, - Opcode.IF_NEZ, - Opcode.SGET_OBJECT, - Opcode.INVOKE_INTERFACE, - Opcode.MOVE_RESULT, - Opcode.IF_EQZ + Opcode.CHECK_CAST, + Opcode.INVOKE_DIRECT, + Opcode.MOVE_RESULT_OBJECT ), strings = listOf("toggleMenuItemMutations") ) diff --git a/src/main/kotlin/app/revanced/patches/music/utils/flyoutbuttonhook/patch/FlyoutButtonHookPatch.kt b/src/main/kotlin/app/revanced/patches/music/utils/flyoutbutton/patch/FlyoutButtonContainerResourcePatch.kt similarity index 78% rename from src/main/kotlin/app/revanced/patches/music/utils/flyoutbuttonhook/patch/FlyoutButtonHookPatch.kt rename to src/main/kotlin/app/revanced/patches/music/utils/flyoutbutton/patch/FlyoutButtonContainerResourcePatch.kt index c4138ee33..252e5b17c 100644 --- a/src/main/kotlin/app/revanced/patches/music/utils/flyoutbuttonhook/patch/FlyoutButtonHookPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/utils/flyoutbutton/patch/FlyoutButtonContainerResourcePatch.kt @@ -1,11 +1,11 @@ -package app.revanced.patches.music.utils.flyoutbuttonhook.patch +package app.revanced.patches.music.utils.flyoutbutton.patch import app.revanced.patcher.data.ResourceContext import app.revanced.patcher.patch.ResourcePatch import app.revanced.util.resources.ResourceUtils import app.revanced.util.resources.ResourceUtils.copyResources -class FlyoutButtonHookPatch : ResourcePatch { +class FlyoutButtonContainerResourcePatch : ResourcePatch { override fun execute(context: ResourceContext) { /** @@ -26,8 +26,12 @@ class FlyoutButtonHookPatch : ResourcePatch { resourceGroups.forEach { context.copyResources("music/flyout", it) } } + val resourceFileNames = arrayOf( + "yt_outline_play_arrow_half_circle_black_24" + ).map { "$it.png" }.toTypedArray() + fun createGroup(directory: String) = ResourceUtils.ResourceGroup( - directory, "yt_outline_play_arrow_half_circle_black_24.png" + directory, *resourceFileNames ) arrayOf("xxxhdpi", "xxhdpi", "xhdpi", "hdpi", "mdpi")