package_web_record.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import file_utils
  2. import package_utils_record
  3. import os.path
  4. import sys
  5. import json
  6. import importlib
  7. from PIL import Image, ImageDraw
  8. def packageWeb():
  9. if len(sys.argv) < 2:
  10. print('argument is missing')
  11. return 1
  12. # 打包配置的路径
  13. packageConfig = sys.argv[1]
  14. jsonText = file_utils.readFile(packageConfig)
  15. config = json.loads(jsonText)
  16. print('*************config*****************')
  17. print(jsonText)
  18. print('************************************')
  19. package(config, config['sdk'])
  20. def package(config, sdk):
  21. print('use script 3rd')
  22. jsonConfig = {}
  23. game = config['app']
  24. subChannel = None
  25. if 'subChannel' in config:
  26. subChannel = config['subChannel']
  27. jsonConfig['subChannel'] = config['subChannel']
  28. # 必须参数
  29. jsonConfig['packageName'] = config['packageName']
  30. jsonConfig['name'] = config['name']
  31. # 可选参数
  32. if 'outName' in config:
  33. jsonConfig['outName'] = config['outName']
  34. if 'outPath' in config:
  35. jsonConfig['outPath'] = config['outPath']
  36. if 'changeIcon' in config:
  37. jsonConfig['changeIcon'] = toBoolean(config['changeIcon'])
  38. if 'addLauncher' in config:
  39. jsonConfig['addLauncher'] = toBoolean(config['addLauncher'])
  40. if 'versionCode' in config:
  41. jsonConfig['versionCode'] = config['versionCode']
  42. if 'versionName' in config:
  43. jsonConfig['versionName'] = config['versionName']
  44. if 'targetSdkVersion' in config:
  45. jsonConfig['targetSdkVersion'] = config['targetSdkVersion']
  46. if 'v2disable' in config:
  47. jsonConfig['v2disable'] = toBoolean(config['v2disable'])
  48. # sdk相关参数
  49. if 'properties' in config:
  50. jsonConfig['properties'] = config['properties']
  51. jsonConfig['bgMusic'] = config['bgMusic']
  52. if 'recordConfig' in config:
  53. setRecordConfig(jsonConfig, config['recordConfig'])
  54. configData = jsonConfig['configData']
  55. properties = jsonConfig['properties']
  56. configData['agent'] = properties['agent']
  57. configData['buildTime'] = properties['version']
  58. configData['appId'] = properties['appid']
  59. configData['appkey'] = properties['appkey']
  60. if 'host' in properties:
  61. configData['host'] = properties['host']
  62. if 'deleteList' in config:
  63. jsonConfig['deleteList'] = config['deleteList']
  64. # 获取sdk相关配置
  65. getSdkConfig(sdk, jsonConfig, config)
  66. # 生成配置文件
  67. createOrUpdateConfigFile(game, sdk, jsonConfig)
  68. # 拷贝资源
  69. copyRes(game, sdk, subChannel, config)
  70. # 打包
  71. package_utils_record.packConsole(game, sdk, subChannel)
  72. def setRecordConfig(config, recordConfig):
  73. configData = None
  74. if 'configData' in config:
  75. configData = config['configData']
  76. configData['bgMusic'] = 'bg_music.mp3'
  77. configData['gameId'] = recordConfig['gameId']
  78. configData['gameName'] = recordConfig['gameName']
  79. configData['gameIcon'] = recordConfig['gameIcon']
  80. configData['gameUrl'] = recordConfig['gameUrl']
  81. configData['skinId'] = recordConfig['skinId']
  82. configData['host'] = recordConfig['host']
  83. configData['appDownUrl'] = recordConfig['appDownUrl']
  84. else:
  85. config['configData'] = {
  86. 'bgMusic':'bg_music.mp3',
  87. 'gameId':recordConfig['gameId'],
  88. 'gameName':recordConfig['gameName'],
  89. 'gameIcon':recordConfig['gameIcon'],
  90. 'gameUrl':recordConfig['gameUrl'],
  91. 'skinId':recordConfig['skinId'],
  92. 'host':recordConfig['host'],
  93. 'appDownUrl':recordConfig['appDownUrl']
  94. }
  95. def toBoolean(booleanStr):
  96. if type(booleanStr) == bool:
  97. return booleanStr
  98. if booleanStr == 'true':
  99. return True
  100. return False
  101. def getSdkConfig(sdk, jsonConfig, config):
  102. scriptPath = os.path.join(file_utils.getCurrentPath(), 'sdk_script')
  103. sdkScript = getScriptMapping(sdk)
  104. targetScript = os.path.join(scriptPath, '%s.py' % sdkScript)
  105. if not os.path.exists(targetScript):
  106. print('%s no exists' % targetScript)
  107. return 0
  108. sys.path.append(scriptPath)
  109. module = importlib.import_module(sdkScript)# 动态导入相应模块
  110. module.getSdkConfig(jsonConfig, config)# 执行脚本功能
  111. sys.path.remove(scriptPath)
  112. def createOrUpdateConfigFile(game, sdk, jsonConfig):
  113. '''
  114. 更新配置文件
  115. '''
  116. if 'subChannel' not in jsonConfig:
  117. return 0
  118. print('createOrUpdateConfigFile ...')
  119. channelPath = file_utils.getChannelPath(game, sdk)
  120. configPath = os.path.join(channelPath, 'config.json')
  121. #configPath = os.path.join(file_utils.getCurrentPath(), 'test', 'test.json')
  122. if os.path.exists(configPath):
  123. # 更新数据
  124. jsonText = file_utils.readFile(configPath)
  125. config = json.loads(jsonText)
  126. count = 0
  127. if type(config) == list:
  128. for item in config:
  129. if item['subChannel'] == jsonConfig['subChannel']:
  130. print('find same config ...')
  131. del config[count]
  132. break
  133. count += 1
  134. config.append(jsonConfig)
  135. createConfigFile(config, configPath)
  136. elif type(config) == dict:
  137. if config['subChannel'] == jsonConfig['subChannel']:
  138. print('find same config ...')
  139. createConfigFile(jsonConfig, configPath)
  140. else:
  141. print('add a new config ...')
  142. config = [config, jsonConfig]
  143. createConfigFile(config, configPath)
  144. else:
  145. print('create a new config ...')
  146. createConfigFile([jsonConfig], configPath)
  147. def createConfigFile(jsonConfig, configPath):
  148. '''
  149. 创建配置文件
  150. '''
  151. jsonStr = json.dumps(jsonConfig, ensure_ascii=False)
  152. print('*************out config*************')
  153. print(jsonStr)
  154. print('************************************')
  155. file_utils.createFile(configPath, jsonStr)
  156. def copyRes(game, sdk, subChannel, config):
  157. '''
  158. 拷贝资源
  159. '''
  160. if subChannel is None:
  161. return 0
  162. channelPath = file_utils.getChannelPath(game, sdk)
  163. subChannelPath = os.path.join(channelPath, subChannel)
  164. if 'icon' in config and os.path.exists(config['icon']):
  165. mipmapSupport = ['mipmap-xhdpi', 'mipmap-xxhdpi', 'mipmap-xxxhdpi']
  166. for mipmap in mipmapSupport:
  167. mipmapPath = os.path.join(subChannelPath, 'icon', mipmap)
  168. if not os.path.exists(mipmapPath):
  169. os.makedirs(mipmapPath)
  170. iconPath = os.path.join(subChannelPath, 'icon', mipmap, 'record_sdk_icon.png')
  171. if not os.path.exists(iconPath):
  172. file_utils.createFile(iconPath, '')
  173. if mipmap == 'mipmap-xhdpi':
  174. size = 96
  175. elif mipmap == 'mipmap-xxhdpi':
  176. size = 144
  177. else:
  178. size = 192
  179. roundRectangleIcon(config['icon'], iconPath, size)
  180. #file_utils.copyFile(config['icon'], iconPath)
  181. if 'splash' in config and os.path.exists(config['splash']):
  182. splashPath = os.path.join(subChannelPath, 'splash', 'drawable-hdpi', 'shanshen_sdk_launcher_bg.jpg')
  183. file_utils.copyFile(config['splash'], splashPath)
  184. if 'bgMusic' in config and os.path.exists(config['bgMusic']):
  185. musicPath = os.path.join(subChannelPath, 'assets', 'bg_music.mp3')
  186. file_utils.copyFile(config['bgMusic'], musicPath)
  187. if 'copyList' in config:
  188. for item in config['copyList']:
  189. if item['toFile'] == '':
  190. continue
  191. if not os.path.exists(item['fromFile']):
  192. continue
  193. toFile = item['toFile']
  194. if toFile[:3] == 'res':
  195. toFile = 'image' + toFile[3:]
  196. resPath = getPackagePath(subChannelPath, toFile)
  197. file_utils.copyFile(item['fromFile'], resPath)
  198. if 'package' in config and os.path.exists(config['package']):
  199. newGameApk = config['package']
  200. gameApk = file_utils.getFullGameApk(game)
  201. file_utils.copyFile(newGameApk, gameApk)
  202. if 'languageList' in config:
  203. for item in config['languageList']:
  204. languagePath = os.path.join(subChannelPath, 'merge', item['apkLanguage'])
  205. file_utils.copyFile(item['language'], languagePath)
  206. def roundRectangleIcon(iconPath, outPath, outSize):
  207. markerPath = os.path.join(file_utils.getFullInternalPath(), 'marker', 'marker.png')
  208. img = Image.open(iconPath).resize((192, 192)).convert("RGBA")
  209. marker = Image.open(markerPath).resize((192, 192)).convert("RGBA")
  210. img.paste(marker, (0, 0), marker)
  211. rad = 30 # 设置半径
  212. circle = Image.new('L', (rad * 2, rad * 2), 0)
  213. draw = ImageDraw.Draw(circle)
  214. draw.ellipse((0, 0, rad * 2, rad * 2), fill=255)
  215. alpha = Image.new('L', img.size, 255)
  216. w, h = img.size
  217. alpha.paste(circle.crop((0, 0, rad, rad)), (0, 0))
  218. alpha.paste(circle.crop((0, rad, rad, rad * 2)), (0, h - rad))
  219. alpha.paste(circle.crop((rad, 0, rad * 2, rad)), (w - rad, 0))
  220. alpha.paste(circle.crop((rad, rad, rad * 2, rad * 2)), (w - rad, h - rad))
  221. img.putalpha(alpha)
  222. img.resize((outSize, outSize)).save(outPath, 'png')
  223. def getPackagePath(basePath, packageName):
  224. '''
  225. 包名对应的目录
  226. '''
  227. packageNameSplit = packageName.replace('\\', '/').split('/')
  228. newPath = basePath
  229. for item in packageNameSplit:
  230. newPath = os.path.join(newPath, item)
  231. return newPath
  232. def getMappingSdk(config):
  233. return config['sdk']
  234. def getScriptMapping(sdk):
  235. return sdk
  236. #packageWeb()