├── LICENSE ├── README.md └── build ├── package.py └── 上传游戏资源到正式服.bat /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 shangdibaozi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 功能 2 | - 资源和代码分离 3 | - 重新构建微信配置文件 4 | - 支持资源热更新 5 | - 解决资源冲突问题 6 | - 图片压缩 7 | 8 | # 环境 9 | - 7z.exe 10 | - python3 11 | 12 | # 使用方法 13 | 将build目录下的文件拷贝到cocos creator项目的build目录下。点击“上传资源到正式服.bat”即可执行一键部署。 14 | -------------------------------------------------------------------------------- /build/package.py: -------------------------------------------------------------------------------- 1 | import shutil 2 | import os 3 | import time 4 | import json 5 | import sys 6 | import tinify 7 | import os.path 8 | import paramiko 9 | import re 10 | 11 | def renameMainJs(cwd): 12 | path = "./wechatgame" 13 | for item in os.listdir(path): 14 | if 'main' in item: 15 | fullPath = os.path.join(cwd, path, item) 16 | os.rename(fullPath, os.path.join(cwd, path, 'main.js')) 17 | 18 | def buildSettingJson(cwd): 19 | jsonStr = None 20 | path = "./wechatgame/src" 21 | fullPath = None 22 | for item in os.listdir(path): 23 | fullPath = os.path.join(cwd, path, item) 24 | if "settings" in item and os.path.isfile(fullPath): 25 | break 26 | with open(fullPath, 'r', encoding='utf-8') as rFile: 27 | lines = rFile.readlines() 28 | newLines = [] 29 | for line in lines: 30 | if '\n' in line: 31 | line = line.strip('\n') 32 | line = line.strip() 33 | newLines.append(line) 34 | 35 | pattern = re.compile("[0-9a-zA-Z]") 36 | result = [] 37 | for line in newLines: 38 | if len(line) > 0 and line[0] != '"' and ':' in line and pattern.match(line[0]): 39 | line = '"' + line 40 | index = line.index(':') 41 | line = line[:index] + '"' + line[index:] 42 | result.append(line) 43 | 44 | jsonStr = ''.join(result) 45 | jsonStr = jsonStr.strip('window._CCSettings = ').strip(';') 46 | 47 | os.remove(fullPath) 48 | 49 | with open(os.path.join(cwd, path, 'settings.json'), 'w', encoding='utf-8') as wFile: 50 | wFile.write(jsonStr) 51 | 52 | 53 | 54 | 55 | def copyFolder(folderName): 56 | src = "./wechatgame/res" 57 | # dst = "./wechatgameres/%s/res" % time.strftime("%Y_%m_%d", time.localtime()) 58 | dst = "./wechatgameres/%s/res" % folderName 59 | 60 | # 判断文件夹是否存在 61 | if os.path.exists(dst): 62 | # 删除文件夹 63 | shutil.rmtree(dst) 64 | 65 | if os.path.exists(src): 66 | # 剪切文件夹 67 | # 如果在使用命令的过程中,微信开发者工具没有关闭,会引发“src目录不是空的”的异常 68 | shutil.move(src, dst) 69 | 70 | # 移动settings.json 71 | path = os.path.join(os.getcwd(), "./wechatgame/src/settings.json") 72 | shutil.move(path, dst) 73 | 74 | # 拷贝shader1.png和startBtn.png 75 | # shutil.copy(os.path.join("./wechatgameres", 'share1.png'), dst) 76 | # shutil.copy(os.path.join("./wechatgameres", 'startBtn.png'), dst) 77 | 78 | def rebuildConfig(resUrl): 79 | # resUrl = 'https://h5.qiqugame.cn:30013' 80 | 81 | lines = [] 82 | # 读取game.js 83 | with open('wechatgame/game.js', "r", encoding="utf-8") as file: 84 | while True: 85 | line = file.readline() 86 | if not line: 87 | break 88 | lines.append(line) 89 | 90 | # 填写远程ftp服务器地址 91 | for i in range(len(lines)): 92 | if 'wxDownloader.REMOTE_SERVER_ROOT' in lines[i]: 93 | lines[i] = 'wxDownloader.REMOTE_SERVER_ROOT = "%s";\n' % resUrl 94 | break 95 | 96 | # 写入XMLHttpRequest 97 | newLines = [] 98 | isNeedT = False 99 | for line in lines: 100 | if "src/settings" in line: 101 | isNeedT = True 102 | newLines.append("var xhr = new XMLHttpRequest();\n") 103 | newLines.append("xhr.onreadystatechange = function () {\n") 104 | newLines.append("\tif (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {\n") 105 | newLines.append("\t\tvar response = xhr.responseText;\n") 106 | newLines.append("\t\twindow._CCSettings = JSON.parse(response);\n") 107 | else: 108 | if 'main.' in line: 109 | line = "require('main');\n" 110 | if isNeedT: 111 | newLines.append("\t\t" + line) 112 | else: 113 | newLines.append(line) 114 | newLines.append("\n\t}\n") 115 | newLines.append("};\n") 116 | newLines.append("xhr.open(\"GET\", '%s/res/settings.json', true);\n" % resUrl) 117 | newLines.append("xhr.send();\n") 118 | 119 | # 重新覆写game.js 120 | with open('wechatgame/game.js', "w", encoding = 'utf-8') as file: 121 | file.writelines(newLines) 122 | 123 | 124 | lines.clear() 125 | with open('wechatgame/project.config.json', 'r', encoding='utf-8') as file: 126 | allLines = file.readlines() 127 | ss = ''.join(allLines) 128 | jsonObj = json.loads(ss, encoding='utf-8') 129 | jsonObj['description'] = '项目配置文件。' 130 | setting = jsonObj['setting'] 131 | setting['urlCheck'] = False 132 | setting['es6'] = False 133 | setting['postcss'] = True 134 | setting['minified'] = False 135 | setting['newFeature'] = False 136 | 137 | # json.dumps在默认情况下,对于非ascii字符生成的是相对应的字符编码,而非原始字符,只需要ensure_ascii = False 138 | # sort_keys:是否按照字典排序(a-z)输出,True代表是,False代表否。 139 | # indent=4:设置缩进格数,一般由于Linux的习惯,这里会设置为4。 140 | # separators:设置分隔符,在dic = {'a': 1, 'b': 2, 'c': 3}这行代码里可以看到冒号和逗号后面都带了个空格,这也是因为Python的默认格式也是如此, 141 | # 如果不想后面带有空格输出,那就可以设置成separators=(',', ':'),如果想保持原样,可以写成separators=(', ', ': ')。 142 | rebuildContent = json.dumps(jsonObj, ensure_ascii = False, sort_keys = False, indent = 4, separators = (',', ' : ')) 143 | lines.append(rebuildContent) 144 | 145 | with open('wechatgame/project.config.json', 'w', encoding='utf-8') as file: 146 | file.writelines(lines) 147 | 148 | def tinypng(src, dst): 149 | """ 150 | 将初步打包的项目下的图片资源上传到tinypng上进行压缩 151 | src和dst要穿完整路径 152 | """ 153 | tinify.key = "去tinypng官网申请的key" 154 | 155 | fromFilePath = src 156 | toFilePath = dst 157 | 158 | def compress_core(inputFile, outputFile): 159 | source = tinify.from_file(inputFile) 160 | source.to_file(outputFile) 161 | 162 | for root, dirs, files in os.walk(fromFilePath): 163 | for name in files: 164 | fileName, fileSuffix = os.path.splitext(name) # 解析文件名和文件类型后缀 165 | if fileSuffix == '.png' or fileSuffix == '.jpg': 166 | toFullPath = toFilePath + root[len(fromFilePath):] 167 | toFullName = os.path.join(toFullPath, name) 168 | print(toFullName) 169 | if os.path.isdir(toFullPath): 170 | pass 171 | else: 172 | os.mkdir(toFullPath) 173 | 174 | compress_core(toFullName, toFullName) 175 | 176 | def upload(folderName): 177 | ip = '服务端ip' 178 | port = 端口 179 | username = '帐号' 180 | password = '密码' 181 | 182 | cwd = os.getcwd() 183 | 184 | def sftp(src, dest): 185 | """ 186 | 上传压缩包到服务端 187 | """ 188 | transport = paramiko.Transport((ip, port)) 189 | transport.connect(username = username, password = password) 190 | sftp = paramiko.SFTPClient.from_transport(transport) 191 | sftp.put(src, dest) 192 | sftp.close() 193 | return True 194 | 195 | def ssh_exec_command(commands): 196 | ssh = paramiko.SSHClient() 197 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 198 | ssh.connect(hostname = ip, port = port, username = username, password = password) 199 | 200 | for command in commands: 201 | stdin, stdout, stderr = ssh.exec_command(command) 202 | print(stdout.read().decode()) 203 | print(stderr.read().decode()) 204 | ssh.close() 205 | # 先上传文件到服务端 206 | # 然后解压缩文件 207 | resPath = os.path.join(cwd, "wechatgameres/res.zip") 208 | if sftp(resPath, '/home/fish/fishres/res.zip'): 209 | cmds = [] 210 | cmds.append('cd /home/fish/fishres;') 211 | cmds.append('rm -rf %s;' % folderName) 212 | cmds.append('sh unzip.sh') 213 | ssh_exec_command([''.join(cmds)]) 214 | print('success') 215 | 216 | def zipFolder(folderName): 217 | """ 218 | 调用7z对文件进行压缩操作 219 | """ 220 | if os.path.isfile("./wechatgameres/res.zip"): 221 | os.remove("./wechatgameres/res.zip") 222 | # src = "./wechatgameres/%s" % time.strftime("%Y_%m_%d", time.localtime()) 223 | src = "./wechatgameres/%s" % folderName 224 | os.system('"D:\\Program Files\\7-Zip\\7z.exe" a ./wechatgameres/res.zip %s' % src) 225 | 226 | if __name__ == '__main__': 227 | folderName = time.strftime("%Y_%m_%d_%H_%M", time.localtime()) 228 | print('-----------------------开始重构微信小游戏配置文件--------') 229 | rebuildConfig('https://h5.qiqugame.cn:40013/%s' % folderName) 230 | cwd = os.getcwd() 231 | fromFilePath = os.path.join(cwd, "wechatgame/res") 232 | toFilePath = os.path.join(cwd, "wechatgame/res") 233 | print('-----------------------开始压缩图片资源------------------') 234 | tinypng(fromFilePath, toFilePath) 235 | print('-----------------------重命名main.js--------------------') 236 | renameMainJs(cwd) 237 | print('-----------------------创建settings.json文件------------') 238 | buildSettingJson(cwd) 239 | print('-----------------------开始拷贝资源文件------------------') 240 | copyFolder(folderName) 241 | print('-----------------------开始压缩文件----------------------') 242 | zipFolder(folderName) 243 | print('-----------------------开始上传文件----------------------') 244 | upload(folderName) 245 | -------------------------------------------------------------------------------- /build/上传游戏资源到正式服.bat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shangdibaozi/ccwg-onekey/ae12fd20510652f92581409a38a27590a1af29b3/build/上传游戏资源到正式服.bat --------------------------------------------------------------------------------