-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathppg.py
More file actions
536 lines (430 loc) · 19.5 KB
/
Copy pathppg.py
File metadata and controls
536 lines (430 loc) · 19.5 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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
import os
import sys
import json
import subprocess
import platform
import time
import re
import shutil
import logging
import tempfile
import zipfile
import requests
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import parse_qs, urlparse
from logging.handlers import RotatingFileHandler
PPG_APP_ID = "1118200"
MOD_INFO_FILE = "modinfo.json"
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
RotatingFileHandler('ppg_mod_manager.log', maxBytes=5*1024*1024, backupCount=2),
logging.StreamHandler(sys.stdout)
]
)
class ModManagerConfig:
def __init__(self):
self.ppg_dir = ""
self.mods_dir = ""
self.contraptions_dir = ""
self.steamcmd_dir = ""
self.steamcmd_exe_name = "steamcmd.exe" if platform.system() == "Windows" else "steamcmd.sh"
self.config_file_path = self._get_config_path()
self.load_config()
def _get_config_path(self):
if platform.system() == "Windows":
config_dir = os.path.join(os.getenv('APPDATA', os.path.expanduser("~")), 'PyPPGModManager')
else:
config_dir = os.path.join(os.path.expanduser('~'), '.config', 'PyPPGModManager')
os.makedirs(config_dir, exist_ok=True)
return os.path.join(config_dir, 'config.json')
def load_config(self):
if os.path.exists(self.config_file_path):
try:
with open(self.config_file_path, 'r') as f:
data = json.load(f)
self.ppg_dir = data.get("ppg_dir", "")
self.steamcmd_dir = data.get("steamcmd_dir", "")
if (self.ppg_dir and os.path.isdir(self.ppg_dir) and
self.steamcmd_dir and os.path.isfile(os.path.join(self.steamcmd_dir, self.steamcmd_exe_name))):
self.mods_dir = os.path.join(self.ppg_dir, "Mods")
os.makedirs(self.mods_dir, exist_ok=True)
logging.info(f"Config loaded successfully")
return
except Exception as e:
logging.error(f"Error loading config: {e}")
self.setup_new_config()
def setup_new_config(self):
logging.info("Setting up new configuration")
while True:
self.ppg_dir = input(f"People Playground directory: ").strip()
ppg_exe_win = os.path.join(self.ppg_dir, "People Playground.exe")
ppg_exe_linux = os.path.join(self.ppg_dir, "People Playground.x86_64")
if os.path.isdir(self.ppg_dir) and (os.path.exists(ppg_exe_win) or os.path.exists(ppg_exe_linux)):
break
print("Invalid PPG directory. Please try again.")
self.mods_dir = os.path.join(self.ppg_dir, "Mods")
os.makedirs(self.mods_dir, exist_ok=True)
self.contraptions_dir = os.path.join(self.ppg_dir, "Contraptions")
os.makedirs(self.contraptions_dir, exist_ok=True)
default_dir = os.path.join(os.getenv('APPDATA', os.path.expanduser('~')), 'PyPPGModManager', 'SteamCMD')
steamcmd_input = input(f"SteamCMD directory (default: {default_dir}): ").strip()
self.steamcmd_dir = steamcmd_input if steamcmd_input else default_dir
os.makedirs(self.steamcmd_dir, exist_ok=True)
steamcmd_path = os.path.join(self.steamcmd_dir, self.steamcmd_exe_name)
if not os.path.isfile(steamcmd_path):
if input("Download SteamCMD? (yes/no): ").lower() == 'yes':
self.download_steamcmd()
else:
print("SteamCMD is required. Exiting.")
sys.exit(1)
self.save_config()
def save_config(self):
config_data = {"ppg_dir": self.ppg_dir, "steamcmd_dir": self.steamcmd_dir}
try:
with open(self.config_file_path, 'w') as f:
json.dump(config_data, f, indent=4)
logging.info(f"Configuration saved")
except Exception as e:
logging.error(f"Failed to save configuration: {e}")
def download_steamcmd(self):
logging.info(f"Downloading SteamCMD")
os.makedirs(self.steamcmd_dir, exist_ok=True)
system = platform.system()
if system == "Windows":
url = "https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip"
archive_name = "steamcmd.zip"
elif system == "Linux":
url = "https://steamcdn-a.akamaihd.net/client/installer/steamcmd_linux.tar.gz"
archive_name = "steamcmd_linux.tar.gz"
elif system == "Darwin":
url = "https://steamcdn-a.akamaihd.net/client/installer/steamcmd_osx.tar.gz"
archive_name = "steamcmd_osx.tar.gz"
else:
logging.error(f"Unsupported OS: {system}")
return
try:
archive_path = os.path.join(self.steamcmd_dir, archive_name)
response = requests.get(url, stream=True)
with open(archive_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
shutil.unpack_archive(archive_path, self.steamcmd_dir)
os.remove(archive_path)
if system != "Windows":
steamcmd_sh = os.path.join(self.steamcmd_dir, "steamcmd.sh")
if os.path.isfile(steamcmd_sh):
os.chmod(steamcmd_sh, 0o755)
logging.info("SteamCMD downloaded and extracted")
except Exception as e:
logging.error(f"Error downloading SteamCMD: {e}")
def sanitize_filename(name):
name = re.sub(r'[<>:"/\\|?*]', '_', name)
name = re.sub(r'\s+', ' ', name).strip()
return name if name else "Untitled Mod"
def get_mod_details_from_steam(mod_id):
url = "https://api.steampowered.com/ISteamRemoteStorage/GetPublishedFileDetails/v1/"
data = {'itemcount': 1, 'publishedfileids[0]': mod_id}
try:
response = requests.post(url, data=data)
data = response.json()
if data['response']['result'] == 1:
details = data['response']['publishedfiledetails'][0]
tags = []
if 'tags' in details:
tags = [tag.get('tag', '').strip() for tag in details['tags']]
return {
"mod_id": details['publishedfileid'],
"time_updated": details.get('time_updated', 0),
"title": details.get('title', f'Mod {mod_id}'),
"tags": tags
}
except Exception as e:
logging.error(f"Error getting mod details: {e}")
return None
def download_mod_via_ggnetwork(mod_id, target_dir):
logging.info(f"Downloading mod {mod_id} via GGNetwork")
try:
api_url = "https://api.ggntw.com/steam.request"
payload = {"url": f"https://steamcommunity.com/sharedfiles/filedetails/?id={mod_id}"}
headers = {
'Content-Type': 'application/json',
'Origin': 'https://ggntw.com',
'Referer': 'https://ggntw.com/'
}
response = requests.post(api_url, json=payload, headers=headers)
data = response.json()
if data.get("result") == 1 and "url" in data:
cdn_url = data["url"]
temp_zip = os.path.join(tempfile.gettempdir(), f"pyppg_{mod_id}.zip")
with requests.get(cdn_url, stream=True) as r:
with open(temp_zip, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
with zipfile.ZipFile(temp_zip, 'r') as zip_ref:
zip_ref.extractall(target_dir)
os.remove(temp_zip)
return True, target_dir, "Success"
else:
return False, None, data.get("error_msg", "Unknown error")
except Exception as e:
logging.error(f"GGNetwork download error: {e}")
return False, None, str(e)
class ModManagerHTTPHandler(BaseHTTPRequestHandler):
def log_message(self, format_str, *args):
logging.info(f"{self.address_string()} - {format_str % args}")
def _send_response(self, status_code, data):
self.send_response(status_code)
self.send_header('Content-type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
self.wfile.write(json.dumps(data).encode('utf-8'))
def do_OPTIONS(self):
self.send_response(204)
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
self.end_headers()
def _find_mod_path(self, mod_id):
directories = [config.mods_dir, config.contraptions_dir]
for directory in directories:
if not os.path.isdir(directory):
continue
for folder_name in os.listdir(directory):
mod_path = os.path.join(directory, folder_name)
if not os.path.isdir(mod_path):
continue
info_file = os.path.join(mod_path, MOD_INFO_FILE)
if os.path.isfile(info_file):
try:
with open(info_file, 'r') as f:
info = json.load(f)
if str(info.get("mod_id")) == str(mod_id):
return mod_path, info
except:
pass
return None, None
def _read_mod_info(self, mod_path):
info_file = os.path.join(mod_path, MOD_INFO_FILE)
if os.path.isfile(info_file):
try:
with open(info_file, 'r') as f:
return json.load(f)
except:
pass
return None
def _write_mod_info(self, mod_path, info):
info_file = os.path.join(mod_path, MOD_INFO_FILE)
try:
with open(info_file, 'w') as f:
json.dump(info, f, indent=4)
return True
except:
return False
def do_GET(self):
parsed_url = urlparse(self.path)
query = parse_qs(parsed_url.query)
mod_id = query.get('id', [None])[0]
path = parsed_url.path
try:
if path == '/status':
self._send_response(200, {"status": "online", "version": "1.5.0"})
elif path == '/is_installed':
if not mod_id:
self._send_response(400, {"error": "Missing mod ID"})
return
mod_path, mod_info = self._find_mod_path(mod_id)
if mod_path and mod_info:
self._send_response(200, {
"mod_id": mod_id,
"installed": True,
"enabled": mod_info.get("enabled", True),
"title": mod_info.get("title", "Unknown"),
"is_contraption": mod_info.get("is_contraption", False)
})
else:
self._send_response(200, {"mod_id": mod_id, "installed": False})
elif path == '/install':
if not mod_id:
self._send_response(400, {"error": "Missing mod ID"})
return
success, message = self._install_mod(mod_id)
self._send_response(200, {"success": success, "message": message})
elif path == '/delete':
if not mod_id:
self._send_response(400, {"error": "Missing mod ID"})
return
success, message = self._delete_mod(mod_id)
self._send_response(200, {"success": success, "message": message})
elif path == '/check_update':
if not mod_id:
self._send_response(400, {"error": "Missing mod ID"})
return
response = self._check_mod_update(mod_id)
self._send_response(200, response)
elif path == '/check_updates':
results = self._check_all_mod_updates()
self._send_response(200, {"success": True, "data": results})
elif path == '/toggle_mod_enabled':
if not mod_id:
self._send_response(400, {"error": "Missing mod ID"})
return
enable = query.get('enable', ['true'])[0].lower() == 'true'
success, message = self._toggle_mod(mod_id, enable)
self._send_response(200, {"success": success, "message": message})
else:
self._send_response(404, {"error": "Endpoint not found"})
except Exception as e:
logging.error(f"Error handling request: {e}")
self._send_response(500, {"error": f"Server error: {e}"})
def _install_mod(self, mod_id):
temp_dir = None
success, download_path, message = self._download_with_steamcmd(mod_id)
if not success:
logging.info(f"SteamCMD failed, trying GGNetwork")
temp_dir = tempfile.mkdtemp(prefix=f"pyppg_mod_{mod_id}_")
success, download_path, message = download_mod_via_ggnetwork(mod_id, temp_dir)
if not success or not download_path:
return False, f"Failed to download mod: {message}"
steam_details = get_mod_details_from_steam(mod_id)
title = steam_details.get("title", f"Mod {mod_id}") if steam_details else f"Mod {mod_id}"
is_contraption = False
if steam_details and "tags" in steam_details:
is_contraption = any(tag.lower() == "contraptions" for tag in steam_details["tags"])
safe_title = sanitize_filename(title)
mod_dirname = f"{safe_title} [{mod_id}]"
if is_contraption:
mod_final_path = os.path.join(config.contraptions_dir, mod_dirname)
else:
mod_final_path = os.path.join(config.mods_dir, mod_dirname)
old_path, _ = self._find_mod_path(mod_id)
if old_path and old_path != mod_final_path:
shutil.rmtree(old_path)
if os.path.exists(mod_final_path):
shutil.rmtree(mod_final_path)
try:
shutil.move(download_path, mod_final_path)
except Exception as e:
return False, f"Failed to install mod: {e}"
if temp_dir is not None and os.path.exists(temp_dir):
try:
shutil.rmtree(temp_dir)
except:
pass
mod_info = {
"mod_id": mod_id,
"title": title,
"folder_name": mod_dirname,
"steam_time_updated": steam_details.get("time_updated", 0) if steam_details else 0,
"local_install_time": int(time.time()),
"enabled": True,
"is_contraption": is_contraption
}
self._write_mod_info(mod_final_path, mod_info)
if is_contraption:
return True, f"Contraption '{title}' installed successfully"
else:
return True, f"Mod '{title}' installed successfully"
def _download_with_steamcmd(self, mod_id):
steamcmd_exe = os.path.join(config.steamcmd_dir, config.steamcmd_exe_name)
if not os.path.isfile(steamcmd_exe):
return False, None, "SteamCMD not found"
download_dir = os.path.join(config.steamcmd_dir, "Downloads")
os.makedirs(download_dir, exist_ok=True)
expected_path = os.path.join(download_dir, "steamapps", "workshop", "content", PPG_APP_ID, mod_id)
if os.path.exists(expected_path):
shutil.rmtree(expected_path)
cmd = [
steamcmd_exe,
"+force_install_dir", download_dir,
"+login", "anonymous",
"+workshop_download_item", PPG_APP_ID, mod_id, "validate",
"+quit"
]
try:
result = subprocess.run(cmd, capture_output=True, text=True, cwd=config.steamcmd_dir)
if result.returncode == 0 and os.path.isdir(expected_path):
return True, expected_path, "SteamCMD download successful"
else:
return False, None, "SteamCMD download failed"
except Exception as e:
return False, None, f"SteamCMD error: {e}"
def _delete_mod(self, mod_id):
mod_path, _ = self._find_mod_path(mod_id)
if not mod_path:
return False, f"Mod {mod_id} not found"
try:
shutil.rmtree(mod_path)
return True, f"Mod deleted successfully"
except Exception as e:
return False, f"Failed to delete mod: {e}"
def _check_mod_update(self, mod_id):
mod_path, local_info = self._find_mod_path(mod_id)
if not mod_path or not local_info:
return {"mod_id": mod_id, "installed": False, "updateAvailable": False}
steam_details = get_mod_details_from_steam(mod_id)
if not steam_details:
return {
"mod_id": mod_id,
"installed": True,
"title": local_info.get("title"),
"updateAvailable": False,
"error": "Could not fetch Steam details",
"enabled": local_info.get("enabled", True),
"is_contraption": local_info.get("is_contraption", False)
}
local_time = local_info.get("steam_time_updated", 0)
steam_time = steam_details["time_updated"]
update_available = steam_time > local_time
return {
"mod_id": mod_id,
"installed": True,
"title": steam_details["title"],
"updateAvailable": update_available,
"localVersionTime": local_time,
"steamVersionTime": steam_time,
"enabled": local_info.get("enabled", True),
"is_contraption": local_info.get("is_contraption", False)
}
def _check_all_mod_updates(self):
results = []
directories = [config.mods_dir, config.contraptions_dir]
for directory in directories:
if not os.path.isdir(directory):
continue
for folder_name in os.listdir(directory):
mod_path = os.path.join(directory, folder_name)
if not os.path.isdir(mod_path):
continue
info = self._read_mod_info(mod_path)
if info and "mod_id" in info:
results.append(self._check_mod_update(info["mod_id"]))
return results
def _toggle_mod(self, mod_id, enable):
mod_path, mod_info = self._find_mod_path(mod_id)
if not mod_path or not mod_info:
return False, f"Mod {mod_id} not found"
mod_info["enabled"] = enable
if self._write_mod_info(mod_path, mod_info):
status = "enabled" if enable else "disabled"
return True, f"Mod '{mod_info.get('title', mod_id)}' is now {status}"
else:
return False, f"Failed to update mod status"
config = ModManagerConfig()
def run_server(port=8000):
server = HTTPServer(('localhost', port), ModManagerHTTPHandler)
logging.info(f"Server started on http://localhost:{port}")
try:
server.serve_forever()
except KeyboardInterrupt:
logging.info("Server shutdown requested")
finally:
server.server_close()
logging.info("Server shut down")
if __name__ == "__main__":
if not os.path.isdir(config.ppg_dir) or \
not os.path.isfile(os.path.join(config.steamcmd_dir, config.steamcmd_exe_name)):
logging.critical("Critical paths invalid. Exiting.")
sys.exit(1)
run_server()