11package app.morphe.patches.youtube.layout.hide.endscreensuggestedvideo
22
3+ import app.morphe.patcher.Fingerprint
4+ import app.morphe.patcher.InstructionLocation.MatchAfterWithin
35import app.morphe.patcher.extensions.InstructionExtensions.addInstructionsWithLabels
4- import app.morphe.patcher.extensions.InstructionExtensions.getInstruction
6+ import app.morphe.patcher.fieldAccess
7+ import app.morphe.patcher.methodCall
58import app.morphe.patcher.patch.bytecodePatch
6- import app.morphe.patcher.util.smali.ExternalLabel
79import app.morphe.patches.shared.misc.settings.preference.SwitchPreference
810import app.morphe.patches.youtube.misc.extension.sharedExtensionPatch
911import app.morphe.patches.youtube.misc.settings.PreferenceScreen
1012import app.morphe.patches.youtube.shared.Constants.COMPATIBILITY_YOUTUBE
1113import app.morphe.util.getMutableMethod
1214import app.morphe.util.getReference
13- import app.morphe.util.indexOfFirstInstructionOrThrow
14- import app.morphe.util.indexOfFirstInstructionReversedOrThrow
15+ import com.android.tools.smali.dexlib2.AccessFlags
1516import com.android.tools.smali.dexlib2.Opcode
16- import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction
17+ import com.android.tools.smali.dexlib2.iface.reference.FieldReference
1718import com.android.tools.smali.dexlib2.iface.reference.MethodReference
1819
1920private const val EXTENSION_CLASS_DESCRIPTOR =
@@ -24,9 +25,7 @@ val hideEndScreenSuggestedVideoPatch = bytecodePatch(
2425 name = " Hide end screen suggested video" ,
2526 description = " Adds an option to hide the suggested video at the end of videos." ,
2627) {
27- dependsOn(
28- sharedExtensionPatch,
29- )
28+ dependsOn(sharedExtensionPatch)
3029
3130 compatibleWith(COMPATIBILITY_YOUTUBE )
3231
@@ -35,44 +34,56 @@ val hideEndScreenSuggestedVideoPatch = bytecodePatch(
3534 SwitchPreference (" morphe_end_screen_suggested_video" ),
3635 )
3736
38- RemoveOnLayoutChangeListenerFingerprint . let {
39- val endScreenMethod = it.instructionMatches.last().getInstruction< ReferenceInstruction >()
40- .getReference< MethodReference >() !! .getMutableMethod()
37+ val autoNavStatusMethod = AutoNavStatusFingerprint .match(
38+ AutoNavConstructorFingerprint .originalClassDef
39+ ).method
4140
42- endScreenMethod.apply {
43- val autoNavStatusMethodName = AutoNavStatusFingerprint .match(
44- AutoNavConstructorFingerprint .classDef
45- ).originalMethod.name
41+ val endScreenMethod = RemoveOnLayoutChangeListenerFingerprint .instructionMatches[1 ]
42+ .instruction.getReference<MethodReference >()!! .getMutableMethod()
4643
47- val invokeIndex = indexOfFirstInstructionOrThrow {
48- val reference = getReference<MethodReference >()
49- reference?.name == autoNavStatusMethodName &&
50- reference.returnType == " Z" &&
51- reference.parameterTypes.isEmpty()
52- }
53- val iGetObjectIndex = indexOfFirstInstructionReversedOrThrow(invokeIndex, Opcode .IGET_OBJECT )
54- val invokeReference = getInstruction<ReferenceInstruction >(invokeIndex).reference
55- val iGetObjectReference = getInstruction<ReferenceInstruction >(iGetObjectIndex).reference
56- val opcodeName = getInstruction(invokeIndex).opcode.name
44+ val endScreenSuggestedVideoFingerprint = Fingerprint (
45+ definingClass = endScreenMethod.definingClass,
46+ name = endScreenMethod.name,
47+ accessFlags = listOf (AccessFlags .PUBLIC , AccessFlags .FINAL ),
48+ returnType = " V" ,
49+ parameters = listOf (),
50+ filters = listOf (
51+ fieldAccess(
52+ opcode = Opcode .IGET_OBJECT ,
53+ definingClass = " this" ,
54+ type = autoNavStatusMethod.definingClass
55+ ),
56+ methodCall(
57+ opcode = Opcode .INVOKE_VIRTUAL ,
58+ smali = autoNavStatusMethod.toString(),
59+ location = MatchAfterWithin (3 )
60+ )
61+ )
62+ )
63+
64+ endScreenSuggestedVideoFingerprint.let {
65+ it.method.apply {
66+ val autoNavField = it.instructionMatches.first().instruction.getReference<FieldReference >()!!
5767
5868 addInstructionsWithLabels(
5969 0 ,
6070 """
6171 invoke-static {}, $EXTENSION_CLASS_DESCRIPTOR ->hideEndScreenSuggestedVideo()Z
6272 move-result v0
63- if-eqz v0, :show_end_screen_recommendation
73+ if-eqz v0, :ignore
6474
65- iget-object v0, p0, $iGetObjectReference
75+ iget-object v0, p0, $autoNavField
6676
6777 # This reference checks whether autoplay is turned on.
68- $opcodeName { v0 }, $invokeReference
78+ invoke-virtual { v0 }, $autoNavStatusMethod
6979 move-result v0
7080
7181 # Hide suggested video end screen only when autoplay is turned off.
72- if-nez v0, :show_end_screen_recommendation
82+ if-nez v0, :ignore
7383 return-void
74- """ ,
75- ExternalLabel (" show_end_screen_recommendation" , getInstruction(0 ))
84+ :ignore
85+ nop
86+ """
7687 )
7788 }
7889 }
0 commit comments