package_web_record.py 10 KB

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