feat(reddit/reddit-settings): preferences are added to settings only when options.json values are specified

This commit is contained in:
inotia00 2023-07-06 11:47:59 +09:00
parent d771589db1
commit 498e5d2001
7 changed files with 151 additions and 108 deletions

View File

@ -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")

View File

@ -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")

View File

@ -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

View File

@ -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")

View File

@ -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<Int>()
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"

View File

@ -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("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?><resources><string name=\"label_acknowledgements\">ReVanced Extended</string></resources>")
}
}
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"
)
)
}
}

View File

@ -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()
}
}