|
| 1 | +''' |
| 2 | +get registry values for steam games |
| 3 | +''' |
| 4 | + |
| 5 | +import os, _winreg |
| 6 | +import xbmc |
| 7 | +from config import log |
| 8 | + |
| 9 | +# https://github.com/lutris/lutris/blob/master/lutris/util/steam.py |
| 10 | +def vdf_parse(steam_config_file, config): |
| 11 | + """Parse a Steam config file and return the contents as a dict.""" |
| 12 | + line = " " |
| 13 | + while line: |
| 14 | + try: |
| 15 | + line = steam_config_file.readline() |
| 16 | + except UnicodeDecodeError: |
| 17 | + log("Error while reading Steam VDF file {}. Returning {}".format(steam_config_file, config), xbmc.LOGERROR) |
| 18 | + return config |
| 19 | + if not line or line.strip() == "}": |
| 20 | + return config |
| 21 | + while not line.strip().endswith("\""): |
| 22 | + nextline = steam_config_file.readline() |
| 23 | + if not nextline: |
| 24 | + break |
| 25 | + line = line[:-1] + nextline |
| 26 | + |
| 27 | + line_elements = line.strip().split("\"") |
| 28 | + if len(line_elements) == 3: |
| 29 | + key = line_elements[1] |
| 30 | + steam_config_file.readline() # skip '{' |
| 31 | + config[key] = vdf_parse(steam_config_file, {}) |
| 32 | + else: |
| 33 | + try: |
| 34 | + config[line_elements[1]] = line_elements[3] |
| 35 | + except IndexError: |
| 36 | + log('Malformed config file: {}'.format(line), xbmc.LOGERROR) |
| 37 | + return config |
| 38 | + |
| 39 | +def is_installed(app_id): |
| 40 | + app = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, "Software\\Valve\\Steam\\Apps\\" + app_id) |
| 41 | + try: |
| 42 | + i = 0 |
| 43 | + while 1: |
| 44 | + name, value, type = _winreg.EnumValue(app, i) |
| 45 | + if name == "Installed": |
| 46 | + return value |
| 47 | + i += 1 |
| 48 | + except WindowsError: |
| 49 | + return None |
| 50 | + |
| 51 | +def get_registry_values(registry_path): |
| 52 | + app_dict = {} |
| 53 | + |
| 54 | + if os.name == 'nt': |
| 55 | + apps = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, "Software\\Valve\\Steam\\Apps") |
| 56 | + try: |
| 57 | + i = 0 |
| 58 | + while True: |
| 59 | + app_id = _winreg.EnumKey(apps, i) |
| 60 | + #print(app_id) |
| 61 | + installed = is_installed(app_id) |
| 62 | + if installed is None: |
| 63 | + i += 1 |
| 64 | + continue |
| 65 | + else: |
| 66 | + app_dict[app_id] = installed |
| 67 | + i += 1 |
| 68 | + |
| 69 | + except WindowsError: |
| 70 | + pass |
| 71 | + else: |
| 72 | + with open(registry_path, 'r') as file: |
| 73 | + vdf = vdf_parse(file, {}) |
| 74 | + apps = vdf['Registry']['HKCU']['Software']['Valve']['Steam']['Apps'] |
| 75 | + app_dict = dict((k, v) for (k, v) in apps.iteritems() if v.get('installed', '0') == '1') |
| 76 | + |
| 77 | + return app_dict |
0 commit comments