From 6a6c85aea54f07b1853cbb758c1701ef058ce80c Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Mon, 9 Dec 2024 02:53:06 +0400 Subject: [PATCH] refactor: Extract internal utility method for overriding feature flags --- .../youtube/patches/OpenVideosFullscreen.java | 2 +- .../youtube/patches/theme/ThemePatch.java | 2 +- .../layout/miniplayer/MiniplayerPatch.kt | 42 +++++++++---------- .../player/fullscreen/OpenVideosFullscreen.kt | 24 +++-------- .../layout/seekbar/SeekbarColorPatch.kt | 35 ++++------------ .../youtube/layout/theme/ThemePatch.kt | 23 +++------- .../fix/cairo/DisableCairoSettingsPatch.kt | 5 +-- .../kotlin/app/revanced/util/BytecodeUtils.kt | 15 +++++++ 8 files changed, 56 insertions(+), 92 deletions(-) diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/OpenVideosFullscreen.java b/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/OpenVideosFullscreen.java index e66135e7f4..a15d7f6414 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/OpenVideosFullscreen.java +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/OpenVideosFullscreen.java @@ -8,7 +8,7 @@ public class OpenVideosFullscreen { /** * Injection point. */ - public static boolean openVideoFullscreenPortrait() { + public static boolean openVideoFullscreenPortrait(boolean original) { return Settings.OPEN_VIDEOS_FULLSCREEN_PORTRAIT.get(); } } diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/theme/ThemePatch.java b/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/theme/ThemePatch.java index 0f8fb3304a..0a8dfda9bb 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/theme/ThemePatch.java +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/theme/ThemePatch.java @@ -55,7 +55,7 @@ private static boolean anyEquals(int value, int... of) { /** * Injection point. */ - public static boolean gradientLoadingScreenEnabled() { + public static boolean gradientLoadingScreenEnabled(boolean original) { return GRADIENT_LOADING_SCREEN_ENABLED; } } diff --git a/patches/src/main/kotlin/app/revanced/patches/youtube/layout/miniplayer/MiniplayerPatch.kt b/patches/src/main/kotlin/app/revanced/patches/youtube/layout/miniplayer/MiniplayerPatch.kt index 2112c1b40f..57d40a6c52 100644 --- a/patches/src/main/kotlin/app/revanced/patches/youtube/layout/miniplayer/MiniplayerPatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/youtube/layout/miniplayer/MiniplayerPatch.kt @@ -240,14 +240,14 @@ val miniplayerPatch = bytecodePatch( ), ) - fun MutableMethod.insertBooleanOverride(index: Int, methodName: String) { + fun MutableMethod.insertMiniplayerBooleanOverride(index: Int, methodName: String) { val register = getInstruction(index).registerA addInstructions( index, """ - invoke-static {v$register}, $EXTENSION_CLASS_DESCRIPTOR->$methodName(Z)Z - move-result v$register - """, + invoke-static {v$register}, $EXTENSION_CLASS_DESCRIPTOR->$methodName(Z)Z + move-result v$register + """ ) } @@ -257,29 +257,25 @@ val miniplayerPatch = bytecodePatch( * Adds an override to force legacy tablet miniplayer to be used or not used. */ fun MutableMethod.insertLegacyTabletMiniplayerOverride(index: Int) { - insertBooleanOverride(index, "getLegacyTabletMiniplayerOverride") + insertMiniplayerBooleanOverride(index, "getLegacyTabletMiniplayerOverride") } /** * Adds an override to force modern miniplayer to be used or not used. */ fun MutableMethod.insertModernMiniplayerOverride(index: Int) { - insertBooleanOverride(index, "getModernMiniplayerOverride") + insertMiniplayerBooleanOverride(index, "getModernMiniplayerOverride") } - fun Fingerprint.insertLiteralValueBooleanOverride( + fun Fingerprint.insertMiniplayerFeatureFlagBooleanOverride( literal: Long, extensionMethod: String, - ) { - method.apply { - val literalIndex = indexOfFirstLiteralInstructionOrThrow(literal) - val targetIndex = indexOfFirstInstructionOrThrow(literalIndex, Opcode.MOVE_RESULT) - - insertBooleanOverride(targetIndex + 1, extensionMethod) - } - } + ) = method.insertFeatureFlagBooleanOverride( + literal, + "$EXTENSION_CLASS_DESCRIPTOR->$extensionMethod(Z)Z" + ) - fun Fingerprint.insertLiteralValueFloatOverride( + fun Fingerprint.insertMiniplayerFeatureFlagFloatOverride( literal: Long, extensionMethod: String, ) { @@ -370,24 +366,24 @@ val miniplayerPatch = bytecodePatch( } if (is_19_23_or_greater) { - miniplayerModernConstructorFingerprint.insertLiteralValueBooleanOverride( + miniplayerModernConstructorFingerprint.insertMiniplayerFeatureFlagBooleanOverride( MINIPLAYER_DRAG_DROP_FEATURE_KEY, "enableMiniplayerDragAndDrop", ) } if (is_19_25_or_greater) { - miniplayerModernConstructorFingerprint.insertLiteralValueBooleanOverride( + miniplayerModernConstructorFingerprint.insertMiniplayerFeatureFlagBooleanOverride( MINIPLAYER_MODERN_FEATURE_LEGACY_KEY, "getModernMiniplayerOverride", ) - miniplayerModernConstructorFingerprint.insertLiteralValueBooleanOverride( + miniplayerModernConstructorFingerprint.insertMiniplayerFeatureFlagBooleanOverride( MINIPLAYER_MODERN_FEATURE_KEY, "getModernFeatureFlagsActiveOverride", ) - miniplayerModernConstructorFingerprint.insertLiteralValueBooleanOverride( + miniplayerModernConstructorFingerprint.insertMiniplayerFeatureFlagBooleanOverride( MINIPLAYER_DOUBLE_TAP_FEATURE_KEY, "enableMiniplayerDoubleTapAction", ) @@ -426,19 +422,19 @@ val miniplayerPatch = bytecodePatch( } if (is_19_36_or_greater) { - miniplayerModernConstructorFingerprint.insertLiteralValueBooleanOverride( + miniplayerModernConstructorFingerprint.insertMiniplayerFeatureFlagBooleanOverride( MINIPLAYER_ROUNDED_CORNERS_FEATURE_KEY, "setRoundedCorners", ) } if (is_19_43_or_greater) { - miniplayerOnCloseHandlerFingerprint.insertLiteralValueBooleanOverride( + miniplayerOnCloseHandlerFingerprint.insertMiniplayerFeatureFlagBooleanOverride( MINIPLAYER_DISABLED_FEATURE_KEY, "getMiniplayerOnCloseHandler" ) - miniplayerModernConstructorFingerprint.insertLiteralValueBooleanOverride( + miniplayerModernConstructorFingerprint.insertMiniplayerFeatureFlagBooleanOverride( MINIPLAYER_HORIZONTAL_DRAG_FEATURE_KEY, "setHorizontalDrag", ) diff --git a/patches/src/main/kotlin/app/revanced/patches/youtube/layout/player/fullscreen/OpenVideosFullscreen.kt b/patches/src/main/kotlin/app/revanced/patches/youtube/layout/player/fullscreen/OpenVideosFullscreen.kt index 0111ea8806..621381ae5c 100644 --- a/patches/src/main/kotlin/app/revanced/patches/youtube/layout/player/fullscreen/OpenVideosFullscreen.kt +++ b/patches/src/main/kotlin/app/revanced/patches/youtube/layout/player/fullscreen/OpenVideosFullscreen.kt @@ -1,7 +1,5 @@ package app.revanced.patches.youtube.layout.player.fullscreen -import app.revanced.patcher.extensions.InstructionExtensions.addInstructions -import app.revanced.patcher.extensions.InstructionExtensions.getInstruction import app.revanced.patcher.patch.bytecodePatch import app.revanced.patches.all.misc.resources.addResources import app.revanced.patches.all.misc.resources.addResourcesPatch @@ -9,10 +7,7 @@ import app.revanced.patches.shared.misc.settings.preference.SwitchPreference import app.revanced.patches.youtube.misc.extension.sharedExtensionPatch import app.revanced.patches.youtube.misc.settings.PreferenceScreen import app.revanced.patches.youtube.misc.settings.settingsPatch -import app.revanced.util.indexOfFirstInstructionOrThrow -import app.revanced.util.indexOfFirstLiteralInstructionOrThrow -import com.android.tools.smali.dexlib2.Opcode -import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction +import app.revanced.util.insertFeatureFlagBooleanOverride private const val EXTENSION_CLASS_DESCRIPTOR = "Lapp/revanced/extension/youtube/patches/OpenVideosFullscreen;" @@ -35,19 +30,10 @@ val openVideosFullscreenPatch = bytecodePatch( ) execute { - openVideosFullscreenPortraitFingerprint.method.apply { - val constIndex = indexOfFirstLiteralInstructionOrThrow(OPEN_VIDEOS_FULLSCREEN_PORTRAIT_FEATURE_FLAG) - val resultIndex = indexOfFirstInstructionOrThrow(constIndex, Opcode.MOVE_RESULT) - val register = getInstruction(resultIndex).registerA - - addInstructions( - resultIndex + 1, - """ - invoke-static { }, $EXTENSION_CLASS_DESCRIPTOR->openVideoFullscreenPortrait()Z - move-result v$register - """ - ) - } + openVideosFullscreenPortraitFingerprint.method.insertFeatureFlagBooleanOverride( + OPEN_VIDEOS_FULLSCREEN_PORTRAIT_FEATURE_FLAG, + "$EXTENSION_CLASS_DESCRIPTOR->openVideoFullscreenPortrait(Z)Z" + ) // Add resources and setting last, in case the user force patches an old incompatible version. diff --git a/patches/src/main/kotlin/app/revanced/patches/youtube/layout/seekbar/SeekbarColorPatch.kt b/patches/src/main/kotlin/app/revanced/patches/youtube/layout/seekbar/SeekbarColorPatch.kt index a2f0797928..a9f347ad08 100644 --- a/patches/src/main/kotlin/app/revanced/patches/youtube/layout/seekbar/SeekbarColorPatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/youtube/layout/seekbar/SeekbarColorPatch.kt @@ -24,6 +24,7 @@ import app.revanced.util.getReference import app.revanced.util.indexOfFirstInstructionOrThrow import app.revanced.util.indexOfFirstLiteralInstructionOrThrow import app.revanced.util.inputStreamFromBundledResource +import app.revanced.util.insertFeatureFlagBooleanOverride import com.android.tools.smali.dexlib2.Opcode import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction import com.android.tools.smali.dexlib2.iface.instruction.TwoRegisterInstruction @@ -228,19 +229,10 @@ val seekbarColorPatch = bytecodePatch( // 19.25+ changes - playerSeekbarGradientConfigFingerprint.method.apply { - val literalIndex = indexOfFirstLiteralInstructionOrThrow(PLAYER_SEEKBAR_GRADIENT_FEATURE_FLAG) - val resultIndex = indexOfFirstInstructionOrThrow(literalIndex, Opcode.MOVE_RESULT) - val register = getInstruction(resultIndex).registerA - - addInstructions( - resultIndex + 1, - """ - invoke-static { v$register }, $EXTENSION_CLASS_DESCRIPTOR->playerSeekbarGradientEnabled(Z)Z - move-result v$register - """ - ) - } + playerSeekbarGradientConfigFingerprint.method.insertFeatureFlagBooleanOverride( + PLAYER_SEEKBAR_GRADIENT_FEATURE_FLAG, + "$EXTENSION_CLASS_DESCRIPTOR->playerSeekbarGradientEnabled(Z)Z" + ) lithoLinearGradientFingerprint.method.addInstruction( 0, @@ -255,19 +247,10 @@ val seekbarColorPatch = bytecodePatch( launchScreenLayoutTypeFingerprint, mainActivityOnCreateFingerprint ).forEach { fingerprint -> - fingerprint.method.apply { - val literalIndex = indexOfFirstLiteralInstructionOrThrow(launchScreenLayoutTypeLotteFeatureFlag) - val resultIndex = indexOfFirstInstructionOrThrow(literalIndex, Opcode.MOVE_RESULT) - val register = getInstruction(resultIndex).registerA - - addInstructions( - resultIndex + 1, - """ - invoke-static { v$register }, $EXTENSION_CLASS_DESCRIPTOR->useLotteLaunchSplashScreen(Z)Z - move-result v$register - """ - ) - } + fingerprint.method.insertFeatureFlagBooleanOverride( + launchScreenLayoutTypeLotteFeatureFlag, + "$EXTENSION_CLASS_DESCRIPTOR->useLotteLaunchSplashScreen(Z)Z" + ) } // Hook the splash animation drawable to set the a seekbar color theme. diff --git a/patches/src/main/kotlin/app/revanced/patches/youtube/layout/theme/ThemePatch.kt b/patches/src/main/kotlin/app/revanced/patches/youtube/layout/theme/ThemePatch.kt index c658ee676d..a5835d727f 100644 --- a/patches/src/main/kotlin/app/revanced/patches/youtube/layout/theme/ThemePatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/youtube/layout/theme/ThemePatch.kt @@ -1,7 +1,6 @@ package app.revanced.patches.youtube.layout.theme import app.revanced.patcher.extensions.InstructionExtensions.addInstructions -import app.revanced.patcher.extensions.InstructionExtensions.getInstruction import app.revanced.patcher.patch.PatchException import app.revanced.patcher.patch.bytecodePatch import app.revanced.patcher.patch.resourcePatch @@ -17,10 +16,7 @@ import app.revanced.patches.youtube.misc.extension.sharedExtensionPatch import app.revanced.patches.youtube.misc.settings.PreferenceScreen import app.revanced.patches.youtube.misc.settings.settingsPatch import app.revanced.util.forEachChildElement -import app.revanced.util.indexOfFirstInstructionOrThrow -import app.revanced.util.indexOfFirstLiteralInstructionOrThrow -import com.android.tools.smali.dexlib2.Opcode -import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction +import app.revanced.util.insertFeatureFlagBooleanOverride import org.w3c.dom.Element private const val EXTENSION_CLASS_DESCRIPTOR = @@ -212,19 +208,10 @@ val themePatch = bytecodePatch( SwitchPreference("revanced_gradient_loading_screen"), ) - useGradientLoadingScreenFingerprint.method.apply { - val literalIndex = indexOfFirstLiteralInstructionOrThrow(GRADIENT_LOADING_SCREEN_AB_CONSTANT) - val isEnabledIndex = indexOfFirstInstructionOrThrow(literalIndex, Opcode.MOVE_RESULT) - val isEnabledRegister = getInstruction(isEnabledIndex).registerA - - addInstructions( - isEnabledIndex + 1, - """ - invoke-static { }, $EXTENSION_CLASS_DESCRIPTOR->gradientLoadingScreenEnabled()Z - move-result v$isEnabledRegister - """, - ) - } + useGradientLoadingScreenFingerprint.method.insertFeatureFlagBooleanOverride( + GRADIENT_LOADING_SCREEN_AB_CONSTANT, + "$EXTENSION_CLASS_DESCRIPTOR->gradientLoadingScreenEnabled(Z)Z" + ) mapOf( themeHelperLightColorFingerprint to lightThemeBackgroundColor, diff --git a/patches/src/main/kotlin/app/revanced/patches/youtube/misc/fix/cairo/DisableCairoSettingsPatch.kt b/patches/src/main/kotlin/app/revanced/patches/youtube/misc/fix/cairo/DisableCairoSettingsPatch.kt index d6fafe5398..cd058ca590 100644 --- a/patches/src/main/kotlin/app/revanced/patches/youtube/misc/fix/cairo/DisableCairoSettingsPatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/youtube/misc/fix/cairo/DisableCairoSettingsPatch.kt @@ -34,10 +34,7 @@ internal val disableCairoSettingsPatch = bytecodePatch( * uYouPlus#1468. */ cairoFragmentConfigFingerprint.method.apply { - val literalIndex = indexOfFirstLiteralInstructionOrThrow( - CAIRO_CONFIG_LITERAL_VALUE, - ) - + val literalIndex = indexOfFirstLiteralInstructionOrThrow(CAIRO_CONFIG_LITERAL_VALUE) val resultIndex = indexOfFirstInstructionOrThrow(literalIndex, Opcode.MOVE_RESULT) val register = getInstruction(resultIndex).registerA diff --git a/patches/src/main/kotlin/app/revanced/util/BytecodeUtils.kt b/patches/src/main/kotlin/app/revanced/util/BytecodeUtils.kt index ec75cb0a5d..8e5a85056a 100644 --- a/patches/src/main/kotlin/app/revanced/util/BytecodeUtils.kt +++ b/patches/src/main/kotlin/app/revanced/util/BytecodeUtils.kt @@ -17,6 +17,7 @@ import app.revanced.patches.shared.misc.mapping.resourceMappings import com.android.tools.smali.dexlib2.Opcode import com.android.tools.smali.dexlib2.iface.Method import com.android.tools.smali.dexlib2.iface.instruction.Instruction +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.WideLiteralInstruction import com.android.tools.smali.dexlib2.iface.reference.Reference @@ -402,6 +403,20 @@ fun Method.findInstructionIndicesReversedOrThrow(opcode: Opcode): List { return instructions } +internal fun MutableMethod.insertFeatureFlagBooleanOverride(literal: Long, extensionsMethod: String) { + val literalIndex = indexOfFirstLiteralInstructionOrThrow(literal) + val index = indexOfFirstInstructionOrThrow(literalIndex, Opcode.MOVE_RESULT) + val register = getInstruction(index).registerA + + addInstructions( + index + 1, + """ + invoke-static { v$register }, $extensionsMethod + move-result v$register + """ + ) +} + /** * Called for _all_ instructions with the given literal value. */