11package app.revanced.patches.shared.layout.branding
22
3+ import app.revanced.patcher.patch.PatchException
34import app.revanced.patcher.patch.ResourcePatch
45import app.revanced.patcher.patch.ResourcePatchBuilder
56import app.revanced.patcher.patch.ResourcePatchContext
@@ -14,7 +15,7 @@ import java.util.logging.Logger
1415
1516private const val REVANCED_ICON = " ReVanced*Logo" // Can never be a valid path.
1617
17- private val mipmapDirectories = arrayOf(
18+ internal val mipmapDirectories = arrayOf(
1819 // Target app does not have ldpi icons.
1920 " mdpi" ,
2021 " hdpi" ,
@@ -83,16 +84,13 @@ internal fun baseCustomBrandingPatch(
8384 description = """
8485 The icon to apply to the app.
8586
86- If a path to a folder is provided, the folder must contain the following folders:
87-
87+ If a path to a folder is provided, the folder must contain one or more of the following folders:
8888 ${formatResourceFileList(mipmapDirectories)}
8989
9090 Each of these folders must contain the following files:
91-
9291 ${formatResourceFileList((iconResourceFileNamesPng + legacyIconResourceFileNamesPng))}
9392
9493 Optionally, a 'drawable' folder with the monochrome icon files:
95-
9694 ${formatResourceFileList(monochromeIconFileNames)}
9795 """ .trimIndentMultiline(),
9896 )
@@ -127,40 +125,48 @@ internal fun baseCustomBrandingPatch(
127125 ResourceGroup (" mipmap-anydpi" , * adaptiveIconFileNames)
128126 )
129127 } else {
130- val mipmapIconResourceGroups = mipmapDirectories.map { directory ->
131- ResourceGroup (
132- directory,
133- * iconResourceFileNamesPng,
134- )
135- }
136-
137128 val filePath = File (iconPathTrimmed)
138129 val resourceDirectory = get(" res" )
130+ var replacedResources = false
139131
140132 // Replace mipmap icons.
141- mipmapIconResourceGroups.forEach { groupResources ->
133+ mipmapDirectories.map { directory ->
134+ ResourceGroup (
135+ directory,
136+ * iconResourceFileNamesPng,
137+ )
138+ }.forEach { groupResources ->
142139 val groupResourceDirectoryName = groupResources.resourceDirectoryName
143140 val fromDirectory = filePath.resolve(groupResourceDirectoryName)
144141 val toDirectory = resourceDirectory.resolve(groupResourceDirectoryName)
145142
146143 groupResources.resources.forEach { iconFileName ->
147- Files .write(
148- toDirectory.resolve(iconFileName).toPath(),
149- fromDirectory.resolve(iconFileName).readBytes(),
150- )
144+ val replacement = fromDirectory.resolve(iconFileName)
145+ if (replacement.exists()) {
146+ Files .write(
147+ toDirectory.resolve(iconFileName).toPath(),
148+ replacement.readBytes(),
149+ )
150+ replacedResources = true
151+ }
151152 }
152153 }
153154
154155 // Replace monochrome icons if provided.
155- monochromeIconFileNames.forEach { fileName ->
156- val replacementMonochrome = filePath.resolve(" drawable" ).resolve(fileName )
157- if (replacementMonochrome .exists()) {
156+ monochromeIconFileNames.forEach { iconFileName ->
157+ val replacement = filePath.resolve(" drawable" ).resolve(iconFileName )
158+ if (replacement .exists()) {
158159 Files .write(
159- resourceDirectory.resolve(" drawable" ).resolve(fileName ).toPath(),
160- replacementMonochrome .readBytes(),
160+ resourceDirectory.resolve(" drawable" ).resolve(iconFileName ).toPath(),
161+ replacement .readBytes(),
161162 )
163+ replacedResources = true
162164 }
163165 }
166+
167+ if (! replacedResources) {
168+ throw PatchException (" Could not find any replacement images in patch option path: $iconPathTrimmed " )
169+ }
164170 }
165171
166172 // Change the app name.
0 commit comments