Skip to content

Commit

Permalink
refactor: Less copy paste of code. Add a description to exception if …
Browse files Browse the repository at this point in the history
…the resource is not found
  • Loading branch information
LisoUseInAIKyrios committed Apr 19, 2024
1 parent 3f25821 commit b3c4298
Show file tree
Hide file tree
Showing 23 changed files with 102 additions and 75 deletions.
1 change: 1 addition & 0 deletions api/revanced-patches.api
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ public final class app/revanced/patches/shared/misc/mapping/ResourceMappingPatch
public static final field INSTANCE Lapp/revanced/patches/shared/misc/mapping/ResourceMappingPatch;
public synthetic fun execute (Lapp/revanced/patcher/data/Context;)V
public fun execute (Lapp/revanced/patcher/data/ResourceContext;)V
public final fun firstIdForResource (Ljava/lang/String;Ljava/lang/String;)J
}

public final class app/revanced/patches/shared/misc/mapping/ResourceMappingPatch$ResourceElement {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app.revanced.patches.shared.misc.mapping

import app.revanced.patcher.data.ResourceContext
import app.revanced.patcher.patch.PatchException
import app.revanced.patcher.patch.ResourcePatch
import org.w3c.dom.Element
import java.util.*
Expand Down Expand Up @@ -60,4 +61,15 @@ object ResourceMappingPatch : ResourcePatch() {
}

data class ResourceElement(val type: String, val name: String, val id: Long)

/**
* @throws PatchException if the resource is not found.
*/
fun firstIdForResource(type: String, name: String) : Long {
val resource = resourceMappings.find {
it.type == type && it.name == name
} ?: throw PatchException("Could not find resource type: $type with name: $name")

return resource.id
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ object PremiumNavbarTabResourcePatch : ResourcePatch() {
internal var premiumTabId = -1L

override fun execute(context: ResourceContext) {
premiumTabId = ResourceMappingPatch.resourceMappings.single {
it.type == "id" && it.name == "premium_tab"
}.id
premiumTabId = ResourceMappingPatch.firstIdForResource("id", "premium_tab")

showBottomNavigationItemsTextId = ResourceMappingPatch.resourceMappings.single {
it.type == "bool" && it.name == "show_bottom_navigation_items_text"
}.id
showBottomNavigationItemsTextId = ResourceMappingPatch.firstIdForResource(
"bool",
"show_bottom_navigation_items_text"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ object HideAdsResourcePatch : ResourcePatch() {

LithoFilterPatch.addFilter(FILTER_CLASS_DESCRIPTOR)

adAttributionId = ResourceMappingPatch.resourceMappings.single { it.name == "ad_attribution" }.id
adAttributionId = ResourceMappingPatch.firstIdForResource("id", "ad_attribution")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ internal object AlbumCardsResourcePatch : ResourcePatch() {
SwitchPreference("revanced_hide_album_cards")
)

albumCardId = ResourceMappingPatch.resourceMappings.single {
it.type == "layout" && it.name == "album_card"
}.id
albumCardId = ResourceMappingPatch.firstIdForResource(
"layout",
"album_card"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ internal object CrowdfundingBoxResourcePatch : ResourcePatch() {
SwitchPreference("revanced_hide_crowdfunding_box")
)

crowdfundingBoxId = ResourceMappingPatch.resourceMappings.single {
it.type == "layout" && it.name == "donation_companion"
}.id
crowdfundingBoxId = ResourceMappingPatch.firstIdForResource(
"layout",
"donation_companion"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ internal object HideEndscreenCardsResourcePatch : ResourcePatch() {
SwitchPreference("revanced_hide_endscreen_cards")
)

fun findEndscreenResourceId(name: String) = ResourceMappingPatch.resourceMappings.single {
it.type == "layout" && it.name == "endscreen_element_layout_$name"
}.id
fun findEndscreenResourceId(name: String) = ResourceMappingPatch.firstIdForResource(
"layout",
"endscreen_element_layout_$name"
)

layoutCircle = findEndscreenResourceId("circle")
layoutIcon = findEndscreenResourceId("icon")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ internal object HideFilterBarResourcePatch : ResourcePatch() {
)
)

relatedChipCloudMarginId = "related_chip_cloud_reduced_margins".layoutResourceId("layout")
filterBarHeightId = "filter_bar_height".layoutResourceId()
barContainerHeightId = "bar_container_height".layoutResourceId()
relatedChipCloudMarginId = ResourceMappingPatch.firstIdForResource("layout", "related_chip_cloud_reduced_margins")
filterBarHeightId = ResourceMappingPatch.firstIdForResource("dimen", "filter_bar_height")
barContainerHeightId = ResourceMappingPatch.firstIdForResource("dimen", "bar_container_height")
}

private fun String.layoutResourceId(type: String = "dimen") =
ResourceMappingPatch.resourceMappings.single { it.type == type && it.name == this }.id
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package app.revanced.patches.youtube.layout.hide.floatingmicrophone

import app.revanced.patcher.data.ResourceContext
import app.revanced.patcher.patch.PatchException
import app.revanced.patcher.patch.ResourcePatch
import app.revanced.patcher.patch.annotation.Patch
import app.revanced.patches.all.misc.resources.AddResourcesPatch
Expand All @@ -26,7 +25,6 @@ internal object HideFloatingMicrophoneButtonResourcePatch : ResourcePatch() {
SwitchPreference("revanced_hide_floating_microphone_button")
)

fabButtonId = ResourceMappingPatch.resourceMappings.find { it.type == "id" && it.name == "fab" }?.id
?: throw PatchException("Can not find required fab button resource id")
fabButtonId = ResourceMappingPatch.firstIdForResource("id", "fab")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import app.revanced.patcher.patch.ResourcePatch
import app.revanced.patcher.patch.annotation.Patch
import app.revanced.patches.all.misc.resources.AddResourcesPatch
import app.revanced.patches.shared.misc.mapping.ResourceMappingPatch
import app.revanced.patches.shared.misc.settings.preference.SwitchPreference
import app.revanced.patches.youtube.misc.settings.SettingsPatch

@Patch(
Expand All @@ -19,8 +18,9 @@ internal object HideLayoutComponentsResourcePatch : ResourcePatch() {
internal var expandButtonDownId: Long = -1

override fun execute(context: ResourceContext) {
expandButtonDownId = ResourceMappingPatch.resourceMappings.single {
it.type == "layout" && it.name == "expand_button_down"
}.id
expandButtonDownId = ResourceMappingPatch.firstIdForResource(
"layout",
"expand_button_down"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ object HideInfocardsResourcePatch : ResourcePatch() {
SwitchPreference("revanced_hide_info_cards")
)

drawerResourceId = ResourceMappingPatch.resourceMappings.single {
it.type == "id" && it.name == "info_cards_drawer_header"
}.id
drawerResourceId = ResourceMappingPatch.firstIdForResource(
"id",
"info_cards_drawer_header"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,16 @@ object HideShortsComponentsResourcePatch : ResourcePatch() {
SwitchPreference("revanced_hide_shorts_navigation_bar"),
)

// Cannot use first, as this resource does not exist in newer targets.
ResourceMappingPatch.resourceMappings.find {
it.type == "layout" && it.name == "reel_multiple_items_shelf"
}?.also {
reelMultipleItemShelfId = it.id
}

reelPlayerRightCellButtonHeight = ResourceMappingPatch.resourceMappings.first {
it.type == "dimen" && it.name == "reel_player_right_cell_button_height"
}.id
reelPlayerRightCellButtonHeight = ResourceMappingPatch.firstIdForResource(
"dimen",
"reel_player_right_cell_button_height"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ internal object DisableSuggestedVideoEndScreenResourcePatch : ResourcePatch() {
SwitchPreference("revanced_disable_suggested_video_end_screen")
)

sizeAdjustableLiteAutoNavOverlay = ResourceMappingPatch.resourceMappings.single {
it.type == "layout" && it.name == "size_adjustable_lite_autonav_overlay"
}.id
sizeAdjustableLiteAutoNavOverlay = ResourceMappingPatch.firstIdForResource(
"layout",
"size_adjustable_lite_autonav_overlay"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ internal object CustomPlayerOverlayOpacityResourcePatch : ResourcePatch() {
TextPreference("revanced_player_overlay_opacity", inputType = InputType.NUMBER)
)

scrimOverlayId = ResourceMappingPatch.resourceMappings.single {
it.type == "id" && it.name == "scrim_overlay"
}.id
scrimOverlayId = ResourceMappingPatch.firstIdForResource(
"id",
"scrim_overlay"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ internal object ReturnYouTubeDislikeResourcePatch : ResourcePatch() {
intent = SettingsPatch.newIntent("revanced_ryd_settings_intent")
)

oldUIDislikeId = ResourceMappingPatch.resourceMappings.single {
it.type == "id" && it.name == "dislike_button"
}.id
oldUIDislikeId = ResourceMappingPatch.firstIdForResource(
"id",
"dislike_button"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ internal object SeekbarColorResourcePatch : ResourcePatch() {
internal var inlineTimeBarPlayedNotHighlightedColorId = -1L

override fun execute(context: ResourceContext) {
fun findColorResource(resourceName: String): Long {
return ResourceMappingPatch.resourceMappings
.find { it.type == "color" && it.name == resourceName }?.id
?: throw PatchException("Could not find color resource: $resourceName")
}

reelTimeBarPlayedColorId =
findColorResource("reel_time_bar_played_color")
ResourceMappingPatch.firstIdForResource(
"color",
"reel_time_bar_played_color"
)
inlineTimeBarColorizedBarPlayedColorDarkId =
findColorResource("inline_time_bar_colorized_bar_played_color_dark")
ResourceMappingPatch.firstIdForResource(
"color",
"inline_time_bar_colorized_bar_played_color_dark"
)
inlineTimeBarPlayedNotHighlightedColorId =
findColorResource("inline_time_bar_played_not_highlighted_color")
ResourceMappingPatch.firstIdForResource(
"color",
"inline_time_bar_played_not_highlighted_color"
)

// Edit the resume playback drawable and replace the progress bar with a custom drawable
context.xmlEditor["res/drawable/resume_playback_progressbar_drawable.xml"].use { editor ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ object SponsorBlockBytecodePatch : BytecodePatch(
val controlsMethodResult = PlayerControlsBytecodePatch.showPlayerControlsFingerprintResult

val controlsLayoutStubResourceId =
ResourceMappingPatch.resourceMappings.single { it.type == "id" && it.name == "controls_layout_stub" }.id
ResourceMappingPatch.firstIdForResource("id", "controls_layout_stub")
val zoomOverlayResourceId =
ResourceMappingPatch.resourceMappings.single { it.type == "id" && it.name == "video_zoom_overlay_stub" }.id
ResourceMappingPatch.firstIdForResource("id", "video_zoom_overlay_stub")

methods@ for (method in controlsMethodResult.mutableClass.methods) {
val instructions = method.implementation?.instructions!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ object SpoofSignatureResourcePatch : ResourcePatch() {
internal var scrubbedPreviewThumbnailResourceId: Long = -1

override fun execute(context: ResourceContext) {
scrubbedPreviewThumbnailResourceId = ResourceMappingPatch.resourceMappings.single {
it.type == "id" && it.name == "thumbnail"
}.id
scrubbedPreviewThumbnailResourceId = ResourceMappingPatch.firstIdForResource(
"id",
"thumbnail"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ internal object NavigationBarHookResourcePatch : ResourcePatch() {
internal var actionBarSearchResultsViewMicId: Long = -1

override fun execute(context: ResourceContext) {
imageOnlyTabResourceId = ResourceMappingPatch.resourceMappings.first {
it.type == "layout" && it.name == "image_only_tab"
}.id
imageOnlyTabResourceId = ResourceMappingPatch.firstIdForResource(
"layout",
"image_only_tab"
)

actionBarSearchResultsViewMicId = ResourceMappingPatch.resourceMappings.first {
it.type == "layout" && it.name == "action_bar_search_results_view_mic"
}.id
actionBarSearchResultsViewMicId = ResourceMappingPatch.firstIdForResource(
"layout",
"action_bar_search_results_view_mic"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ object BottomControlsResourcePatch : ResourcePatch(), Closeable {
resourceContext = context
targetDocumentEditor = context.xmlEditor[TARGET_RESOURCE]

bottomUiContainerResourceId = ResourceMappingPatch.resourceMappings
.single { it.type == "id" && it.name == "bottom_ui_container_stub" }.id
bottomUiContainerResourceId = ResourceMappingPatch.firstIdForResource(
"id",
"bottom_ui_container_stub"
)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ object SettingsResourcePatch : BaseSettingsResourcePatch(

// Used for a fingerprint from SettingsPatch.
appearanceStringId =
ResourceMappingPatch.resourceMappings.find {
it.type == "string" && it.name == "app_theme_appearance_dark"
}!!.id
ResourceMappingPatch.firstIdForResource(
"string",
"app_theme_appearance_dark"
)

arrayOf(
ResourceGroup("layout", "revanced_settings_with_toolbar.xml"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ internal object CustomPlaybackSpeedResourcePatch : ResourcePatch() {
var speedUnavailableId: Long = -1

override fun execute(context: ResourceContext) {
speedUnavailableId = ResourceMappingPatch.resourceMappings.single {
it.type == "string" && it.name == "varispeed_unavailable_message"
}.id
speedUnavailableId = ResourceMappingPatch.firstIdForResource(
"string",
"varispeed_unavailable_message"
)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package app.revanced.patches.youtube.video.videoqualitymenu

import app.revanced.patcher.data.ResourceContext
import app.revanced.patcher.patch.PatchException
import app.revanced.patcher.patch.ResourcePatch
import app.revanced.patcher.patch.annotation.Patch
import app.revanced.patches.all.misc.resources.AddResourcesPatch
Expand All @@ -22,10 +21,10 @@ object RestoreOldVideoQualityMenuResourcePatch : ResourcePatch() {
SwitchPreference("revanced_restore_old_video_quality_menu")
)

fun findResource(name: String) = ResourceMappingPatch.resourceMappings.find { it.name == name }?.id
?: throw PatchException("Could not find resource")

// Used for the old type of the video quality menu.
videoQualityBottomSheetListFragmentTitle = findResource("video_quality_bottom_sheet_list_fragment_title")
videoQualityBottomSheetListFragmentTitle = ResourceMappingPatch.firstIdForResource(
"layout",
"video_quality_bottom_sheet_list_fragment_title"
)
}
}

0 comments on commit b3c4298

Please sign in to comment.