Add script to generate ConfigLib persist format from Ham modconfig.
Some checks failed
Build and Publish Docker Image (Gitea Registry) / docker (push) Failing after 7s
Some checks failed
Build and Publish Docker Image (Gitea Registry) / docker (push) Failing after 7s
This commit is contained in:
34
tools/gen_configlib_persist.py
Normal file
34
tools/gen_configlib_persist.py
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
"""Convert Ham-style modconfig (array of {name, value, description}) to ConfigLib on-disk format."""
|
||||||
|
import pathlib
|
||||||
|
import re
|
||||||
|
|
||||||
|
SRC = pathlib.Path(__file__).resolve().parents[1] / "1765064717/data/scripts/modconfig/HamGalaxySettings-1765064717.lua"
|
||||||
|
OUT = pathlib.Path(__file__).resolve().parents[1] / "tools/HamGalaxySettings-1765064717-persist.lua"
|
||||||
|
|
||||||
|
# value may be number, bool, or double-quoted string (possibly containing commas)
|
||||||
|
_value = r'(?:"(?:\\.|[^"])*"|true|false|[0-9.+-]+(?:e[+-]?[0-9]+)?)'
|
||||||
|
pat = re.compile(
|
||||||
|
rf'\{{\s*name\s*=\s*"([^"]+)"\s*,\s*value\s*=\s*({_value})\s*,\s*description\s*=\s*"((?:\\.|[^"])*)"\s*\}}',
|
||||||
|
re.DOTALL,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
src = SRC.read_text(encoding="utf-8")
|
||||||
|
rows = []
|
||||||
|
for m in pat.finditer(src):
|
||||||
|
name, val, desc = m.group(1), m.group(2).strip(), m.group(3).replace('\\"', '"')
|
||||||
|
rows.append((name, val, desc))
|
||||||
|
if not rows:
|
||||||
|
raise SystemExit("no rows parsed")
|
||||||
|
lines = ["return {", ""]
|
||||||
|
for i, (name, val, desc) in enumerate(rows, 1):
|
||||||
|
d = desc.replace('"', '\\"')
|
||||||
|
lines.append(f' {name} = {{ value = {val}, description = "{d}", sorting = {i} }},')
|
||||||
|
lines.append("}")
|
||||||
|
OUT.write_text("\n".join(lines) + "\n", encoding="utf-8")
|
||||||
|
print(f"wrote {OUT} ({len(rows)} rows)")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user