-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteamfile.py
293 lines (240 loc) · 8.69 KB
/
steamfile.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env python3
'''Steam-specific path and file functions.'''
import collections
import os
from pathlib import Path
import re
import string
import shutil
__dir__ = (
'dirsize', 'readSteamFile', 'isSteamLibrary', 'isSteamBase', 'getLibraryPaths',
'Game', 'Library')
size = collections.namedtuple('size', 'totalSize fileCount')
def dirsize(path):
'''Get bytes used by directory and amount of files.
> dirsize('/path/to/directory/')
size(134240, 27)
> dirsize('/path/to/file.txt')
size(5836, 1)
> dirsize('notanactualfile')
size(0, 0)'''
if not os.path.exists(path):
return size(0, 0)
elif os.path.isfile(path):
try:
return size(os.lstat(path).st_size, 1)
except os.error:
return size(0, 1)
totalSize = 0
seen = set()
for dirpath, dirs, files in os.walk(path):
for file in files:
file = os.path.join(dirpath, file)
try:
stat = os.lstat(file)
except os.error:
continue
if stat.st_ino in seen:
continue
else:
seen.add(stat.st_ino)
totalSize += stat.st_size
return size(totalSize, len(seen))
def bytesize(size, digits=1, binary=False):
divisor = 1024 if binary else 1000
for prefix in " KMGTPEZY":
if prefix == ' ': prefix = ''
if abs(size) < divisor:
break
elif prefix != "Y":
size /= divisor
return "{} {}{}B".format(round(size, digits), prefix, "i"*bool(prefix and binary))
STEAMENTRY = re.compile(r'^\t"(.+?)"\t\t"(.+?)"')
def readSteamFile(path):
'''Scrapes top-level information from Steam-formatted ACF or VDF file as dictionary.
Any numerical indexes are placed into dict['_list'].'''
info = {'_list': []}
path = Path(path)
with path.open(encoding='utf8') as file:
for line in file.readlines():
result = STEAMENTRY.findall(line)
if not result:
continue
key, value = result[0]
if key.isdigit():
info['_list'].append(value)
else:
info[key] = value
return info
def isSteamBase(path):
'''Return True if the path is the base Steam library, containing /steamapps/libraryfolders.vdf'''
return (Path(path) / 'steamapps' / 'libraryfolders.vdf').is_file()
def isSteamLibrary(path):
'''Return True if the path is a valid Steam library, containing /steamapps/common/'''
return isSteamBase(path) or (Path(path) / 'steamapps' / 'common').is_dir()
#
# extremely OS-dependant steamBaseFinder()
#
if os.name == 'nt':
registryPath = None
try:
import winreg
except ModuleNotFoundError:
pass
else:
try:
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, 'Software\\Valve\\Steam') as key:
registryPath = winreg.QueryValueEx(key, 'SteamPath')[0]
# Make a little nicer
registryPath = Path(registryPath.title().replace('X86', 'x86'))
except FileNotFoundError:
pass
try:
from ctypes import windll
except ModuleNotFoundError:
driveLetters = []
else:
def _doesDriveExist(letter):
return (windll.kernel32.GetLogicalDrives() >> (ord(letter.upper()) - 65) & 1) != 0
driveLetters = [a + ':/' for a in string.ascii_uppercase if _doesDriveExist(a)]
def steamBaseFinder():
if registryPath is not None:
yield registryPath
progfileKeys = ('ProgramFiles', 'ProgramFiles(x86)', 'ProgramW6432')
for i in progfileKeys:
if i in os.environ:
yield Path(os.environ[i]) / 'Steam'
for a in driveLetters:
a = Path(a)
yield a / 'Program Files (x86)' / 'Steam'
yield a / 'Program Files' / 'Steam'
yield a / 'Steam'
else:
_UNIXBASES = (
'~/Library/Application Support/Steam/', #macOS
'~/.local/share/Steam', #Most Linux distros
'~/.steam/' #Some Linux distros
)
def steamBaseFinder():
for i in _UNIXBASES:
yield Path(i).expanduser()
def getSteamBase(log=lambda path: None):
for path in steamBaseFinder():
log(str(path))
if path and isSteamBase(path):
return path
else:
return None
def getLibraryPaths(base=None, log=lambda path: None):
'''Returns a list of paths to Steam libraries found. If 'base' is not provided it will be found.'''
if base is None:
base = getSteamBase(log)
if base is None: #it feels wrong doing that twice
return []
try:
vdf = readSteamFile(Path(base) / 'steamapps' / 'libraryfolders.vdf')
except FileNotFoundError:
return []
return [base] + [Path(i) for i in vdf['_list']]
class SteamFileError(Exception):
pass
class Game:
__slots__ = 'ID name installdir sizeEstimate size'.split()
def __init__(self, ID, name, installdir, sizeEstimate=0, size=None):
self.ID = ID
for i in '(™) ™ (c) (C) (r) (R) © ®'.split():
name = name.replace(i, '')
self.name = name
self.installdir = installdir
self.sizeEstimate = sizeEstimate
self.size = size
def __repr__(self):
size = self.size or self.sizeEstimate or 0
if size:
size = ('~' * self.size is None) + bytesize(size, 2, binary=True)
else:
size = 'Shortcut'
return '<Game {!r} ({})>'.format(
self.name, size)
@classmethod
def fromACF(cls, path):
'''Game, given path to ACF reference.'''
info = readSteamFile(path)
if 'appid' not in info:
raise SteamFileError('Invalid acf reference')
folder = info.get('installdir') or None
installdir = None
if folder:
installdir = Path(os.path.split(path)[0]) / 'common' / folder
ID = info.get('appid')
name = info.get('name') or folder
sizeEstimate = int(info.get('SizeOnDisk', 0))
return cls(ID, name, installdir, sizeEstimate, size=None)
def getSize(self):
if self.size is None:
self.size = dirsize(self.installdir).totalSize
class Drive:
__slots__ = ['path', 'games', 'sizeTotal', 'sizeUsed', 'sizeFree', 'sizeGames']
def __repr__(self):
return '<Library {!r} ({} games, {}{})>'.format(
self.path, len(self.games),
"~" * any(game.size is None for game in self.games),
bytesize(sum(game.size or game.sizeEstimate for game in self.games)
, 2, binary=True))
def __init__(self, path):
p = Path(path)
self.path = p.drive or p.root
self.games = []
self.sizeTotal, self.sizeUsed, self.sizeFree = shutil.disk_usage(str(path))
self.sizeGames = 0
def appendLibrary(self, path, log=lambda *a: None):
'''List of games, given path to library (NOT /steamapps).'''
gamespath = Path(path) / 'steamapps'
for i in os.listdir(gamespath):
if i.startswith('appmanifest_') and i.endswith('.acf'):
try:
game = Game.fromACF(gamespath / i)
self.games.append(game)
except SteamFileError:
log('Invalid file ' + i)
def getSize(self):
for game in self.games:
game.getSize()
self.games.sort(key=lambda g: g.size, reverse=True)
self.sizeGames = sum(game.size for game in self.games)
def shortcutGames(fp):
'''Obtain games linked with shortcuts.'''
# TODO: Fix this hotfix!
return []
file = open(fp, 'rb')
tokens = []
txt = b''
while True:
char = file.read(1)
if not char:
break
if char == b'\0':
if txt:
try:
tokens.append(txt.decode())
except UnicodeDecodeError:
pass
txt = b''
else:
txt += char
file.close()
games = []
game = {}
for i, t in enumerate(tokens):
if t.isnumeric():
if game:
games.append(Game(**game))
game = {}
elif t == '\1appname':
game['name'] = tokens[i+1]
elif t == '\1StartDir':
game['installdir'] = tokens[i+1][1:-1]
elif t == '\1Exe':
# To ensure shortcut games are unique, their ID is the path to their executable.
game['ID'] = tokens[i+1][1:-1]
return games