Skip to content

Commit

Permalink
feat(Reddit): Add Change version code patch
Browse files Browse the repository at this point in the history
  • Loading branch information
anddea committed Jun 23, 2024
1 parent b7c330a commit ec7da6d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package app.revanced.patches.reddit.misc.versioncode

import app.revanced.patcher.data.ResourceContext
import app.revanced.patcher.patch.PatchException
import app.revanced.patcher.patch.options.PatchOption.PatchExtensions.booleanPatchOption
import app.revanced.patcher.patch.options.PatchOption.PatchExtensions.intPatchOption
import app.revanced.patches.reddit.utils.compatibility.Constants.COMPATIBLE_PACKAGE
import app.revanced.util.patch.BaseResourcePatch
import app.revanced.util.valueOrThrow
import org.w3c.dom.Element

@Suppress("unused")
object ChangeVersionCodePatch : BaseResourcePatch(
name = "Change version code",
description = "Changes the version code of the app. By default the highest version code is set. " +
"This allows older versions of an app to be installed " +
"if their version code is set to the same or a higher value and can stop app stores to update the app.",
compatiblePackages = COMPATIBLE_PACKAGE,
use = false
) {
private val ChangeVersionCode by booleanPatchOption(
key = "ChangeVersionCode",
default = false,
title = "Change version code",
description = "Changes the version code of the app.",
required = true
)

private val VersionCode = intPatchOption(
key = "VersionCode",
default = Int.MAX_VALUE,
title = "Version code",
description = "The version code to use.",
required = true
)

override fun execute(context: ResourceContext) {
if (ChangeVersionCode == false) {
println("INFO: Version code will remain unchanged as 'ChangeVersionCode' is false.")
return
}

val versionCode = VersionCode.valueOrThrow()

if (versionCode < 1) {
throw PatchException(
"Invalid versionCode: $versionCode, " +
"Version code should be larger than 1 and smaller than ${Int.MAX_VALUE}."
)
}

context.document["AndroidManifest.xml"].use { document ->
val manifestElement = document.getElementsByTagName("manifest").item(0) as Element
manifestElement.setAttribute("android:versionCode", "$versionCode")
}
}
}
3 changes: 3 additions & 0 deletions src/main/kotlin/app/revanced/util/ResourceUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ val classLoader: ClassLoader = object {}.javaClass.classLoader
fun PatchOption<String>.valueOrThrow() = value
?: throw PatchException("Invalid patch option: $title.")

fun PatchOption<Int?>.valueOrThrow() = value
?: throw PatchException("Invalid patch option: $title.")

fun PatchOption<String>.lowerCaseOrThrow() = valueOrThrow()
.lowercase()

Expand Down

0 comments on commit ec7da6d

Please sign in to comment.