script.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. ANDROID_NS = 'http://schemas.android.com/apk/res/android'
  10. def script_init(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
  11. gw_data_center.pack_small = False
  12. logging.info('script_init: ' + str(channel_sdk_info))
  13. def script_pre(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
  14. return
  15. def script_pre(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
  16. sdk_id = channel_sdk_info['id']
  17. if sdk_id == "512":
  18. modify_manifest(extract_dir)
  19. def intoFirstLine(target, repl, cont):
  20. # srcStr = "onBackPressed"
  21. # regexp = '.method (?:public|protected) +?'+target+"\([^.]+?\.locals +[0-9]{1,2}"
  22. regexp = '.method (?:public|protected) +?' + target + "\([^.]+?\.locals +([0-9]{1,2})"
  23. # print(regexp)
  24. reObj = re.compile(regexp)
  25. matchO = reObj.search(cont)
  26. if matchO:
  27. # print(matchO.group(0))
  28. methodHead = matchO.group(0)
  29. if matchO.group(1) == "0":
  30. methodHead = re.sub("locals +0", "locals 1", methodHead)
  31. repl = methodHead + r'\n\n ' + repl + r'\n\n'
  32. cont = reObj.sub(repl, cont)
  33. else:
  34. # todo call .super method
  35. cont += r'.method protected ' + target + r'()V\n\n .locals 0\n\n ' + repl + r'\n\n .end method\n'
  36. return cont
  37. def fixLifecycleMethod(preDir):
  38. for tfile in glob.glob(preDir + "/" + "smali*/org/bojoy/publish/PublishActivity.smali"):
  39. if os.path.isfile(tfile):
  40. with open(tfile, 'r+') as f:
  41. cont = f.read()
  42. changes = {
  43. "onCreate": "invoke-static {p0}, Lcom/baidu/gamesdk/BDGameSDK;->setContext(Landroid/app/Activity;)V",
  44. }
  45. for k, v in changes.iteritems():
  46. cont = intoFirstLine(k, v, cont)
  47. f.seek(0)
  48. f.write(cont)
  49. f.close()
  50. def handle_public_xml(public_xml_path):
  51. if not os.path.exists(public_xml_path):
  52. logging.info('public_xml is null: ' + public_xml_path)
  53. return
  54. new_lines = []
  55. with open(public_xml_path, 'r+') as f:
  56. for line in f.readlines():
  57. # l = line.strip()
  58. if line.find("qihoo_game_sdk_authenticator") > -1:
  59. continue
  60. new_lines.append(line)
  61. f.seek(0)
  62. f.truncate()
  63. f.writelines(new_lines)
  64. return
  65. def modify_manifest(decompileDir):
  66. ET.register_namespace('android', ANDROID_NS)
  67. xmlparse = os.path.join(decompileDir, 'AndroidManifest.xml')
  68. root_node = ET.parse(xmlparse)
  69. root = root_node.getroot()
  70. name = '{' + ANDROID_NS + '}name'
  71. authorities = '{' + ANDROID_NS + '}foregroundServiceType'
  72. package_name = root.attrib.get('package')
  73. if package_name == None:
  74. return
  75. providers = root.findall('./application/service')
  76. if providers != None:
  77. for provider in providers:
  78. providerName = provider.attrib.get(name)
  79. if 'com.netease.ntunisdk.CcMomentRecordingForegroundService' == providerName:
  80. # 使用try 主要是为了 防止此属性不在时,导致的错误,而程序终止
  81. try:
  82. del provider.attrib[authorities]
  83. except:
  84. ""
  85. root_node.write(xmlparse, 'utf-8')
  86. if __name__ == '__main__':
  87. # script("/tmp/ss", "/mnt/share/yanghuang/python_test/any_dir", {"id": "309"}, "", "")
  88. xml_path = "E:/apk/youhua/jingmenfengyue_216122/res/values/public.xml"
  89. handle_public_xml(xml_path)
  90. # hideSplash("/mnt/share/yanghuang/python_test/cr")