feat(YouTube): add Change shorts repeat state patch

This commit is contained in:
inotia00 2024-03-25 06:57:10 +09:00
parent c02510a289
commit 83e201e96e
7 changed files with 219 additions and 5 deletions

View File

@ -0,0 +1,158 @@
package app.revanced.patches.youtube.shorts.repeat
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.InstructionExtensions.addInstruction
import app.revanced.patcher.extensions.InstructionExtensions.addInstructions
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.annotation.CompatiblePackage
import app.revanced.patcher.patch.annotation.Patch
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
import app.revanced.patches.youtube.shorts.repeat.fingerprints.ReelEnumConstructorFingerprint
import app.revanced.patches.youtube.shorts.repeat.fingerprints.ReelEnumStaticFingerprint
import app.revanced.patches.youtube.utils.integrations.Constants.SHORTS
import app.revanced.patches.youtube.utils.settings.SettingsPatch
import app.revanced.patches.youtube.utils.settings.SettingsPatch.contexts
import app.revanced.util.containsReferenceInstructionIndex
import app.revanced.util.copyXmlNode
import app.revanced.util.exception
import app.revanced.util.findMutableMethodOf
import app.revanced.util.getStringInstructionIndex
import app.revanced.util.getTargetIndex
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction
@Patch(
name = "Change shorts repeat state",
description = "Adds an options for whether shorts should repeat, autoplay, or stop.",
dependencies = [SettingsPatch::class],
compatiblePackages = [
CompatiblePackage(
"com.google.android.youtube",
[
"18.29.38",
"18.30.37",
"18.31.40",
"18.32.39",
"18.33.40",
"18.34.38",
"18.35.36",
"18.36.39",
"18.37.36",
"18.38.44",
"18.39.41",
"18.40.34",
"18.41.39",
"18.42.41",
"18.43.45",
"18.44.41",
"18.45.43",
"18.46.45",
"18.48.39",
"18.49.37",
"19.01.34",
"19.02.39"
]
)
]
)
@Suppress("unused")
object ShortsRepeatPatch : BytecodePatch(
setOf(ReelEnumConstructorFingerprint)
) {
override fun execute(context: BytecodeContext) {
ReelEnumConstructorFingerprint.result?.let {
it.mutableMethod.apply {
ReelEnumStaticFingerprint.resolve(context, it.mutableClass)
arrayOf(
"REEL_LOOP_BEHAVIOR_END_SCREEN" to "endScreen",
"REEL_LOOP_BEHAVIOR_REPEAT" to "repeat",
"REEL_LOOP_BEHAVIOR_SINGLE_PLAY" to "singlePlay"
).map { (enumName, fieldName) ->
injectEnum(enumName, fieldName)
}
val endScreenStringIndex = getStringInstructionIndex("REEL_LOOP_BEHAVIOR_END_SCREEN")
val endScreenReferenceIndex = getTargetIndex(endScreenStringIndex, Opcode.SPUT_OBJECT)
val endScreenReference = getInstruction<ReferenceInstruction>(endScreenReferenceIndex).reference.toString()
val enumMethodName = ReelEnumStaticFingerprint.result?.mutableMethod?.name
?: throw ReelEnumStaticFingerprint.exception
val enumMethodCall = "$definingClass->$enumMethodName(I)$definingClass"
context.injectHook(endScreenReference, enumMethodCall)
}
} ?: throw ReelEnumConstructorFingerprint.exception
/**
* Copy arrays
*/
contexts.copyXmlNode("youtube/shorts/host", "values/arrays.xml", "resources")
/**
* Add settings
*/
SettingsPatch.addPreference(
arrayOf(
"PREFERENCE: SHORTS_SETTINGS",
"SETTINGS: SHORTS_PLAYER_PARENT",
"SETTINGS: CHANGE_SHORTS_REPEAT_STATE"
)
)
SettingsPatch.updatePatchStatus("Change shorts repeat state")
}
private fun MutableMethod.injectEnum(
enumName: String,
fieldName: String
) {
val stringIndex = getStringInstructionIndex(enumName)
val insertIndex = getTargetIndex(stringIndex, Opcode.SPUT_OBJECT)
val insertRegister = getInstruction<OneRegisterInstruction>(insertIndex).registerA
addInstruction(
insertIndex + 1,
"sput-object v$insertRegister, $SHORTS->$fieldName:Ljava/lang/Enum;"
)
}
private fun BytecodeContext.injectHook(
endScreenReference: String,
enumMethodCall: String
) {
classes.forEach { classDef ->
classDef.methods.filter { method ->
method.parameters.size == 1
&& method.parameters[0].startsWith("L")
&& method.returnType == "V"
&& method.containsReferenceInstructionIndex(endScreenReference)
}.forEach { targetMethod ->
proxy(classDef)
.mutableClass
.findMutableMethodOf(targetMethod)
.apply {
for ((index, instruction) in implementation!!.instructions.withIndex()) {
if (instruction.opcode != Opcode.INVOKE_STATIC)
continue
if ((instruction as ReferenceInstruction).reference.toString() != enumMethodCall)
continue
val register = getInstruction<OneRegisterInstruction>(index + 1).registerA
addInstructions(
index + 2, """
invoke-static {v$register}, $SHORTS->changeShortsRepeatState(Ljava/lang/Enum;)Ljava/lang/Enum;
move-result-object v$register
"""
)
}
}
}
}
}
}

View File

@ -0,0 +1,12 @@
package app.revanced.patches.youtube.shorts.repeat.fingerprints
import app.revanced.patcher.fingerprint.MethodFingerprint
object ReelEnumConstructorFingerprint : MethodFingerprint(
returnType = "V",
strings = listOf(
"REEL_LOOP_BEHAVIOR_SINGLE_PLAY",
"REEL_LOOP_BEHAVIOR_REPEAT",
"REEL_LOOP_BEHAVIOR_END_SCREEN"
)
)

View File

@ -0,0 +1,11 @@
package app.revanced.patches.youtube.shorts.repeat.fingerprints
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.MethodFingerprint
import com.android.tools.smali.dexlib2.AccessFlags
object ReelEnumStaticFingerprint : MethodFingerprint(
accessFlags = AccessFlags.PUBLIC or AccessFlags.STATIC,
parameters = listOf("I"),
returnType = "L"
)

View File

@ -110,6 +110,10 @@ fun Method.containsWideLiteralInstructionIndex(literal: Long) =
fun Method.containsMethodReferenceNameInstructionIndex(methodName: String) =
getTargetIndexWithMethodReferenceName(methodName) >= 0
fun Method.containsReferenceInstructionIndex(reference: String) =
getTargetIndexWithReference(reference) >= 0
/**
* Traverse the class hierarchy starting from the given root class.
*
@ -223,11 +227,17 @@ fun MutableMethod.getTargetIndexWithMethodReferenceNameReversed(startIndex: Int,
return -1
}
fun MutableMethod.getTargetIndexWithReference(reference: String)
= getTargetIndexWithReference(0, reference)
fun Method.getTargetIndexWithReference(reference: String) = implementation?.let {
it.instructions.indexOfFirst { instruction ->
(instruction as? ReferenceInstruction)?.reference.toString().contains(reference)
}
} ?: -1
fun MutableMethod.getTargetIndexWithReferenceReversed(reference: String)
= getTargetIndexWithReferenceReversed(implementation!!.instructions.size - 1, reference)
fun MutableMethod.getTargetIndexWithReference(reference: String) =
getTargetIndexWithReference(0, reference)
fun MutableMethod.getTargetIndexWithReferenceReversed(reference: String) =
getTargetIndexWithReferenceReversed(implementation!!.instructions.size - 1, reference)
fun MutableMethod.getTargetIndexWithReference(startIndex: Int, reference: String) =
implementation!!.instructions.let {

View File

@ -58,6 +58,11 @@ Tap here to learn more about DeArrow."</string>
<string name="revanced_bypass_ambient_mode_restrictions_summary_off">Ambient mode is disabled in battery saver mode.</string>
<string name="revanced_bypass_ambient_mode_restrictions_summary_on">Ambient mode is enabled in battery saver mode.</string>
<string name="revanced_bypass_ambient_mode_restrictions_title">Bypass ambient mode restrictions</string>
<string name="revanced_change_shorts_repeat_state_entry_auto_play">Autoplay</string>
<string name="revanced_change_shorts_repeat_state_entry_default">Default</string>
<string name="revanced_change_shorts_repeat_state_entry_pause">Pause</string>
<string name="revanced_change_shorts_repeat_state_entry_repeat">Repeat</string>
<string name="revanced_change_shorts_repeat_state_title">Change shorts repeat state</string>
<string name="revanced_change_player_flyout_panel_toggle_summary_off">Switch toggles are used.</string>
<string name="revanced_change_player_flyout_panel_toggle_summary_on">Text toggles are used.</string>
<string name="revanced_change_player_flyout_panel_toggle_title">Change toggle type</string>
@ -142,7 +147,6 @@ Note: This feature hasn't been tested."</string>
<string name="revanced_disable_resuming_shorts_player_summary_off">Shorts player will resume on app startup</string>
<string name="revanced_disable_resuming_shorts_player_summary_on">Shorts player will not resume on app startup</string>
<string name="revanced_disable_resuming_shorts_player_title">Disable resuming Shorts player</string>
<string name="revanced_disable_speed_overlay_summary">"Disable 'Playing at 2x speed' while holding down.
Note: Disabling the speed overlay restores the 'Slide to seek' behavior of the old layout."</string>

View File

@ -508,6 +508,7 @@
<Preference android:title="Hide time stamp" android:summary="@string/revanced_patches_excluded" android:selectable="false"/>
<Preference android:title=" " android:selectable="false" android:summary="@string/revanced_shorts" />
<Preference android:title="Change shorts repeat state" android:summary="@string/revanced_patches_excluded" android:selectable="false"/>
<Preference android:title="Disable resuming shorts on startup" android:summary="@string/revanced_patches_excluded" android:selectable="false"/>
<Preference android:title="Hide shorts components" android:summary="@string/revanced_patches_excluded" android:selectable="false"/>
<Preference android:title="Shorts outline button" android:summary="@string/revanced_patches_excluded" android:selectable="false"/>
@ -686,6 +687,9 @@
<!-- SETTINGS: SHORTS_PLAYER_PARENT
<Preference android:title=" " android:selectable="false" android:summary="@string/revanced_shorts_player_title" />SETTINGS: SHORTS_PLAYER_PARENT -->
<!-- SETTINGS: CHANGE_SHORTS_REPEAT_STATE
<ListPreference android:entries="@array/revanced_change_shorts_repeat_state_entry" android:title="@string/revanced_change_shorts_repeat_state_title" android:key="revanced_change_shorts_repeat_state" android:defaultValue="2" android:entryValues="@array/revanced_change_shorts_repeat_state_entry_value" />SETTINGS: CHANGE_SHORTS_REPEAT_STATE -->
<!-- SETTINGS: DISABLE_RESUMING_SHORTS_PLAYER
<SwitchPreference android:title="@string/revanced_disable_resuming_shorts_player_title" android:key="revanced_disable_resuming_shorts_player" android:defaultValue="true" android:summaryOn="@string/revanced_disable_resuming_shorts_player_summary_on" android:summaryOff="@string/revanced_disable_resuming_shorts_player_summary_off" />SETTINGS: DISABLE_RESUMING_SHORTS_PLAYER -->

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="revanced_change_shorts_repeat_state_entry">
<item>@string/revanced_change_shorts_repeat_state_entry_default</item>
<item>@string/revanced_change_shorts_repeat_state_entry_repeat</item>
<item>@string/revanced_change_shorts_repeat_state_entry_auto_play</item>
<item>@string/revanced_change_shorts_repeat_state_entry_pause</item>
</string-array>
<string-array name="revanced_change_shorts_repeat_state_entry_value">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
</resources>