From be62437ab3a55100ecec57f7dea44eae09ea84f0 Mon Sep 17 00:00:00 2001 From: inotia00 Date: Sat, 7 Jan 2023 17:01:39 +0900 Subject: [PATCH] minor improve --- .../fingerprints/PivotBarFingerprint.kt | 25 +++++++ .../patch/CreateButtonRemoverBytecodePatch.kt | 65 +++++++++++-------- .../patch/DoubleBackToClosePatch.kt | 1 - .../resourceid/patch/SharedResourceIdPatch.kt | 2 + .../swiperefresh/patch/SwipeRefreshPatch.kt | 3 +- 5 files changed, 66 insertions(+), 30 deletions(-) create mode 100644 src/main/kotlin/app/revanced/patches/youtube/layout/general/pivotbar/createbutton/bytecode/fingerprints/PivotBarFingerprint.kt diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/general/pivotbar/createbutton/bytecode/fingerprints/PivotBarFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/general/pivotbar/createbutton/bytecode/fingerprints/PivotBarFingerprint.kt new file mode 100644 index 000000000..0c7fdce04 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/general/pivotbar/createbutton/bytecode/fingerprints/PivotBarFingerprint.kt @@ -0,0 +1,25 @@ +package app.revanced.patches.youtube.layout.general.pivotbar.createbutton.bytecode.fingerprints + +import app.revanced.patcher.extensions.or +import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint +import app.revanced.patches.youtube.misc.resourceid.patch.SharedResourcdIdPatch +import org.jf.dexlib2.iface.instruction.WideLiteralInstruction +import org.jf.dexlib2.AccessFlags +import org.jf.dexlib2.Opcode + +object PivotBarFingerprint : MethodFingerprint( + returnType = "L", + access = AccessFlags.PUBLIC or AccessFlags.FINAL, + opcodes = listOf( + Opcode.INVOKE_VIRTUAL, + Opcode.MOVE_RESULT_OBJECT, + Opcode.RETURN_OBJECT + ), + customFingerprint = { methodDef -> + methodDef.definingClass == "Lcom/google/android/apps/youtube/app/ui/pivotbar/PivotBar;" && + methodDef.implementation?.instructions?.any { instruction -> + instruction.opcode.ordinal == Opcode.CONST.ordinal && + (instruction as? WideLiteralInstruction)?.wideLiteral == SharedResourcdIdPatch.imageWithTextTabId + } == true + } +) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/general/pivotbar/createbutton/bytecode/patch/CreateButtonRemoverBytecodePatch.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/general/pivotbar/createbutton/bytecode/patch/CreateButtonRemoverBytecodePatch.kt index 523bb6f55..dfd31ca27 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/layout/general/pivotbar/createbutton/bytecode/patch/CreateButtonRemoverBytecodePatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/general/pivotbar/createbutton/bytecode/patch/CreateButtonRemoverBytecodePatch.kt @@ -3,17 +3,18 @@ package app.revanced.patches.youtube.layout.general.pivotbar.createbutton.byteco import app.revanced.patcher.annotation.Name import app.revanced.patcher.annotation.Version import app.revanced.patcher.data.BytecodeContext -import app.revanced.patcher.patch.annotations.DependsOn import app.revanced.patcher.patch.BytecodePatch import app.revanced.patcher.patch.PatchResult import app.revanced.patcher.patch.PatchResultError import app.revanced.patcher.patch.PatchResultSuccess -import app.revanced.patches.youtube.layout.general.pivotbar.createbutton.bytecode.fingerprints.PivotBarCreateButtonViewFingerprint +import app.revanced.patcher.patch.annotations.DependsOn +import app.revanced.patches.youtube.layout.general.pivotbar.createbutton.bytecode.fingerprints.* import app.revanced.patches.youtube.misc.resourceid.patch.SharedResourcdIdPatch import app.revanced.shared.annotation.YouTubeCompatibility +import app.revanced.shared.extensions.toErrorResult import app.revanced.shared.util.integrations.Constants.GENERAL_LAYOUT -import app.revanced.shared.util.pivotbar.InjectionUtils.injectHook import app.revanced.shared.util.pivotbar.InjectionUtils.REGISTER_TEMPLATE_REPLACEMENT +import app.revanced.shared.util.pivotbar.InjectionUtils.injectHook import org.jf.dexlib2.dexbacked.reference.DexBackedMethodReference import org.jf.dexlib2.iface.instruction.ReferenceInstruction @@ -22,7 +23,10 @@ import org.jf.dexlib2.iface.instruction.ReferenceInstruction @YouTubeCompatibility @Version("0.0.1") class CreateButtonRemoverBytecodePatch : BytecodePatch( - listOf(PivotBarCreateButtonViewFingerprint) + listOf( + PivotBarCreateButtonViewFingerprint, + PivotBarFingerprint + ) ) { override fun execute(context: BytecodeContext): PatchResult { @@ -30,31 +34,35 @@ class CreateButtonRemoverBytecodePatch : BytecodePatch( * Resolve fingerprints */ - val createButtonResult = PivotBarCreateButtonViewFingerprint.result ?: return PatchResultError("PivotBarCreateButtonViewFingerprint failed") - val createButtonMethod = createButtonResult.mutableMethod - val createButtonInstructions = createButtonMethod.implementation!!.instructions + PivotBarFingerprint.result?.let { result -> + val startIndex = result.scanResult.patternScanResult!!.startIndex + val pivotBarInstructions = result.mutableMethod.implementation!!.instructions + createRef = (pivotBarInstructions.elementAt(startIndex) as ReferenceInstruction).reference as DexBackedMethodReference + } ?: return PivotBarFingerprint.toErrorResult() - createButtonInstructions.filter { instruction -> - val fieldReference = (instruction as? ReferenceInstruction)?.reference as? DexBackedMethodReference - fieldReference?.let { it.definingClass == "Lcom/google/android/apps/youtube/app/ui/pivotbar/PivotBar;" && it.name == "c" } == true - }.forEach { instruction -> - if (!isSeondary) { - isSeondary = true; - return@forEach + PivotBarCreateButtonViewFingerprint.result?.let { result -> + with (result.mutableMethod){ + val createButtonInstructions = implementation!!.instructions + createButtonInstructions.filter { instruction -> + val fieldReference = (instruction as? ReferenceInstruction)?.reference as? DexBackedMethodReference + fieldReference?.let { it.definingClass == createRef.definingClass && it.name == createRef.name } == true + }.forEach { instruction -> + if (!isSeondary) { + isSeondary = true; + return@forEach + } + + /* + * Inject hooks + */ + + injectHook(hook, createButtonInstructions.indexOf(instruction) + 2) + + return PatchResultSuccess() + } + return PatchResultError("Could not find the method to hook.") } - - insertIndex = createButtonInstructions.indexOf(instruction) + 2 - - /* - * Inject hooks - */ - - createButtonMethod.injectHook(hook, insertIndex) - - return PatchResultSuccess() - } - - return PatchResultError("Could not find the method to hook.") + } ?: return PivotBarCreateButtonViewFingerprint.toErrorResult() } internal companion object { @@ -63,7 +71,8 @@ class CreateButtonRemoverBytecodePatch : BytecodePatch( "->" + "hideCreateButton(Landroid/view/View;)V" - private var insertIndex: Int = 0 + private lateinit var createRef: DexBackedMethodReference + private var isSeondary: Boolean = false } } diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/doublebacktoclose/patch/DoubleBackToClosePatch.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/doublebacktoclose/patch/DoubleBackToClosePatch.kt index ff030a184..5a324bef7 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/misc/doublebacktoclose/patch/DoubleBackToClosePatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/doublebacktoclose/patch/DoubleBackToClosePatch.kt @@ -1,6 +1,5 @@ package app.revanced.patches.youtube.misc.doublebacktoclose.patch -import app.revanced.patcher.annotation.Description import app.revanced.patcher.annotation.Name import app.revanced.patcher.annotation.Version import app.revanced.patcher.data.BytecodeContext diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/resourceid/patch/SharedResourceIdPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/resourceid/patch/SharedResourceIdPatch.kt index e1e4a6693..1e231130a 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/misc/resourceid/patch/SharedResourceIdPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/resourceid/patch/SharedResourceIdPatch.kt @@ -25,6 +25,7 @@ class SharedResourcdIdPatch : ResourcePatch { var educationTextViewResourceId: Long = -1 var emptycolorLabelId: Long = -1 var imageOnlyTabId: Long = -1 + var imageWithTextTabId: Long = -1 var layoutCircle: Long = -1 var layoutIcon: Long = -1 var layoutVideo: Long = -1 @@ -47,6 +48,7 @@ class SharedResourcdIdPatch : ResourcePatch { educationTextViewResourceId = findSharedResourceId("id", "user_education_text_view") emptycolorLabelId = findSharedResourceId("color", "inline_time_bar_colorized_bar_empty_color_dark") imageOnlyTabId = findSharedResourceId("layout", "image_only_tab") + imageWithTextTabId = findSharedResourceId("layout", "image_with_text_tab") layoutCircle = findSharedResourceId("layout", "endscreen_element_layout_circle") layoutIcon = findSharedResourceId("layout", "endscreen_element_layout_icon") layoutVideo = findSharedResourceId("layout", "endscreen_element_layout_video") diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/swiperefresh/patch/SwipeRefreshPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/swiperefresh/patch/SwipeRefreshPatch.kt index 4a5065ca2..7a5169c98 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/misc/swiperefresh/patch/SwipeRefreshPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/swiperefresh/patch/SwipeRefreshPatch.kt @@ -11,6 +11,7 @@ import app.revanced.patcher.patch.PatchResult import app.revanced.patcher.patch.PatchResultSuccess import app.revanced.patches.youtube.misc.swiperefresh.fingerprint.SwipeRefreshLayoutFingerprint import app.revanced.shared.annotation.YouTubeCompatibility +import app.revanced.shared.extensions.toErrorResult import org.jf.dexlib2.iface.instruction.OneRegisterInstruction @Name("enable-swipe-refresh") @@ -23,7 +24,7 @@ class SwipeRefreshPatch : BytecodePatch( ) ) { override fun execute(context: BytecodeContext): PatchResult { - val result = SwipeRefreshLayoutFingerprint.result!! + val result = SwipeRefreshLayoutFingerprint.result ?:return SwipeRefreshLayoutFingerprint.toErrorResult() val method = result.mutableMethod val index = result.scanResult.patternScanResult!!.endIndex val register = (method.instruction(index) as OneRegisterInstruction).registerA