Skip to content

Commit 04f8509

Browse files
authored
Merge pull request #13 from jared-skinner/master
added support for windows registry
2 parents becd685 + 83ee853 commit 04f8509

3 files changed

Lines changed: 94 additions & 42 deletions

File tree

resources/config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import xbmc
2+
import xbmcaddon
3+
4+
__addon__ = xbmcaddon.Addon()
5+
6+
def log(msg, level=xbmc.LOGDEBUG):
7+
if __addon__.getSetting('debug') == 'true' or level != xbmc.LOGDEBUG:
8+
9+
xbmc.log('[%s] %s' % (__addon__.getAddonInfo('id'), msg), level=level)

resources/main.py

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import os
32
import requests
43
import routing
@@ -10,46 +9,17 @@
109
import xbmcaddon
1110
import xbmcgui
1211
import xbmcplugin
12+
import registry
13+
14+
from config import *
1315

1416
__addon__ = xbmcaddon.Addon()
1517

16-
plugin = routing.Plugin()
18+
if os.name == 'nt':
19+
import _winreg
1720

18-
def log(msg, level=xbmc.LOGDEBUG):
19-
20-
if __addon__.getSetting('debug') == 'true' or level != xbmc.LOGDEBUG:
21-
22-
xbmc.log('[%s] %s' % (__addon__.getAddonInfo('id'), msg), level=level)
23-
24-
# https://github.com/lutris/lutris/blob/master/lutris/util/steam.py
25-
def vdf_parse(steam_config_file, config):
26-
"""Parse a Steam config file and return the contents as a dict."""
27-
line = " "
28-
while line:
29-
try:
30-
line = steam_config_file.readline()
31-
except UnicodeDecodeError:
32-
log("Error while reading Steam VDF file {}. Returning {}".format(steam_config_file, config), xbmc.LOGERROR)
33-
return config
34-
if not line or line.strip() == "}":
35-
return config
36-
while not line.strip().endswith("\""):
37-
nextline = steam_config_file.readline()
38-
if not nextline:
39-
break
40-
line = line[:-1] + nextline
41-
42-
line_elements = line.strip().split("\"")
43-
if len(line_elements) == 3:
44-
key = line_elements[1]
45-
steam_config_file.readline() # skip '{'
46-
config[key] = vdf_parse(steam_config_file, {})
47-
else:
48-
try:
49-
config[line_elements[1]] = line_elements[3]
50-
except IndexError:
51-
log('Malformed config file: {}'.format(line), xbmc.LOGERROR)
52-
return config
21+
22+
plugin = routing.Plugin()
5323

5424
@plugin.route('/')
5525
def index():
@@ -139,11 +109,7 @@ def installed():
139109

140110
return
141111

142-
with open(os.path.join(__addon__.getSetting('steam-path'), 'registry.vdf'), 'r') as file:
143-
vdf = vdf_parse(file, {})
144-
apps = vdf['Registry']['HKCU']['Software']['Valve']['Steam']['Apps']
145-
apps = dict((k, v) for (k, v) in apps.iteritems() if v.get('installed', '0') == '1')
146-
112+
apps = registry.get_registry_values(os.path.join(__addon__.getSetting('steam-path'), 'registry.vdf'))
147113
data = response.json()
148114

149115
for entry in data['response']['games']:

resources/registry.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

Comments
 (0)