package_web_record.py 11 KB

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