|
| 1 | +package app.revanced.patches.youtube.layout.branding.header |
| 2 | + |
| 3 | +import app.revanced.patcher.data.ResourceContext |
| 4 | +import app.revanced.patcher.patch.PatchException |
| 5 | +import app.revanced.patcher.patch.ResourcePatch |
| 6 | +import app.revanced.patcher.patch.annotation.CompatiblePackage |
| 7 | +import app.revanced.patcher.patch.annotation.Patch |
| 8 | +import app.revanced.patcher.patch.options.PatchOption.PatchExtensions.stringPatchOption |
| 9 | +import app.revanced.util.ResourceGroup |
| 10 | +import app.revanced.util.copyResources |
| 11 | +import java.io.File |
| 12 | + |
| 13 | +@Patch( |
| 14 | + name = "Change header", |
| 15 | + description = "Change the in app header. Defaults to the ReVanced header.", |
| 16 | + compatiblePackages = [ |
| 17 | + CompatiblePackage("com.google.android.youtube") |
| 18 | + ], |
| 19 | + use = false |
| 20 | +) |
| 21 | +@Suppress("unused") |
| 22 | +object ChangeHeaderPatch : ResourcePatch() { |
| 23 | + private const val HEADER_NAME = "yt_wordmark_header" |
| 24 | + private const val PREMIUM_HEADER_NAME = "yt_premium_wordmark_header" |
| 25 | + private const val REVANCED_HEADER_NAME = "ReVanced" |
| 26 | + private const val REVANCED_BORDERLESS_HEADER_NAME = "ReVanced (borderless logo)" |
| 27 | + |
| 28 | + private val targetResourceDirectoryNames = arrayOf( |
| 29 | + "xxxhdpi", |
| 30 | + "xxhdpi", |
| 31 | + "xhdpi", |
| 32 | + "mdpi", |
| 33 | + "hdpi", |
| 34 | + ).map { dpi -> |
| 35 | + "drawable-$dpi" |
| 36 | + } |
| 37 | + |
| 38 | + private val variants = arrayOf("light", "dark") |
| 39 | + |
| 40 | + private val header by stringPatchOption( |
| 41 | + key = "header", |
| 42 | + default = REVANCED_BORDERLESS_HEADER_NAME, |
| 43 | + values = mapOf( |
| 44 | + "YouTube" to HEADER_NAME, |
| 45 | + "YouTube Premium" to PREMIUM_HEADER_NAME, |
| 46 | + "ReVanced" to REVANCED_HEADER_NAME, |
| 47 | + "ReVanced (borderless logo)" to REVANCED_BORDERLESS_HEADER_NAME, |
| 48 | + ), |
| 49 | + title = "Header", |
| 50 | + description = """ |
| 51 | + The header to use in top bar or the path to a custom header. |
| 52 | + The path to a folder containing one or more of the following folders matching the DPI of your device: |
| 53 | +
|
| 54 | + ${targetResourceDirectoryNames.joinToString("\n") { "- $it" }} |
| 55 | +
|
| 56 | + These folders must contain the following files: |
| 57 | +
|
| 58 | + ${variants.joinToString("\n") { variant -> "- ${HEADER_NAME}_$variant.png" }} |
| 59 | + """.trimIndent(), |
| 60 | + required = true, |
| 61 | + ) |
| 62 | + |
| 63 | + override fun execute(context: ResourceContext) { |
| 64 | + // The directories to copy the header to. |
| 65 | + val targetResourceDirectories = targetResourceDirectoryNames.mapNotNull { |
| 66 | + context["res"].resolve(it).takeIf(File::exists) |
| 67 | + } |
| 68 | + // The files to replace in the target directories. |
| 69 | + val targetResourceFiles = targetResourceDirectoryNames.map { directoryName -> |
| 70 | + ResourceGroup( |
| 71 | + directoryName, |
| 72 | + *variants.map { variant -> "${HEADER_NAME}_$variant.png" }.toTypedArray() |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * A function that overwrites both header variants from [from] to [to] in the target resource directories. |
| 78 | + */ |
| 79 | + val overwriteFromTo: (String, String) -> Unit = { from: String, to: String -> |
| 80 | + targetResourceDirectories.forEach { directory -> |
| 81 | + variants.forEach { variant -> |
| 82 | + val fromPath = directory.resolve("${from}_$variant.png") |
| 83 | + val toPath = directory.resolve("${to}_$variant.png") |
| 84 | + |
| 85 | + fromPath.copyTo(toPath, true) |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // Functions to overwrite the header to the different variants. |
| 91 | + val toPremium = { overwriteFromTo(PREMIUM_HEADER_NAME, HEADER_NAME) } |
| 92 | + val toHeader = { overwriteFromTo(HEADER_NAME, PREMIUM_HEADER_NAME) } |
| 93 | + val toReVanced = { |
| 94 | + // Copy the ReVanced header to the resource directories. |
| 95 | + targetResourceFiles.forEach { context.copyResources("change-header/revanced", it) } |
| 96 | + |
| 97 | + // Overwrite the premium with the custom header as well. |
| 98 | + toHeader() |
| 99 | + } |
| 100 | + val toReVancedBorderless = { |
| 101 | + // Copy the ReVanced borderless header to the resource directories. |
| 102 | + targetResourceFiles.forEach { context.copyResources("change-header/revanced-borderless", it) } |
| 103 | + |
| 104 | + // Overwrite the premium with the custom header as well. |
| 105 | + toHeader() |
| 106 | + } |
| 107 | + val toCustom = { |
| 108 | + var copiedReplacementImages = false |
| 109 | + // For all the resource groups in the custom header folder, copy them to the resource directories. |
| 110 | + File(header!!).listFiles { file -> file.isDirectory }?.forEach { folder -> |
| 111 | + val targetDirectory = context["res"].resolve(folder.name) |
| 112 | + // Skip if the target directory (DPI) doesn't exist. |
| 113 | + if (!targetDirectory.exists()) return@forEach |
| 114 | + |
| 115 | + folder.listFiles { file -> file.isFile }?.forEach { |
| 116 | + val targetResourceFile = targetDirectory.resolve(it.name) |
| 117 | + |
| 118 | + it.copyTo(targetResourceFile, true) |
| 119 | + copiedReplacementImages = true |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if (!copiedReplacementImages) throw PatchException("Could not find any custom images resources in directory: $header") |
| 124 | + |
| 125 | + // Overwrite the premium with the custom header as well. |
| 126 | + toHeader() |
| 127 | + } |
| 128 | + |
| 129 | + when (header) { |
| 130 | + HEADER_NAME -> toHeader |
| 131 | + PREMIUM_HEADER_NAME -> toPremium |
| 132 | + REVANCED_HEADER_NAME -> toReVanced |
| 133 | + REVANCED_BORDERLESS_HEADER_NAME -> toReVancedBorderless |
| 134 | + else -> toCustom |
| 135 | + }() |
| 136 | + } |
| 137 | +} |
0 commit comments