sdk_script.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. ret = deleteSplash(game, sdk, subChannel, config)
  21. if ret:
  22. return ret
  23. return copyWechatCode(game, sdk, subChannel, config)
  24. def checkConfig(config):
  25. '''
  26. 检查配置
  27. '''
  28. if 'properties' not in config:
  29. print('properties not exists in config')
  30. return False
  31. if 'ysdk' not in config:
  32. print('ysdk not exists in config')
  33. return False
  34. properties = config['properties']
  35. if 'agent' not in properties or 'version' not in properties:
  36. print('agent or version not exists in properties')
  37. return False
  38. '''if 'appid' not in config or 'appkey' not in config:
  39. print('appid or appkey not exists in config')
  40. return False'''
  41. return True
  42. def createJmhyProperties(game, sdk, subChannel, config):
  43. '''
  44. 创建jmhy.properties
  45. '''
  46. print('create jmhy.properties')
  47. propValue = config['properties']
  48. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  49. properties = os.path.join(decompliePath, 'assets', 'jmhy.properties')
  50. content = ''
  51. for key in propValue:
  52. content = '%s%s=%s\n' % (content, key, propValue[key])
  53. file_utils.createFile(properties, content)
  54. return 0
  55. def createProperties(game, sdk, subChannel, config):
  56. '''
  57. 创建ysdkconf.ini
  58. '''
  59. print('create ysdkconf.ini')
  60. ysdkConfig = config['ysdk']
  61. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  62. properties = os.path.join(decompliePath, 'assets', 'ysdkconf.ini')
  63. content = ''
  64. for key in ysdkConfig:
  65. content = '%s%s=%s\n' % (content, key, ysdkConfig[key])
  66. file_utils.createFile(properties, content)
  67. return 0
  68. def createSdkProperties(game, sdk, subChannel, config):
  69. '''
  70. 创建sdk.properties
  71. '''
  72. print('create sdk.properties')
  73. zysdkConfig = config['zysdk_properties']
  74. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  75. properties = os.path.join(decompliePath, 'assets', 'sdk.properties')
  76. content = ''
  77. for key in zysdkConfig:
  78. content = '%s%s=%s\n' % (content, key, zysdkConfig[key])
  79. file_utils.createFile(properties, content)
  80. return 0
  81. def copyWechatCode(game, sdk, subChannel, config):
  82. '''
  83. 拷贝微信sdk的代码
  84. '''
  85. print('copy WXEntryActivity.smali')
  86. sdkPath = file_utils.getFullSDKPath(sdk)
  87. WXEntryActivity = 'WXEntryActivity.smali'
  88. wxFile = os.path.join(sdkPath, 'wxSmali', WXEntryActivity)
  89. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  90. smaliPath = os.path.join(decompliePath, 'smali')
  91. targetPath = file_utils.getPackagePath(smaliPath, config['packageName'])
  92. targetFile = os.path.join(targetPath, 'wxapi', WXEntryActivity)
  93. ret = file_utils.copyFile(wxFile, targetFile)
  94. if ret:
  95. return ret
  96. file_utils.replaceContent(targetFile, '${packageName}', config['packageName'].replace('.', '/'))
  97. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  98. changeLauncherLaunchMode(manifest)
  99. xml_utils.changeLauncherAttr(manifest, 'configChanges', 'orientation|screenSize|keyboardHidden')
  100. return 0
  101. def deleteSplash(game, sdk, subChannel, config):
  102. '''
  103. 删除闪屏
  104. '''
  105. if game != 'wzjh':
  106. return 0
  107. channelPath = file_utils.getSubChannelPath(game, sdk, subChannel)
  108. print('delete Splash...')
  109. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  110. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  111. activity = xml_utils.getLauncherActivityName(manifest)
  112. activity = xml_utils.removeLauncherActivity(manifest)
  113. xml_utils.deleteActivityByName(manifest,'com.hugenstar.nanobox.NaNoUnityContext')
  114. addLauncherActivity(manifest, config['screenOrientation'], 'com.hugenstar.nanobox.NaNoUnityContext')
  115. return 0
  116. def changeLauncherLaunchMode(manifest):
  117. '''
  118. 修改启动的activity的launchMode
  119. '''
  120. for key in namespaces:
  121. ET.register_namespace(key, namespaces[key])
  122. tree = ET.parse(manifest)
  123. root = tree.getroot()
  124. launcherActivity = xml_utils.getLauncherActivity(root)
  125. if launcherActivity is None:
  126. return 1
  127. attrName = xml_utils.getNamespacesFormat('android:launchMode', namespaces)
  128. if attrName not in launcherActivity.attrib or 'standard' == launcherActivity.attrib[attrName]:
  129. launcherActivity.attrib[attrName] = 'singleTop'
  130. tree.write(manifest, encoding)
  131. return 0
  132. def getScreenOrientation(game, sdk, subChannel, config):
  133. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  134. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  135. return getLauncherAttr(manifest, 'screenOrientation')
  136. def getLauncherAttr(manifest, attrType):
  137. '''
  138. 获取启动的activity的属性
  139. '''
  140. for key in namespaces:
  141. ET.register_namespace(key, namespaces[key])
  142. tree = ET.parse(manifest)
  143. root = tree.getroot()
  144. launcherActivity = xml_utils.getLauncherActivity(root)
  145. if launcherActivity is None:
  146. return None
  147. attrName = xml_utils.getNamespacesFormat('android:%s' % attrType, namespaces)
  148. if attrName in launcherActivity.attrib:
  149. return launcherActivity.attrib[attrName]
  150. return None
  151. def changePlaceholders(game, sdk, subChannel, config):
  152. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  153. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  154. file_utils.replaceContent(manifest, '${screenOrientation}', config['screenOrientation'])
  155. def addLauncherActivity(manifest, screenOrientation, activity):
  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. # activity
  164. '''<activity android:name=".LauncherActivity"
  165. android:theme="@style/LauncherStyle">
  166. <intent-filter>
  167. <action android:name="android.intent.action.MAIN" />
  168. <category android:name="android.intent.category.LAUNCHER" />
  169. </intent-filter>
  170. </activity>'''
  171. activity = ET.Element('activity', {'android:name' : activity,
  172. 'android:theme' : '@android:style/Theme.Holo.Light.NoActionBar.Fullscreen',
  173. 'android:launchMode' : 'singleTop',
  174. 'android:configChanges' : 'orientation|screenSize|keyboardHidden',
  175. 'android:screenOrientation' : screenOrientation})
  176. intent = ET.Element('intent-filter')
  177. action = ET.Element('action', {'android:name' : 'android.intent.action.MAIN'})
  178. category = ET.Element('category', {'android:name' : 'android.intent.category.LAUNCHER'})
  179. intent.append(action)
  180. intent.append(category)
  181. activity.append(intent)
  182. intent2 = ET.Element('intent-filter')
  183. category = ET.Element('category', {'android:name' : 'android.intent.category.LEANBACK_LAUNCHER'})
  184. intent2.append(category)
  185. activity.append(intent2)
  186. application = root.find('application')
  187. application.insert(0, activity)
  188. tree.write(manifest, encoding)