#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Extra2D 目录结构重构脚本 用于批量移动文件和更新 include 路径 """ import os import shutil import re BASE_DIR = r"d:\Extra2D\Extra2D" def move_file(src, dst): if os.path.exists(src): os.makedirs(os.path.dirname(dst), exist_ok=True) shutil.move(src, dst) print(f"移动: {src} -> {dst}") else: print(f"文件不存在: {src}") def move_remaining_files(): moves = [ (r"include\extra2d\2d\renderer\UIMeshBuffer.h", r"include\extra2d\renderer\2d\UIMeshBuffer.h"), (r"src\2d\components\Sprite.cpp", r"src\renderer\2d\components\Sprite.cpp"), (r"src\2d\components\SpriteFrame.cpp", r"src\renderer\2d\components\SpriteFrame.cpp"), (r"src\2d\renderer\Batcher2d.cpp", r"src\renderer\2d\Batcher2d.cpp"), (r"src\2d\renderer\RenderDrawInfo.cpp", r"src\renderer\2d\RenderDrawInfo.cpp"), (r"src\2d\renderer\RenderEntity.cpp", r"src\renderer\2d\RenderEntity.cpp"), (r"src\2d\renderer\StencilManager.cpp", r"src\renderer\2d\StencilManager.cpp"), (r"src\2d\renderer\UIMeshBuffer.cpp", r"src\renderer\2d\UIMeshBuffer.cpp"), ] for src_rel, dst_rel in moves: src = os.path.join(BASE_DIR, src_rel) dst = os.path.join(BASE_DIR, dst_rel) move_file(src, dst) def update_includes(): path_mappings = { r'': r'', r'': r'', r'': r'', r'': r'', r'': r'', r'': r'', r'': r'', r'': r'', r'': r'', r'': r'', r'': r'', r'': r'', r'': r'', r'': r'', r'': r'', r'': r'', r'': r'', r'': r'', r'': r'', r'': r'', r'': r'', r'': r'', r'': r'', r'': r'', r'': r'', r'': r'', } extensions = ['.h', '.hpp', '.cpp', '.c', '.inl'] for root, dirs, files in os.walk(BASE_DIR): for file in files: if any(file.endswith(ext) for ext in extensions): filepath = os.path.join(root, file) try: with open(filepath, 'r', encoding='utf-8') as f: content = f.read() original = content for old_pattern, new_path in path_mappings.items(): content = re.sub(old_pattern, new_path, content) if content != original: with open(filepath, 'w', encoding='utf-8') as f: f.write(content) print(f"更新: {filepath}") except Exception as e: print(f"错误处理 {filepath}: {e}") def delete_empty_dirs(): empty_dirs = [ r"include\extra2d\core", r"include\extra2d\2d", r"src\core", r"src\2d", ] for dir_rel in empty_dirs: dir_path = os.path.join(BASE_DIR, dir_rel) if os.path.exists(dir_path): try: shutil.rmtree(dir_path) print(f"删除目录: {dir_path}") except Exception as e: print(f"无法删除 {dir_path}: {e}") if __name__ == "__main__": print("=== 移动剩余文件 ===") move_remaining_files() print("\n=== 更新 include 路径 ===") update_includes() print("\n=== 删除空目录 ===") delete_empty_dirs() print("\n完成!")