-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacresources.py
executable file
·69 lines (47 loc) · 1.67 KB
/
macresources.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
from subprocess import run, PIPE
import tempfile
def read_rsrc_path(path):
BADCHARS = b'\t$" '
if '//' not in path:
with open(path, 'rb') as f:
return f.read()
else:
path, _, rest = path.partition('//')
rtype, rid, *_ = rest.split('/')
try:
int(rid)
except ValueError:
rid = '"%s"' % rid
type_expr = '\'%s\' (%s)' % (rtype, rid)
rez_code = run(['DeRez', '-only', type_expr, path], stdout=PIPE, check=True).stdout
if len(rez_code) < 2:
raise FileNotFoundError
accum = bytearray()
for l in rez_code.split(b'\n'):
if len(l) >= 6 and l[1:2] == b'$':
hx = l[:43].lstrip(BADCHARS).rstrip(BADCHARS)
accum.extend(bytes.fromhex(hx.decode('ascii')))
return bytes(accum)
def write_rsrc_path(path, data):
STEP = 32
if '//' not in path:
with open(path, 'wb') as f:
f.write(data)
else:
path, _, rest = path.partition('//')
rtype, rid, *args = rest.split('/')
if args:
rname = ', "%s"' % args[0]
args = args[1:]
else:
rname = ""
rid = int(rid)
args = ''.join(', %s' % x for x in args)
with tempfile.NamedTemporaryFile(mode='w', delete=False) as f:
print('data \'%s\' (%d%s%s) {\n' % (rtype, rid, rname, args), file=f)
for o in range(0, len(data), STEP):
chunk = data[o:o+STEP].hex()
print('\t$"%s"' % chunk, file=f)
print('};', file=f)
f.flush()
run(['Rez', '-a', '-o', path, f.name], check=True)