11package app.revanced.patches.twitter.misc.links
22
33import app.revanced.patcher.extensions.InstructionExtensions.addInstructions
4- import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
5- import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction
64import app.revanced.patcher.patch.bytecodePatch
75import app.revanced.patcher.patch.resourcePatch
86import app.revanced.patcher.patch.stringOption
97import app.revanced.patches.shared.PATCH_DESCRIPTION_CHANGE_LINK_SHARING_DOMAIN
108import app.revanced.patches.shared.PATCH_NAME_CHANGE_LINK_SHARING_DOMAIN
11- import app.revanced.patches.shared.misc.mapping.get
12- import app.revanced.patches.shared.misc.mapping.resourceMappingPatch
13- import app.revanced.patches.shared.misc.mapping.resourceMappings
149import app.revanced.patches.twitter.misc.extension.sharedExtensionPatch
15- import app.revanced.util.indexOfFirstLiteralInstructionOrThrow
16- import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction
17- import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
10+ import app.revanced.util.findElementByAttributeValueOrThrow
11+ import app.revanced.util.returnEarly
12+ import java.net.InetAddress
13+ import java.net.UnknownHostException
14+ import java.util.logging.Logger
1815
19- internal var tweetShareLinkTemplateId = - 1L
20- private set
16+ internal const val EXTENSION_CLASS_DESCRIPTOR = " Lapp/revanced/twitter/patches/links/ChangeLinkSharingDomainPatch;"
2117
22- internal val changeLinkSharingDomainResourcePatch = resourcePatch {
23- dependsOn(resourceMappingPatch)
18+ internal val domainNameOption by stringOption(
19+ key = " domainName" ,
20+ default = " https://fxtwitter.com" ,
21+ title = " Domain name" ,
22+ description = " The domain name to use when sharing links." ,
23+ required = true ,
24+ ) {
25+ // Do a courtesy check if the host can be resolved.
26+ // If it does not resolve, then print a warning but use the host anyway.
27+ // Unresolvable hosts should not be rejected, since the patching environment
28+ // may not allow network connections or the network may be down.
29+ try {
30+ InetAddress .getByName(it)
31+ } catch (e: UnknownHostException ) {
32+ Logger .getLogger(this ::class .java.name).warning(
33+ " Host \" $it \" did not resolve to any domain."
34+ )
35+ }
36+ true
37+ }
2438
39+ internal val changeLinkSharingDomainResourcePatch = resourcePatch {
2540 execute {
26- tweetShareLinkTemplateId = resourceMappings[" string" , " tweet_share_link" ]
41+ val domainName = domainNameOption!!
42+
43+ val shareLinkTemplate = if (domainName.endsWith(" /" )) {
44+ " $domainName %1\$ s/status/%2\$ s"
45+ } else {
46+ " $domainName /%1\$ s/status/%2\$ s"
47+ }
48+
49+ document(" res/values/strings.xml" ).use { document ->
50+ document.documentElement.childNodes.findElementByAttributeValueOrThrow(
51+ " name" ,
52+ " tweet_share_link"
53+ ).textContent = shareLinkTemplate
54+ }
2755 }
2856}
2957
30- private const val EXTENSION_CLASS_DESCRIPTOR = " Lapp/revanced/twitter/patches/links/ChangeLinkSharingDomainPatch;"
31-
3258@Suppress(" unused" )
3359val changeLinkSharingDomainPatch = bytecodePatch(
3460 name = PATCH_NAME_CHANGE_LINK_SHARING_DOMAIN ,
@@ -46,26 +72,11 @@ val changeLinkSharingDomainPatch = bytecodePatch(
4672 )
4773 )
4874
49- val domainName by stringOption(
50- key = " domainName" ,
51- default = " fxtwitter.com" ,
52- title = " Domain name" ,
53- description = " The domain name to use when sharing links." ,
54- required = true ,
55- )
56-
5775 execute {
58- linkSharingDomainFingerprint.let {
59- val replacementIndex = it.stringMatches!! .first().index
60- val domainRegister = it.method.getInstruction<OneRegisterInstruction >(
61- replacementIndex
62- ).registerA
76+ val domainName = domainNameOption!!
6377
64- it.method.replaceInstruction(
65- replacementIndex,
66- " const-string v$domainRegister , \" https://$domainName \" " ,
67- )
68- }
78+ // Replace the domain name in the link sharing extension methods.
79+ linkSharingDomainHelperFingerprint.method.returnEarly(domainName)
6980
7081 // Replace the domain name when copying a link with "Copy link" button.
7182 linkBuilderFingerprint.method.addInstructions(
@@ -76,20 +87,5 @@ val changeLinkSharingDomainPatch = bytecodePatch(
7687 return-object p0
7788 """
7889 )
79-
80- // Used in the Share via... dialog.
81- linkResourceGetterFingerprint.method.apply {
82- val templateIdConstIndex = indexOfFirstLiteralInstructionOrThrow(tweetShareLinkTemplateId)
83-
84- // Format the link with the new domain name register (1 instruction below the const).
85- val formatLinkCallIndex = templateIdConstIndex + 1
86- val register = getInstruction<FiveRegisterInstruction >(formatLinkCallIndex).registerE
87-
88- // Replace the original method call with the new method call.
89- replaceInstruction(
90- formatLinkCallIndex,
91- " invoke-static { v$register }, $EXTENSION_CLASS_DESCRIPTOR ->formatResourceLink([Ljava/lang/Object;)Ljava/lang/String;" ,
92- )
93- }
9490 }
9591}
0 commit comments