mirror of
https://github.com/ReVanced/revanced-patches.git
synced 2024-11-10 01:01:56 +01:00
feat(YouTube - Change header): Improve patch option description
This commit is contained in:
parent
e86389e543
commit
3b8bc08d4e
1 changed files with 60 additions and 47 deletions
|
@ -15,62 +15,69 @@ import java.io.File
|
||||||
name = "Change header",
|
name = "Change header",
|
||||||
description = "Applies a custom header in the top left corner within the app. Defaults to the ReVanced header.",
|
description = "Applies a custom header in the top left corner within the app. Defaults to the ReVanced header.",
|
||||||
compatiblePackages = [
|
compatiblePackages = [
|
||||||
CompatiblePackage("com.google.android.youtube")
|
CompatiblePackage("com.google.android.youtube"),
|
||||||
],
|
],
|
||||||
use = false
|
use = false,
|
||||||
)
|
)
|
||||||
@Suppress("unused")
|
@Suppress("unused")
|
||||||
object ChangeHeaderPatch : ResourcePatch() {
|
object ChangeHeaderPatch : ResourcePatch() {
|
||||||
private const val HEADER_NAME = "yt_wordmark_header"
|
private const val HEADER_FILE_NAME = "yt_wordmark_header"
|
||||||
private const val PREMIUM_HEADER_NAME = "yt_premium_wordmark_header"
|
private const val PREMIUM_HEADER_FILE_NAME = "yt_premium_wordmark_header"
|
||||||
private const val REVANCED_HEADER_NAME = "ReVanced"
|
|
||||||
private const val REVANCED_BORDERLESS_HEADER_NAME = "ReVanced (borderless logo)"
|
|
||||||
|
|
||||||
private val targetResourceDirectoryNames = arrayOf(
|
private const val HEADER_OPTION = "header*"
|
||||||
"xxxhdpi",
|
private const val PREMIUM_HEADER_OPTION = "premium*header"
|
||||||
"xxhdpi",
|
private const val REVANCED_HEADER_OPTION = "revanced*"
|
||||||
"xhdpi",
|
private const val REVANCED_BORDERLESS_HEADER_OPTION = "revanced*borderless"
|
||||||
"mdpi",
|
|
||||||
"hdpi",
|
private val targetResourceDirectoryNames = mapOf(
|
||||||
).map { dpi ->
|
"xxxhdpi" to "512px x 192px",
|
||||||
"drawable-$dpi"
|
"xxhdpi" to "387px x 144px",
|
||||||
}
|
"xhdpi" to "258px x 96px",
|
||||||
|
"hdpi" to "194px x 72px",
|
||||||
|
"mdpi" to "129px x 48px",
|
||||||
|
).map { (dpi, dim) ->
|
||||||
|
"drawable-$dpi" to dim
|
||||||
|
}.toMap()
|
||||||
|
|
||||||
private val variants = arrayOf("light", "dark")
|
private val variants = arrayOf("light", "dark")
|
||||||
|
|
||||||
private val header by stringPatchOption(
|
private val header by stringPatchOption(
|
||||||
key = "header",
|
key = "header",
|
||||||
default = REVANCED_BORDERLESS_HEADER_NAME,
|
default = REVANCED_BORDERLESS_HEADER_OPTION,
|
||||||
values = mapOf(
|
values = mapOf(
|
||||||
"YouTube" to HEADER_NAME,
|
"YouTube" to HEADER_OPTION,
|
||||||
"YouTube Premium" to PREMIUM_HEADER_NAME,
|
"YouTube Premium" to PREMIUM_HEADER_OPTION,
|
||||||
"ReVanced" to REVANCED_HEADER_NAME,
|
"ReVanced" to REVANCED_HEADER_OPTION,
|
||||||
"ReVanced (borderless logo)" to REVANCED_BORDERLESS_HEADER_NAME,
|
"ReVanced (borderless logo)" to REVANCED_BORDERLESS_HEADER_OPTION,
|
||||||
),
|
),
|
||||||
title = "Header",
|
title = "Header",
|
||||||
description = """
|
description = """
|
||||||
Either a header name or a path to a custom header folder to use in the top bar.
|
The header to apply to the app.
|
||||||
The path to a folder must contain one or more of the following folders matching the DPI of your device:
|
|
||||||
|
If a path to a folder is provided, the folder must contain one or more of the following folders, depending on the DPI of the device:
|
||||||
|
|
||||||
|
${targetResourceDirectoryNames.keys.joinToString("\n") { "- $it" }}
|
||||||
|
|
||||||
|
Each of the folders must contain all of the following files:
|
||||||
|
|
||||||
|
${variants.joinToString("\n") { variant -> "- ${HEADER_FILE_NAME}_$variant.png" }}
|
||||||
|
|
||||||
${targetResourceDirectoryNames.joinToString("\n") { "- $it" }}
|
The image dimensions must be as follows:
|
||||||
|
${targetResourceDirectoryNames.map { (dpi, dim) -> "- $dpi: $dim" }.joinToString("\n")}
|
||||||
These folders must contain the following files:
|
|
||||||
|
|
||||||
${variants.joinToString("\n") { variant -> "- ${HEADER_NAME}_$variant.png" }}
|
|
||||||
""".trimIndentMultiline(),
|
""".trimIndentMultiline(),
|
||||||
required = true,
|
required = true,
|
||||||
)
|
)
|
||||||
|
|
||||||
override fun execute(context: ResourceContext) {
|
override fun execute(context: ResourceContext) {
|
||||||
// The directories to copy the header to.
|
// The directories to copy the header to.
|
||||||
val targetResourceDirectories = targetResourceDirectoryNames.mapNotNull {
|
val targetResourceDirectories = targetResourceDirectoryNames.keys.mapNotNull {
|
||||||
context["res"].resolve(it).takeIf(File::exists)
|
context["res"].resolve(it).takeIf(File::exists)
|
||||||
}
|
}
|
||||||
// The files to replace in the target directories.
|
// The files to replace in the target directories.
|
||||||
val targetResourceFiles = targetResourceDirectoryNames.map { directoryName ->
|
val targetResourceFiles = targetResourceDirectoryNames.keys.map { directoryName ->
|
||||||
ResourceGroup(
|
ResourceGroup(
|
||||||
directoryName,
|
directoryName,
|
||||||
*variants.map { variant -> "${HEADER_NAME}_$variant.png" }.toTypedArray()
|
*variants.map { variant -> "${HEADER_FILE_NAME}_$variant.png" }.toTypedArray(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,8 +96,8 @@ object ChangeHeaderPatch : ResourcePatch() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Functions to overwrite the header to the different variants.
|
// Functions to overwrite the header to the different variants.
|
||||||
val toPremium = { overwriteFromTo(PREMIUM_HEADER_NAME, HEADER_NAME) }
|
val toPremium = { overwriteFromTo(PREMIUM_HEADER_FILE_NAME, HEADER_FILE_NAME) }
|
||||||
val toHeader = { overwriteFromTo(HEADER_NAME, PREMIUM_HEADER_NAME) }
|
val toHeader = { overwriteFromTo(HEADER_FILE_NAME, PREMIUM_HEADER_FILE_NAME) }
|
||||||
val toReVanced = {
|
val toReVanced = {
|
||||||
// Copy the ReVanced header to the resource directories.
|
// Copy the ReVanced header to the resource directories.
|
||||||
targetResourceFiles.forEach { context.copyResources("change-header/revanced", it) }
|
targetResourceFiles.forEach { context.copyResources("change-header/revanced", it) }
|
||||||
|
@ -106,32 +113,38 @@ object ChangeHeaderPatch : ResourcePatch() {
|
||||||
toHeader()
|
toHeader()
|
||||||
}
|
}
|
||||||
val toCustom = {
|
val toCustom = {
|
||||||
var copiedReplacementImages = false
|
val sourceFolders = File(header!!).listFiles { file -> file.isDirectory }
|
||||||
// For all the resource groups in the custom header folder, copy them to the resource directories.
|
?: throw PatchException("The provided path is not a directory: $header")
|
||||||
File(header!!).listFiles { file -> file.isDirectory }?.forEach { folder ->
|
|
||||||
val targetDirectory = context["res"].resolve(folder.name)
|
|
||||||
// Skip if the target directory (DPI) doesn't exist.
|
|
||||||
if (!targetDirectory.exists()) return@forEach
|
|
||||||
|
|
||||||
folder.listFiles { file -> file.isFile }?.forEach {
|
var copiedFiles = false
|
||||||
val targetResourceFile = targetDirectory.resolve(it.name)
|
|
||||||
|
|
||||||
it.copyTo(targetResourceFile, true)
|
// For each source folder, copy the files to the target resource directories.
|
||||||
copiedReplacementImages = true
|
sourceFolders.forEach { dpiSourceFolder ->
|
||||||
|
val targetDpiFolder = context["res"].resolve(dpiSourceFolder.name)
|
||||||
|
if (!targetDpiFolder.exists()) return@forEach
|
||||||
|
|
||||||
|
val imgSourceFiles = dpiSourceFolder.listFiles { file -> file.isFile }!!
|
||||||
|
imgSourceFiles.forEach { imgSourceFile ->
|
||||||
|
val imgTargetFile = targetDpiFolder.resolve(imgSourceFile.name)
|
||||||
|
imgSourceFile.copyTo(imgTargetFile, true)
|
||||||
|
|
||||||
|
copiedFiles = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!copiedReplacementImages) throw PatchException("Could not find any custom images resources in directory: $header")
|
if (!copiedFiles) {
|
||||||
|
throw PatchException("No header files were copied from the provided path: $header.")
|
||||||
|
}
|
||||||
|
|
||||||
// Overwrite the premium with the custom header as well.
|
// Overwrite the premium with the custom header as well.
|
||||||
toHeader()
|
toHeader()
|
||||||
}
|
}
|
||||||
|
|
||||||
when (header) {
|
when (header) {
|
||||||
HEADER_NAME -> toHeader
|
HEADER_OPTION -> toHeader
|
||||||
PREMIUM_HEADER_NAME -> toPremium
|
PREMIUM_HEADER_OPTION -> toPremium
|
||||||
REVANCED_HEADER_NAME -> toReVanced
|
REVANCED_HEADER_OPTION -> toReVanced
|
||||||
REVANCED_BORDERLESS_HEADER_NAME -> toReVancedBorderless
|
REVANCED_BORDERLESS_HEADER_OPTION -> toReVancedBorderless
|
||||||
else -> toCustom
|
else -> toCustom
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue