From 7bc60943cb2350e89dac091ec9c98c5effd0b8a9 Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Tue, 24 May 2022 02:02:39 +0200 Subject: [PATCH] fix: write while reading resources and remove checking for "." in resource extensions Signed-off-by: oSumAtrIX --- .../patch/bytecode/MicroGBytecodePatch.kt | 47 +++++++++---------- .../patch/resource/MicroGResourcePatch.kt | 12 +++-- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/microg/patch/bytecode/MicroGBytecodePatch.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/microg/patch/bytecode/MicroGBytecodePatch.kt index 6918b6f37..ea309336e 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/misc/microg/patch/bytecode/MicroGBytecodePatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/microg/patch/bytecode/MicroGBytecodePatch.kt @@ -59,30 +59,29 @@ class MicroGBytecodePatch : BytecodePatch( if (instruction.opcode == Opcode.CONST_STRING) { val stringValue = ((instruction as Instruction21c).reference as StringReference).string - val replaceMode = - if (stringValue.startsWith("com.google.android.gms.chimera.container")) // https://github.com/TeamVanced/VancedMicroG/pull/139/file - StringReplaceMode.DO_NOT_REPLACE - else if (stringValue == "com.google" || stringValue.startsWithAny( - "com.google.android.gms.auth.accounts", - "com.google.android.gms.chimera", - "com.google.android.c2dm", - "com.google.android.c2dm", - "com.google.android.gsf", - "com.google.android.c2dm", - "com.google.iid", - "content://com.google.settings" - ) - ) { - StringReplaceMode.REPLACE_WITH_MICROG - } else if (stringValue.startsWithAny( - "com.google.android.youtube.SuggestionsProvider", - "com.google.android.youtube.fileprovider" - ) - ) { - StringReplaceMode.REPLACE_WITH_REVANCED - } else { - StringReplaceMode.DO_NOT_REPLACE - } + val replaceMode = if (stringValue == "com.google" || stringValue.startsWithAny( + "com.google.android.gms.auth.accounts", + "com.google.android.gms.chimera", + "com.google.android.c2dm", + "com.google.android.c2dm", + "com.google.android.gsf", + "com.google.android.c2dm", + "com.google.iid", + "content://com.google.settings" + ) + ) { + StringReplaceMode.REPLACE_WITH_MICROG + } else if (stringValue.startsWith("com.google.android.gms.chimera.container")) // https://github.com/TeamVanced/VancedMicroG/pull/139/file + StringReplaceMode.DO_NOT_REPLACE + else if (stringValue.startsWithAny( + "com.google.android.youtube.SuggestionsProvider", + "com.google.android.youtube.fileprovider" + ) + ) { + StringReplaceMode.REPLACE_WITH_REVANCED + } else { + StringReplaceMode.DO_NOT_REPLACE + } if (replaceMode != StringReplaceMode.DO_NOT_REPLACE) { diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/microg/patch/resource/MicroGResourcePatch.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/microg/patch/resource/MicroGResourcePatch.kt index 627608cd4..6998e0068 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/misc/microg/patch/resource/MicroGResourcePatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/microg/patch/resource/MicroGResourcePatch.kt @@ -19,8 +19,10 @@ import app.revanced.patches.youtube.misc.microg.shared.Constants.REVANCED_PACKAG @Version("0.0.1") class MicroGResourcePatch : ResourcePatch() { override fun execute(data: ResourceData): PatchResult { - data.writer("AndroidManifest.xml").write( - data.reader("AndroidManifest.xml").readText().replace( + val manifest = data.get("AndroidManifest.xml").readText() + + data.get("AndroidManifest.xml").writeText( + manifest.replace( "package=\"com.google.android.youtube\"", "package=\"$REVANCED_PACKAGE_NAME\"" ).replace( " android:label=\"@string/application_name\" ", " android:label=\"{APP_NAME}\" " @@ -47,15 +49,15 @@ class MicroGResourcePatch : ResourcePatch() { val replacement = arrayOf( Pair( "com.google.android.youtube.SuggestionProvider", "$REVANCED_PACKAGE_NAME.SuggestionProvider" - ), - Pair( + ), Pair( "com.google.android.youtube.fileprovider", "$REVANCED_PACKAGE_NAME.fileprovider" ) ) data.forEach { - if (it.extension != ".xml") return@forEach + if (it.extension != "xml") return@forEach + // TODO: use a reader and only replace strings where needed instead of reading & writing the entire file var content = it.readText() replacement.filter { translation -> content.contains(translation.first) }.forEach { translation -> content = content.replace(translation.first, translation.second)