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
|
""" Cocos Creator Playable 广告打包工具 将 web-mobile 构建输出打包成单个 HTML 文件 """
import os import json import base64 import sys import re
def get_script_dir(): """获取脚本所在目录""" return os.path.dirname(os.path.abspath(__file__))
def read_file(file_path, mode='r'): """读取文件内容""" try: if 'b' in mode: with open(file_path, 'rb') as f: return f.read() else: with open(file_path, 'r', encoding='utf-8') as f: return f.read() except Exception as e: print(f"Error reading {file_path}: {e}") return None
def get_base64_prefix(ext): """获取 Base64 数据 URL 前缀""" prefixes = { '.png': 'data:image/png;base64,', '.jpg': 'data:image/jpeg;base64,', '.jpeg': 'data:image/jpeg;base64,', '.gif': 'data:image/gif;base64,', '.mp3': 'data:audio/mpeg;base64,', '.ogg': 'data:audio/ogg;base64,', '.wav': 'data:audio/wav;base64,', '.mp4': 'data:video/mp4;base64,', '.json': 'data:application/json;base64,', '.ttf': 'data:font/ttf;base64,', '.woff': 'data:font/woff;base64,' } return prefixes.get(ext.lower(), '')
def file_to_base64(file_path): """将文件转换为 Base64 字符串""" try: with open(file_path, 'rb') as f: data = f.read() base64_str = base64.b64encode(data).decode('utf-8')
ext = os.path.splitext(file_path)[1] prefix = get_base64_prefix(ext)
return prefix + base64_str if prefix else base64_str except Exception as e: print(f"Error converting {file_path} to base64: {e}") return None
def build_resource_map(res_dir): """构建资源映射表""" res_map = {}
if not os.path.exists(res_dir): print(f"Resource directory not found: {res_dir}") return res_map
for root, dirs, files in os.walk(res_dir): for file in files: file_path = os.path.join(root, file) rel_path = os.path.relpath(file_path, res_dir) rel_path = 'res/' + rel_path.replace('\\', '/')
base64_data = file_to_base64(file_path) if base64_data: res_map[rel_path] = base64_data print(f"Processed: {rel_path}")
return res_map
def create_playable_html(build_dir, output_file): """创建 Playable 广告 HTML"""
template_path = os.path.join(get_script_dir(), 'template.html') html_content = read_file(template_path)
if not html_content: print("Error: template.html not found") return False
settings_path = os.path.join(build_dir, 'src/settings.js') settings_js = read_file(settings_path) if settings_js: html_content = html_content.replace('{#settings}', settings_js)
main_path = os.path.join(build_dir, 'main.js') main_js = read_file(main_path) if main_js: html_content = html_content.replace('{#main}', main_js)
engine_path = os.path.join(build_dir, 'cocos2d-js-min.js') engine_js = read_file(engine_path) if engine_js: html_content = html_content.replace('{#cocosengine}', engine_js)
project_path = os.path.join(build_dir, 'src/project.js') project_js = read_file(project_path) if project_js: html_content = html_content.replace('{#project}', project_js)
res_dir = os.path.join(build_dir, 'res') res_map = build_resource_map(res_dir) res_map_js = f'window.resMap = {json.dumps(res_map, ensure_ascii=False)};' html_content = html_content.replace('{#resMap}', res_map_js)
with open(output_file, 'w', encoding='utf-8') as f: f.write(html_content)
file_size = os.path.getsize(output_file) print(f"\nOutput: {output_file}") print(f"Size: {file_size / 1024:.2f} KB")
if file_size > 2 * 1024 * 1024: print("WARNING: File size exceeds 2MB limit!")
return True
def main(): """主函数""" build_dir = os.path.join(get_script_dir(), 'build', 'web-mobile') output_file = os.path.join(get_script_dir(), 'playable_ad.html')
if len(sys.argv) > 1: build_dir = sys.argv[1] if len(sys.argv) > 2: output_file = sys.argv[2]
print(f"Building Playable Ad...") print(f"Build dir: {build_dir}") print(f"Output: {output_file}") print("-" * 50)
if create_playable_html(build_dir, output_file): print("\nBuild successful!") else: print("\nBuild failed!") sys.exit(1)
if __name__ == '__main__': main()
|