From b2c5babf3fd9ad73daa06e03f4830a9dd7199d0c Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Thu, 23 Nov 2023 00:59:32 +0100 Subject: [PATCH] fix(YouTube - Enable tablet layout): Respect the original device layout --- .../layout/tablet/EnableTabletLayoutPatch.kt | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/tablet/EnableTabletLayoutPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/tablet/EnableTabletLayoutPatch.kt index c99a12b7..d505b37d 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/layout/tablet/EnableTabletLayoutPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/tablet/EnableTabletLayoutPatch.kt @@ -2,8 +2,11 @@ package app.revanced.patches.youtube.layout.tablet import app.revanced.extensions.exception import app.revanced.patcher.data.BytecodeContext +import app.revanced.patcher.extensions.InstructionExtensions.addInstruction import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels import app.revanced.patcher.extensions.InstructionExtensions.getInstruction +import app.revanced.patcher.extensions.InstructionExtensions.getInstructions +import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction import app.revanced.patcher.patch.BytecodePatch import app.revanced.patcher.patch.annotation.CompatiblePackage import app.revanced.patcher.patch.annotation.Patch @@ -13,6 +16,8 @@ import app.revanced.patches.shared.settings.preference.impl.SwitchPreference import app.revanced.patches.youtube.layout.tablet.fingerprints.GetFormFactorFingerprint import app.revanced.patches.youtube.misc.integrations.IntegrationsPatch import app.revanced.patches.youtube.misc.settings.SettingsPatch +import com.android.tools.smali.dexlib2.Opcode +import com.android.tools.smali.dexlib2.builder.instruction.BuilderInstruction10x @Patch( name = "Enable tablet layout", @@ -37,18 +42,28 @@ object EnableTabletLayoutPatch : BytecodePatch( GetFormFactorFingerprint.result?.let { it.mutableMethod.apply { - val returnLargeFormFactorIndex = it.scanResult.patternScanResult!!.endIndex - 4 + val returnCurrentFormFactorIndex = getInstructions().lastIndex - 2 + val returnIsLargeFormFactorLabel = getInstruction(returnCurrentFormFactorIndex - 2) + val returnFormFactorIndex = getInstruction(returnCurrentFormFactorIndex) + + val insertIndex = returnCurrentFormFactorIndex + 1 + + // Replace the labeled instruction with a nop and add the preserved instructions back + replaceInstruction(returnCurrentFormFactorIndex, BuilderInstruction10x(Opcode.NOP)) + addInstruction(insertIndex, returnFormFactorIndex) + + // Because the labeled instruction is now a nop, we can add our own instructions right after it addInstructionsWithLabels( - 0, + insertIndex, """ - invoke-static {}, Lapp/revanced/integrations/patches/EnableTabletLayoutPatch;->enableTabletLayout()Z - move-result v0 + invoke-static { }, Lapp/revanced/integrations/patches/EnableTabletLayoutPatch;->enableTabletLayout()Z + move-result v0 # Free register if-nez v0, :is_large_form_factor """, ExternalLabel( "is_large_form_factor", - getInstruction(returnLargeFormFactorIndex) + returnIsLargeFormFactorLabel ) ) }