Skip to content

Commit

Permalink
(agrf) Make config immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
ahyangyi committed Nov 19, 2024
1 parent fe1d9af commit e764574
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
25 changes: 19 additions & 6 deletions agrf/gorender/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,29 @@ def __init__(self, load_from=None, config=None):
if config is not None:
self.config.update(config)

self._final_config = None

@property
def final_config(self):
if self._final_config is not None:
return self._final_config
self._final_config = self.subset()
return self._final_config

def copy(self):
return Config(config=deepcopy(self.config))

def subset(self, indices):
def subset(self):
new_config = deepcopy(self.config)
for k in ["sprites", "agrf_deltas", "agrf_offsets"]:
if k in new_config:
new_config[k] = [new_config[k][i] for i in indices]

return Config(config=new_config)
if "agrf_subset" in new_config:
indices = new_config["agrf_subset"]
for k in ["sprites", "agrf_deltas", "agrf_offsets"]:
if k in new_config:
new_config[k] = [new_config[k][i] for i in indices]
del new_config["agrf_subset"]

return new_config


def render(config, vox_path, output_path=None):
Expand All @@ -46,7 +59,7 @@ def render(config, vox_path, output_path=None):
palette_clause = []

with tempfile.NamedTemporaryFile("w") as f:
json.dump(config.config, f)
json.dump(config.final_config, f)
f.flush()
subprocess.run(
["gorender", "-s", scales, "-m", f.name, "-p"] + palette_clause + output_clause + [vox_path], check=True
Expand Down
18 changes: 6 additions & 12 deletions agrf/graphics/voxel.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,30 +196,24 @@ def squash(self, ratio, suffix):

@functools.cache
def render(self):
if "agrf_subset" in self.config:
self.config = self.subset(self.config["agrf_subset"]).config
del self.config["agrf_subset"]
voxel_path = self.voxel_getter()
render(self, voxel_path, os.path.join(self.prefix, self.name))

@functools.cache
def spritesheet(self, xdiff=0, ydiff=0, zdiff=0, shift=0, xspan=16, yspan=16):
real_xdiff = 0 if self.config.get("agrf_road_mode", False) else 0.5
real_ydiff = (self.config.get("agrf_zdiff", 0) + zdiff) * self.config.get("agrf_scale", 1)
if "agrf_subset" in self.config:
self.config = self.subset(self.config["agrf_subset"]).config
del self.config["agrf_subset"]

return spritesheet_template(
self,
os.path.join(self.prefix, self.name),
[(x["width"], x.get("height", 0)) for x in self.config["sprites"]],
[x["angle"] for x in self.config["sprites"]],
[(x["width"], x.get("height", 0)) for x in self.final_config["sprites"]],
[x["angle"] for x in self.final_config["sprites"]],
bbox=self.config["size"],
deltas=self.config.get("agrf_deltas", None),
ydeltas=self.config.get("agrf_ydeltas", None),
offsets=self.config.get("agrf_offsets", None),
yoffsets=self.config.get("agrf_yoffsets", None),
deltas=self.final_config.get("agrf_deltas", None),
ydeltas=self.final_config.get("agrf_ydeltas", None),
offsets=self.final_config.get("agrf_offsets", None),
yoffsets=self.final_config.get("agrf_yoffsets", None),
z_scale=self.config.get("z_scale", 1.0),
bbox_joggle=self.config.get("agrf_bbox_joggle", None),
xdiff=real_xdiff,
Expand Down

0 comments on commit e764574

Please sign in to comment.