Project Chimera
Меню

Исходники

313 строк · 14 КиБ

tools/hints/build_hints.py

1#!/usr/bin/env python3
2"""Сборка набора подсказок совместимости `hints.json` для Chimera-Launcher.
3
4Четыре открытых источника, каждый хранится своим слоем со своей
5лицензией, ничего не смешивается:
6
7- ProtonDB — официальный дамп отчётов (github.com/bdefore/protondb-data),
8 ODbL 1.0 / DbCL 1.0. Слой `protondb` — производная база данных, и
9 распространяется под той же ODbL (см. NOTICE.md).
10- PortProton — пресеты на exe (Castro-Fidel/PortWINE,
11 data_from_portwine/scripts/portwine_db/*.ppdb), MIT.
12- protonfixes — фиксы по Steam AppID (Open-Wine-Components/umu-protonfixes,
13 gamefixes-steam/*.py), BSD 2-Clause. Разбираются через ast без исполнения:
14 берутся только вызовы util.* из белого списка с литеральными аргументами.
15- AreWeAntiCheatYet — статус античитов (games.json), MIT.
16
17Запуск:
18 tools/hints/build_hints.py --protondb reports_piiremoved.json --out out/
19
20Скачивает PortProton, protonfixes и AreWeAntiCheatYet сам (git/https),
21дамп ProtonDB берёт файлом: он весит ~70 МБ в архиве, и качать его на
22каждый прогон незачем. Выход: out/hints.json и out/NOTICE.md.
23"""
24
25import argparse
26import ast
27import collections
28import datetime
29import json
30import pathlib
31import re
32import subprocess
33import tempfile
34import urllib.request
35
36PORTPROTON_REPO = "https://github.com/Castro-Fidel/PortWINE.git"
37PORTPROTON_DIR = "data_from_portwine/scripts/portwine_db"
38PROTONFIXES_REPO = "https://github.com/Open-Wine-Components/umu-protonfixes.git"
39AWACY_URL = "https://raw.githubusercontent.com/AreWeAntiCheatYet/AreWeAntiCheatYet/HEAD/games.json"
40
41YEAR = 365 * 24 * 3600
42ENV_RE = re.compile(r"""(?:^|\s)([A-Z][A-Z0-9_]{2,})=("[^"]*"|'[^']*'|[^\s"']+)""")
43
44
45def git_rev(path):
46 return subprocess.check_output(["git", "-C", str(path), "rev-parse", "--short", "HEAD"], text=True).strip()
47
48
49def proton_label(r):
50 v = r.get("variant")
51 custom = (r.get("customProtonVersion") or "").strip()
52 ver = (r.get("protonVersion") or "").strip()
53 if v == "native":
54 return "native"
55 if v == "experimental":
56 return "Proton Experimental"
57 if v == "ge":
58 return custom or ver or None
59 if v in ("official", "older"):
60 return f"Proton {ver}" if ver else None
61 return custom or (f"Proton {ver}" if ver else None)
62
63
64def env_from_launch(opts):
65 head = opts.split("%command%")[0]
66 out = []
67 for k, v in ENV_RE.findall(head):
68 v = v.strip(chr(34) + chr(39))
69 # Пустое значение (LD_PRELOAD= против оверлея Steam) — не настройка.
70 if v and len(v) <= 100:
71 out.append(f"{k}={v}")
72 return out
73
74
75def build_protondb(path):
76 reports = json.load(open(path))
77 now = max(r.get("timestamp", 0) for r in reports)
78 games = {}
79 for r in reports:
80 # В дампе appId — то строка, то число (у ~31 тыс. отчётов из 390):
81 # без приведения отчёты одной игры делились на два ведра, а в JSON
82 # выходило два одинаковых ключа.
83 appid = str((r.get("app") or {}).get("steam", {}).get("appId") or "")
84 if not appid.isdigit():
85 continue
86 res = r.get("responses") or {}
87 g = games.setdefault(appid, {
88 "title": (r.get("app") or {}).get("title") or "",
89 "total": 0, "works": 0, "recent": 0, "recent_works": 0,
90 "oob_tried": 0, "oob_works": 0, "anticheat_hit": 0,
91 "_proton": collections.Counter(), "_env": collections.Counter(),
92 })
93 works = res.get("verdict") == "yes"
94 recent = now - r.get("timestamp", 0) <= YEAR
95 g["total"] += 1
96 g["works"] += works
97 if recent:
98 g["recent"] += 1
99 g["recent_works"] += works
100 if res.get("triedOob") == "yes":
101 g["oob_tried"] += 1
102 g["oob_works"] += res.get("verdictOob") == "yes"
103 if res.get("isImpactedByAntiCheat") == "yes":
104 g["anticheat_hit"] += 1
105 if works and now - r.get("timestamp", 0) <= 2 * YEAR:
106 label = proton_label(res)
107 if label:
108 g["_proton"][label] += 1
109 if res.get("launchOptions"):
110 g["_env"].update(set(env_from_launch(res["launchOptions"])))
111 for g in games.values():
112 g["proton"] = [[k, n] for k, n in g.pop("_proton").most_common(3)]
113 g["env"] = [[k, n] for k, n in g.pop("_env").most_common(5) if n >= 2]
114 for k in [k for k, v in g.items() if v in (0, [], "")]:
115 del g[k]
116 snapshot = datetime.datetime.fromtimestamp(now, datetime.UTC).strftime("%Y-%m-%d")
117 return games, snapshot, len(reports)
118
119
120def parse_ppdb(text):
121 exes, rating, env = [], None, {}
122 for line in text.splitlines():
123 s = line.strip()
124 if s.startswith("#") and s.lower().endswith(".exe"):
125 exes.append(s[1:].strip().lower())
126 elif s.startswith("#Rating="):
127 val = s.split("=", 1)[1].strip()
128 rating = int(val) if val.isdigit() else None
129 elif line.startswith("export PW_"):
130 k, _, v = line[len("export "):].partition("=")
131 v = v.split(" #")[0].strip().strip('"').strip("'")
132 env[k.strip()] = v
133 return exes, rating, env
134
135
136def build_portproton(work):
137 repo = work / "PortWINE"
138 subprocess.run(["git", "clone", "-q", "--depth", "1", "--filter=blob:none", "--sparse", PORTPROTON_REPO, str(repo)], check=True)
139 subprocess.run(["git", "-C", str(repo), "sparse-checkout", "set", PORTPROTON_DIR], check=True)
140 out = {}
141 for f in sorted((repo / PORTPROTON_DIR).glob("*.ppdb")):
142 exes, rating, env = parse_ppdb(f.read_text(errors="replace"))
143 if not exes or not env:
144 continue
145 entry = {"preset": f.name, "env": env}
146 if rating:
147 entry["rating"] = rating
148 verbs = env.get("PW_DLL_INSTALL", "").split()
149 if verbs:
150 entry["verbs"] = verbs
151 for exe in exes:
152 out.setdefault(exe, entry)
153 return out, git_rev(repo)
154
155
156FIX_CALLS = {
157 "protontricks", "winedll_override", "wineexe_override", "set_environment", "del_environment",
158 "append_argument", "replace_command", "disable_esync", "disable_fsync", "disable_nvapi",
159 "disable_ntsync", "regedit_add", "set_cpu_topology_limit", "set_cpu_topology_nsamt",
160 "set_dxvk_option", "set_game_drive", "install_eac_runtime", "install_battleye_runtime",
161}
162
163
164def literal(node):
165 if isinstance(node, ast.Attribute) and isinstance(node.value, ast.Attribute) and node.value.attr == "OverrideOrder":
166 return node.attr
167 return ast.literal_eval(node)
168
169
170def parse_fix(source):
171 tree = ast.parse(source)
172 calls, partial = [], False
173 for fn in tree.body:
174 if not isinstance(fn, ast.FunctionDef) or fn.name not in ("main", "early"):
175 continue
176 for stmt in fn.body:
177 if isinstance(stmt, ast.Expr) and isinstance(stmt.value, ast.Constant):
178 continue
179 call = stmt.value if isinstance(stmt, ast.Expr) else None
180 if (
181 isinstance(call, ast.Call)
182 and isinstance(call.func, ast.Attribute)
183 and isinstance(call.func.value, ast.Name)
184 and call.func.value.id == "util"
185 and call.func.attr in FIX_CALLS
186 ):
187 try:
188 calls.append([fn.name, call.func.attr, [literal(a) for a in call.args]])
189 continue
190 except (ValueError, TypeError, SyntaxError):
191 pass
192 partial = True
193 return calls, partial
194
195
196def build_protonfixes(work):
197 repo = work / "umu-protonfixes"
198 subprocess.run(["git", "clone", "-q", "--depth", "1", PROTONFIXES_REPO, str(repo)], check=True)
199 out = {}
200 for f in sorted((repo / "gamefixes-steam").glob("*.py")):
201 if not f.stem.isdigit() or f.is_symlink() and not f.exists():
202 continue
203 try:
204 calls, partial = parse_fix(f.read_text(errors="replace"))
205 except SyntaxError:
206 continue
207 if not calls:
208 continue
209 entry = {"calls": calls}
210 verbs = [v for _, name, args in calls if name == "protontricks" for v in args if isinstance(v, str)]
211 if verbs:
212 entry["verbs"] = verbs
213 if partial:
214 entry["partial"] = True
215 out[f.stem] = entry
216 return out, git_rev(repo)
217
218
219def build_awacy():
220 games = json.load(urllib.request.urlopen(AWACY_URL, timeout=60))
221 out = {}
222 for g in games:
223 appid = (g.get("storeIds") or {}).get("steam")
224 if appid and str(appid).isdigit():
225 out[str(appid)] = {"status": g.get("status"), "anticheats": g.get("anticheats") or [], "name": g.get("name")}
226 return out
227
228
229NOTICE = """# Chimera-Launcher: подсказки совместимости — лицензии источников
230
231Файл `hints.json` составлен из четырёх открытых источников. Каждый лежит
232в своём слое (`sources`, `by_appid.<id>.<слой>`; пресеты PortProton — `by_exe.<exe>`)
233и распространяется на условиях своего источника.
234
235## ProtonDB — слой `protondb`
236
237Данные ProtonDB (https://www.protondb.com), дамп {protondb_snapshot}
238из https://github.com/bdefore/protondb-data, {protondb_reports} отчётов.
239База данных — Open Database License 1.0
240(https://opendatacommons.org/licenses/odbl/1-0/), содержимое — Database
241Contents License 1.0 (https://opendatacommons.org/licenses/dbcl/1-0/).
242Слой `protondb` — производная база данных (агрегаты по Steam AppID: число
243отчётов, доля рабочих, частые версии Proton и переменные окружения) и
244распространяется под той же ODbL 1.0. Весь слой целиком — этот файл.
245
246## PortProton — слой `portproton`
247
248Пресеты PortProton (https://github.com/Castro-Fidel/PortWINE, коммит
249{portproton_rev}), MIT License, Copyright (c) 2026 Castro-Fidel
250(linux-gaming.ru).
251
252## protonfixes — слой `protonfixes`
253
254Фиксы umu-protonfixes (https://github.com/Open-Wine-Components/umu-protonfixes,
255коммит {protonfixes_rev}), BSD 2-Clause License, Copyright (c) 2018,
256Chris Simons. Взяты только литеральные вызовы util.* из функций main/early;
257файлы с логикой помечены `partial`.
258
259## AreWeAntiCheatYet — слой `anticheat`
260
261https://github.com/AreWeAntiCheatYet/AreWeAntiCheatYet, MIT License,
262Copyright © 2021 Starz0r, Curve.
263"""
264
265
266def main():
267 ap = argparse.ArgumentParser()
268 ap.add_argument("--protondb", required=True, help="reports_piiremoved.json из дампа ProtonDB")
269 ap.add_argument("--out", required=True)
270 args = ap.parse_args()
271 out = pathlib.Path(args.out)
272 out.mkdir(parents=True, exist_ok=True)
273
274 with tempfile.TemporaryDirectory() as tmp:
275 work = pathlib.Path(tmp)
276 protondb, snapshot, n_reports = build_protondb(args.protondb)
277 portproton, pp_rev = build_portproton(work)
278 protonfixes, pf_rev = build_protonfixes(work)
279 anticheat = build_awacy()
280
281 by_appid = collections.defaultdict(dict)
282 for appid, g in protondb.items():
283 by_appid[appid]["protondb"] = g
284 for appid, f in protonfixes.items():
285 by_appid[appid]["protonfixes"] = f
286 for appid, a in anticheat.items():
287 by_appid[appid]["anticheat"] = a
288
289 hints = {
290 "schema": "chimera-launcher.hints",
291 "version": 1,
292 "generated": datetime.datetime.now(datetime.UTC).strftime("%Y-%m-%dT%H:%M:%SZ"),
293 "sources": {
294 "protondb": {"license": "ODbL-1.0", "snapshot": snapshot, "reports": n_reports,
295 "url": "https://github.com/bdefore/protondb-data"},
296 "portproton": {"license": "MIT", "commit": pp_rev, "url": "https://github.com/Castro-Fidel/PortWINE"},
297 "protonfixes": {"license": "BSD-2-Clause", "commit": pf_rev,
298 "url": "https://github.com/Open-Wine-Components/umu-protonfixes"},
299 "anticheat": {"license": "MIT", "url": "https://github.com/AreWeAntiCheatYet/AreWeAntiCheatYet"},
300 },
301 "by_appid": dict(sorted(by_appid.items(), key=lambda kv: int(kv[0]))),
302 "by_exe": dict(sorted(portproton.items())),
303 }
304 (out / "hints.json").write_text(json.dumps(hints, ensure_ascii=False, separators=(",", ":")))
305 (out / "NOTICE.md").write_text(NOTICE.format(
306 protondb_snapshot=snapshot, protondb_reports=n_reports, portproton_rev=pp_rev, protonfixes_rev=pf_rev))
307 print(f"appid: {len(by_appid)} (protondb {len(protondb)}, protonfixes {len(protonfixes)}, "
308 f"anticheat {len(anticheat)}); exe: {len(portproton)}")
309
310
311if __name__ == "__main__":
312 main()
313