refactor(YouTube/Layout switch): refine settings

This commit is contained in:
inotia00
2024-10-03 22:36:27 +09:00
parent 17cabfbc50
commit e88c190460
6 changed files with 67 additions and 63 deletions

View File

@ -2,19 +2,22 @@ package app.revanced.patches.youtube.general.layoutswitch
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.InstructionExtensions.addInstructions
import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.util.smali.ExternalLabel
import app.revanced.patches.youtube.general.layoutswitch.fingerprints.GetFormFactorFingerprint
import app.revanced.patches.shared.fingerprints.CreatePlayerRequestBodyWithModelFingerprint
import app.revanced.patches.youtube.general.layoutswitch.fingerprints.FormFactorEnumConstructorFingerprint
import app.revanced.patches.youtube.general.layoutswitch.fingerprints.LayoutSwitchFingerprint
import app.revanced.patches.youtube.utils.compatibility.Constants.COMPATIBLE_PACKAGE
import app.revanced.patches.youtube.utils.integrations.Constants.GENERAL_CLASS_DESCRIPTOR
import app.revanced.patches.youtube.utils.integrations.Constants.GENERAL_PATH
import app.revanced.patches.youtube.utils.settings.SettingsPatch
import app.revanced.util.getReference
import app.revanced.util.indexOfFirstInstructionOrThrow
import app.revanced.util.indexOfFirstInstructionReversedOrThrow
import app.revanced.util.patch.BaseBytecodePatch
import app.revanced.util.resultOrThrow
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
import com.android.tools.smali.dexlib2.iface.reference.FieldReference
@Suppress("unused")
object LayoutSwitchPatch : BaseBytecodePatch(
@ -23,52 +26,51 @@ object LayoutSwitchPatch : BaseBytecodePatch(
dependencies = setOf(SettingsPatch::class),
compatiblePackages = COMPATIBLE_PACKAGE,
fingerprints = setOf(
GetFormFactorFingerprint,
CreatePlayerRequestBodyWithModelFingerprint,
FormFactorEnumConstructorFingerprint,
LayoutSwitchFingerprint
)
) {
private const val INTEGRATIONS_CLASS_DESCRIPTOR = "$GENERAL_PATH/LayoutSwitchPatch;"
override fun execute(context: BytecodeContext) {
// region patch for enable tablet layout
val formFactorEnumClass = FormFactorEnumConstructorFingerprint
.resultOrThrow()
.mutableMethod
.definingClass
GetFormFactorFingerprint.resultOrThrow().let {
it.mutableMethod.apply {
val jumpIndex = indexOfFirstInstructionReversedOrThrow(Opcode.SGET_OBJECT)
addInstructionsWithLabels(
0, """
invoke-static { }, $GENERAL_CLASS_DESCRIPTOR->enableTabletLayout()Z
move-result v0 # Free register
if-nez v0, :is_large_form_factor
""",
ExternalLabel(
"is_large_form_factor",
getInstruction(jumpIndex)
)
)
CreatePlayerRequestBodyWithModelFingerprint.resultOrThrow().mutableMethod.apply {
val index = indexOfFirstInstructionOrThrow {
val reference = getReference<FieldReference>()
opcode == Opcode.IGET &&
reference?.definingClass == formFactorEnumClass &&
reference.type == "I"
}
val register = getInstruction<TwoRegisterInstruction>(index).registerA
addInstructions(
index + 1, """
invoke-static {v$register}, $INTEGRATIONS_CLASS_DESCRIPTOR->getFormFactor(I)I
move-result v$register
"""
)
}
// endregion
// region patch for enable phone layout
LayoutSwitchFingerprint.resultOrThrow().let {
it.mutableMethod.apply {
val insertIndex = indexOfFirstInstructionReversedOrThrow(Opcode.IF_NEZ)
val insertRegister = getInstruction<OneRegisterInstruction>(insertIndex).registerA
val index = indexOfFirstInstructionReversedOrThrow(Opcode.IF_NEZ)
val register = getInstruction<OneRegisterInstruction>(index).registerA
addInstructions(
insertIndex, """
invoke-static {v$insertRegister}, $GENERAL_CLASS_DESCRIPTOR->enablePhoneLayout(I)I
move-result v$insertRegister
index, """
invoke-static {v$register}, $INTEGRATIONS_CLASS_DESCRIPTOR->getWidthDp(I)I
move-result v$register
"""
)
}
}
// endregion
/**
* Add settings
*/

View File

@ -0,0 +1,12 @@
package app.revanced.patches.youtube.general.layoutswitch.fingerprints
import app.revanced.patcher.fingerprint.MethodFingerprint
internal object FormFactorEnumConstructorFingerprint : MethodFingerprint(
returnType = "V",
strings = listOf(
"UNKNOWN_FORM_FACTOR",
"SMALL_FORM_FACTOR",
"LARGE_FORM_FACTOR"
)
)

View File

@ -1,25 +0,0 @@
package app.revanced.patches.youtube.general.layoutswitch.fingerprints
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.MethodFingerprint
import com.android.tools.smali.dexlib2.AccessFlags
import com.android.tools.smali.dexlib2.Opcode
internal object GetFormFactorFingerprint : MethodFingerprint(
accessFlags = AccessFlags.PUBLIC or AccessFlags.STATIC,
returnType = "L",
parameters = listOf("Landroid/content/Context;", "Ljava/util/List;"),
opcodes = listOf(
Opcode.SGET_OBJECT,
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT_OBJECT,
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT,
Opcode.IF_EQZ,
Opcode.SGET_OBJECT,
Opcode.RETURN_OBJECT,
Opcode.INVOKE_STATIC,
Opcode.MOVE_RESULT_OBJECT,
Opcode.RETURN_OBJECT
)
)