script.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # coding:utf8
  2. __author__ = 'dong'
  3. import os
  4. import logging
  5. import xml.dom.minidom
  6. import re
  7. import shutil
  8. import glob
  9. from xml.etree.ElementTree import ElementTree, Element
  10. import distutils.dir_util
  11. import gw_file_system
  12. import gw_data_center
  13. from xml.etree import ElementTree as ET
  14. ANDROID_NS = 'http://schemas.android.com/apk/res/android'
  15. def script_init(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 == "549":
  18. gw_data_center.pack_small = False
  19. logging.info('script_init: ' + str(channel_sdk_info))
  20. def script_first(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
  21. # logging.info('------------SDK:'+SDK)
  22. # logging.info('------------decompileDir:'+decompileDir)
  23. # logging.info('------------channelSdkInfo:'+str(channelSdkInfo))
  24. # logging.info('------------new_game_channel_info:'+str(new_game_channel_info))
  25. # logging.info('------------gameInfo:'+str(gameInfo))
  26. # logging.info("------rm baidu smali------")
  27. # restdir = decompileDir+"/"+"smali_classes2/com/baidu/gamesdk"
  28. # if os.path.exists(restdir):
  29. # shutil.rmtree(restdir)
  30. logging.info("-----debug exec first func-----")
  31. # game_apk = os.path.join(decompileDir, "game.apk")
  32. game_apk = decompileDir + "/../game.apk"
  33. if os.path.exists(game_apk):
  34. # A3AEECD8
  35. gw_file_system.delete_apk_file_by_aapt(game_apk, "assets/39285EFA.dex")
  36. logging.info("-----debug-----")
  37. def script(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
  38. # s3 = decompileDir + "/smali_classes3"
  39. # A3AEECD8.dex
  40. dex = decompileDir + "../39285EFA.dex"
  41. asserts = os.path.join(decompileDir, "assets/39285EFA.dex")
  42. if os.path.exists(asserts) and os.path.exists(os.path.join(dex)):
  43. distutils.dir_util.copy_tree(dex, asserts)
  44. logging.info("-----debug-----")
  45. # 分包
  46. split_package(decompileDir)
  47. sdk_id = channelSdkInfo['id']
  48. if sdk_id == "323":
  49. modify_manifest(decompileDir)
  50. def script_last(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
  51. logging.info('------------channelSdkInfo:' + str(channelSdkInfo))
  52. if channelSdkInfo['SDKName'] == 'reversefor360':
  53. replace_super_application(decompileDir)
  54. def split_package(decompileDir):
  55. s1 = os.path.join(decompileDir, "smali", "com")
  56. smali_classes2_dir = ''
  57. f_idx = 2
  58. while True:
  59. tmp = gw_file_system.get_full_path(os.path.join(decompileDir, 'smali_classes%d' % f_idx))
  60. tmp = tmp.replace('\\', '/')
  61. tmp = re.sub('/+', '/', tmp)
  62. if os.path.exists(tmp):
  63. f_idx += 1
  64. else:
  65. smali_classes2_dir += tmp
  66. break
  67. # endwhile
  68. os.mkdir(smali_classes2_dir)
  69. s3 = os.path.join(smali_classes2_dir, "com")
  70. list_package = ["netease"]
  71. if not os.path.exists(s3):
  72. distutils.dir_util.mkpath(s3)
  73. move_package(s1, s3, list_package)
  74. def move_package(s1, s3, list_package):
  75. for d in list_package:
  76. src = os.path.join(s1, d)
  77. if os.path.exists(src):
  78. dst = os.path.join(s3, d)
  79. distutils.dir_util.copy_tree(src, dst)
  80. distutils.dir_util.remove_tree(src)
  81. change_map = {".super Landroid/support/multidex/MultiDexApplication;": ".super Lcom/kf/framework/KFApplication;",
  82. "invoke-direct {p0}, Landroid/support/multidex/MultiDexApplication;-><init>()V": "invoke-direct {p0}, Lcom/kf/framework/KFApplication;-><init>()V",
  83. "invoke-super {p0, p1}, Landroid/support/multidex/MultiDexApplication;->attachBaseContext(Landroid/content/Context;)V": "invoke-super {p0, p1}, Lcom/kf/framework/KFApplication;->attachBaseContext(Landroid/content/Context;)V",
  84. "invoke-super {p0}, Landroid/support/multidex/MultiDexApplication;->onCreate()V": "invoke-super {p0}, Lcom/kf/framework/KFApplication;->onCreate()V", }
  85. def replace_super_application(decompileDir):
  86. smali_files = glob.glob(
  87. os.path.join(decompileDir, 'smali*/com/netease/ntunisdk/application/NtSdkApplication.smali'))
  88. if len(smali_files) == 1:
  89. game_application_file = smali_files[0]
  90. print game_application_file
  91. if os.path.isfile(game_application_file):
  92. with open(game_application_file, "r+") as f:
  93. file_str = f.read()
  94. for k, v in change_map.items():
  95. file_str = file_str.replace(k, v)
  96. with open(game_application_file, "w+") as f:
  97. f.write(file_str)
  98. def modify_manifest(decompileDir):
  99. ET.register_namespace('android', ANDROID_NS)
  100. xmlparse = os.path.join(decompileDir, 'AndroidManifest.xml')
  101. root_node = ET.parse(xmlparse)
  102. root = root_node.getroot()
  103. name = '{' + ANDROID_NS + '}name'
  104. authorities = '{' + ANDROID_NS + '}foregroundServiceType'
  105. package_name = root.attrib.get('package')
  106. if package_name == None:
  107. return
  108. providers = root.findall('./application/service')
  109. if providers != None:
  110. for provider in providers:
  111. providerName = provider.attrib.get(name)
  112. if 'com.netease.ntunisdk.CcMomentRecordingForegroundService' == providerName:
  113. # 使用try 主要是为了 防止此属性不在时,导致的错误,而程序终止
  114. try:
  115. del provider.attrib[authorities]
  116. except:
  117. ""
  118. root_node.write(xmlparse, 'utf-8')
  119. if __name__ == '__main__':
  120. replace_super_application("D:\\work\\tx360")