1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
""" Cocos Creator Facebook Playable Ad 打包脚本 功能:将 Cocos Creator Web 构建产物打包为单个 HTML 文件 """
from xml.dom.minidom import parse import xml.dom.minidom import json import os import time import sys import codecs import cgi import HTMLParser import re import base64 import zipfile
if sys.getdefaultencoding() != 'utf-8': reload(sys) sys.setdefaultencoding('utf-8')
class PlayableCompiler: """Playable Ad 编译器"""
def __init__(self): self.root_dir = os.getcwd() print("RootDir: " + self.root_dir)
self.build_dir = os.path.join(self.root_dir, 'build', 'web-mobile')
self.html_path = os.path.join(self.build_dir, 'index.html') self.settings_path = os.path.join(self.build_dir, 'src', 'settings.js') self.main_path = os.path.join(self.build_dir, 'main.js') self.engine_path = os.path.join(self.build_dir, 'cocos2d-js-min.js') self.project_path = os.path.join(self.build_dir, 'src', 'project.js')
self.res_path = os.path.join(self.build_dir, 'res')
self.setting_match_key = '{#settings}' self.main_match_key = '{#main}' self.engine_match_key = '{#cocosengine}' self.project_match_key = '{#project}' self.res_map_match_key = '{#resMap}'
self.file_byte_list = ['.png', '.jpg', '.jpeg', '.mp3', '.ogg', '.wav', '.m4a']
self.base64_prefix = { '.png': 'data:image/png;base64,', '.jpg': 'data:image/jpeg;base64,', '.jpeg': 'data:image/jpeg;base64,', '.mp3': 'data:audio/mpeg;base64,', '.ogg': 'data:audio/ogg;base64,', '.wav': 'data:audio/wav;base64,', '.m4a': 'data:audio/mp4;base64,' }
self.res_map = {}
def read_file(self, file_path, chunk_size=1024*1024): """ 读取文件内容 对于二进制文件(图片、音频)返回 base64 编码 对于文本文件返回字符串内容 """ ext = os.path.splitext(file_path)[1].lower()
if ext in self.file_byte_list: with open(file_path, 'rb') as f: data = f.read() base64_str = base64.b64encode(data) prefix = self.base64_prefix.get(ext, '') return prefix + base64_str if prefix else base64_str elif ext == '': return None else: with open(file_path, 'r') as f: return f.read()
def write_file(self, path, data): """写入文件""" with open(path, 'w') as f: f.write(data)
def build_res_map(self, current_path, base_res_path): """ 递归构建资源映射表 将所有资源文件转为 base64 并保存到映射表 """ try: file_list = os.listdir(current_path) for file_name in file_list: abs_path = os.path.join(current_path, file_name)
if os.path.isdir(abs_path): self.build_res_map(abs_path, base_res_path) elif os.path.isfile(abs_path): data = self.read_file(abs_path) if data: rel_path = 'res' + abs_path.replace(base_res_path, '') self.res_map[rel_path] = data print("资源已映射: " + rel_path) except Exception as e: print("构建资源映射出错: " + str(e))
def get_res_map_script(self): """生成资源映射表的 JavaScript 代码""" res_str = "window.resMap = " + json.dumps(self.res_map) return res_str
def compile(self): """执行打包""" print("\n=== 开始打包 Playable Ad ===\n")
print("1. 读取 HTML 模板...") html_content = self.read_file(self.html_path) if not html_content: print("错误:无法读取 index.html") return False
print("2. 内嵌 settings.js...") settings_content = self.read_file(self.settings_path) if settings_content: html_content = html_content.replace( self.setting_match_key, settings_content, 1 )
print("3. 内嵌 project.js...") project_content = self.read_file(self.project_path) if project_content: html_content = html_content.replace( self.project_match_key, project_content, 1 )
print("4. 内嵌 main.js...") main_content = self.read_file(self.main_path) if main_content: html_content = html_content.replace( self.main_match_key, main_content, 1 )
print("5. 内嵌引擎代码...") engine_content = self.read_file(self.engine_path) if engine_content: html_content = html_content.replace( self.engine_match_key, engine_content, 1 )
print("6. 构建资源映射表...") if os.path.exists(self.res_path): self.build_res_map(self.res_path, self.res_path) res_script = self.get_res_map_script() html_content = html_content.replace( self.res_map_match_key, res_script, 1 )
print("7. 写入输出文件...") output_path = os.path.join(self.build_dir, 'index.html') self.write_file(output_path, html_content)
file_size = os.path.getsize(output_path) file_size_mb = file_size / (1024 * 1024) print("\n=== 打包完成 ===") print("输出文件: " + output_path) print("文件大小: %.2f MB (%.2f KB)" % (file_size_mb, file_size / 1024))
if file_size_mb > 2: print("\n警告:文件大小超过 2MB 限制!") print("建议:压缩图片、减少资源或移除不必要的功能") else: print("✓ 文件大小符合 Facebook Playable Ad 要求")
return True
def main(): """主函数""" compiler = PlayableCompiler() success = compiler.compile()
if success: print("\n打包成功!") return 0 else: print("\n打包失败!") return 1
if __name__ == '__main__': sys.exit(main())
|