From e565fed07ebbb7aa1df86089b5f9a0cd549d25df Mon Sep 17 00:00:00 2001
From: inotia00 <108592928+inotia00@users.noreply.github.com>
Date: Thu, 30 May 2024 19:15:45 +0900
Subject: [PATCH] fix(YouTube/Settings): `CairoSettings` is applied due to A/B
testing https://github.com/inotia00/ReVanced_Extended/issues/2099
---
.../utils/fix/cairo/CairoSettingsPatch.kt | 46 +++++++++++++++++++
.../CarioFragmentConfigFingerprint.kt | 17 +++++++
.../youtube/utils/settings/SettingsPatch.kt | 6 +--
3 files changed, 66 insertions(+), 3 deletions(-)
create mode 100644 src/main/kotlin/app/revanced/patches/youtube/utils/fix/cairo/CairoSettingsPatch.kt
create mode 100644 src/main/kotlin/app/revanced/patches/youtube/utils/fix/cairo/fingerprints/CarioFragmentConfigFingerprint.kt
diff --git a/src/main/kotlin/app/revanced/patches/youtube/utils/fix/cairo/CairoSettingsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/utils/fix/cairo/CairoSettingsPatch.kt
new file mode 100644
index 000000000..c05895e50
--- /dev/null
+++ b/src/main/kotlin/app/revanced/patches/youtube/utils/fix/cairo/CairoSettingsPatch.kt
@@ -0,0 +1,46 @@
+package app.revanced.patches.youtube.utils.fix.cairo
+
+import app.revanced.patcher.data.BytecodeContext
+import app.revanced.patcher.extensions.InstructionExtensions.addInstruction
+import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
+import app.revanced.patcher.patch.BytecodePatch
+import app.revanced.patcher.patch.annotation.Patch
+import app.revanced.patches.youtube.misc.minimizedplayback.MinimizedPlaybackPatch
+import app.revanced.patches.youtube.utils.fix.cairo.fingerprints.CarioFragmentConfigFingerprint
+import app.revanced.util.getTargetIndex
+import app.revanced.util.getWideLiteralInstructionIndex
+import com.android.tools.smali.dexlib2.Opcode
+import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
+
+@Patch(
+ description = "Fixes issues where Cairo Fragment is applied."
+)
+object CairoSettingsPatch : BytecodePatch(
+ setOf(CarioFragmentConfigFingerprint)
+) {
+ override fun execute(context: BytecodeContext) {
+
+ /**
+ * Cairo Fragment was added since YouTube v19.04.38.
+ * Disable this for the following reasons:
+ * 1. [MinimizedPlaybackPatch] does not activate the Minimized playback setting of Cairo Fragment.
+ * 2. Some patches implemented in RVX do not yet support Cairo Fragments.
+ *
+ * See ReVanced_Extended#2099
+ * or uYouPlus#1468
+ * for screenshots of the Cairo Fragment.
+ */
+ CarioFragmentConfigFingerprint.result?.let {
+ it.mutableMethod.apply {
+ val targetIndex = getTargetIndex(getWideLiteralInstructionIndex(45532100), Opcode.MOVE_RESULT)
+ val targetRegister = getInstruction(targetIndex).registerA
+
+ addInstruction(
+ targetIndex + 1,
+ "const/4 v$targetRegister, 0x0"
+ )
+ }
+ }
+
+ }
+}
diff --git a/src/main/kotlin/app/revanced/patches/youtube/utils/fix/cairo/fingerprints/CarioFragmentConfigFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/utils/fix/cairo/fingerprints/CarioFragmentConfigFingerprint.kt
new file mode 100644
index 000000000..b777e0e75
--- /dev/null
+++ b/src/main/kotlin/app/revanced/patches/youtube/utils/fix/cairo/fingerprints/CarioFragmentConfigFingerprint.kt
@@ -0,0 +1,17 @@
+package app.revanced.patches.youtube.utils.fix.cairo.fingerprints
+
+import app.revanced.patcher.extensions.or
+import app.revanced.util.fingerprint.LiteralValueFingerprint
+import com.android.tools.smali.dexlib2.AccessFlags
+
+/**
+ * Added in YouTube v19.04.38
+ *
+ * When this value is TRUE, Cairo Fragment is used.
+ * In this case, some of patches may be broken, so set this value to FALSE.
+ */
+internal object CarioFragmentConfigFingerprint : LiteralValueFingerprint(
+ returnType = "Z",
+ accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL,
+ literalSupplier = { 45532100 }
+)
\ No newline at end of file
diff --git a/src/main/kotlin/app/revanced/patches/youtube/utils/settings/SettingsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/utils/settings/SettingsPatch.kt
index fe3fd4658..c825e5b89 100644
--- a/src/main/kotlin/app/revanced/patches/youtube/utils/settings/SettingsPatch.kt
+++ b/src/main/kotlin/app/revanced/patches/youtube/utils/settings/SettingsPatch.kt
@@ -4,6 +4,7 @@ import app.revanced.patcher.data.ResourceContext
import app.revanced.patches.shared.elements.StringsElementsUtils.removeStringsElements
import app.revanced.patches.shared.mapping.ResourceMappingPatch
import app.revanced.patches.youtube.utils.compatibility.Constants.COMPATIBLE_PACKAGE
+import app.revanced.patches.youtube.utils.fix.cairo.CairoSettingsPatch
import app.revanced.patches.youtube.utils.integrations.IntegrationsPatch
import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch
import app.revanced.patches.youtube.utils.settings.ResourceUtils.addPreference
@@ -30,7 +31,8 @@ object SettingsPatch : BaseResourcePatch(
IntegrationsPatch::class,
ResourceMappingPatch::class,
SharedResourceIdPatch::class,
- SettingsBytecodePatch::class
+ SettingsBytecodePatch::class,
+ CairoSettingsPatch::class
),
compatiblePackages = COMPATIBLE_PACKAGE,
requiresIntegrations = true
@@ -46,7 +48,6 @@ object SettingsPatch : BaseResourcePatch(
internal var upward1842 = false
internal var upward1849 = false
internal var upward1902 = false
- internal var upward1909 = false
internal var upward1912 = false
override fun execute(context: ResourceContext) {
@@ -81,7 +82,6 @@ object SettingsPatch : BaseResourcePatch(
upward1842 = 234302000 <= playServicesVersion
upward1849 = 235000000 <= playServicesVersion
upward1902 = 240204000 < playServicesVersion
- upward1909 = 241002000 <= playServicesVersion
upward1912 = 241302000 <= playServicesVersion
break