diff options
Diffstat (limited to 'deckit/modules')
| -rw-r--r-- | deckit/modules/games.py | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/deckit/modules/games.py b/deckit/modules/games.py new file mode 100644 index 0000000..c14de2e --- /dev/null +++ b/deckit/modules/games.py @@ -0,0 +1,82 @@ +# deck-it - games module +# Copyright (C) 2026 Danilo M. <danix@danix.xyz> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +"""Now playing for PC games: Steam, PCSX2 and DuckStation. + +The game is the first Hyprland window that is one. Its name, cover and total +playtime come from the launcher's own files, so no account is needed. +""" + +import re + + +def vdf(text: str) -> dict: + """Valve's KeyValues text: "key" "value" pairs and "key" { ... } blocks.""" + root, stack, key = {}, [], None + cur = root + for m in re.finditer(r'"((?:[^"\\]|\\.)*)"|([{}])', text): + s, brace = m.groups() + if brace == "{": + new = {} + cur[key] = new + stack.append(cur) + cur, key = new, None + elif brace == "}": + cur, key = (stack.pop() if stack else cur), None + elif key is None: + key = s + else: + cur[key], key = s, None + return root + + +def _get(d, *keys): + """d[k1][k2]..., keys matched without case (KeyValues ignores it).""" + for k in keys: + if not isinstance(d, dict): + return None + d = next((v for kk, v in d.items() if kk.lower() == k.lower()), None) + return d + + +def _lstr(data: bytes, i: int): + """The length-prefixed string at i and the offset after it, or (None, i).""" + if i + 4 > len(data): + return None, i + n = int.from_bytes(data[i:i + 4], "little") + if not 0 < n <= 4096 or i + 4 + n > len(data): + return None, i + try: + return data[i + 4:i + 4 + n].decode(), i + 4 + n + except UnicodeDecodeError: + return None, i + + +def cache_serials(data: bytes) -> dict: + """title -> serial from an emulator's cache/gamelist.cache, whose entries + (PCSX2 and DuckStation alike) start with path, serial and title strings.""" + out = {} + for i in range(len(data) - 5): + if data[i + 4] != 0x2F: # an entry's path starts with "/" + continue + path, j = _lstr(data, i) + if not path: + continue + serial, j = _lstr(data, j) + title, _ = _lstr(data, j) + if serial and title: + out.setdefault(title, serial) + return out |
