From 1e659807cf0af2e4434c4c4990189af9be5c611b Mon Sep 17 00:00:00 2001 From: inotia00 <108592928+inotia00@users.noreply.github.com> Date: Sat, 22 Jun 2024 20:51:29 +0900 Subject: [PATCH] feat(Reddit): add `Change version code` patch --- .../versioncode/ChangeVersionCodePatch.kt | 57 +++++++++++++++++++ .../kotlin/app/revanced/util/ResourceUtils.kt | 3 + 2 files changed, 60 insertions(+) create mode 100644 src/main/kotlin/app/revanced/patches/reddit/misc/versioncode/ChangeVersionCodePatch.kt diff --git a/src/main/kotlin/app/revanced/patches/reddit/misc/versioncode/ChangeVersionCodePatch.kt b/src/main/kotlin/app/revanced/patches/reddit/misc/versioncode/ChangeVersionCodePatch.kt new file mode 100644 index 000000000..45aa21500 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/reddit/misc/versioncode/ChangeVersionCodePatch.kt @@ -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") + } + } +} diff --git a/src/main/kotlin/app/revanced/util/ResourceUtils.kt b/src/main/kotlin/app/revanced/util/ResourceUtils.kt index 3f3374a8b..fd3d4b43f 100644 --- a/src/main/kotlin/app/revanced/util/ResourceUtils.kt +++ b/src/main/kotlin/app/revanced/util/ResourceUtils.kt @@ -18,6 +18,9 @@ val classLoader: ClassLoader = object {}.javaClass.classLoader fun PatchOption.valueOrThrow() = value ?: throw PatchException("Invalid patch option: $title.") +fun PatchOption.valueOrThrow() = value + ?: throw PatchException("Invalid patch option: $title.") + fun PatchOption.lowerCaseOrThrow() = valueOrThrow() .lowercase()