""" Asset converter for Geometry Dash Clone Converts Scratch SVG assets to PNG and creates an asset mapping. """ import json import os from pathlib import Path try: import cairosvg HAS_CAIRO = True except ImportError: HAS_CAIRO = False print("Warning: cairosvg not installed. SVG conversion disabled.") from PIL import Image EXTRACTED_DIR = Path("extracted") ASSETS_DIR = Path("assets") def ensure_assets_dir(): """Create assets directory if it doesn't exist.""" ASSETS_DIR.mkdir(exist_ok=True) def load_project_json(): """Load and parse the Scratch project.json.""" with open(EXTRACTED_DIR / "project.json", encoding="utf-8") as f: return json.load(f) def get_asset_mapping(project_data): """Build a mapping of asset names to files.""" mapping = { "costumes": {}, "sounds": {} } for target in project_data["targets"]: sprite_name = target["name"] for costume in target.get("costumes", []): name = costume["name"] file = costume.get("md5ext", "") if file: mapping["costumes"][f"{sprite_name}/{name}"] = { "file": file, "rotation_center_x": costume.get("rotationCenterX", 0), "rotation_center_y": costume.get("rotationCenterY", 0), } for sound in target.get("sounds", []): name = sound["name"] file = sound.get("md5ext", "") if file: mapping["sounds"][f"{sprite_name}/{name}"] = { "file": file, "rate": sound.get("rate", 44100), "sample_count": sound.get("sampleCount", 0), } return mapping def convert_svg_to_png(svg_path: Path, png_path: Path, scale: float = 2.0): """Convert SVG to PNG with optional scaling.""" if not HAS_CAIRO: return False try: cairosvg.svg2png( url=str(svg_path), write_to=str(png_path), scale=scale ) return True except Exception as e: print(f"Error converting {svg_path}: {e}") return False def convert_key_assets(): """Convert key game assets to PNG.""" ensure_assets_dir() project_data = load_project_json() mapping = get_asset_mapping(project_data) # Key assets to convert key_assets = [ # Player costumes "Player/cube1", "Player/cube2", "Player/cube3", "Player/cube4", "Player/cube5", "Player/ship1", "Player/ship2", "Player/ship3", "Player/ship4", "Player/ship5", "Player/Hitbox for Spike", "Player/Hitbox for Portal", # Level assets "Level Assets/Blocks Grid", "Level Assets/Assets Spikes", "Level Assets/Assets", "Level Assets/Stair", # Background "Background/background1", "Background/background2", "Background/background3", # UI elements "Menu/Menu1", "Menu/Menu2", ] converted = [] for asset_key in key_assets: if asset_key in mapping["costumes"]: info = mapping["costumes"][asset_key] src_file = EXTRACTED_DIR / info["file"] if src_file.suffix.lower() == ".svg": # Convert SVG to PNG safe_name = asset_key.replace("/", "_").replace(" ", "_") dst_file = ASSETS_DIR / f"{safe_name}.png" if convert_svg_to_png(src_file, dst_file): converted.append(asset_key) print(f"Converted: {asset_key} -> {dst_file.name}") elif src_file.suffix.lower() == ".png": # Copy PNG directly safe_name = asset_key.replace("/", "_").replace(" ", "_") dst_file = ASSETS_DIR / f"{safe_name}.png" import shutil shutil.copy(src_file, dst_file) converted.append(asset_key) print(f"Copied: {asset_key} -> {dst_file.name}") # Copy sound files key_sounds = [ "Music/Golden Haze", "Music/Stereo Madness 2", "Music/Space Battle", "Player/Bonk", "Player/explode_11", "Menu/Menu Music", "Menu/Start", ] for sound_key in key_sounds: if sound_key in mapping["sounds"]: info = mapping["sounds"][sound_key] src_file = EXTRACTED_DIR / info["file"] if src_file.exists(): safe_name = sound_key.replace("/", "_").replace(" ", "_") dst_file = ASSETS_DIR / f"{safe_name}{src_file.suffix}" import shutil shutil.copy(src_file, dst_file) print(f"Copied sound: {sound_key} -> {dst_file.name}") print(f"\nConverted {len(converted)} assets to {ASSETS_DIR}/") return converted def create_asset_manifest(): """Create a JSON manifest of converted assets.""" ensure_assets_dir() manifest = { "player": { "cube": [], "ship": [], }, "obstacles": {}, "sounds": {}, "music": {}, } # Scan assets directory for asset_file in ASSETS_DIR.glob("*.png"): name = asset_file.stem if name.startswith("Player_cube"): manifest["player"]["cube"].append(name) elif name.startswith("Player_ship"): manifest["player"]["ship"].append(name) for asset_file in ASSETS_DIR.glob("*.wav"): name = asset_file.stem if name.startswith("Music_"): manifest["music"][name] = str(asset_file) else: manifest["sounds"][name] = str(asset_file) for asset_file in ASSETS_DIR.glob("*.mp3"): name = asset_file.stem manifest["music"][name] = str(asset_file) # Save manifest with open(ASSETS_DIR / "manifest.json", "w") as f: json.dump(manifest, f, indent=2) print(f"Created asset manifest: {ASSETS_DIR / 'manifest.json'}") return manifest if __name__ == "__main__": print("=== Geometry Dash Asset Converter ===\n") # Convert assets convert_key_assets() # Create manifest create_asset_manifest() print("\nDone!")