script.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # encoding: utf-8
  2. import re
  3. __author__ = 'pengtao'
  4. import logging
  5. import os
  6. import yaml
  7. import xml.etree.ElementTree as ET
  8. import re
  9. logger = logging.getLogger('script.py')
  10. ANDROID_NS = 'http://schemas.android.com/apk/res/android'
  11. def script(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
  12. print("")
  13. xmlparse(decompileDir)
  14. def script_last(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
  15. print("")
  16. replace_rose_resource(decompileDir)
  17. fix_install_fail_bug(decompileDir)
  18. fix_android8_bug_rose(decompileDir)
  19. fix_rose_recharge_screen_orientation(decompileDir)
  20. def fix_rose_recharge_screen_orientation(decompile_dir_path):
  21. developer_path = os.path.join(decompile_dir_path, 'assets', 'developer.properties')
  22. print(developer_path)
  23. is_landscape = True
  24. with open(developer_path, 'r') as f:
  25. smali_str_arr = f.readlines()
  26. for line in smali_str_arr:
  27. if 'screen_oriention' in line:
  28. results = re.findall(re.compile(r'\d'), line)
  29. if len(results) != 0:
  30. orientation_value = results[0]
  31. print('orientation_value', orientation_value)
  32. is_landscape = int(orientation_value) == 0
  33. break
  34. print('is_landscape', is_landscape)
  35. namespace = '{http://schemas.android.com/apk/res/android}'
  36. ET.register_namespace('android', 'http://schemas.android.com/apk/res/android')
  37. manifest_path = os.path.join(decompile_dir_path, 'AndroidManifest.xml')
  38. xml_tree = ET.parse(manifest_path)
  39. xml_root = xml_tree.getroot()
  40. activity_nodes = xml_root.findall('.//activity')
  41. print(activity_nodes)
  42. for activity_node in activity_nodes:
  43. print(activity_node.attrib)
  44. activity_name = activity_node.get('%sname' % namespace)
  45. print(activity_name)
  46. if 'com.wanwu.sdkkit.gameplatform.activity.RechargeActivity' == activity_name:
  47. if is_landscape:
  48. activity_node.set('%sscreenOrientation' % namespace, 'landscape')
  49. else:
  50. activity_node.set('%sscreenOrientation' % namespace, 'portrait')
  51. xml_tree.write(manifest_path, encoding='utf-8', xml_declaration=True)
  52. break
  53. def fix_android8_bug_rose(decompile_dir):
  54. style_xml_path = os.path.join(decompile_dir, 'res', 'values', 'styles.xml')
  55. xml_tree = ET.parse(style_xml_path)
  56. xml_root = xml_tree.getroot()
  57. style_roots = xml_root.findall('./style')
  58. for style_root in style_roots:
  59. if style_root.get('name') == 'ydzs_splash_translucent':
  60. items = style_root.findall('./item')
  61. for item in items:
  62. print('item name', item.get('name'))
  63. print(item.findtext('.'))
  64. if item.get('name') == 'android:windowIsTranslucent':
  65. item.text = 'false'
  66. xml_tree.write(style_xml_path, encoding='utf-8', xml_declaration=True)
  67. break
  68. namespace = '{http://schemas.android.com/apk/res/android}'
  69. ET.register_namespace('android', 'http://schemas.android.com/apk/res/android')
  70. manifest_path = os.path.join(decompile_dir, 'AndroidManifest.xml')
  71. xml_tree = ET.parse(manifest_path)
  72. xml_root = xml_tree.getroot()
  73. act_list = xml_root.findall('.//activity')
  74. for act in act_list:
  75. act_name = act.get('%sname' % namespace)
  76. print(act_name)
  77. if 'com.wanwu.sdkkit.gameplatform.activity.RechargeActivity' == act_name:
  78. act_theme = act.get('%stheme' % namespace)
  79. print('act_theme', act_theme)
  80. act.set('%stheme' % namespace, '@style/ydzs_splash_translucent')
  81. xml_tree.write(manifest_path, encoding='utf-8', xml_declaration=True)
  82. break
  83. def xmlparse(decompileDir):
  84. ET.register_namespace("android",ANDROID_NS)
  85. xmlpath = os.path.join(decompileDir, 'AndroidManifest.xml')
  86. root_node = ET.parse(xmlpath)
  87. root = root_node.getroot()
  88. name = '{' + ANDROID_NS + '}name'
  89. authorities = '{' + ANDROID_NS + '}authorities'
  90. package_name = root.attrib.get('package')
  91. if package_name is None:
  92. return
  93. providers = root.findall("./application/provider")
  94. if providers != None:
  95. for provider in providers:
  96. providerName = provider.attrib.get(name)
  97. if 'android.support.haojieru.v4.content.FileProvider' == providerName:
  98. provider.set(authorities, package_name + '.rose2.fileProvider')
  99. root_node.write(xmlpath, 'utf-8')
  100. def fix_install_fail_bug(decompile_dir_path):
  101. namespace = '{http://schemas.android.com/apk/res/android}'
  102. ET.register_namespace('android', 'http://schemas.android.com/apk/res/android')
  103. manifest_path = os.path.join(decompile_dir_path, 'AndroidManifest.xml')
  104. xml_tree = ET.parse(manifest_path)
  105. xml_root = xml_tree.getroot()
  106. application_node = xml_root.find('./application')
  107. etract_value = application_node.get('%sextractNativeLibs' % namespace)
  108. if etract_value is not None and etract_value == 'false':
  109. # 修改extractNativeLib
  110. application_node.set('%sextractNativeLibs' % namespace, 'true')
  111. xml_tree.write(manifest_path, encoding='utf-8', xml_declaration=True)
  112. yaml_path = os.path.join(decompile_dir_path, 'apktool.yml')
  113. file = open(yaml_path, 'r')
  114. yaml_result = yaml.load(file, Loader=yaml.BaseLoader)
  115. sdk_info = yaml_result['sdkInfo']
  116. yaml_result['packageInfo']['renameManifestPackage'] = None
  117. print(sdk_info)
  118. if sdk_info.has_key('targetSdkVersion') and int(sdk_info['targetSdkVersion']) > 29:
  119. sdk_info['targetSdkVersion'] = '29'
  120. print(sdk_info)
  121. file.close()
  122. with open(yaml_path, 'w') as f:
  123. # yaml.dump(yaml_result, f)
  124. yaml.safe_dump(yaml_result, f,allow_unicode=True, default_flow_style=False)
  125. def find_r_smali_and_replace(smali_file_path, regex_str):
  126. with open(smali_file_path, 'r') as f:
  127. smali_str = f.read()
  128. pattern = re.compile(regex_str)
  129. resource_arr = pattern.findall(smali_str)
  130. if len(resource_arr) > 0:
  131. print(smali_file_path)
  132. for r_str in resource_arr:
  133. virtual_name = r_str[r_str.index(' ') + 1:r_str.index(',')]
  134. print(virtual_name)
  135. resource_type = r_str[r_str.index('R$') + 2:r_str.index(';')].capitalize()
  136. print(resource_type)
  137. resource_name = r_str[r_str.index('->') + 2:r_str.index(':')]
  138. print(resource_name)
  139. new_r_str = "const-string %s, \"%s\"\n\n\tinvoke-static {%s}, Lcom/ydzs/framework/MommyUtils;->get%sId(Ljava/lang/String;)I\n\n\tmove-result %s" % (
  140. virtual_name, resource_name, virtual_name, resource_type, virtual_name)
  141. print(new_r_str)
  142. smali_str = smali_str.replace(r_str, new_r_str)
  143. with open(smali_file_path, 'w') as f:
  144. f.write(smali_str)
  145. def replace_rose_resource(d_dir):
  146. r_regex_str = r'sget[^\n]*R\$layout[^\n]*I|sget[^\n]*R\$id[^\n]*I|sget[^\n]*R\$drawable[^\n]*I|sget[^\n]*R\$string[^\n]*I|sget[^\n]*R\$array[^\n]*I|sget[^\n]*R\$bool[^\n]*I|sget[^\n]*R\$integer[^\n]*I'
  147. for dirpath, dirnames, filenames in os.walk(d_dir):
  148. if r'com\ydzs' in dirpath:
  149. for smali_file in filenames:
  150. smali_file_path = os.path.join(dirpath, smali_file)
  151. # print(smali_file_path)
  152. find_r_smali_and_replace(smali_file_path, r_regex_str)
  153. if __name__ == '__main__':
  154. xmlparse("D:/x7debug")