/
hlextract.py
213 lines (189 loc) · 8.04 KB
/
hlextract.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
# Copyright (c) 2011 seb26. All rights reserved.
# Source code is licensed under the terms of the Modified BSD License.
import os
import re
from subprocess import Popen, PIPE, STDOUT
from collections import defaultdict
class HLExtract:
def __init__(self, volatile=False):
if volatile is True:
self.volatile = True
else:
self.volatile = False
def info(self, package, infol, **kwargs):
if type(infol) is list:
info = [ 'info ' + i for i in infol ]
return self.cmd_console(package, info)
else:
return self.cmd_console(package, list('info ' + infol))
def find(self, package, items, **kwargs):
""" Find an item (file or dir) inside the package.
package - package name (if vpk, set to 'pak01_dir.vpk' and also set 'game' appropriately)
items - list or str (if only 1 item to search) of item names to search for.
kwargs:
game - set if vpk
"""
if type(items) is list:
send = [ 'find ' + i for i in items ]
else:
send = list('find ' + items)
res = self.cmd_console(package, send)
resd = defaultdict(list)
if len(res) != 0:
for t in res.items():
item = t[0][5:]
resd[item].append(t[1].values()[0])
return resd
else:
return False
def extract(self, package, outdir, extr, **kwargs):
if os.path.splitext(p)[1] == '.gcf':
pdirname = package.split('\\')[-1] # the filename of the gcf
else:
pdirname = package.split('\\')[-2] # the directory containing the vpk (most commonly the game's abbreviated name)
if 'multidir' in kwargs and kwargs['multidir'] is True:
if type(extr) is list:
edir = {}
temp = defaultdict(list)
for fn in extr:
wdir = os.path.split(fn)[0]
temp[wdir].append(fn) # Add each fn to the appropriate working dir list.
for t in temp.items():
fdir = t[0]
f = t[1]
edir[fdir] = '-e "' + '" -e "'.join(f) + '"'
for t in edir.items():
xpath = os.path.join(outdir, pdirname, t[0])
if not os.path.exists(xpath):
os.makedirs(xpath)
self.cmd_close(package, dir=xpath, extr=t[1], options=kwargs['options'])
else:
raise Exception('multidir is set but given object is not a list of multiple items')
else:
if type(extr) is list:
if len(extr) > 1:
extr_cmd = '-e "' + '" -e "'.join(extr) + '"'
elif len(extr) == 1:
extr_cmd = r'-e "%s"' % extr
elif type(extr) is str:
extr_cmd = r'-e "%s"' % extr
xpath = os.path.join(outdir, pdirname)
if not os.path.exists(xpath):
os.makedirs(xpath)
self.cmd_close(package, dir=xpath, extr=extr_cmd, options=kwargs['options'])
def validate(self, package, validr, **kwargs):
if type(validr) is list:
validr_cmd = '-s -t "' + '" -t "'.join(validr) + '"'
else:
validr_cmd = '-s -t "%s"' % validr
output = self.cmd_output(package, validr_cmd)
if output is None:
return True # Everything validated successfully
else:
return False # There were some issues. Expand on this later (need to get hands on broken GCF for testing).
def defrag(self, package, **kwargs):
""" Defrags package (-f flag). Ignores volatile state.
"""
defrag_cmdl = []
defrag_cmdl.append('-f') # Defrag package.
defrag_cmdl.append('-s') # Silent.
if 'options' in kwargs:
defrag_cmdl.append(self._cmd_options(options))
defrag_cmd = ' '.join(defrag_cmdl)
return None # self.cmd_close(p,
def _cmd_options(self, options):
""" Returns a formatted string when given options.
Example:
'xftv' returns '-x -f -t -v'
['x', 'f', 't', 'v'] returns '-x -f -t -v'
"""
allowed = 'smqvor'
return '-' + ' -'.join([i for i in options if i in allowed])
def cmd_console(self, p, cmdl, **kwargs):
""" Runs a HLExtract -c process and returns results as a dict.
p - package name (if vpk, set to 'pak01_dir.vpk' and also set 'game' appropriately)
cmdl - list containing commands, e.g.: [ 'status', r'info root\tf' ]
Results are returned as a dict with the key name corresponding to the command.
Example:
x = cmd_console(package, [ 'status', r'info root\tf' ])
Results for 'status' and 'info root\tf' are stored in
x['status'] and x['info root\tf'] respectively.
"""
cmd_output = []
cmd_output.append('-p "%s"' % p)
if self.volatile is True:
cmd_output.append('-v')
cmd_output.append('-c')
cmd_output.append('-s') # Need silent on to enable cleaner parsing.
cmdl_x = '-x "' + '" -x "'.join(cmdl) + '"'
cmd_output.append(cmdl_x)
cmd_output.append('-x "exit"') # Closing the process so it doesn't run forever.
cmd = r'bin\HLExtract.exe' + ' ' + ' '.join(cmd_output)
process = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
output = []
for z in process.stdout.readlines():
output.append(z.strip('\r\n'))
d = defaultdict(list)
info = {}
for line in output:
r = re.compile(r'.+?>(.+)')
m = r.match(line)
if m:
key = m.group(1)
elif line == '':
continue
else:
d[key].append(line.strip(' '))
for y in d.items():
x = [ i.split(': ') for i in y[1]]
info[y[0]] = dict((t[0].lower(), t[1]) for t in x if len(t) == 2)
return info
def cmd_close(self, p, **kwargs):
""" Run a HLExtract process and then terminate.
p - package name (if vpk, set to 'pak01_dir.vpk' and also set 'game' appropriately)
kwargs: dir, extr, validr, options
wdir - directory to extract to
extr - str, items to extract
validr - str, items to extract
options - string, additional HLExtract cmdline options.
"""
cmd_close = []
cmd_close.append('-p "%s"' % p)
if self.volatile is True:
cmd_close.append('-v')
if 'dir' in kwargs:
cmd_close.append('-d "%s"' % kwargs['dir'])
if 'extr' in kwargs:
cmd_close.append(kwargs['extr'])
if 'validr' in kwargs:
cmd_close.append(kwargs['validr'])
if 'options' in kwargs:
options = self._cmd_options(kwargs['options'])
cmd = r'bin\HLExtract.exe' + ' ' + ' '.join(cmd_close)
process = os.popen(cmd)
process.close()
def cmd_output(self, p, cmdl, **kwargs):
""" Run HLExtract and return the output.
p - package (needs full path, use getPackagePath())
cmdl - list, containing commands e.g.: [ '-ld', '-f' ]
kwargs:
options - string, containing options
"""
cmd_output = []
cmd_output.append('-p "%s"' % p)
if 'options' in kwargs:
cmd_output.append(self._cmd_options(kwargs['options']))
if self.volatile is True:
cmd_output.append('-v')
if type(cmdl) is list:
cmd_output.extend(cmdl) # Simply extend with the list of commands gien.
else:
cmd_output.append(cmdl)
cmd = r'bin\HLExtract.exe' + ' ' + ' '.join(cmd_output)
""" Begin code that I don't fully understand yet. """
process = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
output = []
for z in process.stdout.readlines():
output.append(z.strip('\r\n'))
process.close()
return output