-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparity_integrations.py
More file actions
389 lines (345 loc) · 13.7 KB
/
Copy pathparity_integrations.py
File metadata and controls
389 lines (345 loc) · 13.7 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
"""External integrations: RA injection, bezels, EmuMovies, screenshots, OBS, high scores."""
from __future__ import annotations
import configparser
import json
import re
import shutil
import subprocess
from datetime import datetime
from pathlib import Path
from urllib.parse import urlencode
from urllib.request import urlopen
from archives import safe_zip_extract
from backend_io import atomic_copy_stream, atomic_write_text, download_file
from state_store import secure_text_write
PLATFORM_BEZEL_REPO = {
"NES": "thebezelproject/bezelproject-NintendoEntertainmentSystem",
"SNES": "thebezelproject/bezelproject-SuperNintendoEntertainmentSystem",
"Nintendo 64": "thebezelproject/bezelproject-Nintendo64",
"Game Boy Advance": "thebezelproject/bezelproject-GameBoyAdvance",
"Sega Genesis": "thebezelproject/bezelproject-SegaMegadrive",
"PlayStation": "thebezelproject/bezelproject-SonyPlayStation",
"Arcade": "thebezelproject/bezelproject-MAME",
}
def inject_retroachievements(credentials, home=None):
home = Path(home or Path.home())
username = str(credentials.get("username") or "").strip()
api_key = str(credentials.get("api_key") or "").strip()
if not username or not api_key:
raise ValueError("RetroAchievements credentials are required.")
updated, skipped = [], []
def write_kv(path: Path, mapping: dict, separator=" = "):
path.parent.mkdir(parents=True, exist_ok=True)
existing = path.read_text() if path.is_file() else ""
lines = existing.splitlines()
keys = set(mapping)
out = []
seen = set()
for line in lines:
matched = False
for key in keys:
if re.match(rf"^\s*{re.escape(key)}\s*=", line):
out.append(f"{key}{separator}{mapping[key]}")
seen.add(key)
matched = True
break
if not matched:
out.append(line)
for key, value in mapping.items():
if key not in seen:
out.append(f"{key}{separator}{value}")
atomic_write_text(path, "\n".join(out) + ("\n" if out else ""), mode=0o600)
updated.append(str(path))
retro_cfgs = [
home / ".config/retroarch/retroarch.cfg",
home / ".var/app/org.libretro.RetroArch/config/retroarch/retroarch.cfg",
]
for cfg in retro_cfgs:
try:
write_kv(cfg, {
"cheevos_enable": "true",
"cheevos_username": f'"{username}"',
"cheevos_password": f'"{api_key}"',
"cheevos_token": f'"{api_key}"',
}, separator=" = ")
except OSError as error:
skipped.append(f"{cfg}: {error}")
dolphin_inis = [
home / ".config/dolphin-emu/Dolphin.ini",
home / ".var/app/org.DolphinEmu.dolphin-emu/config/dolphin-emu/Dolphin.ini",
]
for ini in dolphin_inis:
try:
write_kv(ini, {
"Enabled": "True",
"Username": username,
"Password": api_key,
}, separator=" = ")
except OSError as error:
skipped.append(f"{ini}: {error}")
pcsx2_inis = [
home / ".config/PCSX2/inis/PCSX2.ini",
home / ".var/app/net.pcsx2.PCSX2/config/PCSX2/inis/PCSX2.ini",
]
for ini in pcsx2_inis:
try:
write_kv(ini, {
"Enabled": "true",
"Username": username,
"Token": api_key,
}, separator=" = ")
except OSError as error:
skipped.append(f"{ini}: {error}")
return {"updated": updated, "skipped": skipped}
def bezel_project_urls():
return {
platform: f"https://codeload.github.com/{repo}/zip/refs/heads/master"
for platform, repo in PLATFORM_BEZEL_REPO.items()
}
def download_bezel(platform, dest_dir, opener=urlopen):
urls = bezel_project_urls()
if platform not in urls:
raise ValueError(f"No Bezel Project mapping for {platform}.")
dest = Path(dest_dir)
dest.mkdir(parents=True, exist_ok=True)
archive = dest / f"{platform.replace(' ', '_')}-bezels.zip"
download_file(
urls[platform], archive,
max_bytes=512 * 1024 * 1024,
timeout=120,
opener=opener,
)
extract_to = dest / platform.replace(" ", "_")
if extract_to.is_symlink():
raise ValueError("Bezel extraction destination may not be a symlink.")
# Extract into a staging dir first: a corrupt or unsafe new archive must
# not destroy the user's previously working bezel set.
staging = dest / f".{extract_to.name}.extracting"
if staging.exists():
shutil.rmtree(staging, ignore_errors=True)
staging.mkdir(parents=True, exist_ok=True)
try:
safe_zip_extract(archive, staging)
if extract_to.exists():
shutil.rmtree(extract_to)
staging.replace(extract_to)
finally:
if staging.exists():
shutil.rmtree(staging, ignore_errors=True)
return str(extract_to)
def load_emumovies_credentials(data_dir):
path = Path(data_dir) / "emumovies.json"
if path.is_file():
try:
data = json.loads(path.read_text())
except (OSError, json.JSONDecodeError):
data = {}
if isinstance(data, dict) and data.get("username"):
return data
from env_config import emumovies_from_env
return emumovies_from_env()
def save_emumovies_credentials(data_dir, username, password):
path = Path(data_dir) / "emumovies.json"
secure_text_write(path, json.dumps({"username": username, "password": password}, indent=2))
return {"configured": True}
def download_emumovies_media(game, credentials, media_root, media_type="box"):
username = str((credentials or {}).get("username") or "").strip()
password = str((credentials or {}).get("password") or "").strip()
if not username or not password:
raise ValueError("Configure EmuMovies credentials in Settings first.")
# EmuMovies requires a licensed account; attempt their search endpoint shape.
platform = str(game.get("platform") or "Arcade")
name = str(game.get("name") or "")
query = urlencode({"system": platform, "search": name})
url = f"https://api.emumovies.com/v1/media/{media_type}?{query}"
import base64
token = base64.b64encode(f"{username}:{password}".encode()).decode()
root = Path(media_root) / "emumovies" / re.sub(r"[^a-z0-9]+", "-", name.casefold()).strip("-")
root.mkdir(parents=True, exist_ok=True)
destination = root / f"{media_type}.jpg"
try:
download_file(
url,
destination,
expected_types=("image/",),
max_bytes=32 * 1024 * 1024,
timeout=30,
opener=urlopen,
headers={"Authorization": f"Basic {token}"},
)
except Exception as error: # noqa: BLE001 - surface remote/API failures cleanly
raise ValueError(f"EmuMovies request failed: {error}") from error
return str(destination)
def capture_screenshot(dest_path, window_hint=""):
dest = Path(dest_path)
dest.parent.mkdir(parents=True, exist_ok=True)
commands = []
if shutil.which("gnome-screenshot"):
commands.append(["gnome-screenshot", "-f", str(dest)])
if shutil.which("spectacle"):
commands.append(["spectacle", "-b", "-n", "-o", str(dest)])
if shutil.which("scrot"):
commands.append(["scrot", str(dest)])
if shutil.which("import"):
commands.append(["import", "-window", "root", str(dest)])
last_error = None
for command in commands:
try:
subprocess.run(command, check=True, capture_output=True, timeout=20)
if dest.is_file():
return str(dest)
except (OSError, subprocess.SubprocessError) as error:
last_error = error
raise FileNotFoundError(last_error or "No screenshot tool found (gnome-screenshot, spectacle, scrot, or ImageMagick import).")
def mame_highscore_path(home=None):
home = Path(home or Path.home())
candidates = [
home / ".mame/hi",
home / ".config/mame/hi",
home / ".var/app/org.mamedev.MAME/config/mame/hi",
]
for path in candidates:
if path.exists():
return path
return candidates[0]
def read_local_highscores(game, home=None):
rom = str(game.get("rom_name") or Path(game.get("path", "")).stem).strip()
if not rom:
return []
root = mame_highscore_path(home)
scores = []
for path in root.glob(f"{rom}*"):
try:
size = path.stat().st_size
scores.append({"file": path.name, "size": size, "label": path.stem})
except OSError:
pass
return scores
def obs_recording_directory(home=None):
home = Path(home or Path.home())
profile_roots = (
home / ".config/obs-studio/basic/profiles",
home / ".var/app/com.obsproject.Studio/config/obs-studio/basic/profiles",
)
for profiles in profile_roots:
if not profiles.is_dir():
continue
for profile in sorted(profiles.iterdir(), reverse=True):
ini = profile / "basic.ini"
if not ini.is_file():
continue
parser = configparser.ConfigParser()
try:
parser.read(ini)
except configparser.Error:
continue
for section in ("SimpleOutput", "AdvOut"):
if parser.has_option(section, "FilePath"):
path = parser.get(section, "FilePath").strip()
if path:
return Path(path).expanduser()
if parser.has_option(section, "RecFilePath"):
path = parser.get(section, "RecFilePath").strip()
if path:
return Path(path).expanduser()
return home / "Videos"
def find_latest_recording(directory, since=None):
root = Path(directory).expanduser()
if not root.is_dir():
return None
extensions = {".mp4", ".mkv", ".mov", ".flv", ".webm"}
candidates = []
for path in root.rglob("*"):
if not path.is_file() or path.suffix.casefold() not in extensions:
continue
try:
modified = path.stat().st_mtime
except OSError:
continue
if since and modified < since.timestamp() - 1:
continue
candidates.append((modified, path))
if not candidates:
return None
return str(max(candidates)[1])
def obs_recording_status(home=None):
directory = obs_recording_directory(home)
latest = find_latest_recording(directory)
return {
"running": bool(shutil.which("obs") and _pgrep("obs")),
"recording": bool(_pgrep("obs")),
"directory": str(directory),
"latest_recording": latest,
}
def _pgrep(name):
try:
result = subprocess.run(["pgrep", "-x", name], capture_output=True, text=True, timeout=5, check=False)
return result.returncode == 0
except (OSError, subprocess.SubprocessError):
return False
def attach_recording(game, video_path):
path = Path(video_path).expanduser()
if not path.is_file():
raise FileNotFoundError("Recording file not found.")
game["video_recording"] = str(path)
if not game.get("video"):
game["video"] = str(path)
return str(path)
def auto_attach_obs_recording(game, started, settings=None, home=None):
settings = settings or {}
if settings.get("obs_auto_attach") is False:
return None
directory = settings.get("obs_recording_path") or obs_recording_directory(home)
recording = find_latest_recording(directory, since=started)
if recording:
return attach_recording(game, recording)
return None
def export_highscores(game, export_dir, home=None):
root = mame_highscore_path(home)
export = Path(export_dir)
export.mkdir(parents=True, exist_ok=True)
copied = []
for score in read_local_highscores(game, home):
source = root / score["file"]
if not source.is_file():
continue
destination = export / score["file"]
shutil.copy2(source, destination)
copied.append(str(destination))
manifest = export / "highscores.json"
atomic_write_text(manifest, json.dumps({
"format": 1,
"exported_at": datetime.now().isoformat(timespec="seconds"),
"game": game.get("name"),
"rom": game.get("rom_name") or Path(game.get("path", "")).stem,
"files": copied,
}, indent=2) + "\n")
return {"files": copied, "manifest": str(manifest)}
def import_highscores(game, import_dir, home=None):
folder = Path(import_dir).expanduser()
manifest = folder / "highscores.json"
if manifest.is_file():
try:
payload = json.loads(manifest.read_text())
except (OSError, json.JSONDecodeError) as error:
raise ValueError("High score bundle is invalid.") from error
files = payload.get("files", []) if isinstance(payload, dict) else []
else:
files = [str(path) for path in folder.glob("*") if path.is_file()]
target = mame_highscore_path(home)
target.mkdir(parents=True, exist_ok=True)
rom = str(game.get("rom_name") or Path(game.get("path", "")).stem).strip()
restored = []
for file_path in files:
source = Path(file_path)
if not source.is_file():
source = folder / source.name
if not source.is_file():
continue
destination = target / (source.name if rom in source.name else f"{rom}-{source.name}")
with source.open("rb") as stream:
atomic_copy_stream(stream, destination, mode=0o600)
restored.append(str(destination))
if not restored:
raise FileNotFoundError("No high score files were found in the import bundle.")
return restored