sdk_script.py 4.6 KB

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