fix(reddit/reddit-settings): now when the patch is applied through the RVX Manager, the settings are added normally

This commit is contained in:
inotia00 2023-07-24 04:16:36 +09:00
parent 3066e1cafa
commit 152888535b
8 changed files with 96 additions and 193 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,16 +0,0 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<resources>
<string name="revanced_disable_screenshot_popup_summary">Disables the popup that shows up when taking a screenshot</string>
<string name="revanced_disable_screenshot_popup_title">Disable screenshot popup</string>
<string name="revanced_extended_settings_title">ReVanced Extended</string>
<string name="revanced_hide_create_button_summary">Hide create button in navigation</string>
<string name="revanced_hide_create_button_title">Hide create button</string>
<string name="revanced_hide_discover_button_summary">Hide discover button or communities button in navigation</string>
<string name="revanced_hide_discover_button_title">Hide discover / community button</string>
<string name="revanced_open_links_directly_summary">Skips over redirection URLs to external links</string>
<string name="revanced_open_links_directly_title">Open links directly</string>
<string name="revanced_open_links_externally_summary">Open links outside of the app directly in your browser</string>
<string name="revanced_open_links_externally_title">Open links externally</string>
<string name="revanced_sanitize_url_query_summary">Removes (tracking) query parameters from the URLs when sharing links</string>
<string name="revanced_sanitize_url_query_title">Sanitize sharing links</string>
</resources>