forked from DeferW/ssmc-wiki-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_guide_sections.py
More file actions
98 lines (80 loc) · 2.88 KB
/
Copy pathextract_guide_sections.py
File metadata and controls
98 lines (80 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from __future__ import annotations
import argparse
import json
import re
from datetime import datetime, timezone
from pathlib import Path
HEADING_RE = re.compile(r"^\s*(#{1,6})\s+(.+?)\s*$")
REAGENT_RE = re.compile(r'<GuideReagentEmbed\s+Reagent="([^"]+)"\s*/?>')
GROUP_RE = re.compile(r'<GuideReagentGroupEmbed\s+Group="([^"]+)"[^>]*/?>')
def parse_guide(path: Path) -> list[dict[str, object]]:
headings: list[str] = []
entries: list[dict[str, object]] = []
for raw_line in path.read_text(encoding="utf-8-sig").splitlines():
heading = HEADING_RE.match(raw_line)
if heading:
level = len(heading.group(1))
title = heading.group(2).strip()
headings = headings[: level - 1]
headings.append(title)
continue
reagent = REAGENT_RE.search(raw_line)
if reagent:
entries.append({
"type": "reagent",
"id": reagent.group(1),
"sectionPath": headings.copy(),
})
continue
group = GROUP_RE.search(raw_line)
if group:
entries.append({
"type": "group",
"id": group.group(1),
"sectionPath": headings.copy(),
})
return entries
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--game-source", required=True, type=Path)
parser.add_argument("--output", required=True, type=Path)
parser.add_argument("--commit", default="unknown")
args = parser.parse_args()
guide_root = args.game_source / "Resources/ServerInfo/Guidebook/_RMC14"
guides = {
"ordnance": guide_root / "Chemicals/OT.xml",
"medicine": guide_root / "Chemicals/Medicine.xml",
"drinks": guide_root / "Guides/RMCGuideDrinks.xml",
}
classification_guide = guide_root / "Chemicals/RMCChemicals.xml"
missing = [
str(path)
for path in (*guides.values(), classification_guide)
if not path.is_file()
]
if missing:
raise FileNotFoundError("Missing guide files: " + ", ".join(missing))
result = {
"schemaVersion": 1,
"source": {
"repository": "MetalSage/space-stories-cm14",
"branch": "master",
"commit": args.commit,
},
"generatedAt": datetime.now(timezone.utc).isoformat().replace("+00:00", "Z"),
"guides": {
key: parse_guide(path)
for key, path in guides.items()
},
"classificationGuide": {
"path": classification_guide.relative_to(args.game_source).as_posix(),
"entries": parse_guide(classification_guide),
},
}
args.output.parent.mkdir(parents=True, exist_ok=True)
args.output.write_text(
json.dumps(result, ensure_ascii=False, indent=2) + "\n",
encoding="utf-8",
)
if __name__ == "__main__":
main()