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