2023-09-20 05:33:02 +02:00
|
|
|
package app.revanced.patches.youtube.layout.tabletminiplayer
|
2022-08-14 17:32:32 +02:00
|
|
|
|
2022-10-05 04:03:10 +02:00
|
|
|
import app.revanced.patcher.data.BytecodeContext
|
2023-06-07 03:46:13 +02:00
|
|
|
import app.revanced.patcher.extensions.InstructionExtensions.addInstructions
|
2023-10-22 23:31:50 +02:00
|
|
|
import app.revanced.patcher.fingerprint.MethodFingerprint
|
2022-10-05 04:03:10 +02:00
|
|
|
import app.revanced.patcher.patch.BytecodePatch
|
2023-08-27 21:40:49 +02:00
|
|
|
import app.revanced.patcher.patch.PatchException
|
2023-09-20 05:33:02 +02:00
|
|
|
import app.revanced.patcher.patch.annotation.CompatiblePackage
|
|
|
|
import app.revanced.patcher.patch.annotation.Patch
|
2022-08-14 17:32:32 +02:00
|
|
|
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
|
2024-01-27 02:34:01 +01:00
|
|
|
import app.revanced.patches.all.misc.resources.AddResourcesPatch
|
|
|
|
import app.revanced.patches.shared.misc.settings.preference.SwitchPreference
|
2023-08-27 21:40:49 +02:00
|
|
|
import app.revanced.patches.youtube.layout.tabletminiplayer.fingerprints.MiniPlayerDimensionsCalculatorParentFingerprint
|
|
|
|
import app.revanced.patches.youtube.layout.tabletminiplayer.fingerprints.MiniPlayerOverrideFingerprint
|
|
|
|
import app.revanced.patches.youtube.layout.tabletminiplayer.fingerprints.MiniPlayerOverrideNoContextFingerprint
|
|
|
|
import app.revanced.patches.youtube.layout.tabletminiplayer.fingerprints.MiniPlayerResponseModelSizeCheckFingerprint
|
2023-09-20 05:33:02 +02:00
|
|
|
import app.revanced.patches.youtube.misc.integrations.IntegrationsPatch
|
|
|
|
import app.revanced.patches.youtube.misc.settings.SettingsPatch
|
2024-01-27 02:34:01 +01:00
|
|
|
import app.revanced.util.exception
|
2023-08-27 21:40:49 +02:00
|
|
|
import com.android.tools.smali.dexlib2.Opcode
|
|
|
|
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
|
2022-08-14 17:32:32 +02:00
|
|
|
|
2023-09-20 05:33:02 +02:00
|
|
|
@Patch(
|
|
|
|
name = "Tablet mini player",
|
2024-01-01 16:30:04 +01:00
|
|
|
description = "Adds an option to enable the tablet mini player layout.",
|
2024-01-27 02:34:01 +01:00
|
|
|
dependencies = [IntegrationsPatch::class, SettingsPatch::class, AddResourcesPatch::class],
|
2023-09-20 05:33:02 +02:00
|
|
|
compatiblePackages = [
|
|
|
|
CompatiblePackage(
|
|
|
|
"com.google.android.youtube", arrayOf(
|
2023-09-28 01:28:25 +02:00
|
|
|
"18.32.39",
|
2023-10-08 03:37:42 +02:00
|
|
|
"18.37.36",
|
2023-11-12 16:43:11 +01:00
|
|
|
"18.38.44",
|
2023-11-12 17:01:49 +01:00
|
|
|
"18.43.45",
|
|
|
|
"18.44.41",
|
2024-01-16 19:54:06 +01:00
|
|
|
"18.45.43",
|
|
|
|
"18.48.39",
|
|
|
|
"18.49.37",
|
2024-01-17 21:24:55 +01:00
|
|
|
"19.01.34",
|
2024-01-24 14:19:02 +01:00
|
|
|
"19.02.39",
|
2024-02-04 18:38:19 +01:00
|
|
|
"19.03.35",
|
|
|
|
"19.03.36",
|
|
|
|
"19.04.37"
|
2023-09-20 05:33:02 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
@Suppress("unused")
|
|
|
|
object TabletMiniPlayerPatch : BytecodePatch(
|
|
|
|
setOf(
|
2023-06-28 15:43:45 +02:00
|
|
|
MiniPlayerDimensionsCalculatorParentFingerprint,
|
2023-03-19 23:24:31 +01:00
|
|
|
MiniPlayerResponseModelSizeCheckFingerprint,
|
2023-08-27 21:40:49 +02:00
|
|
|
MiniPlayerOverrideFingerprint
|
2022-08-14 17:32:32 +02:00
|
|
|
)
|
|
|
|
) {
|
2023-08-27 21:40:49 +02:00
|
|
|
override fun execute(context: BytecodeContext) {
|
2024-01-27 02:34:01 +01:00
|
|
|
AddResourcesPatch(this::class)
|
|
|
|
|
2024-03-02 08:27:05 +01:00
|
|
|
SettingsPatch.PreferenceScreen.GENERAL_LAYOUT.addPreferences(
|
|
|
|
SwitchPreference("revanced_tablet_miniplayer")
|
|
|
|
)
|
2022-08-22 03:58:04 +02:00
|
|
|
|
2023-03-19 23:24:31 +01:00
|
|
|
// First resolve the fingerprints via the parent fingerprint.
|
2023-09-20 05:33:02 +02:00
|
|
|
MiniPlayerDimensionsCalculatorParentFingerprint.result
|
|
|
|
?: throw MiniPlayerDimensionsCalculatorParentFingerprint.exception
|
2023-06-28 15:43:45 +02:00
|
|
|
val miniPlayerClass = MiniPlayerDimensionsCalculatorParentFingerprint.result!!.classDef
|
2022-08-14 17:32:32 +02:00
|
|
|
|
|
|
|
/*
|
2023-03-19 23:24:31 +01:00
|
|
|
* No context parameter method.
|
2022-08-14 17:32:32 +02:00
|
|
|
*/
|
2022-10-05 04:03:10 +02:00
|
|
|
MiniPlayerOverrideNoContextFingerprint.resolve(context, miniPlayerClass)
|
2022-08-14 17:32:32 +02:00
|
|
|
val (method, _, parameterRegister) = MiniPlayerOverrideNoContextFingerprint.addProxyCall()
|
2023-03-19 23:24:31 +01:00
|
|
|
|
|
|
|
// Insert right before the return instruction.
|
2022-08-14 17:32:32 +02:00
|
|
|
val secondInsertIndex = method.implementation!!.instructions.size - 1
|
2023-03-19 23:24:31 +01:00
|
|
|
method.insertOverride(
|
|
|
|
secondInsertIndex, parameterRegister
|
|
|
|
/** same register used to return **/
|
|
|
|
)
|
2022-08-14 17:32:32 +02:00
|
|
|
|
2023-05-02 03:36:17 +02:00
|
|
|
/*
|
|
|
|
* Override every return instruction with the proxy call.
|
|
|
|
*/
|
2023-08-27 21:40:49 +02:00
|
|
|
MiniPlayerOverrideFingerprint.result?.let { result ->
|
|
|
|
result.mutableMethod.let { method ->
|
|
|
|
val appNameStringIndex = result.scanResult.stringsScanResult!!.matches.first().index + 2
|
|
|
|
context.toMethodWalker(method).nextMethod(appNameStringIndex, true)
|
|
|
|
.getMethod() as MutableMethod
|
|
|
|
}.apply {
|
|
|
|
implementation!!.let { implementation ->
|
|
|
|
val returnIndices = implementation.instructions
|
|
|
|
.withIndex()
|
|
|
|
.filter { (_, instruction) -> instruction.opcode == Opcode.RETURN }
|
|
|
|
.map { (index, _) -> index }
|
2023-05-02 03:36:17 +02:00
|
|
|
|
2023-08-27 21:40:49 +02:00
|
|
|
if (returnIndices.isEmpty()) throw PatchException("No return instructions found.")
|
2023-05-02 03:36:17 +02:00
|
|
|
|
2023-08-27 21:40:49 +02:00
|
|
|
// This method clobbers register p0 to return the value, calculate to override.
|
|
|
|
val returnedRegister = implementation.registerCount - parameters.size
|
2023-05-02 03:36:17 +02:00
|
|
|
|
2023-08-27 21:40:49 +02:00
|
|
|
// Hook the returned register on every return instruction.
|
|
|
|
returnIndices.forEach { index -> insertOverride(index, returnedRegister) }
|
|
|
|
}
|
2023-05-02 03:36:17 +02:00
|
|
|
}
|
2023-08-27 21:40:49 +02:00
|
|
|
|
|
|
|
return@let
|
|
|
|
} ?: throw MiniPlayerOverrideFingerprint.exception
|
2023-05-02 03:36:17 +02:00
|
|
|
|
2022-08-14 17:32:32 +02:00
|
|
|
/*
|
2023-03-19 23:24:31 +01:00
|
|
|
* Size check return value override.
|
2022-08-14 17:32:32 +02:00
|
|
|
*/
|
2023-03-19 23:24:31 +01:00
|
|
|
MiniPlayerResponseModelSizeCheckFingerprint.addProxyCall()
|
2022-08-14 17:32:32 +02:00
|
|
|
}
|
|
|
|
|
2023-03-19 23:24:31 +01:00
|
|
|
// Helper methods.
|
2023-09-20 05:33:02 +02:00
|
|
|
private fun MethodFingerprint.addProxyCall(): Triple<MutableMethod, Int, Int> {
|
|
|
|
val (method, scanIndex, parameterRegister) = this.unwrap()
|
|
|
|
method.insertOverride(scanIndex, parameterRegister)
|
2022-08-14 17:32:32 +02:00
|
|
|
|
2023-09-20 05:33:02 +02:00
|
|
|
return Triple(method, scanIndex, parameterRegister)
|
|
|
|
}
|
2022-08-14 17:32:32 +02:00
|
|
|
|
2023-09-20 05:33:02 +02:00
|
|
|
private fun MutableMethod.insertOverride(index: Int, overrideRegister: Int) {
|
|
|
|
this.addInstructions(
|
|
|
|
index,
|
|
|
|
"""
|
2024-01-01 16:33:07 +01:00
|
|
|
invoke-static {v$overrideRegister}, Lapp/revanced/integrations/youtube/patches/TabletMiniPlayerOverridePatch;->getTabletMiniPlayerOverride(Z)Z
|
2022-08-14 17:32:32 +02:00
|
|
|
move-result v$overrideRegister
|
2023-06-07 03:46:13 +02:00
|
|
|
"""
|
2023-09-20 05:33:02 +02:00
|
|
|
)
|
|
|
|
}
|
2022-08-14 17:32:32 +02:00
|
|
|
|
2024-03-02 08:27:05 +01:00
|
|
|
private fun MethodFingerprint.unwrap(): Triple<MutableMethod, Int, Int> {
|
2023-09-20 05:33:02 +02:00
|
|
|
val result = this.result!!
|
|
|
|
val scanIndex = result.scanResult.patternScanResult!!.endIndex
|
|
|
|
val method = result.mutableMethod
|
|
|
|
val instructions = method.implementation!!.instructions
|
|
|
|
val parameterRegister = (instructions[scanIndex] as OneRegisterInstruction).registerA
|
2022-08-14 17:32:32 +02:00
|
|
|
|
2023-09-20 05:33:02 +02:00
|
|
|
return Triple(method, scanIndex, parameterRegister)
|
2022-08-14 17:32:32 +02:00
|
|
|
}
|
|
|
|
}
|