diff --git a/src/main/kotlin/app/revanced/patches/all/misc/packagename/ChangePackageNamePatch.kt b/src/main/kotlin/app/revanced/patches/all/misc/packagename/ChangePackageNamePatch.kt index f397d318..40978597 100644 --- a/src/main/kotlin/app/revanced/patches/all/misc/packagename/ChangePackageNamePatch.kt +++ b/src/main/kotlin/app/revanced/patches/all/misc/packagename/ChangePackageNamePatch.kt @@ -4,7 +4,9 @@ import app.revanced.patcher.data.ResourceContext import app.revanced.patcher.patch.ResourcePatch import app.revanced.patcher.patch.annotation.Patch import app.revanced.patcher.patch.options.PatchOption.PatchExtensions.stringPatchOption +import app.revanced.patcher.patch.options.PatchOptionException import org.w3c.dom.Element +import java.io.Closeable @Patch( name = "Change package name", @@ -12,13 +14,11 @@ import org.w3c.dom.Element use = false ) @Suppress("unused") -object ChangePackageNamePatch : ResourcePatch() { - private const val DEFAULT_PACKAGE_NAME_OPTION = "Default" - - private var packageName by stringPatchOption( +object ChangePackageNamePatch : ResourcePatch(), Closeable { + private val packageNameOption = stringPatchOption( key = "packageName", - default = DEFAULT_PACKAGE_NAME_OPTION, - values = mapOf("Default" to DEFAULT_PACKAGE_NAME_OPTION), + default = "Default", + values = mapOf("Default" to "Default"), title = "Package name", description = "The name of the package to rename the app to.", required = true @@ -26,19 +26,37 @@ object ChangePackageNamePatch : ResourcePatch() { it == "Default" || it!!.matches(Regex("^[a-z]\\w*(\\.[a-z]\\w*)+\$")) } + private lateinit var context: ResourceContext + override fun execute(context: ResourceContext) { - fun getOriginalPackageName(context: ResourceContext): String { - context.xmlEditor["AndroidManifest.xml"].use { editor -> - val manifest = editor.file.getElementsByTagName("manifest").item(0) as Element - return manifest.getAttribute("package") - } - } + this.context = context + } - val originalPackageName = getOriginalPackageName(context) - if (packageName == DEFAULT_PACKAGE_NAME_OPTION) packageName = "$originalPackageName.revanced" + /** + * Set the package name to use. + * If this is called multiple times, the first call will set the package name. + * + * @param fallbackPackageName The package name to use if the user has not already specified a package name. + * @return The package name that was set. + * @throws PatchOptionException.ValueValidationException If the package name is invalid. + */ + fun setOrGetFallbackPackageName(fallbackPackageName: String): String { + val packageName = this.packageNameOption.value!! - context["AndroidManifest.xml"].apply { - readText().replace(originalPackageName, packageName!!).let(::writeText) - } + return if (packageName == this.packageNameOption.default) + fallbackPackageName.also { this.packageNameOption.value = it } + else + packageName + } + + override fun close() = context.xmlEditor["AndroidManifest.xml"].use { editor -> + val manifest = editor.file.getElementsByTagName("manifest").item(0) as Element + val originalPackageName = manifest.getAttribute("package") + + var replacementPackageName = this.packageNameOption.value + if (replacementPackageName == this.packageNameOption.default) + replacementPackageName = "$originalPackageName.revanced" + + manifest.setAttribute("package", replacementPackageName) } } \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/music/misc/microg/MicroGBytecodePatch.kt b/src/main/kotlin/app/revanced/patches/music/misc/microg/MicroGBytecodePatch.kt index c1adb1e1..192711a1 100644 --- a/src/main/kotlin/app/revanced/patches/music/misc/microg/MicroGBytecodePatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/misc/microg/MicroGBytecodePatch.kt @@ -4,18 +4,25 @@ import app.revanced.patcher.data.BytecodeContext import app.revanced.patcher.patch.BytecodePatch import app.revanced.patcher.patch.annotation.CompatiblePackage import app.revanced.patcher.patch.annotation.Patch +import app.revanced.patches.all.misc.packagename.ChangePackageNamePatch import app.revanced.patches.music.misc.microg.fingerprints.* import app.revanced.patches.music.misc.microg.shared.Constants.MUSIC_PACKAGE_NAME import app.revanced.patches.music.misc.microg.shared.Constants.REVANCED_MUSIC_PACKAGE_NAME -import app.revanced.patches.youtube.misc.microg.shared.Constants import app.revanced.util.microg.MicroGBytecodeHelper @Patch( name = "Vanced MicroG support", description = "Allows YouTube Music to run without root and under a different package name.", - dependencies = [MicroGResourcePatch::class], - compatiblePackages = [CompatiblePackage("com.google.android.apps.youtube.music")] + dependencies = [ + ChangePackageNamePatch::class, + MicroGResourcePatch::class, + ], + compatiblePackages = [ + CompatiblePackage( + "com.google.android.apps.youtube.music" + ) + ] ) @Suppress("unused") object MicroGBytecodePatch : BytecodePatch( @@ -35,25 +42,29 @@ object MicroGBytecodePatch : BytecodePatch( // - "com.google.android.gms.phenotype.PACKAGE_NAME", // - "com.google.android.gms.phenotype.UPDATE", // - "com.google.android.gms.phenotype", - override fun execute(context: BytecodeContext) = MicroGBytecodeHelper.patchBytecode( - context, - arrayOf( - MicroGBytecodeHelper.packageNameTransform( - Constants.PACKAGE_NAME, - Constants.REVANCED_PACKAGE_NAME + override fun execute(context: BytecodeContext) { + val packageName = ChangePackageNamePatch.setOrGetFallbackPackageName(REVANCED_MUSIC_PACKAGE_NAME) + + MicroGBytecodeHelper.patchBytecode( + context, + arrayOf( + MicroGBytecodeHelper.packageNameTransform( + MUSIC_PACKAGE_NAME, + packageName + ) + ), + MicroGBytecodeHelper.PrimeMethodTransformationData( + PrimeFingerprint, + MUSIC_PACKAGE_NAME, + packageName + ), + listOf( + ServiceCheckFingerprint, + GooglePlayUtilityFingerprint, + CastDynamiteModuleFingerprint, + CastDynamiteModuleV2Fingerprint, + CastContextFetchFingerprint ) - ), - MicroGBytecodeHelper.PrimeMethodTransformationData( - PrimeFingerprint, - MUSIC_PACKAGE_NAME, - REVANCED_MUSIC_PACKAGE_NAME - ), - listOf( - ServiceCheckFingerprint, - GooglePlayUtilityFingerprint, - CastDynamiteModuleFingerprint, - CastDynamiteModuleV2Fingerprint, - CastContextFetchFingerprint ) - ) + } } diff --git a/src/main/kotlin/app/revanced/patches/music/misc/microg/MicroGResourcePatch.kt b/src/main/kotlin/app/revanced/patches/music/misc/microg/MicroGResourcePatch.kt index eb8daf5a..950fba8d 100644 --- a/src/main/kotlin/app/revanced/patches/music/misc/microg/MicroGResourcePatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/misc/microg/MicroGResourcePatch.kt @@ -3,6 +3,7 @@ package app.revanced.patches.music.misc.microg import app.revanced.patcher.data.ResourceContext import app.revanced.patcher.patch.ResourcePatch import app.revanced.patcher.patch.annotation.Patch +import app.revanced.patches.all.misc.packagename.ChangePackageNamePatch import app.revanced.patches.music.misc.microg.shared.Constants.MUSIC_PACKAGE_NAME import app.revanced.patches.music.misc.microg.shared.Constants.REVANCED_MUSIC_APP_NAME import app.revanced.patches.music.misc.microg.shared.Constants.REVANCED_MUSIC_PACKAGE_NAME @@ -14,15 +15,18 @@ import app.revanced.util.microg.MicroGResourceHelper @Patch( description = "Resource patch to allow YouTube Music ReVanced to run without root " + - "and under a different package name." + "and under a different package name.", + dependencies = [ChangePackageNamePatch::class] ) object MicroGResourcePatch : ResourcePatch() { override fun execute(context: ResourceContext) { + val packageName = ChangePackageNamePatch.setOrGetFallbackPackageName(REVANCED_MUSIC_PACKAGE_NAME) + // update manifest MicroGResourceHelper.patchManifest( context, MUSIC_PACKAGE_NAME, - REVANCED_MUSIC_PACKAGE_NAME, + packageName, REVANCED_MUSIC_APP_NAME )