-
Notifications
You must be signed in to change notification settings - Fork 3
/
fix_director_files.py
69 lines (63 loc) · 2.14 KB
/
fix_director_files.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
#!/usr/bin/python3
import os
from io import BytesIO
from struct import unpack
from sys import argv
def read_ident(f):
end = None
sig = f.read(4)
if sig == b'XFIR':
end = '<'
elif sig == b'RIFX':
end = '>'
return end
def read_tag(f, endian='<'):
s = f.read(4)
if endian == '<':
s = s[::-1]
return(s.decode('ascii'))
def fix_filename(filename, file_type):
if filename.lower()[-4:] == '.dir':
if file_type == 'MV93':
filename = filename[:-4] + '.dxr'
elif file_type == 'FGDM':
filename = filename[:-4] + '.dcr'
elif filename.lower()[-4:] == '.cst':
if file_type == 'MV93':
filename = filename[:-4] + '.cxt'
elif file_type == 'FGDC':
filename = filename[:-4] + '.cct'
return filename
if len(argv) > 1:
for i in range(1, len(argv)):
filename = argv[i]
file_obj = open(filename, 'rb').read()
file_bytes = BytesIO(file_obj)
endian = read_ident(file_bytes)
if endian == None:
print('Not a Director file: ' + filename)
continue
file_bytes.seek(0, 2) # Jump to the end of the file
file_size = file_bytes.tell() # Get the current position
file_bytes.seek(0x4)
correct_file_size, = unpack(endian+'I', file_bytes.read(0x4))
# Length indicated by the header does not include the header itself
correct_file_size += 8
if file_size < correct_file_size:
print('Unable to fix file: ' + filename + ' - the file is missing ' + str(correct_file_size - file_size) + ' bytes!')
continue
elif file_size > correct_file_size:
print('Trimming ' + str(file_size - correct_file_size) + ' bytes from file: ' + filename)
file_bytes.seek(0x8)
file_type = read_tag(file_bytes, endian)
correct_filename = fix_filename(filename, file_type)
file_bytes.seek(0)
correct_file_bytes = file_bytes.read(correct_file_size)
if filename != correct_filename or file_size != correct_file_size:
os.remove(filename)
print('Writing file: ' + correct_filename)
open(correct_filename, 'wb').write(correct_file_bytes)
else:
print('File length and extension are correct: ' + filename)
else:
print('Usage: python fix_director_files.py file1.dxr file2.dcr ...')