sdk_script.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. createSdkProperties(game, sdk, subChannel, config)
  15. orientation = getScreenOrientation(game, sdk, subChannel, config)
  16. if orientation is None:
  17. orientation = 'landscape'
  18. config['screenOrientation'] = orientation
  19. changePlaceholders(game, sdk, subChannel, config)
  20. if config['addLauncher']:
  21. ret = addLauncher(game, sdk, subChannel, config)
  22. if ret:
  23. return ret
  24. return copyWechatCode(game, sdk, subChannel, config)
  25. def checkConfig(config):
  26. '''
  27. 检查配置
  28. '''
  29. if 'properties' not in config:
  30. print('properties not exists in config')
  31. return False
  32. if 'ysdk' not in config:
  33. print('ysdk not exists in config')
  34. return False
  35. properties = config['properties']
  36. if 'agent' not in properties or 'version' not in properties:
  37. print('agent or version not exists in properties')
  38. return False
  39. '''if 'appid' not in config or 'appkey' not in config:
  40. print('appid or appkey not exists in config')
  41. return False'''
  42. return True
  43. def createJmhyProperties(game, sdk, subChannel, config):
  44. '''
  45. 创建jmhy.properties
  46. '''
  47. print('create jmhy.properties')
  48. propValue = config['properties']
  49. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  50. properties = os.path.join(decompliePath, 'assets', 'jmhy.properties')
  51. content = ''
  52. for key in propValue:
  53. content = '%s%s=%s\n' % (content, key, propValue[key])
  54. file_utils.createFile(properties, content)
  55. return 0
  56. def createProperties(game, sdk, subChannel, config):
  57. '''
  58. 创建ysdkconf.ini
  59. '''
  60. print('create ysdkconf.ini')
  61. ysdkConfig = config['ysdk']
  62. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  63. properties = os.path.join(decompliePath, 'assets', 'ysdkconf.ini')
  64. content = ''
  65. for key in ysdkConfig:
  66. content = '%s%s=%s\n' % (content, key, ysdkConfig[key])
  67. file_utils.createFile(properties, content)
  68. return 0
  69. def createSdkProperties(game, sdk, subChannel, config):
  70. '''
  71. 创建sdk.properties
  72. '''
  73. print('create sdk.properties')
  74. zysdkConfig = config['zysdk_properties']
  75. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  76. properties = os.path.join(decompliePath, 'assets', 'sdk.properties')
  77. content = ''
  78. for key in zysdkConfig:
  79. content = '%s%s=%s\n' % (content, key, zysdkConfig[key])
  80. file_utils.createFile(properties, content)
  81. return 0
  82. def copyWechatCode(game, sdk, subChannel, config):
  83. '''
  84. 拷贝微信sdk的代码
  85. '''
  86. print('copy WXEntryActivity.smali')
  87. sdkPath = file_utils.getFullSDKPath(sdk)
  88. WXEntryActivity = 'WXEntryActivity.smali'
  89. wxFile = os.path.join(sdkPath, 'smali', WXEntryActivity)
  90. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  91. smaliPath = os.path.join(decompliePath, 'smali')
  92. targetPath = file_utils.getPackagePath(smaliPath, config['packageName'])
  93. targetFile = os.path.join(targetPath, 'wxapi', WXEntryActivity)
  94. ret = file_utils.copyFile(wxFile, targetFile)
  95. if ret:
  96. return ret
  97. file_utils.replaceContent(targetFile, '${packageName}', config['packageName'].replace('.', '/'))
  98. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  99. changeLauncherLaunchMode(manifest)
  100. xml_utils.changeLauncherAttr(manifest, 'configChanges', 'orientation|screenSize|keyboardHidden')
  101. return 0
  102. def addLauncher(game, sdk, subChannel, config):
  103. '''
  104. 添加启动图
  105. '''
  106. channelPath = file_utils.getSubChannelPath(game, sdk, subChannel)
  107. splashPath = os.path.join(channelPath, 'splash')
  108. if len(os.listdir(splashPath)) == 0:
  109. print('dir splash is empty')
  110. return 0
  111. print('add launcher...')
  112. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  113. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  114. activity = xml_utils.getLauncherActivityName(manifest)
  115. if activity == 'com.inner.sdk.ui.YsdkLauncherActivity':
  116. print('add launcher already exist...')
  117. return 1
  118. # 添加关联资源
  119. internalPath = file_utils.getFullInternalPath()
  120. ret = package_utils.copyAppResWithType(decompliePath, internalPath, 'launcher_res')
  121. if ret:
  122. return ret
  123. # 修改主文件信息
  124. print('change launcher config...')
  125. activity = xml_utils.removeLauncherActivity(manifest)
  126. xml_utils.addLauncherActivity(manifest, config['screenOrientation'], 'com.inner.sdk.ui.YsdkLauncherActivity')
  127. # 修改跳转的
  128. launcherActivity = os.path.join(decompliePath, 'smali', 'com', 'inner', 'sdk', 'ui', 'YsdkLauncherActivity.smali')
  129. file_utils.replaceContent(launcherActivity, '{class}', activity)
  130. print('change launcher %s to %s' % (activity, 'com.inner.sdk.ui.YsdkLauncherActivity'))
  131. if 'launcherTime' in config:
  132. timeHex = package_utils.formatHex(config['launcherTime'])
  133. file_utils.replaceContent(launcherActivity, '0x0BB8', timeHex)
  134. return 0
  135. def changeLauncherLaunchMode(manifest):
  136. '''
  137. 修改启动的activity的launchMode
  138. '''
  139. for key in namespaces:
  140. ET.register_namespace(key, namespaces[key])
  141. tree = ET.parse(manifest)
  142. root = tree.getroot()
  143. launcherActivity = xml_utils.getLauncherActivity(root)
  144. if launcherActivity is None:
  145. return 1
  146. attrName = xml_utils.getNamespacesFormat('android:launchMode', namespaces)
  147. if attrName not in launcherActivity.attrib or 'standard' == launcherActivity.attrib[attrName]:
  148. launcherActivity.attrib[attrName] = 'singleTop'
  149. tree.write(manifest, encoding)
  150. return 0
  151. def getScreenOrientation(game, sdk, subChannel, config):
  152. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  153. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  154. return getLauncherAttr(manifest, 'screenOrientation')
  155. def getLauncherAttr(manifest, attrType):
  156. '''
  157. 获取启动的activity的属性
  158. '''
  159. for key in namespaces:
  160. ET.register_namespace(key, namespaces[key])
  161. tree = ET.parse(manifest)
  162. root = tree.getroot()
  163. launcherActivity = xml_utils.getLauncherActivity(root)
  164. if launcherActivity is None:
  165. return None
  166. attrName = xml_utils.getNamespacesFormat('android:%s' % attrType, namespaces)
  167. if attrName in launcherActivity.attrib:
  168. return launcherActivity.attrib[attrName]
  169. return None
  170. def changePlaceholders(game, sdk, subChannel, config):
  171. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  172. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  173. file_utils.replaceContent(manifest, '${screenOrientation}', config['screenOrientation'])