Skip to content

Commit 017d9ac

Browse files
committed
Improved error handling and added fallback logic for method modification
- Added a new exception class `NoMethodFoundError` to handle cases when the target method is not found in the file. - `modify_method` function raises `NoMethodFoundError` exception if the target method is not found. Updated functions that call it to catch and handle exceptions. - Added fallback logic to the `modify_isPremium_stories` function to search for an alternative method signature if the first one is not found.
1 parent 5d5f46f commit 017d9ac

File tree

1 file changed

+129
-58
lines changed

1 file changed

+129
-58
lines changed

tgpatcher.py

Lines changed: 129 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
NC = "\033[0m" # No Color
1919

2020

21+
class NoMethodFoundError(Exception):
22+
"""Exception raised when the method is not found in the file."""
23+
24+
pass
25+
26+
2127
def find_smali_file(root_directory, target_file):
2228
"""Recursively search for the target file within the root directory."""
2329
for dirpath, _, filenames in os.walk(root_directory):
@@ -67,7 +73,9 @@ def modify_method(file_path, method_name, new_method_code):
6773
file.writelines(new_lines)
6874
print(f"{GREEN}INFO: {NC}Method {method_name} modified successfully.")
6975
else:
70-
print(f"{YELLOW}WARN: {NC}Method {method_name} not found in the file.")
76+
raise NoMethodFoundError(
77+
f"{YELLOW}WARN: {NC}Method {method_name} not found in the file."
78+
)
7179

7280

7381
def apply_regex(root_directory, search_pattern, replace_pattern, file_path=None):
@@ -209,7 +217,10 @@ def modify_isPremium(file_path):
209217
" return v0\n",
210218
".end method\n",
211219
]
212-
modify_method(file_path, "public isPremium()Z", new_method_code)
220+
try:
221+
modify_method(file_path, "public isPremium()Z", new_method_code)
222+
except NoMethodFoundError as e:
223+
print(e)
213224

214225

215226
def modify_markMessagesAsDeleted(file_path):
@@ -228,16 +239,22 @@ def modify_markMessagesAsDeleted(file_path):
228239
" return-object v1\n",
229240
".end method\n",
230241
]
231-
modify_method(
232-
file_path,
233-
"public markMessagesAsDeleted(JIZZ)Ljava/util/ArrayList;",
234-
new_method_code,
235-
)
236-
modify_method(
237-
file_path,
238-
"public markMessagesAsDeleted(JLjava/util/ArrayList;ZZII)Ljava/util/ArrayList;",
239-
new_method_code2,
240-
)
242+
try:
243+
modify_method(
244+
file_path,
245+
"public markMessagesAsDeleted(JIZZ)Ljava/util/ArrayList;",
246+
new_method_code,
247+
)
248+
except NoMethodFoundError as e:
249+
print(e)
250+
try:
251+
modify_method(
252+
file_path,
253+
"public markMessagesAsDeleted(JLjava/util/ArrayList;ZZII)Ljava/util/ArrayList;",
254+
new_method_code2,
255+
)
256+
except NoMethodFoundError as e:
257+
print(e)
241258

242259

243260
def modify_isPremium_stories(file_path):
@@ -252,7 +269,21 @@ def modify_isPremium_stories(file_path):
252269
" return p1\n",
253270
".end method\n",
254271
]
255-
modify_method(file_path, "private isPremium(J)Z", new_method_code)
272+
try:
273+
modify_method(file_path, "private isPremium(J)Z", new_method_code)
274+
except NoMethodFoundError:
275+
# Maybe plus messenger ? let's look for 2nd method
276+
new_method_code = [
277+
".method public final isPremium(J)Z\n",
278+
" .locals 1\n",
279+
" const/4 p1, 0x1\n",
280+
" return p1\n",
281+
".end method\n",
282+
]
283+
try:
284+
modify_method(file_path, "public final isPremium(J)Z", new_method_code)
285+
except NoMethodFoundError:
286+
print(f"{YELLOW}No isPremium(J)Z method found in StoriesController!{NC}")
256287

257288

258289
def modify_getCertificateSHA256Fingerprint(file_path):
@@ -269,11 +300,14 @@ def modify_getCertificateSHA256Fingerprint(file_path):
269300
" return-object v0\n",
270301
".end method\n",
271302
]
272-
modify_method(
273-
file_path,
274-
"public static getCertificateSHA256Fingerprint()Ljava/lang/String;",
275-
new_method_code,
276-
)
303+
try:
304+
modify_method(
305+
file_path,
306+
"public static getCertificateSHA256Fingerprint()Ljava/lang/String;",
307+
new_method_code,
308+
)
309+
except NoMethodFoundError as e:
310+
print(e)
277311

278312

279313
def modify_forcePremium(file_path):
@@ -287,11 +321,14 @@ def modify_forcePremium(file_path):
287321
" return p0\n",
288322
".end method\n",
289323
]
290-
modify_method(
291-
file_path,
292-
"static synthetic access$3000(Lorg/telegram/ui/PremiumPreviewFragment;)Z",
293-
new_method_code,
294-
)
324+
try:
325+
modify_method(
326+
file_path,
327+
"static synthetic access$3000(Lorg/telegram/ui/PremiumPreviewFragment;)Z",
328+
new_method_code,
329+
)
330+
except NoMethodFoundError as e:
331+
print(e)
295332

296333

297334
def modify_markStories_method(file_path):
@@ -312,16 +349,22 @@ def modify_markStories_method(file_path):
312349
" return p1\n",
313350
".end method\n",
314351
]
315-
modify_method(
316-
file_path,
317-
"public markStoryAsRead(Lorg/telegram/tgnet/tl/TL_stories$PeerStories;Lorg/telegram/tgnet/tl/TL_stories$StoryItem;Z)Z",
318-
new_method_code1,
319-
)
320-
modify_method(
321-
file_path,
322-
"public markStoryAsRead(JLorg/telegram/tgnet/tl/TL_stories$StoryItem;)Z",
323-
new_method_code2,
324-
)
352+
try:
353+
modify_method(
354+
file_path,
355+
"public markStoryAsRead(Lorg/telegram/tgnet/tl/TL_stories$PeerStories;Lorg/telegram/tgnet/tl/TL_stories$StoryItem;Z)Z",
356+
new_method_code1,
357+
)
358+
except NoMethodFoundError as e:
359+
print(e)
360+
try:
361+
modify_method(
362+
file_path,
363+
"public markStoryAsRead(JLorg/telegram/tgnet/tl/TL_stories$StoryItem;)Z",
364+
new_method_code2,
365+
)
366+
except NoMethodFoundError as e:
367+
print(e)
325368

326369

327370
def modify_isPremiumFeatureAvailable_method(file_path, method_name):
@@ -570,12 +613,18 @@ def modify_isChatNoForwards(file_path):
570613
" return p1\n",
571614
".end method\n",
572615
]
573-
modify_method(file_path, "public isChatNoForwards(J)Z", new_method_code1)
574-
modify_method(
575-
file_path,
576-
"public isChatNoForwards(Lorg/telegram/tgnet/TLRPC$Chat;)Z",
577-
new_method_code2,
578-
)
616+
try:
617+
modify_method(file_path, "public isChatNoForwards(J)Z", new_method_code1)
618+
except NoMethodFoundError as e:
619+
print(e)
620+
try:
621+
modify_method(
622+
file_path,
623+
"public isChatNoForwards(Lorg/telegram/tgnet/TLRPC$Chat;)Z",
624+
new_method_code2,
625+
)
626+
except NoMethodFoundError as e:
627+
print(e)
579628

580629

581630
def modify_checkCanOpenChat(file_path):
@@ -601,21 +650,30 @@ def modify_checkCanOpenChat(file_path):
601650
" return p1\n",
602651
".end method\n",
603652
]
604-
modify_method(
605-
file_path,
606-
"public checkCanOpenChat(Landroid/os/Bundle;Lorg/telegram/ui/ActionBar/BaseFragment;)Z",
607-
new_method_code1,
608-
)
609-
modify_method(
610-
file_path,
611-
"public checkCanOpenChat(Landroid/os/Bundle;Lorg/telegram/ui/ActionBar/BaseFragment;Lorg/telegram/messenger/MessageObject;)Z",
612-
new_method_code2,
613-
)
614-
modify_method(
615-
file_path,
616-
"public checkCanOpenChat(Landroid/os/Bundle;Lorg/telegram/ui/ActionBar/BaseFragment;Lorg/telegram/messenger/MessageObject;Lorg/telegram/messenger/browser/Browser$Progress;)Z",
617-
new_method_code3,
618-
)
653+
try:
654+
modify_method(
655+
file_path,
656+
"public checkCanOpenChat(Landroid/os/Bundle;Lorg/telegram/ui/ActionBar/BaseFragment;)Z",
657+
new_method_code1,
658+
)
659+
except NoMethodFoundError as e:
660+
print(e)
661+
try:
662+
modify_method(
663+
file_path,
664+
"public checkCanOpenChat(Landroid/os/Bundle;Lorg/telegram/ui/ActionBar/BaseFragment;Lorg/telegram/messenger/MessageObject;)Z",
665+
new_method_code2,
666+
)
667+
except NoMethodFoundError as e:
668+
print(e)
669+
try:
670+
modify_method(
671+
file_path,
672+
"public checkCanOpenChat(Landroid/os/Bundle;Lorg/telegram/ui/ActionBar/BaseFragment;Lorg/telegram/messenger/MessageObject;Lorg/telegram/messenger/browser/Browser$Progress;)Z",
673+
new_method_code3,
674+
)
675+
except NoMethodFoundError as e:
676+
print(e)
619677

620678

621679
def modify_is_sponsored_method(file_path):
@@ -627,7 +685,10 @@ def modify_is_sponsored_method(file_path):
627685
" return v0\n",
628686
".end method\n",
629687
]
630-
modify_method(file_path, "public isSponsored()Z", new_method_code)
688+
try:
689+
modify_method(file_path, "public isSponsored()Z", new_method_code)
690+
except NoMethodFoundError as e:
691+
print(e)
631692

632693

633694
def modify_is_proxy_sponsored_method(file_path):
@@ -638,7 +699,10 @@ def modify_is_proxy_sponsored_method(file_path):
638699
"return-void\n",
639700
".end method\n",
640701
]
641-
modify_method(file_path, "private checkPromoInfoInternal(Z)V", new_method_code)
702+
try:
703+
modify_method(file_path, "private checkPromoInfoInternal(Z)V", new_method_code)
704+
except NoMethodFoundError as e:
705+
print(e)
642706

643707

644708
def automate_modification(root_directory, target_file, modification_function):
@@ -677,7 +741,7 @@ def main(selected_patch=None, root_directory=None):
677741

678742
if root_directory == "Telegram":
679743
root_directory = (
680-
input("Give me the decompiled directory path (Default is 'Telegram'):")
744+
input("Give me the decompiled directory path (Default is 'Telegram'): ")
681745
or root_directory
682746
)
683747

@@ -728,6 +792,11 @@ def main(selected_patch=None, root_directory=None):
728792
root_directory,
729793
"private isPremiumFeatureAvailable(I)Z",
730794
modify_isPremiumFeatureAvailable_method,
795+
)
796+
or automate_method_modification(
797+
root_directory,
798+
"public final isPremiumFeatureAvailable(I)Z",
799+
modify_isPremiumFeatureAvailable_method,
731800
),
732801
),
733802
"7": (
@@ -837,7 +906,9 @@ def main(selected_patch=None, root_directory=None):
837906
required=False,
838907
action=argparse.BooleanOptionalAction,
839908
)
840-
parser.add_argument("--dir", help="Specify the directory", required=False, default="Telegram")
909+
parser.add_argument(
910+
"--dir", help="Specify the directory", required=False, default="Telegram"
911+
)
841912

842913
args = parser.parse_args()
843914

0 commit comments

Comments
 (0)