2022-02-12 07:43:42 -08:00
|
|
|
from collections import Counter
|
|
|
|
|
from decimal import Decimal
|
|
|
|
|
from os.path import exists
|
2024-08-24 21:29:39 -07:00
|
|
|
from re import split
|
|
|
|
|
from xml.etree import ElementTree as ET
|
2022-02-12 07:43:42 -08:00
|
|
|
|
2024-08-22 00:18:20 -07:00
|
|
|
|
2022-02-12 07:43:42 -08:00
|
|
|
class SimulTemplateEntity:
|
|
|
|
|
def __init__(self, vfs_root, logger):
|
|
|
|
|
self.vfs_root = vfs_root
|
|
|
|
|
self.logger = logger
|
|
|
|
|
|
|
|
|
|
def get_file(self, base_path, vfs_path, mod):
|
|
|
|
|
default_path = self.vfs_root / mod / base_path
|
2024-08-22 00:18:20 -07:00
|
|
|
file = (default_path / "special" / "filter" / vfs_path).with_suffix(".xml")
|
2022-02-12 07:43:42 -08:00
|
|
|
if not exists(file):
|
2024-08-22 00:18:20 -07:00
|
|
|
file = (default_path / "mixins" / vfs_path).with_suffix(".xml")
|
2022-02-12 07:43:42 -08:00
|
|
|
if not exists(file):
|
2024-08-22 00:18:20 -07:00
|
|
|
file = (default_path / vfs_path).with_suffix(".xml")
|
2022-02-12 07:43:42 -08:00
|
|
|
return file
|
|
|
|
|
|
|
|
|
|
def get_main_mod(self, base_path, vfs_path, mods):
|
|
|
|
|
for mod in mods:
|
|
|
|
|
fp = self.get_file(base_path, vfs_path, mod)
|
|
|
|
|
if fp.exists():
|
|
|
|
|
main_mod = mod
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
# default to first mod
|
|
|
|
|
# it should then not exist
|
|
|
|
|
# it will raise an exception when trying to read it
|
|
|
|
|
main_mod = mods[0]
|
|
|
|
|
return main_mod
|
|
|
|
|
|
|
|
|
|
def apply_layer(self, base_tag, tag):
|
2024-08-29 05:35:53 -07:00
|
|
|
"""Apply tag layer to base_tag."""
|
2024-08-22 00:18:20 -07:00
|
|
|
if tag.get("datatype") == "tokens":
|
|
|
|
|
base_tokens = split(r"\s+", base_tag.text or "")
|
|
|
|
|
tokens = split(r"\s+", tag.text or "")
|
2022-02-12 07:43:42 -08:00
|
|
|
final_tokens = base_tokens.copy()
|
|
|
|
|
for token in tokens:
|
2024-08-22 00:18:20 -07:00
|
|
|
if token.startswith("-"):
|
2022-02-12 07:43:42 -08:00
|
|
|
token_to_remove = token[1:]
|
|
|
|
|
if token_to_remove in final_tokens:
|
|
|
|
|
final_tokens.remove(token_to_remove)
|
|
|
|
|
elif token not in final_tokens:
|
|
|
|
|
final_tokens.append(token)
|
2024-08-22 00:18:20 -07:00
|
|
|
base_tag.text = " ".join(final_tokens)
|
2022-10-22 09:24:45 -07:00
|
|
|
base_tag.set("datatype", "tokens")
|
2024-08-22 00:18:20 -07:00
|
|
|
elif tag.get("op"):
|
|
|
|
|
op = tag.get("op")
|
|
|
|
|
op1 = Decimal(base_tag.text or "0")
|
|
|
|
|
op2 = Decimal(tag.text or "0")
|
2022-10-22 09:24:45 -07:00
|
|
|
# Try converting to integers if possible, to pass validation.
|
2024-08-22 00:18:20 -07:00
|
|
|
if op == "add":
|
2022-10-22 09:24:45 -07:00
|
|
|
base_tag.text = str(int(op1 + op2) if int(op1 + op2) == op1 + op2 else op1 + op2)
|
2024-08-22 00:18:20 -07:00
|
|
|
elif op == "mul":
|
2022-10-22 09:24:45 -07:00
|
|
|
base_tag.text = str(int(op1 * op2) if int(op1 * op2) == op1 * op2 else op1 * op2)
|
2024-08-22 00:18:20 -07:00
|
|
|
elif op == "mul_round":
|
2022-02-12 07:43:42 -08:00
|
|
|
base_tag.text = str(round(op1 * op2))
|
|
|
|
|
else:
|
|
|
|
|
raise ValueError(f"Invalid operator '{op}'")
|
|
|
|
|
else:
|
|
|
|
|
base_tag.text = tag.text
|
2022-10-22 09:24:45 -07:00
|
|
|
for prop in tag.attrib:
|
2024-08-22 00:18:20 -07:00
|
|
|
if prop not in ("disable", "replace", "parent", "merge"):
|
2022-10-22 09:24:45 -07:00
|
|
|
base_tag.set(prop, tag.get(prop))
|
2022-02-12 07:43:42 -08:00
|
|
|
for child in tag:
|
|
|
|
|
base_child = base_tag.find(child.tag)
|
2024-08-22 00:18:20 -07:00
|
|
|
if "disable" in child.attrib:
|
2022-02-12 07:43:42 -08:00
|
|
|
if base_child is not None:
|
|
|
|
|
base_tag.remove(base_child)
|
2024-08-22 00:18:20 -07:00
|
|
|
elif ("merge" not in child.attrib) or (base_child is not None):
|
|
|
|
|
if "replace" in child.attrib and base_child is not None:
|
2022-02-12 07:43:42 -08:00
|
|
|
base_tag.remove(base_child)
|
2022-10-22 09:24:45 -07:00
|
|
|
base_child = None
|
2022-02-12 07:43:42 -08:00
|
|
|
if base_child is None:
|
2024-08-24 21:29:39 -07:00
|
|
|
base_child = ET.Element(child.tag)
|
2022-02-12 07:43:42 -08:00
|
|
|
base_tag.append(base_child)
|
|
|
|
|
self.apply_layer(base_child, child)
|
2024-08-22 00:18:20 -07:00
|
|
|
if "replace" in base_child.attrib:
|
|
|
|
|
del base_child.attrib["replace"]
|
2022-02-12 07:43:42 -08:00
|
|
|
|
2022-10-22 09:24:45 -07:00
|
|
|
def load_inherited(self, base_path, vfs_path, mods):
|
|
|
|
|
entity = self._load_inherited(base_path, vfs_path, mods)
|
|
|
|
|
entity[:] = sorted(entity[:], key=lambda x: x.tag)
|
|
|
|
|
return entity
|
|
|
|
|
|
|
|
|
|
def _load_inherited(self, base_path, vfs_path, mods, base=None):
|
2024-08-29 05:35:53 -07:00
|
|
|
# vfs_path should be relative to base_path in a mod
|
2024-08-22 00:18:20 -07:00
|
|
|
if "|" in vfs_path:
|
2022-10-22 09:24:45 -07:00
|
|
|
paths = vfs_path.split("|", 1)
|
|
|
|
|
base = self._load_inherited(base_path, paths[1], mods, base)
|
2024-08-24 21:29:39 -07:00
|
|
|
return self._load_inherited(base_path, paths[0], mods, base)
|
2022-02-12 07:43:42 -08:00
|
|
|
|
|
|
|
|
main_mod = self.get_main_mod(base_path, vfs_path, mods)
|
|
|
|
|
fp = self.get_file(base_path, vfs_path, main_mod)
|
2024-08-24 21:29:39 -07:00
|
|
|
layer = ET.parse(fp).getroot()
|
2022-02-12 07:43:42 -08:00
|
|
|
for el in layer.iter():
|
|
|
|
|
children = [x.tag for x in el]
|
|
|
|
|
duplicates = [x for x, c in Counter(children).items() if c > 1]
|
|
|
|
|
if duplicates:
|
|
|
|
|
for dup in duplicates:
|
2024-08-24 21:29:39 -07:00
|
|
|
self.logger.warning(
|
|
|
|
|
"Duplicate child node '%s' in tag %s of %s", dup, el.tag, fp
|
|
|
|
|
)
|
2024-08-22 00:18:20 -07:00
|
|
|
if layer.get("parent"):
|
|
|
|
|
parent = self._load_inherited(base_path, layer.get("parent"), mods, base)
|
2022-02-12 07:43:42 -08:00
|
|
|
self.apply_layer(parent, layer)
|
|
|
|
|
return parent
|
2024-08-24 21:29:39 -07:00
|
|
|
if not base:
|
|
|
|
|
return layer
|
|
|
|
|
self.apply_layer(base, layer)
|
|
|
|
|
return base
|
2022-02-12 07:43:42 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_files(vfs_root, mods, vfs_path, *ext_list):
|
2024-08-29 05:35:53 -07:00
|
|
|
"""Find files.
|
|
|
|
|
|
|
|
|
|
Returns a list of 2-size tuple with:
|
2022-02-12 07:43:42 -08:00
|
|
|
- Path relative to the mod base
|
|
|
|
|
- full Path
|
|
|
|
|
"""
|
2024-08-22 00:18:20 -07:00
|
|
|
full_exts = ["." + ext for ext in ext_list]
|
2022-02-12 07:43:42 -08:00
|
|
|
|
|
|
|
|
def find_recursive(dp, base):
|
2024-08-29 05:35:53 -07:00
|
|
|
"""(relative Path, full Path) generator."""
|
2022-02-12 07:43:42 -08:00
|
|
|
if dp.is_dir():
|
2024-08-24 21:29:39 -07:00
|
|
|
if dp.name not in (".svn", ".git") and not dp.name.endswith("~"):
|
2022-02-12 07:43:42 -08:00
|
|
|
for fp in dp.iterdir():
|
|
|
|
|
yield from find_recursive(fp, base)
|
|
|
|
|
elif dp.suffix in full_exts:
|
|
|
|
|
relative_file_path = dp.relative_to(base)
|
|
|
|
|
yield (relative_file_path, dp.resolve())
|
2024-08-22 00:18:20 -07:00
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
(rp, fp)
|
|
|
|
|
for mod in mods
|
|
|
|
|
for (rp, fp) in find_recursive(vfs_root / mod / vfs_path, vfs_root / mod)
|
|
|
|
|
]
|