sdk_script.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import file_utils
  2. import common_utils
  3. import xml_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. ret = deleteSplash(game, sdk, subChannel, config)
  14. addLauncher(game, sdk, subChannel, config)
  15. common_utils.changeApplication(game, sdk, subChannel, config, 'com.inner.sdk.ui.QuickApplication')
  16. return 0
  17. def checkConfig(config):
  18. '''
  19. 检查配置
  20. '''
  21. if 'properties' not in config:
  22. print('properties not exists in config')
  23. return False
  24. properties = config['properties']
  25. if 'agent' not in properties or 'version' not in properties:
  26. print('agent or version not exists in properties')
  27. return False
  28. '''if 'appid' not in config or 'appkey' not in config:
  29. print('appid or appkey not exists in config')
  30. return False'''
  31. return True
  32. def createJmhyProperties(game, sdk, subChannel, config):
  33. '''
  34. 创建jmhy.properties
  35. '''
  36. print('create jmhy.properties')
  37. propValue = config['properties']
  38. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  39. properties = os.path.join(decompliePath, 'assets', 'jmhy.properties')
  40. content = ''
  41. for key in propValue:
  42. content = '%s%s=%s\n' % (content, key, propValue[key])
  43. file_utils.createFile(properties, content)
  44. return 0
  45. def addLauncher(game, sdk, subChannel, config):
  46. '''
  47. 添加启动图
  48. '''
  49. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  50. # 修改主文件信息
  51. print('change launcher config...')
  52. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  53. activity = xml_utils.getLauncherActivityName(manifest)
  54. if activity == 'com.inner.sdk.ui.LauncherActivity':
  55. print('add launcher already exist...')
  56. return 1
  57. orientation = xml_utils.getScreenOrientation(manifest)
  58. activity = xml_utils.removeLauncherActivity(manifest)
  59. addLauncherActivity(manifest, orientation)
  60. # 修改跳转的
  61. launcherActivity = os.path.join(decompliePath, 'smali', 'com', 'inner', 'sdk', 'ui', 'LauncherActivity.smali')
  62. file_utils.replaceContent(launcherActivity, '{class}', activity)
  63. print('change launcher %s to %s' % (activity, 'com.inner.sdk.ui.LauncherActivity'))
  64. #config['oldLauncher'] = activity
  65. return 0
  66. def addLauncherActivity(manifest, screenOrientation):
  67. '''
  68. 添加启动的activity
  69. '''
  70. for key in namespaces:
  71. ET.register_namespace(key, namespaces[key])
  72. tree = ET.parse(manifest)
  73. root = tree.getroot()
  74. # activity
  75. activity = ET.Element('activity', {'android:name' : 'com.inner.sdk.ui.LauncherActivity',
  76. 'android:configChanges' : 'orientation|screenSize|keyboardHidden',
  77. 'android:screenOrientation' : screenOrientation})
  78. intent = ET.Element('intent-filter')
  79. action = ET.Element('action', {'android:name' : 'android.intent.action.MAIN'})
  80. category = ET.Element('category', {'android:name' : 'android.intent.category.LAUNCHER'})
  81. intent.append(action)
  82. intent.append(category)
  83. activity.append(intent)
  84. application = root.find('application')
  85. application.insert(0, activity)
  86. tree.write(manifest, encoding)
  87. def deleteSplash(game, sdk, subChannel, config):
  88. '''
  89. 删除闪屏
  90. '''
  91. if game != 'wzjh':
  92. return 0
  93. channelPath = file_utils.getSubChannelPath(game, sdk, subChannel)
  94. print('delete Splash...')
  95. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  96. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  97. activity = xml_utils.getLauncherActivityName(manifest)
  98. activity = xml_utils.removeLauncherActivity(manifest)
  99. xml_utils.deleteActivityByName(manifest,'com.hugenstar.nanobox.NaNoUnityContext')
  100. addGameLauncherActivity(manifest, config['screenOrientation'], 'com.hugenstar.nanobox.NaNoUnityContext')
  101. return 0
  102. def addGameLauncherActivity(manifest, screenOrientation, activity):
  103. '''
  104. 修改特定游戏启动的activity
  105. '''
  106. for key in namespaces:
  107. ET.register_namespace(key, namespaces[key])
  108. tree = ET.parse(manifest)
  109. root = tree.getroot()
  110. # activity
  111. '''<activity android:name=".LauncherActivity"
  112. android:theme="@style/LauncherStyle">
  113. <intent-filter>
  114. <action android:name="android.intent.action.MAIN" />
  115. <category android:name="android.intent.category.LAUNCHER" />
  116. </intent-filter>
  117. </activity>'''
  118. activity = ET.Element('activity', {'android:name' : activity,
  119. 'android:theme' : '@android:style/Theme.Holo.Light.NoActionBar.Fullscreen',
  120. 'android:launchMode' : 'singleTop',
  121. 'android:configChanges' : 'orientation|screenSize|keyboardHidden',
  122. 'android:screenOrientation' : screenOrientation})
  123. intent = ET.Element('intent-filter')
  124. action = ET.Element('action', {'android:name' : 'android.intent.action.MAIN'})
  125. category = ET.Element('category', {'android:name' : 'android.intent.category.LAUNCHER'})
  126. intent.append(action)
  127. intent.append(category)
  128. activity.append(intent)
  129. intent2 = ET.Element('intent-filter')
  130. category = ET.Element('category', {'android:name' : 'android.intent.category.LEANBACK_LAUNCHER'})
  131. intent2.append(category)
  132. activity.append(intent2)
  133. application = root.find('application')
  134. application.insert(0, activity)
  135. tree.write(manifest, encoding)