Skip to content

Commit

Permalink
feat(youtube/custom-video-buffer): replace patch with removal notice (#…
Browse files Browse the repository at this point in the history
…1718)

Co-authored-by: oSumAtrIX <johan.melkonyan1@web.de>
  • Loading branch information
LisoUseInAIKyrios and oSumAtrIX committed Mar 14, 2023
1 parent 675c970 commit 29f952d
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 230 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package app.revanced.patches.shared.settings.preference.impl

import app.revanced.patches.shared.settings.preference.BasePreference
import app.revanced.patches.shared.settings.preference.IResource
import app.revanced.patches.shared.settings.preference.addSummary
import org.w3c.dom.Document
import org.w3c.dom.Element

/**
* A simple static title and summary that is not backed by any preference key/value,
* and cannot be changed by or interacted with by the user,
*/
internal class NonInteractivePreference(
title: StringResource,
val summary: StringResource,
) : BasePreference("", title) {
override val tag: String = "Preference"

override fun serialize(ownerDocument: Document, resourceCallback: ((IResource) -> Unit)?): Element {
return super.serialize(ownerDocument, resourceCallback).apply {
addSummary(summary.also { resourceCallback?.invoke(it)
setAttribute("android:selectable", false.toString())
})
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app.revanced.patches.youtube.misc.videobuffer.annotations
import app.revanced.patcher.annotation.Compatibility
import app.revanced.patcher.annotation.Package

// TODO: delete this
@Compatibility(
[Package(
"com.google.android.youtube", arrayOf(
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,201 +1,36 @@
package app.revanced.patches.youtube.misc.videobuffer.patch

import app.revanced.extensions.toErrorResult
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.data.toMethodWalker
import app.revanced.patcher.extensions.addInstructions
import app.revanced.patcher.extensions.instruction
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint.Companion.resolve
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.patch.annotations.DependsOn
import app.revanced.patcher.patch.annotations.Patch
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
import app.revanced.patches.shared.settings.preference.impl.InputType
import app.revanced.patches.shared.settings.preference.impl.PreferenceScreen
import app.revanced.patches.shared.settings.preference.impl.StringResource
import app.revanced.patches.shared.settings.preference.impl.TextPreference
import app.revanced.patches.shared.settings.preference.impl.*
import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch
import app.revanced.patches.youtube.misc.videobuffer.annotations.CustomVideoBufferCompatibility
import app.revanced.patches.youtube.misc.videobuffer.fingerprints.InvokeMaxBufferFingerprint
import app.revanced.patches.youtube.misc.videobuffer.fingerprints.MaxBufferFingerprint
import app.revanced.patches.youtube.misc.videobuffer.fingerprints.PlaybackBufferFingerprint
import app.revanced.patches.youtube.misc.videobuffer.fingerprints.ReBufferFingerprint
import org.jf.dexlib2.iface.instruction.OneRegisterInstruction

@Patch
// TODO: delete this patch
@Patch(include = false)
@Name("custom-video-buffer")
@Description("Lets you change the buffers of videos.")
@DependsOn([SettingsPatch::class])
@CustomVideoBufferCompatibility
@Version("0.0.1")
class CustomVideoBufferPatch : BytecodePatch(
listOf(
InvokeMaxBufferFingerprint,
PlaybackBufferFingerprint,
ReBufferFingerprint,
)
) {
class CustomVideoBufferPatch : BytecodePatch() {
override fun execute(context: BytecodeContext): PatchResult {
SettingsPatch.PreferenceScreen.MISC.addPreferences(
PreferenceScreen(
"revanced_custom_video_buffer",
StringResource("revanced_custom_video_buffer_title", "Video buffer settings"),
listOf(
TextPreference(
"revanced_pref_max_buffer_ms",
StringResource("revanced_pref_max_buffer_ms_title", "Maximum buffer size"),
InputType.NUMBER,
"120000",
StringResource(
"revanced_pref_max_buffer_ms_summary",
"The maximum size of a buffer for playback"
)
),
TextPreference(
"revanced_pref_buffer_for_playback_ms",
StringResource("revanced_pref_buffer_for_playback_ms_title", "Maximum buffer for playback"),
InputType.NUMBER,
"2500",
StringResource(
"revanced_pref_buffer_for_playback_ms_summary",
"Maximum size of a buffer for playback"
)
),
TextPreference(
"revanced_pref_buffer_for_playback_after_rebuffer_ms",
StringResource(
"revanced_pref_buffer_for_playback_after_rebuffer_ms_title",
"Maximum buffer for playback after rebuffer"
),
InputType.NUMBER,
"5000",
StringResource(
"revanced_pref_buffer_for_playback_after_rebuffer_ms_summary",
"Maximum size of a buffer for playback after rebuffering"
)
)
),
StringResource("revanced_custom_video_buffer_summary", "Custom settings for video buffer")
NonInteractivePreference(
StringResource("revanced_custom_video_buffer_disclaimer_title", "Custom video buffer"),
StringResource("revanced_custom_video_buffer_disclaimer_title_summary",
"These settings have been removed, because they were not functional for the duration of their existence"),
)
)
BufferType.values().forEach { type ->
type.hook(context)
}

return PatchResultSuccess()
}

/**
* The type of buffer.
*
* @param patchInfo The corresponding information to patch the buffer type.
* @param preparation Optional preparation before patching.
*/
private enum class BufferType(
private val patchInfo: PatchInfo,
private val preparation: (BytecodeContext.() -> Unit)? = null,
) {

PLAYBACK(PatchInfo(PlaybackBufferFingerprint, "getPlaybackBuffer")),
RE(PatchInfo(ReBufferFingerprint, "getReBuffer")),
MAX(
PatchInfo(
MaxBufferFingerprint,
"getMaxBuffer",
PatchInfo.UnwrapInfo(true, -1)
),
preparation@{
InvokeMaxBufferFingerprint.result?.apply {
val maxBufferMethodCallOffset = 2

val maxBufferMethod = this@preparation.toMethodWalker(method)
.nextMethod(scanResult.patternScanResult!!.endIndex + maxBufferMethodCallOffset)
.getMethod()

if (!MaxBufferFingerprint.resolve(
this@preparation,
maxBufferMethod,
// This is inefficient because toMethodWalker technically already has context about this.
// Alternatively you can iterate manually over all classes
// instead of relying on toMethodWalker.
this@preparation.findClass(maxBufferMethod.definingClass)!!.immutableClass,
)
) throw MaxBufferFingerprint.toErrorResult()
} ?: throw InvokeMaxBufferFingerprint.toErrorResult()
});

/**
* Information about a patch.
*
* @param fingerprint The corresponding [MethodFingerprint] for the patch.
* @param integrationsMethodName The corresponding name of the hooking method.
* @param unwrapInfo Optional information on how to treat the [MethodFingerprint].
*/
private class PatchInfo(
val fingerprint: MethodFingerprint,
val integrationsMethodName: String,
val unwrapInfo: UnwrapInfo? = null
) {
/**
* Information on how to treat a [MethodFingerprint].
*
* @param useEndIndex Whether to retrieve information of the [MethodFingerprint]
* from the end index of its pattern scan result.
* @param offset An additional offset to [useEndIndex].
*/
class UnwrapInfo(val useEndIndex: Boolean = false, val offset: Int = 0)
}

fun hook(context: BytecodeContext) {
/**
* The resulting instruction info for unwrapping [MethodFingerprint].
*
* @param index The index of the instruction.
* @param register The register of the instruction.
*/
data class InstructionResult(val index: Int, val register: Int)

/***
* The result of unwrapping [MethodFingerprint].
*
* @param method The method which was retrieved from the [MethodFingerprint].
* @param instructionResult The resulting instruction info for unwrapping [MethodFingerprint].
*/
data class UnwrapResult(val method: MutableMethod, val instructionResult: InstructionResult)

fun MethodFingerprint.unwrap(unwrapInfo: PatchInfo.UnwrapInfo? = null): UnwrapResult {
val result = this.result!!
val method = result.mutableMethod
val scanResult = result.scanResult.patternScanResult!!
val index =
if (unwrapInfo?.useEndIndex == true) scanResult.endIndex
else {
scanResult.startIndex
} + (unwrapInfo?.offset ?: 0)

val register = (method.instruction(index) as OneRegisterInstruction).registerA

return UnwrapResult(method, InstructionResult(index, register))
}

preparation?.invoke(context)

val (method, result) = patchInfo.fingerprint.unwrap(patchInfo.unwrapInfo)
val (index, register) = result

method.addInstructions(
index + 1,
"""
invoke-static {}, Lapp/revanced/integrations/patches/VideoBufferPatch;->${patchInfo.integrationsMethodName}()I
move-result v$register
"""
)
}
}
}

0 comments on commit 29f952d

Please sign in to comment.