-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsession.py
More file actions
393 lines (322 loc) · 12.4 KB
/
Copy pathsession.py
File metadata and controls
393 lines (322 loc) · 12.4 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/bin/env python3
"""
CreatureCodex Session Manager — automated Ymir + WPP pipeline.
Starts Ymir packet capture, waits for WoW to close, then auto-parses
the captures with WowPacketParser and archives the raw .pkt files.
Ymir auto-closes ~5s after WoW exits (built-in behavior).
Requirements: Python 3.10+, Npcap installed (WinPcap compatibility mode)
Usage:
python session.py # Full session: start Ymir → wait for WoW → parse
python session.py --parse # Parse existing .pkt dumps only (no Ymir)
python session.py --no-archive # Don't move .pkt files after parsing
python session.py --wow-dir "C:\WoW\_retail_" # Explicit WoW root
"""
import argparse
import json
import os
import shutil
import subprocess
import sys
import time
from datetime import datetime
from pathlib import Path
SCRIPT_DIR = Path(__file__).resolve().parent
TOOLS_DIR = SCRIPT_DIR / "tools"
YMIR_EXE = TOOLS_DIR / "Ymir" / "ymir_retail.exe"
YMIR_DUMPS = TOOLS_DIR / "Ymir" / "dumps"
YMIR_ARCHIVE = YMIR_DUMPS / "archived"
WPP_EXE = TOOLS_DIR / "WowPacketParser" / "WowPacketParser.exe"
PARSED_DIR = TOOLS_DIR / "parsed"
WOW_PROCESS = "Wow.exe"
YMIR_PROCESS = "ymir_retail.exe"
DATA_DIR = SCRIPT_DIR / "data"
CONFIG_FILE = SCRIPT_DIR / "session_config.json"
# Common WoW install locations to auto-detect
WOW_SEARCH_PATHS = [
Path(r"C:\WoW\_retail_"),
Path(r"C:\World of Warcraft\_retail_"),
Path(r"C:\Program Files (x86)\World of Warcraft\_retail_"),
Path(r"C:\Program Files\World of Warcraft\_retail_"),
Path(r"D:\WoW\_retail_"),
Path(r"D:\World of Warcraft\_retail_"),
Path(r"D:\Games\World of Warcraft\_retail_"),
]
def load_wow_root(cli_override=None):
"""Resolve WoW root directory: CLI flag > config cache > auto-detect > prompt."""
# 1. Explicit CLI override
if cli_override:
root = Path(cli_override).resolve()
if _validate_wow_root(root):
_save_config(root)
return root
print(f"[Config] Warning: --wow-dir '{root}' doesn't look like a WoW root (no WTF/ folder).")
print(f" Continuing anyway — SavedVariables backup may fail.")
_save_config(root)
return root
# 2. Saved config
if CONFIG_FILE.exists():
try:
cfg = json.loads(CONFIG_FILE.read_text(encoding="utf-8"))
cached = Path(cfg.get("wow_root", ""))
if cached.exists():
return cached
print(f"[Config] Cached WoW path no longer exists: {cached}")
except (json.JSONDecodeError, KeyError):
pass
# 3. Auto-detect from common paths
for candidate in WOW_SEARCH_PATHS:
if _validate_wow_root(candidate):
print(f"[Config] Auto-detected WoW root: {candidate}")
_save_config(candidate)
return candidate
# 4. Prompt user
print()
print("[Config] Could not auto-detect your WoW install location.")
print(" Provide it with: python session.py --wow-dir \"C:\\path\\to\\_retail_\"")
print(" (This only needs to be set once — it's saved to session_config.json)")
print()
return None
def _validate_wow_root(path):
"""Check if a path looks like a WoW root (has WTF/ or Interface/)."""
return path.exists() and (
(path / "WTF").exists() or
(path / "Interface").exists() or
(path / "WTF" / "Account").exists()
)
def _save_config(wow_root):
"""Cache the WoW root path for future runs."""
try:
CONFIG_FILE.write_text(
json.dumps({"wow_root": str(wow_root)}, indent=2),
encoding="utf-8"
)
except OSError:
pass # Non-fatal — user can always pass --wow-dir
def is_running(name):
"""Check if a process is running by name."""
result = subprocess.run(
["tasklist", "/FI", f"IMAGENAME eq {name}", "/NH"],
capture_output=True, text=True
)
return name.lower() in result.stdout.lower()
def wait_for_start(name, timeout=600, poll=3):
"""Wait until a process appears."""
elapsed = 0
while elapsed < timeout:
if is_running(name):
return True
time.sleep(poll)
elapsed += poll
return False
def wait_for_exit(name, timeout=None, poll=3):
"""Wait until a process disappears."""
elapsed = 0
while True:
if not is_running(name):
return True
if timeout and elapsed >= timeout:
return False
time.sleep(poll)
elapsed += poll
def find_pkt_files():
"""Find all .pkt files in the Ymir dumps directory."""
if not YMIR_DUMPS.exists():
return []
return sorted(YMIR_DUMPS.glob("*.pkt"))
def snapshot_dir(path):
"""Return set of file paths in a directory (non-recursive)."""
if not path.exists():
return set()
return {f for f in path.iterdir() if f.is_file()}
def parse_with_wpp(pkt_files):
"""Run WowPacketParser on each .pkt file."""
if not WPP_EXE.exists():
print("[WPP] Not found. Run 'Update Tools.bat' first.")
return False
PARSED_DIR.mkdir(parents=True, exist_ok=True)
wpp_dir = WPP_EXE.parent
for pkt in pkt_files:
print(f"[WPP] Parsing {pkt.name}...")
size_mb = pkt.stat().st_size / (1024 * 1024)
print(f" Size: {size_mb:.1f} MB")
# Snapshot WPP directory before parsing (to find new output files)
before_wpp = snapshot_dir(wpp_dir)
before_dumps = snapshot_dir(YMIR_DUMPS)
try:
result = subprocess.run(
[str(WPP_EXE), str(pkt)],
cwd=str(wpp_dir),
timeout=1800 # 30 min max per file
)
if result.returncode != 0:
print(f" Warning: WPP returned code {result.returncode}")
except subprocess.TimeoutExpired:
print(f" Warning: WPP timed out on {pkt.name}")
continue
# Find new files created by WPP (check both directories)
moved = 0
for check_dir, before_set in [(wpp_dir, before_wpp), (YMIR_DUMPS, before_dumps)]:
after = snapshot_dir(check_dir)
new_files = after - before_set
for nf in new_files:
if nf.suffix == ".pkt":
continue # Don't move pkt files
dest = PARSED_DIR / nf.name
if dest.exists():
dest = PARSED_DIR / f"{nf.stem}_{int(time.time())}{nf.suffix}"
shutil.move(str(nf), str(dest))
print(f" Output: {dest.name}")
moved += 1
# Also check for WPP subdirectories (some versions create output folders)
after_wpp = snapshot_dir(wpp_dir)
for item in wpp_dir.iterdir():
if item.is_dir() and item.name.startswith("dump_"):
dest_dir = PARSED_DIR / item.name
if dest_dir.exists():
shutil.rmtree(dest_dir)
shutil.move(str(item), str(dest_dir))
print(f" Output folder: {item.name}/")
moved += 1
if moved == 0:
print(" No output files detected (WPP may output in-place)")
return True
def backup_savedvariables(wow_root):
"""Copy CreatureCodex SavedVariables into data/ with timestamp."""
if not wow_root:
print("[Data] WoW root not configured — skipping SavedVariables backup.")
print(" Set it with: python session.py --wow-dir \"C:\\path\\to\\_retail_\"")
return
wtf = wow_root / "WTF" / "Account"
if not wtf.exists():
print(f"[Data] WTF folder not found at {wow_root} — skipping SavedVariables backup.")
return
# Find all account copies, pick the most recently modified
candidates = sorted(wtf.glob("*/SavedVariables/CreatureCodex.lua"),
key=lambda p: p.stat().st_mtime, reverse=True)
if not candidates:
print("[Data] No CreatureCodex SavedVariables found yet (first session?).")
return
source = candidates[0]
account = source.parent.parent.name
size_kb = source.stat().st_size / 1024
stamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
DATA_DIR.mkdir(parents=True, exist_ok=True)
# Timestamped archive copy
archive_name = f"CreatureCodex_{stamp}.lua"
shutil.copy2(str(source), str(DATA_DIR / archive_name))
# Latest copy (always overwritten)
shutil.copy2(str(source), str(DATA_DIR / "CreatureCodex_latest.lua"))
print(f"[Data] Backed up SavedVariables ({size_kb:.0f} KB, account: {account})")
print(f" Archive: data/{archive_name}")
print(f" Latest: data/CreatureCodex_latest.lua")
def archive_pkts(pkt_files):
"""Move processed .pkt files to archived folder."""
YMIR_ARCHIVE.mkdir(parents=True, exist_ok=True)
for pkt in pkt_files:
dest = YMIR_ARCHIVE / pkt.name
if dest.exists():
dest = YMIR_ARCHIVE / f"{pkt.stem}_{int(time.time())}{pkt.suffix}"
shutil.move(str(pkt), str(dest))
print(f" Archived: {pkt.name}")
def main():
parser = argparse.ArgumentParser(description="CreatureCodex Session Manager")
parser.add_argument("--parse", action="store_true",
help="Parse existing .pkt dumps without running Ymir")
parser.add_argument("--no-archive", action="store_true",
help="Don't archive .pkt files after parsing")
parser.add_argument("--wow-dir", type=str, default=None,
help="Path to WoW _retail_ folder (saved for future runs)")
args = parser.parse_args()
print()
print(" CreatureCodex Session Manager")
print(" ==============================")
print()
wow_root = load_wow_root(args.wow_dir)
# --- Parse-only mode ---
if args.parse:
pkts = find_pkt_files()
if not pkts:
print("No .pkt files found in dumps/")
return
print(f"Found {len(pkts)} packet capture(s)")
print()
parse_with_wpp(pkts)
if not args.no_archive:
print()
archive_pkts(pkts)
print()
backup_savedvariables(wow_root)
print("\nDone.")
return
# --- Full session mode ---
# Check prerequisites
if not YMIR_EXE.exists():
print("[Ymir] Not found. Run 'Update Tools.bat' first.")
return
if not WPP_EXE.exists():
print("[WPP] Not found. Run 'Update Tools.bat' first.")
return
# Start Ymir
if is_running(YMIR_PROCESS):
print("[Ymir] Already running.")
else:
print("[Ymir] Starting packet capture...")
subprocess.Popen(
[str(YMIR_EXE)],
cwd=str(YMIR_EXE.parent),
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
time.sleep(2)
if is_running(YMIR_PROCESS):
print("[Ymir] Capturing.")
else:
print("[Ymir] Failed to start. Is Npcap installed? (https://nmap.org/npcap/)")
print(" Install with WinPcap compatibility mode enabled.")
return
# Wait for WoW
print()
if is_running(WOW_PROCESS):
print(f"[WoW] Already running.")
else:
print(f"[WoW] Launch WoW from Battle.net whenever you're ready.")
print(f" Waiting for {WOW_PROCESS}...", end="", flush=True)
if not wait_for_start(WOW_PROCESS, timeout=600):
print("\n[WoW] Timed out after 10 minutes. Exiting.")
return
print(" detected!")
print()
print("[Session] Capturing packets. Play the game!")
print(" This window will auto-parse when you close WoW.")
print()
# Wait for WoW to close
wait_for_exit(WOW_PROCESS)
print("[WoW] Closed.")
# Ymir auto-closes ~5s after WoW — wait for it
print("[Ymir] Waiting for capture to flush...")
wait_for_exit(YMIR_PROCESS, timeout=30)
time.sleep(2) # Extra buffer for file writes
# Parse
print()
pkts = find_pkt_files()
if not pkts:
print("No packet captures found. (Did Ymir have time to write?)")
return
print(f"Found {len(pkts)} packet capture(s)")
print()
parse_with_wpp(pkts)
if not args.no_archive:
print()
archive_pkts(pkts)
# Back up creature data from SavedVariables
print()
backup_savedvariables(wow_root)
print()
print(f"Done!")
print(f" Parsed output: {PARSED_DIR}")
print(f" Archived: {YMIR_ARCHIVE}")
# Open parsed folder in Explorer
if PARSED_DIR.exists():
os.startfile(str(PARSED_DIR))
if __name__ == "__main__":
main()