"""Add to Steam (Customize Launcher): the game as a non-Steam shortcut in the player's Steam library, with Navi as its icon and the game's title screen and logo as its pictures (launcher/gameart.py). Windows only. Steam keeps the shortcuts of each account in /userdata//config/shortcuts.vdf, a binary key-value file: a map is 0x00 name 0x00 ... 0x08, a string 0x01 name 0x00 value 0x00, a 32-bit integer 0x02 name 0x00 value (0x07 is a 64-bit one). The file is the map "shortcuts" holding one map per shortcut, named "0", "1", ... Each holds appid, AppName, Exe, StartDir, icon, LaunchOptions and Steam's own fields; the other shortcuts are read and written back as they are, and a shortcut of ours already there (by its name or its id) is updated in place. Its pictures go into config/grid named by the shortcut's id: p.png the cover, .png the wide tile, _hero.png the background, _logo.png the logo, and the icon file the shortcut names. The id is CRC-32 of Exe and AppName as Steam writes them, with the top bit set. Steam reads the file when it starts and writes it back when it closes, so a running Steam would neither see the new shortcut nor keep it: Steam is asked to close (steam.exe -shutdown), the files are written, and Steam starts again. The shortcut starts the launcher's pythonw.exe on data/steam_play.pyw, which starts this launcher with --play: the game runs from its own folder with its console hidden, and the Python process waits for it, so Steam shows the game as running for as long as it is (the game's own Restart included). Starting ocarina.exe directly would open a console window beside the game, and NX2navi.exe ends a few seconds after it started the launcher. """ import os import shutil import struct import subprocess import time import zlib from pathlib import Path NAME = 'The Legend of Zelda Ocarina of Time' PLAY_SCRIPT = 'steam_play.pyw' NO_WINDOW = 0x08000000 if os.name == 'nt' else 0 STEAM64_BASE = 76561197960265728 PLAY_TEXT = '''"""Written by the NX2navi launcher (Customize Launcher > Add to Steam): Steam starts the game through this file. It runs the launcher of app/current.txt with --play, which starts the game and waits for it.""" import os import runpy import sys root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) with open(os.path.join(root, 'app', 'current.txt'), encoding='utf-8') as f: version = f.read().strip() os.environ['NX2NAVI_LAUNCHER_ROOT'] = root sys.argv = [os.path.join(root, 'app', version, 'launcher', 'start.pyw'), '--play'] + sys.argv[1:] runpy.run_path(sys.argv[0], run_name='__main__') ''' class SteamError(Exception): pass # ---------------------------------------------------------------- binary VDF T_MAP, T_STR, T_INT, T_FLOAT, T_PTR, T_WSTR, T_COLOR, T_U64, T_END = 0, 1, 2, 3, 4, 5, 6, 7, 8 def vdf_load(b): """A binary VDF as a list of [type, key, value]; a map's value is such a list. Raises SteamError when malformed.""" def cstr(at): end = b.index(b'\0', at) return b[at:end].decode('utf-8', 'surrogateescape'), end + 1 def walk(at, top): items = [] while True: if at >= len(b): if top: return items, at raise SteamError('shortcuts.vdf ends inside a map') t = b[at] at += 1 if t == T_END: return items, at key, at = cstr(at) if t == T_MAP: v, at = walk(at, False) elif t == T_STR: v, at = cstr(at) elif t in (T_INT, T_FLOAT, T_PTR, T_COLOR): v, at = b[at:at + 4], at + 4 elif t == T_U64: v, at = b[at:at + 8], at + 8 else: raise SteamError(f'shortcuts.vdf holds a value of type {t}') items.append([t, key, v]) try: items, at = walk(0, True) except ValueError: raise SteamError('shortcuts.vdf is cut short') from None return items def vdf_dump(items, top=True): out = bytearray() for t, key, v in items: out += bytes([t]) + key.encode('utf-8', 'surrogateescape') + b'\0' if t == T_MAP: out += vdf_dump(v, False) elif t == T_STR: out += v.encode('utf-8', 'surrogateescape') + b'\0' else: out += v if not top or items: out += bytes([T_END]) return bytes(out) def _get(items, key): for it in items: if it[1].lower() == key.lower(): return it return None def _set(items, t, key, v): it = _get(items, key) if it: it[0], it[2] = t, v else: items.append([t, key, v]) def appid(exe, name): """The id Steam gives a shortcut: CRC-32 of its Exe and AppName fields with the top bit set (unsigned).""" return (zlib.crc32((exe + name).encode('utf-8')) & 0xFFFFFFFF) | 0x80000000 def add_shortcut(vdf_bytes, exe, start_dir, options, icon, name=NAME): """shortcuts.vdf (b'' for none) with our shortcut added or updated; returns (new bytes, id).""" items = vdf_load(vdf_bytes) if vdf_bytes else [] root = _get(items, 'shortcuts') if root is None or root[0] != T_MAP: items = [[T_MAP, 'shortcuts', []]] root = items[0] sid = appid(exe, name) entry = None for it in root[2]: if it[0] != T_MAP: continue n, a = _get(it[2], 'AppName'), _get(it[2], 'appid') if (n and n[0] == T_STR and n[2] == name) or (a and a[0] == T_INT and struct.unpack('