wobblyhead hg > minor_monsters
comparison tools/build.py @ 17:fe680946164b
reorganised repo
| author | ark |
|---|---|
| date | 2026-10-20 00:12 +0200 |
| parents | 30bd10284fd8 |
| children | 3a831478eda4 |
comparison
equal
deleted
inserted
replaced
| 16:30bd10284fd8 | 17:fe680946164b |
|---|---|
| 1 #!/usr/bin/env python3 | 1 #!/usr/bin/env python3 |
| 2 """ | 2 """ |
| 3 build the game data from art/ and config/ into game/assets/ and game/data/. | 3 build the runnable game into www/: the game data from art/ and config/ into |
| 4 www/assets/ and www/data/, and a copy of the hand-written files in game/. | |
| 4 | 5 |
| 5 usage: python3 tools/build.py [--force] [--watch] | 6 usage: python3 tools/build.py [--force] [--watch] |
| 6 | 7 |
| 7 steps: sprites, tower levels, maps, masks, sounds, config, checks, previews. | 8 steps: sprites, tower levels, maps, masks, sounds, config, checks, previews, |
| 8 a step | 9 game files. a step is skipped when all its outputs are newer than its inputs |
| 9 is skipped when all its outputs are newer than its inputs and the build | 10 and the build scripts; --force runs every step. --watch reruns the build |
| 10 scripts; --force runs every step. --watch reruns the build whenever a file | 11 whenever a file in art/, art/sounds/, art/digits/, config/ or game/ changes. |
| 11 in art/, art/sounds/, art/digits/ or config/ changes. | |
| 12 | 12 |
| 13 requires python 3.11 or later and pillow. | 13 requires python 3.11 or later and pillow. |
| 14 """ | 14 """ |
| 15 | 15 |
| 16 import base64 | 16 import base64 |
| 17 import json | 17 import json |
| 18 import math | 18 import math |
| 19 import re | 19 import re |
| 20 import shutil | |
| 20 import sys | 21 import sys |
| 21 import time | 22 import time |
| 22 import tomllib | 23 import tomllib |
| 23 from pathlib import Path | 24 from pathlib import Path |
| 24 | 25 |
| 34 ROOT = Path(__file__).resolve().parent.parent | 35 ROOT = Path(__file__).resolve().parent.parent |
| 35 ART = ROOT / "art" | 36 ART = ROOT / "art" |
| 36 SOUNDS = ART / "sounds" | 37 SOUNDS = ART / "sounds" |
| 37 DIGITS = ART / "digits" | 38 DIGITS = ART / "digits" |
| 38 CONFIG = ROOT / "config" | 39 CONFIG = ROOT / "config" |
| 39 ASSETS = ROOT / "game" / "assets" | 40 GAME = ROOT / "game" |
| 40 DATA = ROOT / "game" / "data" | 41 WWW = ROOT / "www" |
| 42 ASSETS = WWW / "assets" | |
| 43 DATA = WWW / "data" | |
| 41 TEMP = ROOT / "temp" | 44 TEMP = ROOT / "temp" |
| 42 PREVIEW = TEMP / "preview" | 45 PREVIEW = TEMP / "preview" |
| 43 # route data of the last mask step, read when the step is skipped | 46 # route data of the last mask step, read when the step is skipped |
| 44 MAPS_CACHE = TEMP / "build" / "maps.json" | 47 MAPS_CACHE = TEMP / "build" / "maps.json" |
| 45 | 48 |
| 287 | 290 |
| 288 # 5. sounds | 291 # 5. sounds |
| 289 | 292 |
| 290 def step_sounds(cfg, force): | 293 def step_sounds(cfg, force): |
| 291 """ | 294 """ |
| 292 embed art/sounds/*.mp3 as base64 in game/data/sounds.js, grouped by | 295 embed art/sounds/*.mp3 as base64 in www/data/sounds.js, grouped by |
| 293 event: NAME.mp3 or the variants NAME_1.mp3, NAME_2.mp3 and so on. | 296 event: NAME.mp3 or the variants NAME_1.mp3, NAME_2.mp3 and so on. |
| 294 """ | 297 """ |
| 295 inputs = sorted(SOUNDS.glob("*.mp3")) | 298 inputs = sorted(SOUNDS.glob("*.mp3")) |
| 296 dst = DATA / "sounds.js" | 299 dst = DATA / "sounds.js" |
| 297 if not stale(inputs, [dst], force): | 300 if not stale(inputs, [dst], force): |
| 401 out.append(math.floor(v + 0.5) if whole else round(v, TOWER_DECIMALS)) | 404 out.append(math.floor(v + 0.5) if whole else round(v, TOWER_DECIMALS)) |
| 402 return out | 405 return out |
| 403 | 406 |
| 404 | 407 |
| 405 def sprite_table(): | 408 def sprite_table(): |
| 406 """name -> [width, height] of every image in game/assets/ except maps.""" | 409 """name -> [width, height] of every image in www/assets/ except maps.""" |
| 407 out = {} | 410 out = {} |
| 408 for p in sorted(ASSETS.glob("*.png")): | 411 for p in sorted(ASSETS.glob("*.png")): |
| 409 with Image.open(p) as im: | 412 with Image.open(p) as im: |
| 410 out[p.stem] = list(im.size) | 413 out[p.stem] = list(im.size) |
| 411 return out | 414 return out |
| 428 out["maps"].append(mp) | 431 out["maps"].append(mp) |
| 429 out["sprites"] = sprite_table() | 432 out["sprites"] = sprite_table() |
| 430 write_text(dst, HEADER.format(src="config/game.toml") | 433 write_text(dst, HEADER.format(src="config/game.toml") |
| 431 + "window.MM = window.MM || {};\nMM.config = " | 434 + "window.MM = window.MM || {};\nMM.config = " |
| 432 + json.dumps(out, indent=1) + ";\n") | 435 + json.dumps(out, indent=1) + ";\n") |
| 433 print("config: wrote game/data/config.js") | 436 print("config: wrote www/data/config.js") |
| 434 | 437 |
| 435 | 438 |
| 436 # 7. checks | 439 # 7. checks |
| 437 | 440 |
| 438 def wave_totals(cfg, waves): | 441 def wave_totals(cfg, waves): |
| 518 preview.tower_sheet(ASSETS, cfg["art"]["pivot"], cfg["placement"]["footprint_radius"], | 521 preview.tower_sheet(ASSETS, cfg["art"]["pivot"], cfg["placement"]["footprint_radius"], |
| 519 cfg["scale"]["sprite"]).save(PREVIEW / "towers.png") | 522 cfg["scale"]["sprite"]).save(PREVIEW / "towers.png") |
| 520 print(f"previews: wrote {PREVIEW.relative_to(ROOT)}/") | 523 print(f"previews: wrote {PREVIEW.relative_to(ROOT)}/") |
| 521 | 524 |
| 522 | 525 |
| 526 # 9. game files | |
| 527 | |
| 528 def game_files(): | |
| 529 """the hand-written files in game/, as paths relative to it.""" | |
| 530 return sorted(p.relative_to(GAME) for p in GAME.rglob("*") | |
| 531 if p.is_file() and p.name != ".DS_Store") | |
| 532 | |
| 533 | |
| 534 def step_game(force): | |
| 535 """ | |
| 536 copy the files in game/ to www/ when they differ in time or size, and | |
| 537 remove files from www/ that are no longer in game/. www/assets/ and | |
| 538 www/data/ are left alone, as the other steps write them. | |
| 539 """ | |
| 540 files = game_files() | |
| 541 copied = 0 | |
| 542 for rel in files: | |
| 543 src, dst = GAME / rel, WWW / rel | |
| 544 a = src.stat() | |
| 545 if not force and dst.exists(): | |
| 546 b = dst.stat() | |
| 547 if b.st_mtime == a.st_mtime and b.st_size == a.st_size: | |
| 548 continue | |
| 549 dst.parent.mkdir(parents=True, exist_ok=True) | |
| 550 shutil.copy2(src, dst) | |
| 551 copied += 1 | |
| 552 keep = set(files) | |
| 553 removed = 0 | |
| 554 for p in sorted(WWW.rglob("*"), reverse=True): | |
| 555 rel = p.relative_to(WWW) | |
| 556 if rel.parts[0] in (ASSETS.name, DATA.name) or rel in keep: | |
| 557 continue | |
| 558 if p.is_file(): | |
| 559 p.unlink() | |
| 560 removed += 1 | |
| 561 elif not any(p.iterdir()): | |
| 562 p.rmdir() | |
| 563 if copied or removed: | |
| 564 print(f"game files: copied {copied}, removed {removed}") | |
| 565 else: | |
| 566 print("game files: up to date") | |
| 567 | |
| 568 | |
| 523 def build(force): | 569 def build(force): |
| 524 warnings.clear() | 570 warnings.clear() |
| 525 try: | 571 try: |
| 526 cfg = tomllib.loads((CONFIG / "game.toml").read_text()) | 572 cfg = tomllib.loads((CONFIG / "game.toml").read_text()) |
| 527 check_config(cfg) | 573 check_config(cfg) |
| 534 maps, masks_changed = step_masks(cfg, force) | 580 maps, masks_changed = step_masks(cfg, force) |
| 535 step_sounds(cfg, force) | 581 step_sounds(cfg, force) |
| 536 step_config(cfg, force, changed) | 582 step_config(cfg, force, changed) |
| 537 step_checks(cfg, maps) | 583 step_checks(cfg, maps) |
| 538 step_previews(cfg, maps, force, changed or masks_changed) | 584 step_previews(cfg, maps, force, changed or masks_changed) |
| 585 step_game(force) | |
| 539 print(f"\n{len(warnings)} warning(s)") | 586 print(f"\n{len(warnings)} warning(s)") |
| 540 return True | 587 return True |
| 541 | 588 |
| 542 | 589 |
| 543 def watch(): | 590 def watch(): |
| 544 def snapshot(): | 591 def snapshot(): |
| 545 files = (list(ART.iterdir()) + list(SOUNDS.glob("*.mp3")) + list(DIGITS.glob("*.png")) | 592 files = (list(ART.iterdir()) + list(SOUNDS.glob("*.mp3")) + list(DIGITS.glob("*.png")) |
| 546 + list(CONFIG.iterdir()) + TOOLS) | 593 + list(CONFIG.iterdir()) + [GAME / p for p in game_files()] + TOOLS) |
| 547 return {p: p.stat().st_mtime for p in files if p.is_file()} | 594 return {p: p.stat().st_mtime for p in files if p.is_file()} |
| 548 | 595 |
| 549 seen = None | 596 seen = None |
| 550 while True: | 597 while True: |
| 551 now = snapshot() | 598 now = snapshot() |
