Repository navigation
Expand file tree
/
Copy pathexportArmAExecutableMods.py
More file actions
421 lines (352 loc) · 14.4 KB
/
Copy pathexportArmAExecutableMods.py
File metadata and controls
421 lines (352 loc) · 14.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import subprocess
import re
import os
import sys
import glob
from datetime import datetime
PATH_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "path.txt")
WORKSHOP_PATH_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "workshop_path.txt")
EXECUTABLES = ["arma3.exe", "arma3_x64.exe", "arma3battleye.exe"]
def load_saved_path():
if os.path.exists(PATH_FILE):
with open(PATH_FILE, "r", encoding="utf-8") as f:
path = f.read().strip()
if path:
return path
return None
def save_path(path):
with open(PATH_FILE, "w", encoding="utf-8") as f:
f.write(path)
def get_arma_path():
saved = load_saved_path()
if saved:
if os.path.isdir(saved):
print(f"Current PATH of ArmA's folder: {saved}")
choice = input("Use this PATH? (Y / N): ").strip().lower()
if choice == "y":
return saved
else:
path_filename = os.path.basename(PATH_FILE)
print(f"Saved PATH is not valid: {saved}")
print(f"You can also manually edit \"{path_filename}\" to update the PATH.")
choice = input("Try again? (Y / N): ").strip().lower()
if choice != "y" and choice != "":
wait_and_exit()
while True:
path = input("Enter the PATH to your Arma 3 folder: ").strip().strip('"')
if os.path.isdir(path):
save_path(path)
print(f"PATH saved to {PATH_FILE}")
return path
else:
path_filename = os.path.basename(PATH_FILE)
print(f"Directory not found: {path}")
print(f"You can also manually edit \"{path_filename}\" to update the PATH.")
choice = input("Try again? (Y / N): ").strip().lower()
if choice != "y" and choice != "":
wait_and_exit()
def wait_and_exit():
input("\nPress any key to close...")
sys.exit(0)
def load_workshop_path():
if os.path.exists(WORKSHOP_PATH_FILE):
with open(WORKSHOP_PATH_FILE, "r", encoding="utf-8") as f:
path = f.read().strip()
if path:
return path
return None
def save_workshop_path(path):
with open(WORKSHOP_PATH_FILE, "w", encoding="utf-8") as f:
f.write(path)
def validate_workshop_path(path, arma_path):
if not os.path.isdir(path):
print(f"Directory not found: {path}")
return False
arma_drive = os.path.splitdrive(arma_path)[0].upper()
workshop_drive = os.path.splitdrive(path)[0].upper()
if arma_drive != workshop_drive:
print(f"Workshop PATH is on drive {workshop_drive} but Arma 3 is on drive {arma_drive}.")
print("They must be on the same drive.")
return False
contents = os.listdir(path)
if not contents:
print("Workshop PATH has been defined but it has no mods.")
return False
return True
def get_workshop_path(arma_path):
saved = load_workshop_path()
if saved:
if validate_workshop_path(saved, arma_path):
print(f"\nCurrent Workshop PATH: {saved}")
choice = input("Use this Workshop PATH? (Y / N): ").strip().lower()
if choice == "y":
return saved
else:
print(f"Saved Workshop PATH is not valid: {saved}")
guessed = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(arma_path))),
"workshop", "content", "107410")
if validate_workshop_path(guessed, arma_path):
print(f"\nDetected Workshop PATH: {guessed}")
choice = input("Use this Workshop PATH? (Y / N): ").strip().lower()
if choice == "y":
save_workshop_path(guessed)
return guessed
while True:
path = input("Enter the Workshop PATH (e.g. ...\\workshop\\content\\107410) (type QUIT to skip): ").strip().strip('"')
if path.lower() == "quit":
return None
if validate_workshop_path(path, arma_path):
save_workshop_path(path)
print(f"Workshop PATH saved to {WORKSHOP_PATH_FILE}")
return path
def resolve_mod_ids(mod_names, mod_paths, workshop_path):
mod_ids = {}
if workshop_path and os.path.isdir(workshop_path):
ws_map = {}
for folder_id in os.listdir(workshop_path):
folder_full = os.path.join(workshop_path, folder_id)
if os.path.isdir(folder_full):
meta = os.path.join(folder_full, "meta.cpp")
if os.path.isfile(meta):
try:
with open(meta, "r", encoding="utf-8", errors="ignore") as f:
content = f.read()
name_match = re.search(r'name\s*=\s*"([^"]+)"', content)
if name_match:
ws_map[name_match.group(1).lower()] = folder_id
except Exception:
pass
for name in mod_names:
clean = name.lstrip("@")
if clean.lower() in ws_map:
mod_ids[name] = ws_map[clean.lower()]
for i, mod_path in enumerate(mod_paths):
name = mod_names[i] if i < len(mod_names) else None
if name and name in mod_ids:
continue
if os.path.isdir(mod_path):
try:
real = os.path.realpath(mod_path)
folder_id = os.path.basename(real)
if folder_id.isdigit() and name:
mod_ids[name] = folder_id
except Exception:
pass
return mod_ids
def check_executables(arma_path):
found = []
missing = []
for exe in EXECUTABLES:
full = os.path.join(arma_path, exe)
if os.path.isfile(full):
found.append(exe)
else:
missing.append(exe)
print("\n--- Executable Check ---")
for exe in found:
print(f" [OK] {exe}")
for exe in missing:
print(f" [MISSING] {exe}")
if "arma3battleye.exe" not in found:
print("\narma3battleye.exe not found - cannot read mod list.")
return False
return True
def check_running_processes(arma_path=None):
running = []
for exe in EXECUTABLES:
if arma_path:
expected = os.path.join(arma_path, exe).replace("'", "''")
cmd = f"Get-CimInstance Win32_Process -Filter \"Name = '{exe}'\" | Where-Object {{ $_.ExecutablePath -eq '{expected}' }} | Select-Object -ExpandProperty ProcessId"
else:
cmd = f'(Get-CimInstance Win32_Process -Filter "Name = \'{exe}\'").ProcessId'
try:
result = subprocess.run(
["powershell", "-NoProfile", "-Command", cmd],
capture_output=True, text=True, timeout=15
)
if result.stdout.strip():
running.append(exe)
print(f" {exe} - running")
else:
print(f" {exe} - not running")
except Exception:
print(f" {exe} - not running")
return running
def get_commandlines(arma_path=None):
all_lines = []
for exe in EXECUTABLES:
if arma_path:
expected = os.path.join(arma_path, exe).replace("'", "''")
cmd = f"Get-CimInstance Win32_Process -Filter \"Name = '{exe}'\" | Where-Object {{ $_.ExecutablePath -eq '{expected}' }} | Select-Object -ExpandProperty CommandLine"
else:
cmd = f'(Get-CimInstance Win32_Process -Filter "Name = \'{exe}\'").CommandLine'
try:
result = subprocess.run(
["powershell", "-NoProfile", "-Command", cmd],
capture_output=True, text=True, timeout=15
)
output = result.stdout.strip()
if output:
for line in output.splitlines():
all_lines.append((exe, line))
except Exception as e:
print(f" Error querying {exe}: {e}")
return all_lines
def extract_mods(commandline):
match = re.search(r'-mod=([^"]*)', commandline)
if not match:
match = re.search(r'-mod=(\S+)', commandline)
if not match:
return [], []
mod_string = match.group(1)
parts = [p.strip() for p in mod_string.split(";") if p.strip()]
mod_names = []
mod_paths = []
for part in parts:
mod_paths.append(part)
at_match = re.search(r'(@[^;]+)$', part.split("\\")[-1].split("/")[-1])
if at_match:
mod_names.append(at_match.group(1))
elif "@" in part:
idx = part.rfind("@")
mod_names.append(part[idx:])
else:
mod_names.append(part)
return mod_names, mod_paths
def main():
print("=== Arma 3 Mod Reader ===\n")
print("Checking for running Arma 3 processes...")
running = check_running_processes()
saved_path = load_saved_path()
if not running:
if saved_path:
has_exes = any(os.path.isfile(os.path.join(saved_path, exe)) for exe in EXECUTABLES)
if has_exes:
print(f"\n{', '.join(EXECUTABLES)} are not running.")
print(f"Saved PATH: {saved_path}")
print("Please run Arma 3 with mods first.")
wait_and_exit()
else:
print(f"\nSaved PATH does not contain any Arma 3 executables: {saved_path}")
print("\nThere's no executables running, do you want to define the PATH? Either way this will close.")
choice = input("(Y / N): ").strip().lower()
if choice == "y":
while True:
path = input("Enter the PATH to your Arma 3 folder (type QUIT to exit): ").strip().strip('"')
if path.lower() == "quit":
wait_and_exit()
if not os.path.isdir(path):
print(f"Directory not found: {path}")
continue
found_exes = [exe for exe in EXECUTABLES if os.path.isfile(os.path.join(path, exe))]
if not found_exes:
print("Not a valid Arma 3 PATH - none of the executables found:")
for exe in EXECUTABLES:
print(f" [MISSING] {exe}")
print("Try again or type QUIT to exit.")
continue
save_path(path)
print(f"PATH saved to {PATH_FILE}")
print("Found executables:")
for exe in EXECUTABLES:
if exe in found_exes:
print(f" [OK] {exe}")
else:
print(f" [MISSING] {exe}")
print("\nPATH saved. Now run Arma 3 with mods and start this tool again.")
break
wait_and_exit()
print(f"\nRunning (system-wide): {', '.join(running)}")
arma_path = get_arma_path()
if not check_executables(arma_path):
wait_and_exit()
print(f"\nVerifying processes are running from defined PATH...")
running_from_path = check_running_processes(arma_path)
if not running_from_path:
print(f"\nExecutables are running but NOT from your defined PATH:")
print(f" {arma_path}")
print("The running Arma 3 instance may be from a different install.")
wait_and_exit()
print("\nReading command lines from running executables...")
cmd_lines = get_commandlines(arma_path)
if not cmd_lines:
print("\nExecutables are running but could not read their command lines.")
wait_and_exit()
all_mods = []
all_paths = []
for exe, line in cmd_lines:
if "-mod=" in line:
mods, paths = extract_mods(line)
all_mods.extend(mods)
all_paths.extend(paths)
if not all_mods:
print("\nExecutables are running but no mods are loaded.")
print("You need to launch Arma 3 with mods enabled for this tool to work.")
wait_and_exit()
seen = set()
unique_mods = []
unique_paths = []
for m, p in zip(all_mods, all_paths):
if m not in seen:
seen.add(m)
unique_mods.append(m)
unique_paths.append(p)
workshop_path = get_workshop_path(arma_path)
mod_ids = resolve_mod_ids(unique_mods, unique_paths, workshop_path)
resolved_count = len(mod_ids)
print(f"\nResolved {resolved_count}/{len(unique_mods)} mod IDs.")
print(f"\nTotal amount of found / loaded mods: {len(unique_mods)}")
print("\nDo you wish to see the list and/or save them to a file?")
print(" 1 - See List")
print(" 2 - Save to File")
print(" 3 - See the list and save to a file")
print(" Q - Quit")
while True:
choice = input("\nChoice: ").strip().lower()
if choice == "q":
wait_and_exit()
elif choice in ("1", "2", "3"):
break
else:
print("Invalid choice. Please enter 1, 2, 3, or Q.")
if choice in ("1", "3"):
print(f"\n--- Active Mods ({len(unique_mods)}) ---")
for idx, mod in enumerate(unique_mods, 1):
mod_id = mod_ids.get(mod)
if mod_id:
print(f" {idx} - {mod} [ ID: {mod_id} ]")
else:
print(f" {idx} - {mod} [ ID: unknown ]")
if choice in ("2", "3"):
today = datetime.now()
date_prefix = today.strftime("%d_%m_%Y")
script_dir = os.path.dirname(os.path.abspath(__file__))
name_filename = f"{date_prefix}_modsWithName.txt"
name_file = os.path.join(script_dir, name_filename)
inc = 1
while os.path.exists(name_file):
inc += 1
name_filename = f"{date_prefix}_modsWithName_{inc}.txt"
name_file = os.path.join(script_dir, name_filename)
mod_line = '"-mod=' + ";".join(unique_mods) + '"'
with open(name_file, "w", encoding="utf-8") as f:
f.write(mod_line)
print(f"\nMods (names) saved to: {name_file}")
id_filename = f"{date_prefix}_modsWithID.txt"
id_file = os.path.join(script_dir, id_filename)
inc = 1
while os.path.exists(id_file):
inc += 1
id_filename = f"{date_prefix}_modsWithID_{inc}.txt"
id_file = os.path.join(script_dir, id_filename)
id_entries = []
for mod in unique_mods:
mod_id = mod_ids.get(mod, "unknown")
id_entries.append(mod_id)
id_line = '"-mod=' + ";".join(id_entries) + '"'
with open(id_file, "w", encoding="utf-8") as f:
f.write(id_line)
print(f"Mods (IDs) saved to: {id_file}")
wait_and_exit()
if __name__ == "__main__":
main()