sdk_script.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import os.path
  2. import xml.etree.ElementTree as ET
  3. import file_utils
  4. import xml_utils
  5. namespaces = {'android': 'http://schemas.android.com/apk/res/android'}
  6. encoding = 'UTF-8'
  7. def execute(game, sdk, config):
  8. if not check_config(config):
  9. return 1
  10. sub_channel = config['subChannel']
  11. create_qingshi_properties(game, sdk, sub_channel, config)
  12. modify_game_activity(game, sdk, sub_channel, config)
  13. def check_config(config) -> bool:
  14. """
  15. 检查配置
  16. """
  17. if 'properties' not in config:
  18. print('properties not exists in config')
  19. return False
  20. properties = config['properties']
  21. if 'QS_PACKAGE_VERSION' not in properties:
  22. print('QS_PACKAGE_VERSION not exists in properties')
  23. return False
  24. if 'QS_CAMPAIGN_ID' not in properties:
  25. print('QS_CAMPAIGN_ID not exists in properties')
  26. return False
  27. if 'QS_APP_ID' not in properties:
  28. print('QS_APP_ID not exists in properties')
  29. return False
  30. if 'QS_APP_KEY' not in properties:
  31. print('QS_APP_KEY not exists in properties')
  32. return False
  33. if 'QS_ONLINE_ENV' not in properties:
  34. print('QS_ONLINE_ENV not exists in properties')
  35. return False
  36. return True
  37. def create_qingshi_properties(game, sdk, sub_channel, config):
  38. """
  39. 创建/assets/qs_game/qs_cfg.properties
  40. """
  41. print('create /assets/qs_game/qs_cfg.properties')
  42. add_analytics_id(config)
  43. prop_value = config['properties']
  44. decompile_path = file_utils.get_decompile_path(game, sdk, sub_channel, config['cache'])
  45. qs_game_path = os.path.join(decompile_path, 'assets', 'qs_game')
  46. properties = os.path.join(qs_game_path, 'qs_cfg.properties')
  47. if os.path.exists(properties):
  48. os.remove(properties)
  49. content = ''
  50. for key in prop_value:
  51. content = '%s%s=%s\n' % (content, key, prop_value[key])
  52. print('write properties : %s = %s' % (key, prop_value[key]))
  53. file_utils.create_file(properties, content)
  54. return 0
  55. def modify_game_activity(game, sdk, sub_channel, config):
  56. if 'packageName' not in config or 'oldPackageName' not in config:
  57. print('modify game activity action failed!!!')
  58. return
  59. package_name = config['packageName']
  60. old_package_name = config['oldPackageName']
  61. decompile_path = file_utils.get_decompile_path(game, sdk, sub_channel, config['cache'])
  62. manifest = os.path.join(decompile_path, 'AndroidManifest.xml')
  63. attr_name = xml_utils.get_namespaces_format('android:name', namespaces)
  64. for key in namespaces:
  65. ET.register_namespace(key, namespaces[key])
  66. target_tree = ET.parse(manifest)
  67. target_root = target_tree.getroot()
  68. appNode = target_root.find('application')
  69. activitys = appNode.findall('activity')
  70. for activity in activitys:
  71. intent_filter = activity.find('intent-filter')
  72. if intent_filter is not None:
  73. action = intent_filter.find('action')
  74. if action is not None and action.attrib[attr_name] == old_package_name:
  75. action.attrib[attr_name] = package_name
  76. break
  77. target_tree.write(manifest, encoding)
  78. def add_analytics_id(config):
  79. if 'logSdk' not in config:
  80. return
  81. print('add analytics id...')
  82. log_sdk = config['logSdk']
  83. if log_sdk[0] == 'jrtt':
  84. config['properties']['QS_ANALYTICS_ID'] = '1'
  85. elif log_sdk == 'gdt':
  86. pass
  87. elif log_sdk == 'ks':
  88. pass
  89. elif log_sdk == 'uc':
  90. pass
  91. elif log_sdk == 'bd':
  92. pass