feat(Reddit): add Change version code patch

This commit is contained in:
inotia00 2024-06-22 20:51:29 +09:00
parent dcd658fc2b
commit 1e659807cf
2 changed files with 60 additions and 0 deletions

View File

@ -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")
}
}
}

View File

@ -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()