refactor: simplify casting instructions

This commit is contained in:
inotia00
2023-05-11 16:35:02 +09:00
parent 2b70050200
commit 3b6848ab1a
16 changed files with 247 additions and 384 deletions

View File

@ -19,10 +19,10 @@ import app.revanced.util.enum.ResourceType.*
class SharedResourceIdPatch : ResourcePatch {
internal companion object {
var accessibilityProgressTimeId: Long = -1
var accessibilityVideoTimeId: Long = -1
var accountSwitcherAccessibilityId: Long = -1
var adAttributionId: Long = -1
var appearanceStringId: Long = -1
var appRelatedEndScreenResultsId: Long = -1
var autoNavPreviewId: Long = -1
var backgroundCategoryId: Long = -1
var barContainerHeightId: Long = -1
@ -42,6 +42,7 @@ class SharedResourceIdPatch : ResourcePatch {
var imageOnlyTabId: Long = -1
var inlineTimeBarColorizedBarPlayedColorDarkId: Long = -1
var inlineTimeBarPlayedNotHighlightedColorId: Long = -1
var insetOverlayViewLayoutId: Long = -1
var layoutCircleId: Long = -1
var layoutIconId: Long = -1
var layoutVideoId: Long = -1
@ -55,9 +56,12 @@ class SharedResourceIdPatch : ResourcePatch {
var reelRemixId: Long = -1
var relatedChipCloudMarginId: Long = -1
var rightCommentId: Long = -1
var scrimOverlayId: Long = -1
var searchSuggestionEntryId: Long = -1
var scrubbingId: Long = -1
var slimMetadataToggleButtonId: Long = -1
var suggestedActionId: Long = -1
var totalTimeId: Long = -1
var toolTipId: Long = -1
var videoQualityFragmentId: Long = -1
}
@ -69,10 +73,10 @@ class SharedResourceIdPatch : ResourcePatch {
.single { it.type == type.value && it.name == name }.id
accessibilityProgressTimeId = find(STRING, "accessibility_player_progress_time")
accessibilityVideoTimeId = find(STRING, "accessibility_video_time")
accountSwitcherAccessibilityId = find(STRING, "account_switcher_accessibility_label")
adAttributionId = find(ID, "ad_attribution")
appearanceStringId = find(STRING, "app_theme_appearance_dark")
appRelatedEndScreenResultsId = find(LAYOUT, "app_related_endscreen_results")
autoNavPreviewId = find(ID, "autonav_preview_stub")
backgroundCategoryId = find(STRING, "pref_background_and_offline_category")
barContainerHeightId = find(DIMEN, "bar_container_height")
@ -92,6 +96,7 @@ class SharedResourceIdPatch : ResourcePatch {
imageOnlyTabId = find(LAYOUT, "image_only_tab")
inlineTimeBarColorizedBarPlayedColorDarkId = find(COLOR, "inline_time_bar_colorized_bar_played_color_dark")
inlineTimeBarPlayedNotHighlightedColorId = find(COLOR, "inline_time_bar_played_not_highlighted_color")
insetOverlayViewLayoutId = find(ID, "inset_overlay_view_layout")
layoutCircleId = find(LAYOUT, "endscreen_element_layout_circle")
layoutIconId = find(LAYOUT, "endscreen_element_layout_icon")
layoutVideoId = find(LAYOUT, "endscreen_element_layout_video")
@ -105,9 +110,12 @@ class SharedResourceIdPatch : ResourcePatch {
reelRemixId = find(ID, "reel_dyn_remix")
relatedChipCloudMarginId = find(LAYOUT, "related_chip_cloud_reduced_margins")
rightCommentId = find(DRAWABLE, "ic_right_comment_32c")
scrimOverlayId = find(ID, "scrim_overlay")
searchSuggestionEntryId = find(LAYOUT, "search_suggestion_entry")
scrubbingId = find(DIMEN, "vertical_touch_offset_to_enter_fine_scrubbing")
slimMetadataToggleButtonId = find(COLOR, "slim_metadata_toggle_button")
suggestedActionId = find(LAYOUT, "suggested_action")
totalTimeId = find(STRING, "total_time")
toolTipId = find(LAYOUT, "tooltip_content_view")
videoQualityFragmentId = find(LAYOUT, "video_quality_bottom_sheet_list_fragment_title")

View File

@ -0,0 +1,16 @@
package app.revanced.patches.youtube.misc.sponsorblock.bytecode.fingerprints
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patches.youtube.misc.resourceid.patch.SharedResourceIdPatch.Companion.insetOverlayViewLayoutId
import app.revanced.util.bytecode.isWideLiteralExists
import org.jf.dexlib2.Opcode
object OverlayViewLayoutFingerprint : MethodFingerprint(
opcodes = listOf(
Opcode.CONST,
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT_OBJECT,
Opcode.CHECK_CAST
),
customFingerprint = { it.definingClass.endsWith("YouTubeControlsOverlay;") && it.isWideLiteralExists(insetOverlayViewLayoutId) }
)

View File

@ -5,6 +5,7 @@ import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.addInstruction
import app.revanced.patcher.extensions.addInstructions
import app.revanced.patcher.extensions.instruction
import app.revanced.patcher.extensions.replaceInstruction
import app.revanced.patcher.patch.BytecodePatch
@ -14,15 +15,19 @@ import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.patch.annotations.DependsOn
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
import app.revanced.patches.shared.annotation.YouTubeCompatibility
import app.revanced.patches.shared.fingerprints.TotalTimeFingerprint
import app.revanced.patches.youtube.misc.overridespeed.bytecode.patch.OverrideSpeedHookPatch
import app.revanced.patches.youtube.misc.playercontrols.patch.PlayerControlsPatch
import app.revanced.patches.youtube.misc.resourceid.patch.SharedResourceIdPatch
import app.revanced.patches.youtube.misc.resourceid.patch.SharedResourceIdPatch.Companion.insetOverlayViewLayoutId
import app.revanced.patches.youtube.misc.resourceid.patch.SharedResourceIdPatch.Companion.totalTimeId
import app.revanced.patches.youtube.misc.sponsorblock.bytecode.fingerprints.*
import app.revanced.patches.youtube.misc.timebar.patch.HookTimeBarPatch
import app.revanced.patches.youtube.misc.videoid.legacy.patch.LegacyVideoIdPatch
import app.revanced.patches.youtube.misc.videoid.mainstream.patch.MainstreamVideoIdPatch
import app.revanced.util.bytecode.BytecodeHelper.injectInit
import app.revanced.util.bytecode.BytecodeHelper.updatePatchStatus
import app.revanced.util.bytecode.getWideLiteralIndex
import org.jf.dexlib2.Opcode
import org.jf.dexlib2.builder.BuilderInstruction
import org.jf.dexlib2.builder.instruction.BuilderInstruction3rc
@ -48,7 +53,9 @@ import org.jf.dexlib2.iface.reference.MethodReference
class SponsorBlockBytecodePatch : BytecodePatch(
listOf(
EndScreenEngagementPanelsFingerprint,
PlayerControllerFingerprint
OverlayViewLayoutFingerprint,
PlayerControllerFingerprint,
TotalTimeFingerprint
)
) {
override fun execute(context: BytecodeContext): PatchResult {
@ -158,6 +165,37 @@ class SponsorBlockBytecodePatch : BytecodePatch(
)
}
/**
* Append the new time to the player layout
*/
TotalTimeFingerprint.result?.mutableMethod?.let {
it.apply {
val targetIndex = getWideLiteralIndex(totalTimeId) + 2
val targetRegister = instruction<OneRegisterInstruction>(targetIndex).registerA
addInstructions(
targetIndex + 1, """
invoke-static {v$targetRegister}, $INTEGRATIONS_PLAYER_CONTROLLER_CLASS_DESCRIPTOR->appendTimeWithoutSegments(Ljava/lang/String;)Ljava/lang/String;
move-result-object v$targetRegister
"""
)
}
} ?: return TotalTimeFingerprint.toErrorResult()
/**
* Initialize the SponsorBlock view
*/
OverlayViewLayoutFingerprint.result?.mutableMethod?.let{
it.apply{
val targetIndex = getWideLiteralIndex(insetOverlayViewLayoutId) + 3
val targetRegister = instruction<OneRegisterInstruction>(targetIndex).registerA
addInstruction(
targetIndex + 1,
"invoke-static {v$targetRegister}, $INTEGRATIONS_BUTTON_CLASS_DESCRIPTOR/ui/SponsorBlockViewController;->initialize(Landroid/view/ViewGroup;)V"
)
}
} ?: return OverlayViewLayoutFingerprint.toErrorResult()
/**
* Replace strings

View File

@ -1,85 +0,0 @@
package app.revanced.patches.youtube.misc.sponsorblock.bytecode.patch
import app.revanced.extensions.findMutableMethodOf
import app.revanced.extensions.toResult
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.addInstruction
import app.revanced.patcher.extensions.addInstructions
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.annotations.DependsOn
import app.revanced.patches.shared.annotation.YouTubeCompatibility
import app.revanced.patches.shared.patch.mapping.ResourceMappingPatch
import org.jf.dexlib2.Opcode
import org.jf.dexlib2.iface.instruction.OneRegisterInstruction
import org.jf.dexlib2.iface.instruction.formats.Instruction31i
@Name("sponsorblock-secondary-bytecode-patch")
@DependsOn([ResourceMappingPatch::class])
@YouTubeCompatibility
@Version("0.0.1")
class SponsorBlockSecondaryBytecodePatch : BytecodePatch() {
// list of resource names to get the id of
private val resourceIds = arrayOf(
"string" to "total_time",
"id" to "inset_overlay_view_layout"
).map { (type, name) ->
ResourceMappingPatch
.resourceMappings
.single { it.type == type && it.name == name }.id
}
private var patchSuccessArray = Array(resourceIds.size) {false}
override fun execute(context: BytecodeContext): PatchResult {
context.classes.forEach { classDef ->
classDef.methods.forEach { method ->
with(method.implementation) {
this?.instructions?.forEachIndexed { index, instruction ->
when (instruction.opcode) {
Opcode.CONST -> {
when ((instruction as Instruction31i).wideLiteral) {
resourceIds[0] -> { // total time
val targetIndex = index + 2
val targetInstruction = instructions.elementAt(targetIndex)
val targetRegister = (targetInstruction as OneRegisterInstruction).registerA
val mutableMethod = context.proxy(classDef).mutableClass.findMutableMethodOf(method)
mutableMethod.addInstructions(
targetIndex + 1, """
invoke-static {v$targetRegister}, Lapp/revanced/integrations/sponsorblock/SegmentPlaybackController;->appendTimeWithoutSegments(Ljava/lang/String;)Ljava/lang/String;
move-result-object v$targetRegister
"""
)
patchSuccessArray[0] = true
}
resourceIds[1] -> { // player overlay
val insertIndex = index + 3
val invokeInstruction = instructions.elementAt(insertIndex)
if (invokeInstruction.opcode != Opcode.CHECK_CAST) return@forEachIndexed
val targetRegister = (invokeInstruction as OneRegisterInstruction).registerA
val mutableMethod = context.proxy(classDef).mutableClass.findMutableMethodOf(method)
mutableMethod.addInstruction(
insertIndex + 1,
"invoke-static {v$targetRegister}, Lapp/revanced/integrations/sponsorblock/ui/SponsorBlockViewController;->initialize(Landroid/view/ViewGroup;)V"
)
patchSuccessArray[1] = true
}
}
}
else -> return@forEachIndexed
}
}
}
}
}
return toResult(patchSuccessArray.indexOf(false))
}
}

View File

@ -12,7 +12,6 @@ import app.revanced.patcher.patch.annotations.Patch
import app.revanced.patches.shared.annotation.YouTubeCompatibility
import app.revanced.patches.youtube.misc.settings.resource.patch.SettingsPatch
import app.revanced.patches.youtube.misc.sponsorblock.bytecode.patch.SponsorBlockBytecodePatch
import app.revanced.patches.youtube.misc.sponsorblock.bytecode.patch.SponsorBlockSecondaryBytecodePatch
import app.revanced.util.resources.ResourceUtils
import app.revanced.util.resources.ResourceUtils.copyResources
import app.revanced.util.resources.ResourceUtils.copyXmlNode
@ -23,8 +22,7 @@ import app.revanced.util.resources.ResourceUtils.copyXmlNode
@DependsOn(
[
SettingsPatch::class,
SponsorBlockBytecodePatch::class,
SponsorBlockSecondaryBytecodePatch::class
SponsorBlockBytecodePatch::class
]
)
@YouTubeCompatibility