|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import sys |
| 4 | + |
| 5 | +from sourcepp import vpkpp |
| 6 | + |
| 7 | + |
| 8 | +def build_addon(addon_dir: str, output_dir_parent: str) -> None: |
| 9 | + output_dir = os.path.join(output_dir_parent, "p2ce_" + os.path.basename(addon_dir)) |
| 10 | + os.makedirs(output_dir, exist_ok=True) |
| 11 | + print(f"Building addon {os.path.basename(addon_dir)} to {os.path.relpath(output_dir, os.getcwd())}") |
| 12 | + |
| 13 | + to_pack: list[str] = [] |
| 14 | + for content_entry_name in os.listdir(addon_dir): |
| 15 | + content_entry_path = os.path.join(addon_dir, content_entry_name) |
| 16 | + if content_entry_name.startswith('.') or content_entry_name == "addon.kv3" or content_entry_name == "media": |
| 17 | + # We can't pack media yet. Remove that check when this is no longer the case |
| 18 | + print(f"Copying {content_entry_name}") |
| 19 | + if os.path.isdir(content_entry_path): |
| 20 | + shutil.copytree(content_entry_path, os.path.join(output_dir, content_entry_name), dirs_exist_ok=True) |
| 21 | + else: |
| 22 | + shutil.copy(content_entry_path, output_dir) |
| 23 | + continue |
| 24 | + |
| 25 | + print(f"Packing {content_entry_name}") |
| 26 | + to_pack.append(content_entry_path) |
| 27 | + |
| 28 | + if len(to_pack) > 0: |
| 29 | + vpk = vpkpp.VPK.create(os.path.join(output_dir, "pak01_dir.vpk")) |
| 30 | + for entry in to_pack: |
| 31 | + if os.path.isdir(entry): |
| 32 | + vpk.add_directory(os.path.basename(entry), entry) |
| 33 | + elif os.path.isfile(entry): |
| 34 | + vpk.add_entry_from_file(os.path.basename(entry), entry) |
| 35 | + vpk.bake() |
| 36 | + |
| 37 | + |
| 38 | +def zip_addons(parent_dir: str, stem: str) -> None: |
| 39 | + print(f"Zipping contents of {parent_dir} into {stem}.zip") |
| 40 | + shutil.make_archive(os.path.join(parent_dir, stem), "zip", parent_dir) |
| 41 | + |
| 42 | + |
| 43 | +def build(addon_root_dir: str) -> None: |
| 44 | + output_dir_parent = os.path.join(addon_root_dir, "_out") |
| 45 | + |
| 46 | + addon_count = 0 |
| 47 | + for addon_dir_name in os.listdir(addon_root_dir): |
| 48 | + if addon_dir_name.startswith(('.', '_')): |
| 49 | + continue |
| 50 | + addon_dir = os.path.join(addon_root_dir, addon_dir_name) |
| 51 | + if not os.path.isdir(addon_dir): |
| 52 | + continue |
| 53 | + build_addon(addon_dir, output_dir_parent) |
| 54 | + addon_count += 1 |
| 55 | + |
| 56 | + zip_addons(output_dir_parent, "addons") |
| 57 | + print(f"Completed, built {addon_count} addons") |
| 58 | + |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + build(os.path.realpath(os.path.join(os.path.dirname(__file__), os.path.pardir)) if len(sys.argv) < 2 else sys.argv[1]) |
0 commit comments