"""Script à usage unique (PAS exécuté au runtime du moteur) : copie les images de fond CraftPix depuis assets/background// (pack source, non versionné dans static/) vers static/game/backgrounds//.png, et écrit static/game/backgrounds/manifest.json (dimensions RÉELLES en pixels de chaque image, lues dans l'en-tête PNG — nécessaires pour proposer une taille par défaut cohérente à la pose de l'objet de scène "fond", voir game_engine/scenes/add_scene_object.py). Réutilisé une fois par moi via `python scripts/game/generate_background_manifest.py`, jamais importé par le moteur. Accès réservé au rôle admin (voir game_engine/labels/background_library.py, routes/game/scenes/scene_edit_view.py) : licence CraftPix, ne redistribue pas ces images à des comptes "user" — même règle que la bibliothèque de sprites animaux (game_engine/labels/animal_sprite_library.py).""" import json import os import shutil import struct from typing import Any _BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) _SRC_ROOT = os.path.join(_BASE_DIR, "assets", "background") _DST_ROOT = os.path.join(_BASE_DIR, "static", "game", "backgrounds") # Nom de dossier source -> (slug interne court, libellé FR). Seule # l'image COMBINÉE de premier niveau (game_background_N.png) est reprise # pour l'instant — les calques séparés (layers/) permettraient un effet de # parallaxe, hors scope de ce premier jet. _PACKS = { "craftpix-net-208967-flying-island-battle-backgrounds": ("flying_island", "Île volante"), } def _png_dimensions(path: str) -> tuple[int, int]: """Largeur/hauteur RÉELLES lues dans l'en-tête PNG (chunk IHDR, 8 octets de signature + 4 octets de taille + 4 octets de type "IHDR" avant les 2 x 4 octets de dimensions, big-endian) — évite une dépendance à Pillow (absente de ce projet) pour un besoin aussi ponctuel.""" with open(path, "rb") as f: header = f.read(24) if len(header) < 24 or header[:8] != b"\x89PNG\r\n\x1a\n" or header[12:16] != b"IHDR": raise ValueError(f"pas un PNG valide : {path}") width, height = struct.unpack(">II", header[16:24]) return width, height def main() -> None: manifest: dict[str, Any] = {} for pack_folder, (pack_slug, pack_label) in sorted(_PACKS.items()): pack_dir = os.path.join(_SRC_ROOT, pack_folder, "PNG") if not os.path.isdir(pack_dir): print(f"SKIP (introuvable) : {pack_folder}") continue images: dict[str, Any] = {} for name in sorted(os.listdir(pack_dir)): src_dir = os.path.join(pack_dir, name) src_file = os.path.join(src_dir, f"{name}.png") if not os.path.isfile(src_file): continue dst_dir = os.path.join(_DST_ROOT, pack_slug) os.makedirs(dst_dir, exist_ok=True) dst_file = os.path.join(dst_dir, f"{name}.png") shutil.copy2(src_file, dst_file) width, height = _png_dimensions(dst_file) images[name] = {"width": width, "height": height} if images: manifest[pack_slug] = {"label": pack_label, "images": images} print(f"{pack_slug} ({pack_label}) : {len(images)} images copiées") else: print(f"SKIP (aucune image) : {pack_folder}") os.makedirs(_DST_ROOT, exist_ok=True) manifest_path = os.path.join(_DST_ROOT, "manifest.json") with open(manifest_path, "w", encoding="utf-8") as f: json.dump(manifest, f, ensure_ascii=False, indent=2, sort_keys=True) print(f"\nManifest écrit : {manifest_path}") if __name__ == "__main__": main()