From 152888535b6fea418b0ee3d2211a14adaa3d0ee0 Mon Sep 17 00:00:00 2001 From: inotia00 Date: Mon, 24 Jul 2023 04:16:36 +0900 Subject: [PATCH] fix(reddit/reddit-settings): now when the patch is applied through the RVX Manager, the settings are added normally --- .../navigation/patch/CreateButtonPatch.kt | 38 ---------- .../patch/DiscoverCommunityButtonPatch.kt | 38 ---------- .../patch/NavigationButtonsPatch.kt | 32 ++++---- .../utils/fix/decoding/patch/DecodingPatch.kt | 31 ++++++++ ...AcknowledgementsLabelBuilderFingerprint.kt | 12 +++ .../bytecode/patch/SettingsBytecodePatch.kt | 49 +++++++++---- .../settings/resource/patch/SettingsPatch.kt | 73 +------------------ .../reddit/settings/host/values/strings.xml | 16 ---- 8 files changed, 96 insertions(+), 193 deletions(-) delete mode 100644 src/main/kotlin/app/revanced/patches/reddit/layout/navigation/patch/CreateButtonPatch.kt delete mode 100644 src/main/kotlin/app/revanced/patches/reddit/layout/navigation/patch/DiscoverCommunityButtonPatch.kt create mode 100644 src/main/kotlin/app/revanced/patches/reddit/utils/fix/decoding/patch/DecodingPatch.kt create mode 100644 src/main/kotlin/app/revanced/patches/reddit/utils/settings/bytecode/fingerprints/AcknowledgementsLabelBuilderFingerprint.kt delete mode 100644 src/main/resources/reddit/settings/host/values/strings.xml diff --git a/src/main/kotlin/app/revanced/patches/reddit/layout/navigation/patch/CreateButtonPatch.kt b/src/main/kotlin/app/revanced/patches/reddit/layout/navigation/patch/CreateButtonPatch.kt deleted file mode 100644 index caf9a5082..000000000 --- a/src/main/kotlin/app/revanced/patches/reddit/layout/navigation/patch/CreateButtonPatch.kt +++ /dev/null @@ -1,38 +0,0 @@ -package app.revanced.patches.reddit.layout.navigation.patch - -import app.revanced.patcher.annotation.Description -import app.revanced.patcher.annotation.Name -import app.revanced.patcher.annotation.Version -import app.revanced.patcher.data.BytecodeContext -import app.revanced.patcher.patch.BytecodePatch -import app.revanced.patcher.patch.PatchResult -import app.revanced.patcher.patch.PatchResultSuccess -import app.revanced.patcher.patch.annotations.DependsOn -import app.revanced.patcher.patch.annotations.Patch -import app.revanced.patches.reddit.layout.navigation.patch.NavigationButtonsPatch.Companion.setValue -import app.revanced.patches.reddit.utils.annotations.RedditCompatibility -import app.revanced.patches.reddit.utils.settings.bytecode.patch.SettingsBytecodePatch.Companion.updateSettingsStatus -import app.revanced.patches.reddit.utils.settings.resource.patch.SettingsPatch - -@Patch -@Name("Hide create button") -@Description("Hide create button at navigation bar.") -@DependsOn( - [ - NavigationButtonsPatch::class, - SettingsPatch::class - ] -) -@RedditCompatibility -@Version("0.0.1") -class CreateButtonPatch : BytecodePatch() { - override fun execute(context: BytecodeContext): PatchResult { - - updateSettingsStatus("CreateButtons") - - if (SettingsPatch.RedditSettings == true) - context.setValue("CreateButtons") - - return PatchResultSuccess() - } -} diff --git a/src/main/kotlin/app/revanced/patches/reddit/layout/navigation/patch/DiscoverCommunityButtonPatch.kt b/src/main/kotlin/app/revanced/patches/reddit/layout/navigation/patch/DiscoverCommunityButtonPatch.kt deleted file mode 100644 index 18b494cee..000000000 --- a/src/main/kotlin/app/revanced/patches/reddit/layout/navigation/patch/DiscoverCommunityButtonPatch.kt +++ /dev/null @@ -1,38 +0,0 @@ -package app.revanced.patches.reddit.layout.navigation.patch - -import app.revanced.patcher.annotation.Description -import app.revanced.patcher.annotation.Name -import app.revanced.patcher.annotation.Version -import app.revanced.patcher.data.BytecodeContext -import app.revanced.patcher.patch.BytecodePatch -import app.revanced.patcher.patch.PatchResult -import app.revanced.patcher.patch.PatchResultSuccess -import app.revanced.patcher.patch.annotations.DependsOn -import app.revanced.patcher.patch.annotations.Patch -import app.revanced.patches.reddit.layout.navigation.patch.NavigationButtonsPatch.Companion.setValue -import app.revanced.patches.reddit.utils.annotations.RedditCompatibility -import app.revanced.patches.reddit.utils.settings.bytecode.patch.SettingsBytecodePatch.Companion.updateSettingsStatus -import app.revanced.patches.reddit.utils.settings.resource.patch.SettingsPatch - -@Patch -@Name("Hide discover community button") -@Description("Hide discover button or communities button at navigation bar.") -@DependsOn( - [ - NavigationButtonsPatch::class, - SettingsPatch::class - ] -) -@RedditCompatibility -@Version("0.0.1") -class DiscoverCommunityButtonPatch : BytecodePatch() { - override fun execute(context: BytecodeContext): PatchResult { - - updateSettingsStatus("DiscoverButtons") - - if (SettingsPatch.RedditSettings == true) - context.setValue("DiscoverButtons") - - return PatchResultSuccess() - } -} diff --git a/src/main/kotlin/app/revanced/patches/reddit/layout/navigation/patch/NavigationButtonsPatch.kt b/src/main/kotlin/app/revanced/patches/reddit/layout/navigation/patch/NavigationButtonsPatch.kt index 7f343f4e4..4ebdd896c 100644 --- a/src/main/kotlin/app/revanced/patches/reddit/layout/navigation/patch/NavigationButtonsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/reddit/layout/navigation/patch/NavigationButtonsPatch.kt @@ -1,15 +1,29 @@ package app.revanced.patches.reddit.layout.navigation.patch import app.revanced.extensions.toErrorResult +import app.revanced.patcher.annotation.Description +import app.revanced.patcher.annotation.Name +import app.revanced.patcher.annotation.Version import app.revanced.patcher.data.BytecodeContext import app.revanced.patcher.extensions.InstructionExtensions.addInstruction import app.revanced.patcher.extensions.InstructionExtensions.getInstruction import app.revanced.patcher.patch.BytecodePatch import app.revanced.patcher.patch.PatchResult import app.revanced.patcher.patch.PatchResultSuccess +import app.revanced.patcher.patch.annotations.DependsOn +import app.revanced.patcher.patch.annotations.Patch import app.revanced.patches.reddit.layout.navigation.fingerprints.BottomNavScreenFingerprint +import app.revanced.patches.reddit.utils.annotations.RedditCompatibility +import app.revanced.patches.reddit.utils.settings.bytecode.patch.SettingsBytecodePatch.Companion.updateSettingsStatus +import app.revanced.patches.reddit.utils.settings.resource.patch.SettingsPatch import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction +@Patch +@Name("Hide navigation buttons") +@Description("Hide buttons at navigation bar.") +@DependsOn([SettingsPatch::class]) +@RedditCompatibility +@Version("0.0.1") class NavigationButtonsPatch : BytecodePatch( listOf(BottomNavScreenFingerprint) ) { @@ -28,6 +42,8 @@ class NavigationButtonsPatch : BytecodePatch( } } ?: return BottomNavScreenFingerprint.toErrorResult() + updateSettingsStatus("NavigationButtons") + return PatchResultSuccess() } @@ -35,21 +51,5 @@ class NavigationButtonsPatch : BytecodePatch( const val INTEGRATIONS_METHOD_DESCRIPTOR = "Lapp/revanced/reddit/patches/NavigationButtonsPatch;" + "->hideNavigationButtons(Landroid/view/ViewGroup;)V" - - internal fun BytecodeContext.setValue(patch: String) { - this.classes.forEach { classDef -> - classDef.methods.forEach { method -> - if (classDef.type == "Lapp/revanced/reddit/settingsmenu/SettingsStatus;" && method.name == patch) { - val patchStatusMethod = - this.proxy(classDef).mutableClass.methods.first { it.name == patch } - - patchStatusMethod.addInstruction( - 2, - "return-void" - ) - } - } - } - } } } diff --git a/src/main/kotlin/app/revanced/patches/reddit/utils/fix/decoding/patch/DecodingPatch.kt b/src/main/kotlin/app/revanced/patches/reddit/utils/fix/decoding/patch/DecodingPatch.kt new file mode 100644 index 000000000..ef8b87e61 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/reddit/utils/fix/decoding/patch/DecodingPatch.kt @@ -0,0 +1,31 @@ +package app.revanced.patches.reddit.utils.fix.decoding.patch + +import app.revanced.patcher.data.ResourceContext +import app.revanced.patcher.patch.PatchResult +import app.revanced.patcher.patch.PatchResultSuccess +import app.revanced.patcher.patch.ResourcePatch + +class DecodingPatch : ResourcePatch { + override fun execute(context: ResourceContext): PatchResult { + + arrayOf( + "res/layout/notification_media_cancel_action.xml", + "res/values/styles.xml" + ).forEach { xmlPath -> + context[xmlPath].apply { + writeText( + readText() + .replace( + "@android:drawable/cling_button", + "@android:drawable/sym_def_app_icon" + ).replace( + "@android:drawable/ab_share_pack_material", + "@android:drawable/ic_menu_close_clear_cancel" + ) + ) + } + } + + return PatchResultSuccess() + } +} \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/reddit/utils/settings/bytecode/fingerprints/AcknowledgementsLabelBuilderFingerprint.kt b/src/main/kotlin/app/revanced/patches/reddit/utils/settings/bytecode/fingerprints/AcknowledgementsLabelBuilderFingerprint.kt new file mode 100644 index 000000000..b18e67c95 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/reddit/utils/settings/bytecode/fingerprints/AcknowledgementsLabelBuilderFingerprint.kt @@ -0,0 +1,12 @@ +package app.revanced.patches.reddit.utils.settings.bytecode.fingerprints + +import app.revanced.patcher.extensions.or +import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint +import com.android.tools.smali.dexlib2.AccessFlags + +object AcknowledgementsLabelBuilderFingerprint : MethodFingerprint( + returnType = "Z", + accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL, + parameters = listOf("Landroidx/preference/Preference;"), + strings = listOf("resources.getString(R.st\u2026g.label_acknowledgements)") +) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/reddit/utils/settings/bytecode/patch/SettingsBytecodePatch.kt b/src/main/kotlin/app/revanced/patches/reddit/utils/settings/bytecode/patch/SettingsBytecodePatch.kt index cb37ab173..27e685f29 100644 --- a/src/main/kotlin/app/revanced/patches/reddit/utils/settings/bytecode/patch/SettingsBytecodePatch.kt +++ b/src/main/kotlin/app/revanced/patches/reddit/utils/settings/bytecode/patch/SettingsBytecodePatch.kt @@ -4,25 +4,55 @@ import app.revanced.extensions.toErrorResult import app.revanced.patcher.data.BytecodeContext import app.revanced.patcher.extensions.InstructionExtensions.addInstruction import app.revanced.patcher.extensions.InstructionExtensions.addInstructions +import app.revanced.patcher.extensions.InstructionExtensions.getInstruction import app.revanced.patcher.patch.BytecodePatch import app.revanced.patcher.patch.PatchResult import app.revanced.patcher.patch.PatchResultSuccess import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod +import app.revanced.patches.reddit.utils.settings.bytecode.fingerprints.AcknowledgementsLabelBuilderFingerprint import app.revanced.patches.reddit.utils.settings.bytecode.fingerprints.OssLicensesMenuActivityOnCreateFingerprint import app.revanced.patches.reddit.utils.settings.bytecode.fingerprints.SettingsStatusLoadFingerprint -import kotlin.properties.Delegates +import app.revanced.util.bytecode.getStringIndex +import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction class SettingsBytecodePatch : BytecodePatch( listOf( + AcknowledgementsLabelBuilderFingerprint, OssLicensesMenuActivityOnCreateFingerprint, SettingsStatusLoadFingerprint ) ) { override fun execute(context: BytecodeContext): PatchResult { + /** + * Replace settings label + */ + AcknowledgementsLabelBuilderFingerprint.result?.let { + it.mutableMethod.apply { + val insertIndex = getStringIndex("resources.getString(R.st\u2026g.label_acknowledgements)") + val insertRegister = getInstruction(insertIndex - 1).registerA + + addInstruction( + insertIndex, + "const-string v$insertRegister, \"ReVanced Extended\"" + ) + } + } ?: return AcknowledgementsLabelBuilderFingerprint.toErrorResult() + + /** + * Initialize settings activity + */ OssLicensesMenuActivityOnCreateFingerprint.result?.let { - activityMethod = it.mutableMethod - activityIndex = it.scanResult.patternScanResult!!.startIndex + 1 + it.mutableMethod.apply { + val insertIndex = it.scanResult.patternScanResult!!.startIndex + 1 + + addInstructions( + insertIndex, """ + invoke-static {p0}, $INTEGRATIONS_METHOD_DESCRIPTOR + return-void + """ + ) + } } ?: return OssLicensesMenuActivityOnCreateFingerprint.toErrorResult() settingsMethod = SettingsStatusLoadFingerprint.result?.mutableMethod @@ -35,21 +65,8 @@ class SettingsBytecodePatch : BytecodePatch( private const val INTEGRATIONS_METHOD_DESCRIPTOR = "Lapp/revanced/reddit/settingsmenu/ReVancedSettingActivity;->initializeSettings(Landroid/app/Activity;)V" - private lateinit var activityMethod: MutableMethod - private var activityIndex by Delegates.notNull() private lateinit var settingsMethod: MutableMethod - fun injectActivity() { - activityMethod.apply { - addInstructions( - activityIndex, """ - invoke-static {p0}, $INTEGRATIONS_METHOD_DESCRIPTOR - return-void - """ - ) - } - } - fun updateSettingsStatus(description: String) { settingsMethod.apply { addInstruction( diff --git a/src/main/kotlin/app/revanced/patches/reddit/utils/settings/resource/patch/SettingsPatch.kt b/src/main/kotlin/app/revanced/patches/reddit/utils/settings/resource/patch/SettingsPatch.kt index 6cc431691..663f4d7d3 100644 --- a/src/main/kotlin/app/revanced/patches/reddit/utils/settings/resource/patch/SettingsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/reddit/utils/settings/resource/patch/SettingsPatch.kt @@ -4,8 +4,6 @@ import app.revanced.patcher.annotation.Description import app.revanced.patcher.annotation.Name import app.revanced.patcher.annotation.Version import app.revanced.patcher.data.ResourceContext -import app.revanced.patcher.patch.OptionsContainer -import app.revanced.patcher.patch.PatchOption import app.revanced.patcher.patch.PatchResult import app.revanced.patcher.patch.PatchResultError import app.revanced.patcher.patch.PatchResultSuccess @@ -13,11 +11,9 @@ import app.revanced.patcher.patch.ResourcePatch import app.revanced.patcher.patch.annotations.DependsOn import app.revanced.patcher.patch.annotations.Patch import app.revanced.patches.reddit.utils.annotations.RedditCompatibility +import app.revanced.patches.reddit.utils.fix.decoding.patch.DecodingPatch import app.revanced.patches.reddit.utils.integrations.patch.IntegrationsPatch import app.revanced.patches.reddit.utils.settings.bytecode.patch.SettingsBytecodePatch -import app.revanced.util.resources.ResourceUtils.copyXmlNode -import java.io.FileWriter -import java.nio.file.Files import kotlin.io.path.exists @Patch @@ -25,6 +21,7 @@ import kotlin.io.path.exists @Description("Adds ReVanced settings to Reddit.") @DependsOn( [ + DecodingPatch::class, IntegrationsPatch::class, SettingsBytecodePatch::class ] @@ -35,28 +32,7 @@ class SettingsPatch : ResourcePatch { override fun execute(context: ResourceContext): PatchResult { /** - * For some reason, when you try to compile in ReVanced Manager you'll get an error - * It is presumed to be an APKTOOL issue, but it cannot be fixed right now - * So it has been changed so that settings can be added only through options.json - */ - if (RedditSettings != true) - return PatchResultSuccess() - - - /** - * Copy strings.xml - */ - context.copyXmlNode("reddit/settings/host", "values/strings.xml", "resources") - - - /** - * Initialize settings activity - */ - SettingsBytecodePatch.injectActivity() - - - /** - * Replace settings icon + * Replace settings icon and label */ arrayOf("preferences", "preferences_logged_in").forEach { targetXML -> val resDirectory = context["res"] @@ -71,52 +47,11 @@ class SettingsPatch : ResourcePatch { preference.readText() .replace( "\"@drawable/icon_text_post\" android:title=\"@string/label_acknowledgements\"", - "\"@drawable/icon_beta_planet\" android:title=\"@string/label_acknowledgements\"" + "\"@drawable/icon_beta_planet\" android:title=\"ReVanced Extended\"" ) ) } - - /** - * Replace settings label - */ - val resDirectory = context["res"] - if (!resDirectory.isDirectory) - return PatchResultError("The res folder can not be found.") - - arrayOf( - "values", - "values-de-rDE", - "values-en-rXA", - "values-es-rES", - "values-es-rMX", - "values-fr-rCA", - "values-fr-rFR", - "values-it-rIT", - "values-nl-rNL", - "values-pt-rBR", - "values-pt-rPT", - "values-sv-rSE" - ).forEach { path -> - val directory = resDirectory.resolve("$path-v21") - Files.createDirectories(directory.toPath()) - - FileWriter(directory.resolve("strings.xml")).use { - it.write("ReVanced Extended") - } - } - return PatchResultSuccess() } - - companion object : OptionsContainer() { - internal var RedditSettings: Boolean? by option( - PatchOption.BooleanOption( - key = "RedditSettings", - default = false, - title = "Add settings to Reddit", - description = "Defaults to false to avoid exceptions in ReVanced Manager" - ) - ) - } } \ No newline at end of file diff --git a/src/main/resources/reddit/settings/host/values/strings.xml b/src/main/resources/reddit/settings/host/values/strings.xml deleted file mode 100644 index 48e228a88..000000000 --- a/src/main/resources/reddit/settings/host/values/strings.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - Disables the popup that shows up when taking a screenshot - Disable screenshot popup - ReVanced Extended - Hide create button in navigation - Hide create button - Hide discover button or communities button in navigation - Hide discover / community button - Skips over redirection URLs to external links - Open links directly - Open links outside of the app directly in your browser - Open links externally - Removes (tracking) query parameters from the URLs when sharing links - Sanitize sharing links -