script.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # coding:utf8
  2. import os
  3. import logging
  4. import re
  5. import glob
  6. import shutil
  7. import xml.etree.ElementTree as ET
  8. import gw_data_center
  9. import gw_apk_tool
  10. import distutils.dir_util
  11. ANDROID_NS = 'http://schemas.android.com/apk/res/android'
  12. def script_init(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
  13. gw_data_center.pack_small = False
  14. logging.info('script_init: ' + str(channel_sdk_info))
  15. def script_last(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
  16. s1 = os.path.join(extract_dir, "smali_classes3")
  17. s2 = os.path.join(extract_dir, "smali_classes3", "androidx")
  18. if os.path.exists(s2):
  19. smali_classes2_dir = gw_apk_tool.create_mutil_smali(extract_dir)
  20. s3 = smali_classes2_dir
  21. if not os.path.exists(s3):
  22. distutils.dir_util.mkpath(s3)
  23. gw_apk_tool.move_package(s1, s3, ["androidx"])
  24. sdk_id = channel_sdk_info['id']
  25. if sdk_id == "512" or sdk_id=="589":
  26. move(extract_dir)
  27. else:
  28. move_smallsheep(extract_dir)
  29. return
  30. def move(extract_dir):
  31. s1 = os.path.join(extract_dir, "smali_classes5", "com")
  32. smali_classes2_dir = gw_apk_tool.create_mutil_smali(extract_dir)
  33. s3 = os.path.join(smali_classes2_dir, "com")
  34. if not os.path.exists(s3):
  35. distutils.dir_util.mkpath(s3)
  36. gw_apk_tool.move_package(s1, s3, ["netease"])
  37. def move_smallsheep(extract_dir):
  38. s1 = os.path.join(extract_dir, "smali_classes4", "androidx")
  39. smali_classes2_dir = gw_apk_tool.create_mutil_smali(extract_dir)
  40. s3 = os.path.join(smali_classes2_dir, "androidx")
  41. move_page=["interpolator","lifecycle","localbroadcastmanager","print","slidingpanelayout","tracing","versionedparcelable","legacy","loader","media","savedstate","swiperefreshlayout","vectordrawable","viewpager"]
  42. if not os.path.exists(s3):
  43. distutils.dir_util.mkpath(s3)
  44. gw_apk_tool.move_package(s1, s3, move_page)
  45. s1 = os.path.join(extract_dir, "smali_classes6", "com/netease")
  46. smali_classes2_dir = gw_apk_tool.create_mutil_smali(extract_dir)
  47. s3 = os.path.join(smali_classes2_dir, "com/netease")
  48. if not os.path.exists(s3):
  49. distutils.dir_util.mkpath(s3)
  50. gw_apk_tool.move_package(s1, s3, ["ntunisdk","pharos","screen_recorder","sr101","tx","unifix","unisdk","utils","videoedit"])
  51. def script_pre(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
  52. sdk_id = channel_sdk_info['id']
  53. if sdk_id == "512" or sdk_id=="589":
  54. modify_manifest(extract_dir)
  55. game_mode_config = os.path.join(extract_dir, "res/xml-v22/game_mode_config.xml")
  56. os.remove(game_mode_config)
  57. def intoFirstLine(target, repl, cont):
  58. # srcStr = "onBackPressed"
  59. # regexp = '.method (?:public|protected) +?'+target+"\([^.]+?\.locals +[0-9]{1,2}"
  60. regexp = '.method (?:public|protected) +?' + target + "\([^.]+?\.locals +([0-9]{1,2})"
  61. # print(regexp)
  62. reObj = re.compile(regexp)
  63. matchO = reObj.search(cont)
  64. if matchO:
  65. # print(matchO.group(0))
  66. methodHead = matchO.group(0)
  67. if matchO.group(1) == "0":
  68. methodHead = re.sub("locals +0", "locals 1", methodHead)
  69. repl = methodHead + r'\n\n ' + repl + r'\n\n'
  70. cont = reObj.sub(repl, cont)
  71. else:
  72. # todo call .super method
  73. cont += r'.method protected ' + target + r'()V\n\n .locals 0\n\n ' + repl + r'\n\n .end method\n'
  74. return cont
  75. def fixLifecycleMethod(preDir):
  76. for tfile in glob.glob(preDir + "/" + "smali*/org/bojoy/publish/PublishActivity.smali"):
  77. if os.path.isfile(tfile):
  78. with open(tfile, 'r+') as f:
  79. cont = f.read()
  80. changes = {
  81. "onCreate": "invoke-static {p0}, Lcom/baidu/gamesdk/BDGameSDK;->setContext(Landroid/app/Activity;)V",
  82. }
  83. for k, v in changes.iteritems():
  84. cont = intoFirstLine(k, v, cont)
  85. f.seek(0)
  86. f.write(cont)
  87. f.close()
  88. def handle_public_xml(public_xml_path):
  89. if not os.path.exists(public_xml_path):
  90. logging.info('public_xml is null: ' + public_xml_path)
  91. return
  92. new_lines = []
  93. with open(public_xml_path, 'r+') as f:
  94. for line in f.readlines():
  95. # l = line.strip()
  96. if line.find("qihoo_game_sdk_authenticator") > -1:
  97. continue
  98. new_lines.append(line)
  99. f.seek(0)
  100. f.truncate()
  101. f.writelines(new_lines)
  102. return
  103. def modify_manifest(decompileDir):
  104. ET.register_namespace('android', ANDROID_NS)
  105. xmlparse = os.path.join(decompileDir, 'AndroidManifest.xml')
  106. root_node = ET.parse(xmlparse)
  107. root = root_node.getroot()
  108. name = '{' + ANDROID_NS + '}name'
  109. authorities = '{' + ANDROID_NS + '}foregroundServiceType'
  110. package_name = root.attrib.get('package')
  111. if package_name == None:
  112. return
  113. providers = root.findall('./application/service')
  114. if providers != None:
  115. for provider in providers:
  116. providerName = provider.attrib.get(name)
  117. if 'com.netease.ntunisdk.CcMomentRecordingForegroundService' == providerName:
  118. # 使用try 主要是为了 防止此属性不在时,导致的错误,而程序终止
  119. try:
  120. del provider.attrib[authorities]
  121. except:
  122. ""
  123. root_node.write(xmlparse, 'utf-8')
  124. if __name__ == '__main__':
  125. # script("/tmp/ss", "/mnt/share/yanghuang/python_test/any_dir", {"id": "309"}, "", "")
  126. xml_path = "E:/apk/youhua/jingmenfengyue_216122/res/values/public.xml"
  127. handle_public_xml(xml_path)
  128. # hideSplash("/mnt/share/yanghuang/python_test/cr")