aboutsummaryrefslogtreecommitdiffstats
path: root/deckit/modules/games.py
blob: c14de2e5a68a9eeece0060a21da5a0b34f4085dc (plain)
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
# 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