sdk_script.py 6.8 KB

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