forked from MMOSimca/LibObjectiveProgress-1.0
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_midnight_data.py
More file actions
83 lines (68 loc) · 2.46 KB
/
Copy pathgenerate_midnight_data.py
File metadata and controls
83 lines (68 loc) · 2.46 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
"""
Filter Midnight quest weight data from source and write into WindTools.
"""
import re
import sys
from pathlib import Path
HERE = Path(__file__).parent
WINDTOOLS_FILE = "windtools/Libraries/LibObjectiveProgressWT/ProgressWeightData.lua"
QUEST_BLOCK = re.compile(r"LOP\.QuestBasedWeights\[(\d+)\]\s*=\s*\{([^}]*)\}")
NPC_ENTRY = re.compile(r"\[(\d+)\]\s*=\s*([0-9.]+)")
FN_BODY = re.compile(
r"(function\s+\w+:LoadWeightDataByQuest\(\)\s*\n)(.*?)(end\b)", re.DOTALL
)
def parse_quest_weights(
path: Path, midnight_ids: set[int]
) -> dict[int, dict[int, float]]:
content = path.read_text(encoding="utf-8")
match = re.search(
r"function\s+\w+:LoadWeightDataByQuest\(\).*?\n(.*?)\bend\b", content, re.DOTALL
)
if not match:
print("ERROR: Could not find LoadWeightDataByQuest in source", file=sys.stderr)
sys.exit(1)
result: dict[int, dict[int, float]] = {}
for m in QUEST_BLOCK.finditer(match.group(1)):
qid = int(m.group(1))
if qid not in midnight_ids:
continue
npcs = {
int(n.group(1)): float(n.group(2)) for n in NPC_ENTRY.finditer(m.group(2))
}
if npcs:
result[qid] = npcs
return result
def render_lua(quest_data: dict[int, dict[int, float]]) -> str:
lines = ["\tLOP.QuestBasedWeights = {"]
for quest_id in sorted(quest_data):
npc_map = quest_data[quest_id]
lines.append(f"\t\t[{quest_id}] = {{")
for npc_id in sorted(npc_map):
w = npc_map[npc_id]
lines.append(f"\t\t\t[{npc_id}] = {int(w) if w == int(w) else w},")
lines.append("\t\t},")
lines.append("\t}")
return "\n".join(lines)
def main():
source = HERE / "ProgressWeightData.lua"
ids_file = HERE / "midnight_quest_ids.txt"
windtools_path = HERE / WINDTOOLS_FILE
midnight_ids = {
int(x.strip())
for x in ids_file.read_text(encoding="utf-8").split(",")
if x.strip()
}
print(f"Loaded {len(midnight_ids)} Midnight quest IDs")
quest_data = parse_quest_weights(source, midnight_ids)
print(f"Filtered to {len(quest_data)} Midnight quests")
content = windtools_path.read_text(encoding="utf-8")
new_body = render_lua(quest_data)
windtools_path.write_text(
FN_BODY.sub(
lambda m: f"{m.group(1)}\n{new_body}\n{m.group(3)}", content, count=1
),
encoding="utf-8",
)
print(f"Written to {windtools_path}")
if __name__ == "__main__":
main()