convert_emotions.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/usr/bin/env python3
  2. """
  3. Simple scripts which replaces the old emotion script constants to the new
  4. script constants.
  5. The actual replacement list is in 'emotion_dict'.
  6. Related pull request: https://github.com/rathena/rathena/pull/2527
  7. Note: This only applies the constant changes. You still have to check your scripts
  8. for 'unitemote' (command was removed) and 'emotion' using the third parameter ('target name').
  9. """
  10. import fileinput
  11. import re
  12. import os
  13. import collections
  14. convert_folders = ["../npc", "../src"]
  15. wl_file_extensions = ['.hpp', '.h', '.cpp', '.c', '.txt' ]
  16. script_file_extensions = ['.txt']
  17. bl_files = ['script_constants.hpp']
  18. BACKUP_EXT = '.bak'
  19. emotion_dict = collections.OrderedDict([
  20. ('E_GASP', 'ET_SURPRISE'),
  21. ('E_WHAT', 'ET_QUESTION'),
  22. ('E_HO', 'ET_DELIGHT'),
  23. ('E_LV', 'ET_THROB'),
  24. ('E_SWT', 'ET_SWEAT'),
  25. ('E_IC', 'ET_AHA'),
  26. ('E_AN', 'ET_FRET'),
  27. ('E_AG', 'ET_ANGER'),
  28. ('E_CASH', 'ET_MONEY'),
  29. ('E_DOTS', 'ET_THINK'),
  30. ('E_SCISSORS', 'ET_SCISSOR'),
  31. ('E_ROCK', 'ET_ROCK'),
  32. ('E_PAPER', 'ET_WRAP'),
  33. ('E_KOREA', 'ET_FLAG'),
  34. ('E_LV2', 'ET_BIGTHROB'),
  35. ('E_THX', 'ET_THANKS'),
  36. ('E_WAH', 'ET_KEK'),
  37. ('E_SRY', 'ET_SORRY'),
  38. ('E_HEH', 'ET_SMILE'),
  39. ('E_SWT2', 'ET_PROFUSELY_SWEAT'),
  40. ('E_HMM', 'ET_SCRATCH'),
  41. ('E_NO1', 'ET_BEST'),
  42. ('E_NO', 'ET_STARE_ABOUT'),
  43. ('E_OMG', 'ET_HUK'),
  44. ('E_OH', 'ET_O'),
  45. ('E_X', 'ET_X'),
  46. ('E_HLP', 'ET_HELP'),
  47. ('E_GO', 'ET_GO'),
  48. ('E_SOB', 'ET_CRY'),
  49. ('E_GG', 'ET_KIK'),
  50. ('E_KIS', 'ET_CHUP'),
  51. ('E_KIS2', 'ET_CHUPCHUP'),
  52. ('E_PIF', 'ET_HNG'),
  53. ('E_OK', 'ET_OK'),
  54. ('E_MUTE', 'ET_CHAT_PROHIBIT'),
  55. ('E_INDONESIA', 'ET_INDONESIA_FLAG'),
  56. ('E_BZZ', 'ET_STARE'),
  57. ('E_RICE', 'ET_HUNGRY'),
  58. ('E_AWSM', 'ET_COOL'),
  59. ('E_MEH', 'ET_MERONG'),
  60. ('E_SHY', 'ET_SHY'),
  61. ('E_PAT', 'ET_GOODBOY'),
  62. ('E_MP', 'ET_SPTIME'),
  63. ('E_SLUR', 'ET_SEXY'),
  64. ('E_COM', 'ET_COMEON'),
  65. ('E_YAWN', 'ET_SLEEPY'),
  66. ('E_GRAT', 'ET_CONGRATULATION'),
  67. ('E_HP', 'ET_HPTIME'),
  68. ('E_PHILIPPINES', 'ET_PH_FLAG'),
  69. ('E_MALAYSIA', 'ET_MY_FLAG'),
  70. ('E_SINGAPORE', 'ET_SI_FLAG'),
  71. ('E_BRAZIL', 'ET_BR_FLAG'),
  72. ('E_FLASH', 'ET_SPARK'),
  73. ('E_SPIN', 'ET_CONFUSE'),
  74. ('E_SIGH', 'ET_OHNO'),
  75. ('E_DUM', 'ET_HUM'),
  76. ('E_LOUD', 'ET_BLABLA'),
  77. ('E_OTL', 'ET_OTL'),
  78. ('E_DICE1', 'ET_DICE1'),
  79. ('E_DICE2', 'ET_DICE2'),
  80. ('E_DICE3', 'ET_DICE3'),
  81. ('E_DICE4', 'ET_DICE4'),
  82. ('E_DICE5', 'ET_DICE5'),
  83. ('E_DICE6', 'ET_DICE6'),
  84. ('E_INDIA', 'ET_INDIA_FLAG'),
  85. ('E_LUV', 'ET_LUV'),
  86. ('E_RUSSIA', 'ET_FLAG8'),
  87. ('E_VIRGIN', 'ET_FLAG9'),
  88. ('E_MOBILE', 'ET_MOBILE'),
  89. ('E_MAIL', 'ET_MAIL'),
  90. ('E_CHINESE', 'ET_ANTENNA0'),
  91. ('E_ANTENNA1', 'ET_ANTENNA1'),
  92. ('E_ANTENNA2', 'ET_ANTENNA2'),
  93. ('E_ANTENNA3', 'ET_ANTENNA3'),
  94. ('E_HUM', 'ET_HUM2'),
  95. ('E_ABS', 'ET_ABS'),
  96. ('E_OOPS', 'ET_OOPS'),
  97. ('E_SPIT', 'ET_SPIT'),
  98. ('E_ENE', 'ET_ENE'),
  99. ('E_PANIC', 'ET_PANIC'),
  100. ('E_WHISP', 'ET_WHISP'),
  101. ('E_YUT1', 'ET_YUT1'),
  102. ('E_YUT2', 'ET_YUT2'),
  103. ('E_YUT3', 'ET_YUT3'),
  104. ('E_YUT4', 'ET_YUT4'),
  105. ('E_YUT5', 'ET_YUT5'),
  106. ('E_YUT6', 'ET_YUT6'),
  107. ('E_YUT7', 'ET_YUT7'),
  108. ('E_MAX', 'ET_MAX')
  109. ])
  110. emotion_array = [val for val in emotion_dict.values()]
  111. pattern_oldconst = re.compile(r'\b(' + '|'.join(emotion_dict.keys()) + r')\b', re.IGNORECASE)
  112. pattern_value = re.compile(r'\b(' + '|'.join(["emotion\s+%d+"%i for i in range(len(emotion_array))]) + r')\b', re.IGNORECASE)
  113. # print('\b(' + '|'.join(["emotion\s+%d"%i for i in range(len(emotion_array))]) + r')\b')
  114. def revert_to_backup(filename):
  115. os.rename(filename+BACKUP_EXT, filename)
  116. def apply_substitutions(new_line, is_script):
  117. remove_backup = True # only keep backup if the original file changed
  118. rpl_cnt = 0
  119. # E_GASP -> ET_SURPRISE
  120. new_line, rpl_cnt = pattern_oldconst.subn(lambda x: emotion_dict[x.group().upper()], new_line)
  121. remove_backup = False if rpl_cnt > 0 else remove_backup
  122. if is_script: # script only replacements
  123. # 0 -> ET_SURPRISE
  124. new_line, rpl_cnt = pattern_value.subn(lambda x: 'emotion '+emotion_array[int(x.group().split()[-1])], new_line)
  125. remove_backup = False if rpl_cnt > 0 else remove_backup
  126. # emotion e,0,"Record player#e152a01"; -> emotion e, getnpcid(0,"Record player#e152a01");
  127. new_line, rpl_cnt = re.subn(r"emotion\s+([^,]+)\s*,\s*0\s*,\s*([^;]+);",
  128. "emotion \g<1>, getnpcid(0, \g<2>);", new_line)
  129. remove_backup = False if rpl_cnt > 0 else remove_backup
  130. # emotion e,1; -> emotion e, playerattached();
  131. new_line, rpl_cnt = re.subn(r"emotion\s+([^,]+)\s*,\s*1\s*;",
  132. "emotion \g<1>, playerattached();", new_line)
  133. remove_backup = False if rpl_cnt > 0 else remove_backup
  134. # unitemote <id>,<emotion>; -> emotion <emotion>, <id>;
  135. new_line, rpl_cnt = re.subn(r"unitemote\s+([^,]+)\s*,\s*([^,;]+)\s*;",
  136. "emotion \g<2>, \g<1>;", new_line)
  137. remove_backup = False if rpl_cnt > 0 else remove_backup
  138. return new_line, remove_backup
  139. def replace_emoticons_in_file(filename):
  140. is_script = True if any([filename.endswith(script_ext) for script_ext in script_file_extensions]) else False
  141. with fileinput.FileInput(filename, inplace=True, backup=BACKUP_EXT) as fiFile:
  142. try:
  143. for line in fiFile:
  144. new_line, remove_backup = apply_substitutions(line, is_script)
  145. print(new_line, end='')
  146. if remove_backup:
  147. os.remove(filename+BACKUP_EXT)
  148. except UnicodeDecodeError:
  149. # Encoding error, reapply the backup
  150. revert_to_backup(filename)
  151. fileiter = (os.path.join(root, f)
  152. for conv_folder in convert_folders
  153. for root, _, files in os.walk(conv_folder)
  154. for f in files
  155. if any([f.endswith(wl) for wl in wl_file_extensions])
  156. if not any([bl in f for bl in bl_files])
  157. )
  158. for f in fileiter:
  159. print("Updating file", f)
  160. replace_emoticons_in_file(f)