|
| 1 | +@file:Suppress("SpellCheckingInspection") |
| 2 | + |
| 3 | +package app.revanced.patches.music.layout.miniplayercolor |
| 4 | + |
| 5 | +import app.revanced.patcher.extensions.InstructionExtensions.getInstruction |
| 6 | +import app.revanced.patcher.patch.bytecodePatch |
| 7 | +import app.revanced.patches.all.misc.resources.addResources |
| 8 | +import app.revanced.patches.all.misc.resources.addResourcesPatch |
| 9 | +import app.revanced.patches.music.misc.extension.sharedExtensionPatch |
| 10 | +import app.revanced.patches.music.misc.settings.PreferenceScreen |
| 11 | +import app.revanced.patches.music.misc.settings.settingsPatch |
| 12 | +import app.revanced.patches.shared.misc.mapping.get |
| 13 | +import app.revanced.patches.shared.misc.mapping.resourceMappingPatch |
| 14 | +import app.revanced.patches.shared.misc.mapping.resourceMappings |
| 15 | +import app.revanced.patches.shared.misc.settings.preference.SwitchPreference |
| 16 | +import app.revanced.util.addInstructionsAtControlFlowLabel |
| 17 | +import app.revanced.util.findFreeRegister |
| 18 | +import app.revanced.util.getReference |
| 19 | +import app.revanced.util.indexOfFirstInstructionOrThrow |
| 20 | +import app.revanced.util.indexOfFirstInstructionReversedOrThrow |
| 21 | +import com.android.tools.smali.dexlib2.AccessFlags |
| 22 | +import com.android.tools.smali.dexlib2.Opcode |
| 23 | +import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction |
| 24 | +import com.android.tools.smali.dexlib2.iface.reference.FieldReference |
| 25 | +import com.android.tools.smali.dexlib2.iface.reference.MethodReference |
| 26 | + |
| 27 | +internal var mpp_player_bottom_sheet = -1L |
| 28 | + private set |
| 29 | + |
| 30 | +private const val EXTENSION_CLASS_DESCRIPTOR = "Lapp/revanced/extension/music/patches/ChangeMiniplayerColorPatch;" |
| 31 | + |
| 32 | +@Suppress("unused") |
| 33 | +val changeMiniplayerColor = bytecodePatch( |
| 34 | + name = "Change miniplayer color", |
| 35 | + description = "Adds an option to change the miniplayer background color to match the fullscreen player." |
| 36 | +) { |
| 37 | + dependsOn( |
| 38 | + sharedExtensionPatch, |
| 39 | + settingsPatch, |
| 40 | + addResourcesPatch, |
| 41 | + resourceMappingPatch |
| 42 | + ) |
| 43 | + |
| 44 | + compatibleWith( |
| 45 | + "com.google.android.apps.youtube.music"( |
| 46 | + "7.29.52", |
| 47 | + "8.10.52" |
| 48 | + ) |
| 49 | + ) |
| 50 | + |
| 51 | + execute { |
| 52 | + mpp_player_bottom_sheet = resourceMappings["id", "mpp_player_bottom_sheet"] |
| 53 | + |
| 54 | + addResources("music", "layout.miniplayercolor.changeMiniplayerColor") |
| 55 | + |
| 56 | + PreferenceScreen.PLAYER.addPreferences( |
| 57 | + SwitchPreference("revanced_music_change_miniplayer_color"), |
| 58 | + ) |
| 59 | + |
| 60 | + switchToggleColorFingerprint.match(miniPlayerConstructorFingerprint.classDef).let { |
| 61 | + val relativeIndex = it.patternMatch!!.endIndex + 1 |
| 62 | + |
| 63 | + val invokeVirtualIndex = it.method.indexOfFirstInstructionOrThrow( |
| 64 | + relativeIndex, Opcode.INVOKE_VIRTUAL |
| 65 | + ) |
| 66 | + val colorMathPlayerInvokeVirtualReference = it.method |
| 67 | + .getInstruction<ReferenceInstruction>(invokeVirtualIndex).reference |
| 68 | + |
| 69 | + val iGetIndex = it.method.indexOfFirstInstructionOrThrow( |
| 70 | + relativeIndex, Opcode.IGET |
| 71 | + ) |
| 72 | + val colorMathPlayerIGetReference = it.method |
| 73 | + .getInstruction<ReferenceInstruction>(iGetIndex).reference as FieldReference |
| 74 | + |
| 75 | + val colorGreyIndex = miniPlayerConstructorFingerprint.method.indexOfFirstInstructionReversedOrThrow { |
| 76 | + getReference<MethodReference>()?.name == "getColor" |
| 77 | + } |
| 78 | + val iPutIndex = miniPlayerConstructorFingerprint.method.indexOfFirstInstructionOrThrow( |
| 79 | + colorGreyIndex, Opcode.IPUT |
| 80 | + ) |
| 81 | + val colorMathPlayerIPutReference = miniPlayerConstructorFingerprint.method |
| 82 | + .getInstruction<ReferenceInstruction>(iPutIndex).reference |
| 83 | + |
| 84 | + miniPlayerConstructorFingerprint.classDef.methods.single { method -> |
| 85 | + method.accessFlags == AccessFlags.PUBLIC.value or AccessFlags.FINAL.value && |
| 86 | + method.returnType == "V" && |
| 87 | + method.parameters == it.originalMethod.parameters |
| 88 | + }.apply { |
| 89 | + val insertIndex = indexOfFirstInstructionReversedOrThrow(Opcode.INVOKE_DIRECT) |
| 90 | + val freeRegister = findFreeRegister(insertIndex) |
| 91 | + |
| 92 | + addInstructionsAtControlFlowLabel( |
| 93 | + insertIndex, |
| 94 | + """ |
| 95 | + invoke-static {}, $EXTENSION_CLASS_DESCRIPTOR->changeMiniplayerColor()Z |
| 96 | + move-result v$freeRegister |
| 97 | + if-eqz v$freeRegister, :off |
| 98 | + invoke-virtual { p1 }, $colorMathPlayerInvokeVirtualReference |
| 99 | + move-result-object v$freeRegister |
| 100 | + check-cast v$freeRegister, ${colorMathPlayerIGetReference.definingClass} |
| 101 | + iget v$freeRegister, v$freeRegister, $colorMathPlayerIGetReference |
| 102 | + iput v$freeRegister, p0, $colorMathPlayerIPutReference |
| 103 | + :off |
| 104 | + nop |
| 105 | + """ |
| 106 | + ) |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments