Skip to content

Commit

Permalink
fix(YouTube - Custom Shorts action buttons): Exception is thrown when…
Browse files Browse the repository at this point in the history
… patching with Revancify
  • Loading branch information
anddea committed Feb 11, 2025
1 parent 37131e0 commit fedf389
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ val customBrandingIconPatch = resourcePatch(
copyResources(
"$youtubeMusicIconResourcePath/splash",
it,
createDirectoryIfNotExist = true
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import app.revanced.patches.youtube.utils.settings.settingsPatch
import app.revanced.util.ResourceGroup
import app.revanced.util.Utils.printInfo
import app.revanced.util.copyResources
import app.revanced.util.inputStreamFromBundledResourceOrThrow
import app.revanced.util.inputStreamFromBundledResource
import app.revanced.util.lowerCaseOrThrow
import java.nio.file.Files
import java.nio.file.StandardCopyOption
Expand Down Expand Up @@ -78,18 +78,25 @@ val shortsActionButtonsPatch = resourcePatch(
fromResourceArray.forEach { fromFileName ->
drawableDirectories.forEach { drawableDirectory ->
val fromFile = "$drawableDirectory/$fromFileName.webp"
val fromPath = res.resolve(fromFile).toPath()
val fromFileResolved = res.resolve(fromFile)
val toFile = "$drawableDirectory/$toFileName.webp"
val toPath = res.resolve(toFile).toPath()
val toFileResolved = res.resolve(toFile)
val inputStreamForLegacy =
inputStreamFromBundledResourceOrThrow(sourceResourceDirectory, fromFile)
val inputStreamForNew =
inputStreamFromBundledResourceOrThrow(sourceResourceDirectory, fromFile)
inputStreamFromBundledResource(sourceResourceDirectory, fromFile)

Files.copy(inputStreamForLegacy, fromPath, StandardCopyOption.REPLACE_EXISTING)
// Some directory is missing in the bundles.
if (inputStreamForLegacy != null && fromFileResolved.exists()) {
Files.copy(inputStreamForLegacy, fromFileResolved.toPath(), StandardCopyOption.REPLACE_EXISTING)
}

if (is_19_36_or_greater) {
Files.copy(inputStreamForNew, toPath, StandardCopyOption.REPLACE_EXISTING)
val inputStreamForNew =
inputStreamFromBundledResource(sourceResourceDirectory, fromFile)

// Some directory is missing in the bundles.
if (inputStreamForNew != null && toFileResolved.exists()) {
Files.copy(inputStreamForNew, toFileResolved.toPath(), StandardCopyOption.REPLACE_EXISTING)
}
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions patches/src/main/kotlin/app/revanced/util/ResourceUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -297,17 +297,14 @@ fun Node.insertNode(tagName: String, targetNode: Node, block: Element.() -> Unit
fun ResourcePatchContext.copyResources(
sourceResourceDirectory: String,
vararg resources: ResourceGroup,
createDirectoryIfNotExist: Boolean = false,
) {
val resourceDirectory = get("res")

for (resourceGroup in resources) {
resourceGroup.resources.forEach { resource ->
val resourceDirectoryName = resourceGroup.resourceDirectoryName
if (createDirectoryIfNotExist) {
val targetDirectory = resourceDirectory.resolve(resourceDirectoryName)
if (!targetDirectory.isDirectory) Files.createDirectories(targetDirectory.toPath())
}
val targetDirectory = resourceDirectory.resolve(resourceDirectoryName)
if (!targetDirectory.isDirectory) Files.createDirectories(targetDirectory.toPath())
val resourceFile = "$resourceDirectoryName/$resource"
inputStreamFromBundledResource(
sourceResourceDirectory,
Expand Down Expand Up @@ -427,11 +424,14 @@ fun ResourcePatchContext.copyXmlNode(
resourceDirectory,
targetResource
)?.let { inputStream ->
// Copy nodes from the resources node to the real resource node
elementTag.copyXmlNode(
document(inputStream),
document("res/$targetResource"),
).close()
val outputPath = "res/$targetResource"
if (get(outputPath).exists()) {
// Copy nodes from the resources node to the real resource node
elementTag.copyXmlNode(
document(inputStream),
document(outputPath),
).close()
}
}

/**
Expand Down

0 comments on commit fedf389

Please sign in to comment.