123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- import os.path
- import random
- import file_utils
- import xml_utils
- def change_application(game, sdk, sub_channel, config, target_application):
- """
- 修改最顶级的application
- """
- decompile_path = file_utils.get_decompile_path(game, sdk, sub_channel, config['cache'])
- manifest = os.path.join(decompile_path, 'AndroidManifest.xml')
- application = xml_utils.get_application_attr(manifest, 'name')
- if application is None:
- xml_utils.change_application_attr(manifest, 'name', target_application)
- print('change add application %s' % target_application)
- else:
- smaliPath = os.path.join(decompile_path, 'smali')
- super_application = find_super_application(smaliPath, application)
- print('super application is %s' % super_application)
- if super_application == target_application:
- return
- change_super_application(smaliPath, super_application, target_application.replace('.', '/'))
- print('change super application %s to %s' % (super_application, target_application))
- def change_top_application(game, sdk, sub_channel, config, target_application):
- """
- 修改manifest清单下的application入口。
- PS:例八门渠道,因游戏app继承到渠道app过程中有停住,未进入到渠道app初始化,故改用渠道app为入口,继承原游戏app确保他们的功能正常运行
- """
- decompliePath = file_utils.get_decompile_path(game, sdk, sub_channel, config['cache'])
- manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
- gameApplication = xml_utils.get_application_attr(manifest, 'name')
- print("游戏application" + gameApplication)
- xml_utils.change_application_attr(manifest, 'name', target_application)
- # if application is None:
- # xml_utils.changeApplicationAttr(manifest, 'name', targetApplication)
- #
- # print('change add application %s' % targetApplication)
- # else:
- smaliPath = os.path.join(decompliePath, 'smali')
- superApplication = find_super_application(smaliPath, target_application)
- print('寻找入口application。super application is %s' % superApplication)
- change_super_application(smaliPath, superApplication, gameApplication.replace('.', '/'))
- print('change super application %s to %s' % (target_application, gameApplication))
- def find_super_application(base_path, class_name):
- """
- 获取最顶级的application
- """
- tagSuper = '.super Landroid/app/Application;'
- tag = '.super L'
- applicationFile = file_utils.get_package_path(base_path, class_name) + '.smali'
- topClassName = None
- superClass = None
- with open_file(applicationFile, 'r') as f:
- line = f.readline()
- while line:
- if tagSuper in line:
- topClassName = class_name
- break
- elif tag in line:
- superClass = line[8:-2]
- superClass = superClass.replace('/', '.')
- break
- line = f.readline()
- if topClassName is not None:
- return topClassName
- elif superClass is not None:
- return find_super_application(base_path, superClass)
- def change_super_application(base_path, class_name, target_application):
- """
- 更改application
- """
- applicationFile = file_utils.get_package_path(base_path, class_name) + '.smali'
- # 修改application
- change_supper_application_smali(applicationFile, target_application)
- def change_supper_application_smali(file, application):
- """
- 修改application.smali
- """
- contentSpuer = '.super Landroid/app/Application;'
- constructor = '.method public constructor <init>()V'
- direct = 'invoke-direct'
- invokeSuper = 'invoke-super'
- applicationTag = 'Landroid/app/Application;->'
- newContent = ''
- with open_file(file, 'r+') as f:
- line = f.readline()
- isConstructor = False
- while line:
- if contentSpuer in line:
- newContent += '.super L%s;\n' % application
- elif invokeSuper in line and applicationTag in line:
- newContent += line.replace('android/app/Application', application)
- elif isConstructor and direct in line and applicationTag in line:
- newContent += line.replace('android/app/Application', application)
- isConstructor = False
- else:
- if constructor in line:
- isConstructor = True
- newContent += line
- line = f.readline()
- f.seek(0, 0)
- # 清空内容
- f.truncate()
- f.write(newContent)
- return 0
- def get_random_number(size):
- content = ""
- for i in range(6):
- ch = chr(random.randrange(ord('0'), ord('9') + 1))
- content += ch
- return content
- def open_file(file, mode):
- return file_utils.open_file(file, mode)
|