package_web_record.py 9.2 KB

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