diff options
| author | Danilo M. <danix@danix.xyz> | 2026-11-18 16:42:38 +0100 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-11-18 16:42:38 +0100 |
| commit | 22bb2d5f5494c99acceb4eb9d055df9860fa2c0d (patch) | |
| tree | edcc3ad8ff58c02ab8ab41f24e37aafa596d722c | |
| parent | b733c1a926693f67ad803c06db74a54c83f37dfb (diff) | |
| download | deck-it-22bb2d5f5494c99acceb4eb9d055df9860fa2c0d.tar.gz deck-it-22bb2d5f5494c99acceb4eb9d055df9860fa2c0d.zip | |
feat(games): parse Steam VDF and emulator game list caches
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| -rw-r--r-- | deckit/modules/games.py | 82 | ||||
| -rw-r--r-- | tests/test_games.py | 58 |
2 files changed, 140 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 diff --git a/tests/test_games.py b/tests/test_games.py new file mode 100644 index 0000000..4c41367 --- /dev/null +++ b/tests/test_games.py @@ -0,0 +1,58 @@ +# deck-it - tests for games +# 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. + +import sys +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) + +import unittest + +import deckit.modules.games as games + + +def lp(s: str) -> bytes: + """A gamelist.cache string: little-endian u32 length, then the bytes.""" + b = s.encode() + return len(b).to_bytes(4, "little") + b + + +def cache(*entries) -> bytes: + """A gamelist.cache with (path, serial, title) entries and junk between.""" + return b'GLCE"\0\0\0' + b"".join(lp(p) + lp(s) + lp(t) + b"\x01\x02\0\0\x9f" for p, s, t in entries) + + +class Parsers(unittest.TestCase): + def test_vdf_nested(self): + d = games.vdf('"A"\n{\n\t"name"\t\t"Game \\"X\\""\n\t"apps"\n\t{\n\t\t"10"\t"1"\n\t}\n}\n') + self.assertEqual(d, {"A": {"name": 'Game \\"X\\"', "apps": {"10": "1"}}}) + + def test_get_ignores_case(self): + self.assertEqual(games._get({"Software": {"valve": {"x": "1"}}}, "software", "Valve", "x"), "1") + self.assertIsNone(games._get({"a": "1"}, "a", "b")) + + def test_cache_serials(self): + data = cache(("/g/A (Europe).cue", "SCES-01615", "A (Europe)"), + ("/g/B.iso", "SLES-53559", "B - Most Wanted")) + self.assertEqual(games.cache_serials(data), + {"A (Europe)": "SCES-01615", "B - Most Wanted": "SLES-53559"}) + + def test_cache_serials_garbage(self): + self.assertEqual(games.cache_serials(b"\xff" * 64), {}) + + +if __name__ == "__main__": + unittest.main() |
