mirror of
https://github.com/revanced/revanced-patches
synced 2024-12-01 01:42:54 +01:00
refactor(youtube/custom-branding): improve general code quality
This commit is contained in:
parent
24b5bcdd70
commit
6ed43d820a
@ -9,9 +9,9 @@ import app.revanced.patcher.patch.annotations.DependsOn
|
|||||||
import app.revanced.patcher.patch.annotations.Patch
|
import app.revanced.patcher.patch.annotations.Patch
|
||||||
import app.revanced.patches.youtube.layout.branding.icon.annotations.CustomBrandingCompatibility
|
import app.revanced.patches.youtube.layout.branding.icon.annotations.CustomBrandingCompatibility
|
||||||
import app.revanced.patches.youtube.misc.manifest.patch.FixLocaleConfigErrorPatch
|
import app.revanced.patches.youtube.misc.manifest.patch.FixLocaleConfigErrorPatch
|
||||||
|
import app.revanced.util.resources.ResourceUtils
|
||||||
|
import app.revanced.util.resources.ResourceUtils.copyResources
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileInputStream
|
|
||||||
import java.io.InputStream
|
|
||||||
import java.nio.file.Files
|
import java.nio.file.Files
|
||||||
|
|
||||||
@Patch
|
@Patch
|
||||||
@ -22,36 +22,43 @@ import java.nio.file.Files
|
|||||||
@Version("0.0.1")
|
@Version("0.0.1")
|
||||||
class CustomBrandingPatch : ResourcePatch {
|
class CustomBrandingPatch : ResourcePatch {
|
||||||
override fun execute(context: ResourceContext): PatchResult {
|
override fun execute(context: ResourceContext): PatchResult {
|
||||||
val resDirectory = context["res"]
|
fun copyResources(resourceGroups: List<ResourceUtils.ResourceGroup>) {
|
||||||
if (!resDirectory.isDirectory) return PatchResultError("The res folder can not be found.")
|
iconPath?.let { iconPathString ->
|
||||||
|
val iconPath = File(iconPathString)
|
||||||
|
val resourceDirectory = context["res"]
|
||||||
|
|
||||||
// Icon branding
|
resourceGroups.forEach { group ->
|
||||||
val iconNames = arrayOf(
|
val fromDirectory = iconPath.resolve(group.resourceDirectoryName)
|
||||||
|
val toDirectory = resourceDirectory.resolve(group.resourceDirectoryName)
|
||||||
|
|
||||||
|
group.resources.forEach { iconFileName ->
|
||||||
|
Files.write(
|
||||||
|
toDirectory.resolve(iconFileName).toPath(),
|
||||||
|
fromDirectory.resolve(iconFileName).readBytes()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} ?: resourceGroups.forEach { context.copyResources("branding", it) }
|
||||||
|
}
|
||||||
|
|
||||||
|
val iconResourceFileNames = arrayOf(
|
||||||
"adaptiveproduct_youtube_background_color_108",
|
"adaptiveproduct_youtube_background_color_108",
|
||||||
"adaptiveproduct_youtube_foreground_color_108",
|
"adaptiveproduct_youtube_foreground_color_108",
|
||||||
"ic_launcher",
|
"ic_launcher",
|
||||||
"ic_launcher_round"
|
"ic_launcher_round"
|
||||||
|
).map { "$it.png" }.toTypedArray()
|
||||||
|
|
||||||
|
fun createGroup(directory: String) = ResourceUtils.ResourceGroup(
|
||||||
|
directory, *iconResourceFileNames
|
||||||
)
|
)
|
||||||
|
|
||||||
mapOf(
|
// change the app icon
|
||||||
"xxxhdpi" to 192,
|
arrayOf("xxxhdpi", "xxhdpi", "xhdpi", "hdpi", "mdpi")
|
||||||
"xxhdpi" to 144,
|
.map { "mipmap-$it" }
|
||||||
"xhdpi" to 96,
|
.map(::createGroup)
|
||||||
"hdpi" to 72,
|
.let(::copyResources)
|
||||||
"mdpi" to 48
|
|
||||||
).forEach { (iconDirectory, size) ->
|
|
||||||
iconNames.forEach { iconName ->
|
|
||||||
val iconFile = getIconStream("branding/$size/$iconName.png")
|
|
||||||
?: return PatchResultError("The icon $iconName can not be found.")
|
|
||||||
|
|
||||||
Files.write(
|
// change the name of the app
|
||||||
resDirectory.resolve("mipmap-$iconDirectory").resolve("$iconName.png").toPath(),
|
|
||||||
iconFile.readBytes()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Name branding
|
|
||||||
val manifest = context["AndroidManifest.xml"]
|
val manifest = context["AndroidManifest.xml"]
|
||||||
manifest.writeText(
|
manifest.writeText(
|
||||||
manifest.readText()
|
manifest.readText()
|
||||||
@ -64,15 +71,6 @@ class CustomBrandingPatch : ResourcePatch {
|
|||||||
return PatchResultSuccess()
|
return PatchResultSuccess()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getIconStream(iconPath: String): InputStream? {
|
|
||||||
if (appIconPath == null) {
|
|
||||||
return this.javaClass.classLoader.getResourceAsStream(iconPath)
|
|
||||||
}
|
|
||||||
val file = File(appIconPath!!).resolve(iconPath)
|
|
||||||
if (!file.exists()) return null
|
|
||||||
return FileInputStream(file)
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object : OptionsContainer() {
|
companion object : OptionsContainer() {
|
||||||
private var appName: String? by option(
|
private var appName: String? by option(
|
||||||
PatchOption.StringOption(
|
PatchOption.StringOption(
|
||||||
@ -84,12 +82,12 @@ class CustomBrandingPatch : ResourcePatch {
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
private var appIconPath: String? by option(
|
private var iconPath: String? by option(
|
||||||
PatchOption.StringOption(
|
PatchOption.StringOption(
|
||||||
key = "appIconPath",
|
key = "iconPath",
|
||||||
default = null,
|
default = null,
|
||||||
title = "Application Icon Path",
|
title = "App Icon Path",
|
||||||
description = "A path to the icon of the application."
|
description = "A path containing mipmap resource folders with icons."
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user