diff --git a/src/main/kotlin/app/revanced/patches/youtube/general/audiotracks/AudioTracksPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/general/audiotracks/AudioTracksPatch.kt new file mode 100644 index 000000000..ff8f3058c --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/general/audiotracks/AudioTracksPatch.kt @@ -0,0 +1,68 @@ +package app.revanced.patches.youtube.general.audiotracks + +import app.revanced.patcher.data.BytecodeContext +import app.revanced.patcher.extensions.InstructionExtensions.addInstructions +import app.revanced.patcher.extensions.InstructionExtensions.getInstruction +import app.revanced.patches.youtube.general.audiotracks.fingerprints.StreamingModelBuilderFingerprint +import app.revanced.patches.youtube.utils.integrations.Constants.COMPATIBLE_PACKAGE +import app.revanced.patches.youtube.utils.integrations.Constants.GENERAL_CLASS_DESCRIPTOR +import app.revanced.patches.youtube.utils.settings.SettingsPatch +import app.revanced.util.getTargetIndexWithReference +import app.revanced.util.indexOfFirstInstruction +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.FiveRegisterInstruction +import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction +import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction + +@Suppress("unused") +object AudioTracksPatch : BaseBytecodePatch( + name = "Disable auto audio tracks", + description = "Adds an option to disable audio tracks from being automatically enabled.", + dependencies = setOf(SettingsPatch::class), + compatiblePackages = COMPATIBLE_PACKAGE, + fingerprints = setOf(StreamingModelBuilderFingerprint) +) { + override fun execute(context: BytecodeContext) { + + StreamingModelBuilderFingerprint.resultOrThrow().let { + it.mutableMethod.apply { + val formatStreamModelIndex = indexOfFirstInstruction { + opcode == Opcode.CHECK_CAST + && (this as ReferenceInstruction).reference.toString() == "Lcom/google/android/libraries/youtube/innertube/model/media/FormatStreamModel;" + } + val arrayListIndex = getTargetIndexWithReference(formatStreamModelIndex, "Ljava/util/List;->add(Ljava/lang/Object;)Z") + val insertIndex = getTargetIndexWithReference(arrayListIndex, "Ljava/util/List;->isEmpty()Z") + 2 + + val formatStreamModelRegister = getInstruction(formatStreamModelIndex).registerA + val arrayListRegister = getInstruction(arrayListIndex).registerC + + addInstructions( + insertIndex, """ + invoke-static {v$arrayListRegister}, $GENERAL_CLASS_DESCRIPTOR->getFormatStreamModelArray(Ljava/util/ArrayList;)Ljava/util/ArrayList; + move-result-object v$arrayListRegister + """ + ) + + addInstructions( + formatStreamModelIndex + 1, + "invoke-static {v$formatStreamModelRegister}, $GENERAL_CLASS_DESCRIPTOR->setFormatStreamModelArray(Ljava/lang/Object;)V" + ) + } + } + + /** + * Add settings + */ + SettingsPatch.addPreference( + arrayOf( + "PREFERENCE_SCREEN: GENERAL", + "SETTINGS: DISABLE_AUTO_AUDIO_TRACKS" + ) + ) + + SettingsPatch.updatePatchStatus(this) + + } +} diff --git a/src/main/kotlin/app/revanced/patches/youtube/general/audiotracks/fingerprints/StreamingModelBuilderFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/general/audiotracks/fingerprints/StreamingModelBuilderFingerprint.kt new file mode 100644 index 000000000..360c78bb8 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/general/audiotracks/fingerprints/StreamingModelBuilderFingerprint.kt @@ -0,0 +1,11 @@ +package app.revanced.patches.youtube.general.audiotracks.fingerprints + +import app.revanced.patcher.extensions.or +import app.revanced.patcher.fingerprint.MethodFingerprint +import com.android.tools.smali.dexlib2.AccessFlags + +internal object StreamingModelBuilderFingerprint : MethodFingerprint( + returnType = "L", + accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL, + strings = listOf("vprng") +) \ No newline at end of file diff --git a/src/main/resources/youtube/settings/host/values/strings.xml b/src/main/resources/youtube/settings/host/values/strings.xml index 220a1b1de..98fcbbe73 100644 --- a/src/main/resources/youtube/settings/host/values/strings.xml +++ b/src/main/resources/youtube/settings/host/values/strings.xml @@ -254,6 +254,9 @@ Limitations: Subscriptions Trending Invalid start page, resetting to default. + Disable forced auto audio tracks + Forced auto audio tracks are disabled. + Forced auto audio tracks are enabled. Disable forced auto captions Forced auto captions are disabled. Forced auto captions are enabled. diff --git a/src/main/resources/youtube/settings/xml/revanced_prefs.xml b/src/main/resources/youtube/settings/xml/revanced_prefs.xml index 84630a7d0..f0ed8bdda 100644 --- a/src/main/resources/youtube/settings/xml/revanced_prefs.xml +++ b/src/main/resources/youtube/settings/xml/revanced_prefs.xml @@ -128,6 +128,9 @@ + + @@ -536,6 +539,7 @@ +