sdk_script.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import file_utils
  2. import xml_utils
  3. import package_utils
  4. import os.path
  5. import xml.etree.ElementTree as ET
  6. namespaces = {'android' : 'http://schemas.android.com/apk/res/android'}
  7. encoding = 'UTF-8'
  8. def execute(game, sdk, config):
  9. if not checkConfig(config):
  10. return 1
  11. subChannel = config['subChannel']
  12. createJmhyProperties(game, sdk, subChannel, config)
  13. createProperties(game, sdk, subChannel, config)
  14. if config['addLauncher']:
  15. ret = addLauncher(game, sdk, subChannel, config)
  16. if ret:
  17. return ret
  18. def checkConfig(config):
  19. '''
  20. 检查配置
  21. '''
  22. if 'properties' not in config:
  23. print('properties not exists in config')
  24. return False
  25. properties = config['properties']
  26. if 'agent' not in properties or 'version' not in properties:
  27. print('agent or version not exists in properties')
  28. return False
  29. '''if 'appid' not in config or 'appkey' not in config:
  30. print('appid or appkey not exists in config')
  31. return False'''
  32. return True
  33. def createJmhyProperties(game, sdk, subChannel, config):
  34. '''
  35. 创建jmhy.properties
  36. '''
  37. print('create jmhy.properties')
  38. propValue = config['properties']
  39. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  40. properties = os.path.join(decompliePath, 'assets', 'jmhy.properties')
  41. content = ''
  42. for key in propValue:
  43. content = '%s%s=%s\n' % (content, key, propValue[key])
  44. file_utils.createFile(properties, content)
  45. return 0
  46. def addLauncher(game, sdk, subChannel, config):
  47. '''
  48. 添加启动图
  49. '''
  50. channelPath = file_utils.getSubChannelPath(game, sdk, subChannel)
  51. splashPath = os.path.join(channelPath, 'splash')
  52. if len(os.listdir(splashPath)) == 0:
  53. print('dir splash is empty')
  54. return 0
  55. print('add launcher...')
  56. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  57. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  58. activity = xml_utils.getLauncherActivityName(manifest)
  59. if activity == 'com.inner.sdk.ui.YsdkLauncherActivity':
  60. print('add launcher already exist...')
  61. return 1
  62. # 添加关联资源
  63. internalPath = file_utils.getFullInternalPath()
  64. ret = package_utils.copyAppResWithType(decompliePath, internalPath, 'launcher_res')
  65. if ret:
  66. return ret
  67. # 修改主文件信息
  68. print('change launcher config...')
  69. activity = xml_utils.removeLauncherActivity(manifest)
  70. xml_utils.addLauncherActivity(manifest, config['screenOrientation'], 'com.inner.sdk.ui.YsdkLauncherActivity')
  71. # 修改跳转的
  72. launcherActivity = os.path.join(decompliePath, 'smali', 'com', 'inner', 'sdk', 'ui', 'YsdkLauncherActivity.smali')
  73. file_utils.replaceContent(launcherActivity, '{class}', activity)
  74. print('change launcher %s to %s' % (activity, 'com.inner.sdk.ui.YsdkLauncherActivity'))
  75. if 'launcherTime' in config:
  76. timeHex = package_utils.formatHex(config['launcherTime'])
  77. file_utils.replaceContent(launcherActivity, '0x0BB8', timeHex)
  78. return 0
  79. def changeLauncherLaunchMode(manifest):
  80. '''
  81. 修改启动的activity的launchMode
  82. '''
  83. for key in namespaces:
  84. ET.register_namespace(key, namespaces[key])
  85. tree = ET.parse(manifest)
  86. root = tree.getroot()
  87. launcherActivity = xml_utils.getLauncherActivity(root)
  88. if launcherActivity is None:
  89. return 1
  90. attrName = xml_utils.getNamespacesFormat('android:launchMode', namespaces)
  91. if attrName not in launcherActivity.attrib or 'standard' == launcherActivity.attrib[attrName]:
  92. launcherActivity.attrib[attrName] = 'singleTop'
  93. tree.write(manifest, encoding)
  94. return 0
  95. def getScreenOrientation(game, sdk, subChannel, config):
  96. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  97. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  98. return getLauncherAttr(manifest, 'screenOrientation')
  99. def getLauncherAttr(manifest, attrType):
  100. '''
  101. 获取启动的activity的属性
  102. '''
  103. for key in namespaces:
  104. ET.register_namespace(key, namespaces[key])
  105. tree = ET.parse(manifest)
  106. root = tree.getroot()
  107. launcherActivity = xml_utils.getLauncherActivity(root)
  108. if launcherActivity is None:
  109. return None
  110. attrName = xml_utils.getNamespacesFormat('android:%s' % attrType, namespaces)
  111. if attrName in launcherActivity.attrib:
  112. return launcherActivity.attrib[attrName]
  113. return None
  114. def changePlaceholders(game, sdk, subChannel, config):
  115. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  116. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  117. file_utils.replaceContent(manifest, '${screenOrientation}', config['screenOrientation'])