Files
AVA2/tools/pack_assets.py

190 lines
7.7 KiB
Python
Executable File

#!/usr/bin/env python3
"""
AVA2 Cross-Platform Asset Packaging Tool
Scans, validates, packages, and verifies official AVA2/Metin2 client assets for distribution.
Generates an asset_manifest.json with SHA-256 integrity checksums.
"""
import os
import sys
import json
import hashlib
import shutil
import argparse
from pathlib import Path, PurePosixPath
from make_test_rundir import pack_manifests, MANIFEST_ENTRY, archived_root_path
DEFAULT_ASSET_DIRS = [
"mark",
"locale",
"uiscript",
"textureset",
"sound",
"runtime",
"icon",
"flag",
"Dialog",
"Itemshop",
"Modulo",
"System",
"battlepass",
"season1",
"upstream/python-root",
]
# Optional large 3D asset directory
OPTIONAL_ASSET_DIRS = [
"ymir work",
"bgm",
]
def compute_sha256(filepath):
h = hashlib.sha256()
with open(filepath, "rb") as f:
while chunk := f.read(65536):
h.update(chunk)
return h.hexdigest()
def package_assets(source_dir, output_dir, manifest_path=None, include_heavy=False, pack_maker=None):
source_path = Path(source_dir).resolve()
dest_path = Path(output_dir).resolve()
dest_path.mkdir(parents=True, exist_ok=True)
if pack_maker is None:
candidate = source_path.parent / "AVA2" / "1. Client DEV" / "pack_maker"
pack_maker = candidate if candidate.is_dir() else None
entries = {}
def add(rel, source, override=False):
rel = archived_root_path(str(rel))
parts = PurePosixPath(rel).parts
if not parts or rel.startswith("/") or any(part in ("..", ".") or ":" in part for part in parts):
raise ValueError(f"Invalid virtual asset path: {rel}")
if not include_heavy and parts[0].lower() in ("ymir work", "bgm"):
return
if rel.endswith((".pyc", ".tmp")):
return
key = rel.lower()
# Saved accounts and optional auto-login overrides belong to the user.
if key in ("lib/wa_ac.py", "root/lib/wa_ac.py", "no.py", "root/no.py"):
return
if override or key not in entries:
if not source.is_file():
raise FileNotFoundError(f"Required asset {rel}: {source}")
# Canonical casing also works on Android/Linux.
# Python imports are case-sensitive on Android/Linux (e.g. uiCommon).
entries[key] = (rel if parts[0] == "root" and len(parts) == 2 and rel.endswith(".py") else key, source)
if pack_maker is not None:
pack_maker = Path(pack_maker).resolve()
for xml in pack_manifests(pack_maker):
for rel, source in MANIFEST_ENTRY.findall(xml.read_text(encoding="latin-1")):
add(rel, pack_maker.parent / source)
# Some custom UI images ship loose beside the Windows executable, outside every pack.
# Copy resource formats only; scripts/configuration continue to come from the port.
resource_extensions = {".png", ".jpg", ".jpeg", ".tga", ".dds", ".sub", ".wav", ".ogg", ".mp3", ".gr2", ".spt", ".mse", ".msa", ".msm"}
excluded = {"pack", "pack_maker", "lib", "miles", "mark", "screenshot", "screenshots", "logs"}
for directory, dirs, files in os.walk(pack_maker.parent):
dirs[:] = [d for d in dirs if not d.startswith(".") and d.lower() not in excluded]
for name in files:
source = Path(directory) / name
if source.suffix.lower() in resource_extensions and source.is_file():
add(source.relative_to(pack_maker.parent).as_posix(), source)
scan_dirs = DEFAULT_ASSET_DIRS + (["map", "property", "rework_inv"] if pack_maker is None else [])
if include_heavy and pack_maker is None:
scan_dirs += OPTIONAL_ASSET_DIRS
for rel_dir in scan_dirs:
base = source_path / rel_dir
is_loose = False
if not base.exists():
candidate = source_path / "assets" / "loose" / rel_dir
if candidate.exists():
base = candidate
is_loose = True
else:
continue
for directory, dirs, files in os.walk(base):
dirs[:] = [d for d in dirs if not d.startswith(".") and d not in ("__pycache__", "build")]
for name in files:
if not name.startswith("."):
source = Path(directory) / name
if source.is_file():
rel = source.relative_to(source_path).as_posix()
if rel.startswith("assets/loose/"):
rel = rel[len("assets/loose/"):]
elif rel.startswith("upstream/python-root/"):
rel = "root/" + rel[len("upstream/python-root/"):]
add(rel, source, True)
manifest = {"version": "2.0.0", "source": str(source_path),
"total_files": 0, "total_bytes": 0, "files": {}}
print(f"[*] Packaging {len(entries)} virtual assets into {dest_path}")
for rel, source in entries.values():
target = dest_path / rel
target.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(source, target)
size = source.stat().st_size
manifest["files"][rel] = {"size": size, "sha256": compute_sha256(source)}
manifest["total_bytes"] += size
manifest["total_files"] = len(entries)
out_manifest = Path(manifest_path) if manifest_path else dest_path / "asset_manifest.json"
out_manifest.parent.mkdir(parents=True, exist_ok=True)
out_manifest.write_text(json.dumps(manifest, indent=2, sort_keys=True), encoding="utf-8")
print(f"[+] Packaged {len(entries)} files ({manifest['total_bytes'] / (1024*1024):.2f} MB)")
return manifest
def verify_assets(manifest_path, target_dir):
with open(manifest_path, "r", encoding="utf-8") as f:
manifest = json.load(f)
target_path = Path(target_dir).resolve()
print(f"[*] Verifying {manifest.get('total_files', 0)} assets in {target_path}...")
errors = []
verified = 0
for rel_path, meta in manifest.get("files", {}).items():
fp = target_path / rel_path
if not fp.exists():
errors.append(f"Missing file: {rel_path}")
continue
if fp.stat().st_size != meta["size"]:
errors.append(f"Size mismatch: {rel_path} (expected {meta['size']}, got {fp.stat().st_size})")
continue
actual_hash = compute_sha256(fp)
if actual_hash != meta["sha256"]:
errors.append(f"Hash mismatch: {rel_path}")
continue
verified += 1
if errors:
print(f"[-] Verification FAILED: {len(errors)} errors found:")
for err in errors[:10]:
print(f" - {err}")
return False
else:
print(f"[+] Verification SUCCESS: All {verified} files verified 100% OK!")
return True
def main():
parser = argparse.ArgumentParser(description="AVA2 Cross-Platform Asset Packaging Tool")
parser.add_argument("--source", default=".", help="Source client directory")
parser.add_argument("--output", default="dist/assets", help="Output packaged directory")
parser.add_argument("--manifest", default=None, help="Manifest output path")
parser.add_argument("--pack-maker", default=None, help="Original pack_maker manifests and decoded files")
parser.add_argument("--include-heavy", action="store_true", help="Include large 3D models and BGM")
parser.add_argument("--verify", default=None, help="Verify target directory against specified manifest")
args = parser.parse_args()
if args.verify:
success = verify_assets(args.verify, args.output)
sys.exit(0 if success else 1)
else:
package_assets(args.source, args.output, args.manifest, args.include_heavy, args.pack_maker)
if __name__ == "__main__":
main()