Skip to content

Commit 796d422

Browse files
committed
add first version of hijackcompiler
1 parent 7b0a425 commit 796d422

3 files changed

Lines changed: 144 additions & 0 deletions

File tree

src/hijackcompiler.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from sys import argv, stdout, exit
2+
from pathlib import Path
3+
import logging
4+
from logging import FileHandler
5+
from transformations import run_transforms, load_transforms
6+
7+
import argparse
8+
9+
from srctools.bsp import BSP
10+
11+
12+
OUR_PATH = Path(argv[0]).parent
13+
TRANSFORMS_PATH = OUR_PATH / "transforms"
14+
15+
def main():
16+
parser = argparse.ArgumentParser("HijackCompiler", description="A simple tool used to run postcompiler-like transforms in each compilation step.")
17+
18+
parser.add_argument("map")
19+
20+
parser.add_argument(
21+
"tags",
22+
action="extend",
23+
nargs=argparse.REMAINDER,
24+
default=[]
25+
)
26+
27+
28+
result = parser.parse_args(argv[1:])
29+
30+
map_file = Path(result.map)
31+
32+
handlers = [logging.StreamHandler(stdout),
33+
FileHandler(map_file.with_suffix(".log"))]
34+
35+
logging.basicConfig(handlers=handlers, level=logging.INFO)
36+
LOGGER = logging.getLogger("[Main]")
37+
38+
LOGGER.info("HijackCompiler started.")
39+
40+
LOGGER.info(f"Loading map file: {map_file}")
41+
42+
if not map_file.suffix == ".bsp":
43+
LOGGER.error(f"Unsupported file type: {map_file.suffix}")
44+
exit(1)
45+
46+
47+
loaded_map = BSP(map_file)
48+
49+
LOGGER.info("Loaded!")
50+
LOGGER.info(f"Loading transforms from {TRANSFORMS_PATH}")
51+
52+
load_transforms(TRANSFORMS_PATH)
53+
54+
tags = [x.casefold() for x in result.tags]
55+
56+
LOGGER.info(f"Running transforms with tags: {tags}")
57+
58+
run_transforms(loaded_map, tags)
59+
60+
LOGGER.info("Finished! Saving map...")
61+
loaded_map.save()
62+
63+
64+
65+
66+
67+
68+
if __name__ == "__main__":
69+
main()

src/transformations.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""Handles the transformations"""
2+
3+
from srctools.bsp import BSP
4+
from pathlib import Path
5+
import importlib
6+
import logging
7+
import pkgutil
8+
9+
TRANSFORMATIONS: dict[str, list] = {}
10+
LOGGER = logging.getLogger("[Transformations]")
11+
12+
def Transform(name: str, tags: list[str] = []):
13+
"""Add a function to the transformations list"""
14+
15+
if type(tags) == str:
16+
tags = [tags]
17+
18+
def dec(func):
19+
func = (name, func)
20+
global TRANSFORMATIONS
21+
22+
23+
for tag in tags:
24+
try:
25+
l = TRANSFORMATIONS[tag]
26+
l.append(func)
27+
except KeyError:
28+
TRANSFORMATIONS[tag] = [func]
29+
30+
return func
31+
32+
LOGGER.info(f"Loading module '{name}'")
33+
34+
return dec
35+
36+
37+
38+
39+
def load_transforms(transforms_path: Path):
40+
for item in pkgutil.iter_modules([transforms_path]):
41+
importlib.import_module(transforms_path.name + "." + item.name)
42+
43+
def run_transforms(bsp_file: BSP, tags:list[str]):
44+
45+
already_ran = set()
46+
for tag in tags:
47+
if not tag in TRANSFORMATIONS.keys():
48+
continue
49+
50+
for name, func in TRANSFORMATIONS[tag]:
51+
if not name in already_ran:
52+
LOGGER.info(f"Running transform |{name}|")
53+
func(bsp_file)
54+
already_ran.add(name)
55+

src/transforms/dynamic_priority.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from transformations import Transform
2+
3+
from srctools import Entity, conv_int, Vec
4+
from srctools.bsp import BSP
5+
import logging
6+
7+
LOGGER = logging.getLogger("[Transform][Dynamic Priority]")
8+
9+
@Transform("Dynamic Priority - Part 2", ["after_vrad"])
10+
def dynamic_priority(bsp: BSP):
11+
12+
light: Entity
13+
14+
# These lights are changed so that VRAD doesn't assign lightmap pages for them.
15+
16+
for light in bsp.ents.by_class["_dynpr_rt_spot"]:
17+
light["classname"] = "light_rt_spot"
18+
19+
for light in bsp.ents.by_class["_dynpr_rt"]:
20+
light["classname"] = "light_rt"

0 commit comments

Comments
 (0)