From 498e5d2001ac0ed4dc462d2395377ecbbea46e49 Mon Sep 17 00:00:00 2001 From: inotia00 Date: Thu, 6 Jul 2023 11:47:59 +0900 Subject: [PATCH] feat(reddit/reddit-settings): preferences are added to settings only when `options.json` values are specified --- .../patch/ScreenshotPopupPatch.kt | 4 +- .../openlink/patch/OpenLinksDirectlyPatch.kt | 4 +- .../patch/OpenLinksExternallyPatch.kt | 4 +- .../url/patch/SanitizeUrlQueryPatch.kt | 4 +- ...tingsPatch.kt => SettingsBytecodePatch.kt} | 49 +++---- .../settings/resource/patch/SettingsPatch.kt | 122 ++++++++++++++++++ .../resource/patch/SettingsResourcePatch.kt | 72 ----------- 7 files changed, 151 insertions(+), 108 deletions(-) rename src/main/kotlin/app/revanced/patches/reddit/utils/settings/bytecode/patch/{SettingsPatch.kt => SettingsBytecodePatch.kt} (70%) create mode 100644 src/main/kotlin/app/revanced/patches/reddit/utils/settings/resource/patch/SettingsPatch.kt delete mode 100644 src/main/kotlin/app/revanced/patches/reddit/utils/settings/resource/patch/SettingsResourcePatch.kt diff --git a/src/main/kotlin/app/revanced/patches/reddit/layout/screenshotpopup/patch/ScreenshotPopupPatch.kt b/src/main/kotlin/app/revanced/patches/reddit/layout/screenshotpopup/patch/ScreenshotPopupPatch.kt index 6905428d7..7f4a588dc 100644 --- a/src/main/kotlin/app/revanced/patches/reddit/layout/screenshotpopup/patch/ScreenshotPopupPatch.kt +++ b/src/main/kotlin/app/revanced/patches/reddit/layout/screenshotpopup/patch/ScreenshotPopupPatch.kt @@ -16,8 +16,8 @@ import app.revanced.patcher.util.smali.ExternalLabel import app.revanced.patches.reddit.layout.screenshotpopup.fingerprints.ScreenshotTakenBannerFingerprint import app.revanced.patches.reddit.utils.annotations.RedditCompatibility import app.revanced.patches.reddit.utils.resourceid.patch.SharedResourceIdPatch -import app.revanced.patches.reddit.utils.settings.bytecode.patch.SettingsPatch -import app.revanced.patches.reddit.utils.settings.bytecode.patch.SettingsPatch.Companion.updateSettingsStatus +import app.revanced.patches.reddit.utils.settings.bytecode.patch.SettingsBytecodePatch.Companion.updateSettingsStatus +import app.revanced.patches.reddit.utils.settings.resource.patch.SettingsPatch @Patch @Name("disable-screenshot-popup") diff --git a/src/main/kotlin/app/revanced/patches/reddit/misc/openlink/patch/OpenLinksDirectlyPatch.kt b/src/main/kotlin/app/revanced/patches/reddit/misc/openlink/patch/OpenLinksDirectlyPatch.kt index 6c15d48ae..9e4bb97ee 100644 --- a/src/main/kotlin/app/revanced/patches/reddit/misc/openlink/patch/OpenLinksDirectlyPatch.kt +++ b/src/main/kotlin/app/revanced/patches/reddit/misc/openlink/patch/OpenLinksDirectlyPatch.kt @@ -13,8 +13,8 @@ import app.revanced.patcher.patch.annotations.DependsOn import app.revanced.patcher.patch.annotations.Patch import app.revanced.patches.reddit.misc.openlink.fingerprints.ScreenNavigatorFingerprint import app.revanced.patches.reddit.utils.annotations.RedditCompatibility -import app.revanced.patches.reddit.utils.settings.bytecode.patch.SettingsPatch -import app.revanced.patches.reddit.utils.settings.bytecode.patch.SettingsPatch.Companion.updateSettingsStatus +import app.revanced.patches.reddit.utils.settings.bytecode.patch.SettingsBytecodePatch.Companion.updateSettingsStatus +import app.revanced.patches.reddit.utils.settings.resource.patch.SettingsPatch @Patch @Name("open-links-directly") diff --git a/src/main/kotlin/app/revanced/patches/reddit/misc/openlink/patch/OpenLinksExternallyPatch.kt b/src/main/kotlin/app/revanced/patches/reddit/misc/openlink/patch/OpenLinksExternallyPatch.kt index 94da5a5bd..009c851ef 100644 --- a/src/main/kotlin/app/revanced/patches/reddit/misc/openlink/patch/OpenLinksExternallyPatch.kt +++ b/src/main/kotlin/app/revanced/patches/reddit/misc/openlink/patch/OpenLinksExternallyPatch.kt @@ -15,8 +15,8 @@ import app.revanced.patcher.patch.annotations.Patch import app.revanced.patcher.util.smali.ExternalLabel import app.revanced.patches.reddit.misc.openlink.fingerprints.ScreenNavigatorFingerprint import app.revanced.patches.reddit.utils.annotations.RedditCompatibility -import app.revanced.patches.reddit.utils.settings.bytecode.patch.SettingsPatch -import app.revanced.patches.reddit.utils.settings.bytecode.patch.SettingsPatch.Companion.updateSettingsStatus +import app.revanced.patches.reddit.utils.settings.bytecode.patch.SettingsBytecodePatch.Companion.updateSettingsStatus +import app.revanced.patches.reddit.utils.settings.resource.patch.SettingsPatch import app.revanced.util.bytecode.getStringIndex @Patch diff --git a/src/main/kotlin/app/revanced/patches/reddit/misc/tracking/url/patch/SanitizeUrlQueryPatch.kt b/src/main/kotlin/app/revanced/patches/reddit/misc/tracking/url/patch/SanitizeUrlQueryPatch.kt index 6f6cd9e95..93ae561fa 100644 --- a/src/main/kotlin/app/revanced/patches/reddit/misc/tracking/url/patch/SanitizeUrlQueryPatch.kt +++ b/src/main/kotlin/app/revanced/patches/reddit/misc/tracking/url/patch/SanitizeUrlQueryPatch.kt @@ -15,8 +15,8 @@ import app.revanced.patcher.patch.annotations.Patch import app.revanced.patcher.util.smali.ExternalLabel import app.revanced.patches.reddit.misc.tracking.url.fingerprints.ShareLinkFormatterFingerprint import app.revanced.patches.reddit.utils.annotations.RedditCompatibility -import app.revanced.patches.reddit.utils.settings.bytecode.patch.SettingsPatch -import app.revanced.patches.reddit.utils.settings.bytecode.patch.SettingsPatch.Companion.updateSettingsStatus +import app.revanced.patches.reddit.utils.settings.bytecode.patch.SettingsBytecodePatch.Companion.updateSettingsStatus +import app.revanced.patches.reddit.utils.settings.resource.patch.SettingsPatch @Patch @Name("sanitize-sharing-links") diff --git a/src/main/kotlin/app/revanced/patches/reddit/utils/settings/bytecode/patch/SettingsPatch.kt b/src/main/kotlin/app/revanced/patches/reddit/utils/settings/bytecode/patch/SettingsBytecodePatch.kt similarity index 70% rename from src/main/kotlin/app/revanced/patches/reddit/utils/settings/bytecode/patch/SettingsPatch.kt rename to src/main/kotlin/app/revanced/patches/reddit/utils/settings/bytecode/patch/SettingsBytecodePatch.kt index 439c623ef..28265114e 100644 --- a/src/main/kotlin/app/revanced/patches/reddit/utils/settings/bytecode/patch/SettingsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/reddit/utils/settings/bytecode/patch/SettingsBytecodePatch.kt @@ -1,7 +1,6 @@ package app.revanced.patches.reddit.utils.settings.bytecode.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 @@ -10,27 +9,16 @@ import app.revanced.patcher.extensions.InstructionExtensions.addInstructions 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.patcher.util.proxy.mutableTypes.MutableMethod import app.revanced.patches.reddit.utils.annotations.RedditCompatibility -import app.revanced.patches.reddit.utils.integrations.patch.IntegrationsPatch import app.revanced.patches.reddit.utils.settings.bytecode.fingerprints.OssLicensesMenuActivityOnCreateFingerprint import app.revanced.patches.reddit.utils.settings.bytecode.fingerprints.SettingsStatusLoadFingerprint -import app.revanced.patches.reddit.utils.settings.resource.patch.SettingsResourcePatch +import kotlin.properties.Delegates -@Patch -@Name("settings") -@Description("Adds ReVanced settings to Reddit.") -@DependsOn( - [ - IntegrationsPatch::class, - SettingsResourcePatch::class - ] -) +@Name("settings-bytecode-patch") @RedditCompatibility @Version("0.0.1") -class SettingsPatch : BytecodePatch( +class SettingsBytecodePatch : BytecodePatch( listOf( OssLicensesMenuActivityOnCreateFingerprint, SettingsStatusLoadFingerprint @@ -39,19 +27,11 @@ class SettingsPatch : BytecodePatch( override fun execute(context: BytecodeContext): PatchResult { OssLicensesMenuActivityOnCreateFingerprint.result?.let { - it.mutableMethod.apply { - val insertIndex = it.scanResult.patternScanResult!!.startIndex + 1 - - addInstructions( - insertIndex, """ - invoke-static {p0}, $INTEGRATIONS_METHOD_DESCRIPTOR - return-void - """ - ) - } + activityMethod = it.mutableMethod + activityIndex = it.scanResult.patternScanResult!!.startIndex + 1 } ?: return OssLicensesMenuActivityOnCreateFingerprint.toErrorResult() - targetMethod = SettingsStatusLoadFingerprint.result?.mutableMethod + settingsMethod = SettingsStatusLoadFingerprint.result?.mutableMethod ?: return SettingsStatusLoadFingerprint.toErrorResult() return PatchResultSuccess() @@ -61,10 +41,23 @@ class SettingsPatch : BytecodePatch( private const val INTEGRATIONS_METHOD_DESCRIPTOR = "Lapp/revanced/reddit/settingsmenu/ReVancedSettingActivity;->initializeSettings(Landroid/app/Activity;)V" - private lateinit var targetMethod: MutableMethod + 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) { - targetMethod.apply { + settingsMethod.apply { addInstruction( 0, "invoke-static {}, Lapp/revanced/reddit/settingsmenu/SettingsStatus;->$description()V" 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 new file mode 100644 index 000000000..5ae7b8bee --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/reddit/utils/settings/resource/patch/SettingsPatch.kt @@ -0,0 +1,122 @@ +package app.revanced.patches.reddit.utils.settings.resource.patch + +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 +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.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 +@Name("reddit-settings") +@Description("Adds ReVanced settings to Reddit.") +@DependsOn( + [ + IntegrationsPatch::class, + SettingsBytecodePatch::class + ] +) +@RedditCompatibility +@Version("0.0.1") +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 (!AddRedditSettings.equals("true")) + return PatchResultSuccess() + + + /** + * Copy strings.xml + */ + context.copyXmlNode("reddit/settings/host", "values/strings.xml", "resources") + + + /** + * Initialize settings activity + */ + SettingsBytecodePatch.injectActivity() + + + /** + * Replace settings icon + */ + arrayOf("preferences", "preferences_logged_in").forEach { targetXML -> + val resDirectory = context["res"] + val targetXml = resDirectory.resolve("xml").resolve("$targetXML.xml").toPath() + + if (!targetXml.exists()) + return PatchResultError("The preferences can not be found.") + + val preference = context["res/xml/$targetXML.xml"] + + preference.writeText( + preference.readText() + .replace( + "\"@drawable/icon_text_post\" android:title=\"@string/label_acknowledgements\"", + "\"@drawable/icon_beta_planet\" android:title=\"@string/label_acknowledgements\"" + ) + ) + } + + + /** + * 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 AddRedditSettings: String? by option( + PatchOption.StringOption( + key = "AddRedditSettings", + 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/kotlin/app/revanced/patches/reddit/utils/settings/resource/patch/SettingsResourcePatch.kt b/src/main/kotlin/app/revanced/patches/reddit/utils/settings/resource/patch/SettingsResourcePatch.kt deleted file mode 100644 index 577675dc6..000000000 --- a/src/main/kotlin/app/revanced/patches/reddit/utils/settings/resource/patch/SettingsResourcePatch.kt +++ /dev/null @@ -1,72 +0,0 @@ -package app.revanced.patches.reddit.utils.settings.resource.patch - -import app.revanced.extensions.doRecursively -import app.revanced.extensions.startsWithAny -import app.revanced.patcher.annotation.Name -import app.revanced.patcher.annotation.Version -import app.revanced.patcher.data.ResourceContext -import app.revanced.patcher.patch.PatchResult -import app.revanced.patcher.patch.PatchResultSuccess -import app.revanced.patches.shared.patch.settings.AbstractSettingsResourcePatch -import org.w3c.dom.Element -import kotlin.io.path.exists - -@Name("settings-resource-patch") -@Version("0.0.1") -class SettingsResourcePatch : AbstractSettingsResourcePatch( - "reddit/settings", - "reddit/settings/host", - false -) { - override fun execute(context: ResourceContext): PatchResult { - super.execute(context) - - fun setIcon(targetXML: String) { - context.xmlEditor["res/xml/$targetXML.xml"].use { editor -> - editor.file.doRecursively loop@{ - if (it !is Element) return@loop - - it.getAttributeNode("android:key")?.let { attribute -> - if (attribute.textContent.endsWith("key_pref_acknowledgements")) { - it.getAttributeNode("android:icon").textContent = - "@drawable/icon_beta_planet" - } - } - } - } - } - - arrayOf("preferences", "preferences_logged_in").forEach { targetXML -> - val resDirectory = context["res"] - val targetXml = resDirectory.resolve("xml").resolve("$targetXML.xml").toPath() - - if (targetXml.exists()) - setIcon(targetXML) - } - - // App name - val resourceFileNames = arrayOf("strings.xml") - - context.forEach { - if (!it.name.startsWithAny(*resourceFileNames)) return@forEach - - // for each file in the "layouts" directory replace all necessary attributes content - context.xmlEditor[it.absolutePath].use { editor -> - val resourcesNode = editor.file.getElementsByTagName("resources").item(0) as Element - - for (i in 0 until resourcesNode.childNodes.length) { - val node = resourcesNode.childNodes.item(i) - if (node !is Element) continue - - val element = resourcesNode.childNodes.item(i) as Element - element.textContent = when (element.getAttribute("name")) { - "label_acknowledgements" -> "@string/revanced_extended_settings_title" - else -> continue - } - } - } - } - - return PatchResultSuccess() - } -} \ No newline at end of file