This repository has been archived by the owner on Aug 27, 2023. It is now read-only.
forked from C4nf3ng/Nier2Blender
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdat_unpacker.py
216 lines (185 loc) · 6.08 KB
/
dat_unpacker.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
#encoding = utf-8
import os
import sys
import struct
from .util import to_int
def little_endian_to_float(bs):
return struct.unpack("<f", bs)[0]
def little_endian_to_int(bs):
return int.from_bytes(bs, byteorder='little')
def create_dir(dirpath):
if not os.path.exists(dirpath):
os.makedirs(dirpath)
def read_header(fp):
Magic = fp.read(4)
if list(Magic) == [68, 65, 84, 0]:
FileCount = little_endian_to_int(fp.read(4))
FileTableOffset = little_endian_to_int(fp.read(4))
ExtensionTableOffset = little_endian_to_int(fp.read(4))
NameTableOffset = little_endian_to_int(fp.read(4))
SizeTableOffset = little_endian_to_int(fp.read(4))
hashMapOffset = little_endian_to_int(fp.read(4))
print(
'''FileCount: %08x
FileTableOffset: %08x
ExtensionTableOffset:%08x
NameTableOffset:%08x
SizeTableOffset:%08x
hashMapOffset:%08x
'''%
(FileCount, FileTableOffset, ExtensionTableOffset,NameTableOffset,SizeTableOffset,hashMapOffset)
)
return (FileCount, FileTableOffset, ExtensionTableOffset,NameTableOffset,SizeTableOffset,hashMapOffset)
else:
print('[-] error magic number detected')
return False
def get_fileinfo(fp, index, FileTableOffset, ExtensionTableOffset, NameTableOffset, SizeTableOffset):
fp.seek(FileTableOffset + index * 4)
FileOffset = little_endian_to_int(fp.read(4))
fp.seek(ExtensionTableOffset + index * 4)
Extension = fp.read(4).decode('utf-8')
fp.seek(SizeTableOffset + index * 4)
Size = little_endian_to_int(fp.read(4))
fp.seek(NameTableOffset)
FilenameAlignment = little_endian_to_int(fp.read(4))
i = 0
while i < index:
if list(fp.read(FilenameAlignment))[FilenameAlignment-1] == 0:
i += 1
Filename = fp.read(256).split(b'\x00')[0].decode('ascii')
print(
'''
FileIndex: %d
Filename: %s
FileOffset: %08x
Size: %08x
Extension: %s'''%
(index,Filename,FileOffset,Size,Extension)
)
return index,Filename,FileOffset,Size,Extension
def extract_file(fp, filename, FileOffset, Size, extract_dir):
create_dir(extract_dir)
fp.seek(FileOffset)
FileContent = fp.read(Size)
outfile = open(extract_dir + '/'+filename,'wb')
print("extracting file %s to %s/%s"%(filename,extract_dir,filename))
outfile.write(FileContent)
outfile.close()
if filename.find('wtp') > -1 and False: # Removed due to not needed anymore when using Blender DTT import.
wtp_fp = open(extract_dir + '/'+filename,"rb")
content = wtp_fp.read(Size)
dds_group = content.split(b'DDS ')
dds_group = dds_group[1:]
for i in range(len(dds_group)):
print("unpacking %s to %s/%s"%(filename,extract_dir ,filename.replace('.wtp','_%d.dds'%i)))
dds_fp = open(extract_dir + '/'+filename.replace('.wtp','_%d.dds'%i), "wb")
dds_fp.write(b'DDS ')
dds_fp.write(dds_group[i])
dds_fp.close()
wtp_fp.close()
#os.remove("%s/%s"%(extract_dir,filename))
print("done")
def get_all_files(path):
pass
def extract_hashes(fp, extract_dir, FileCount, hashMapOffset, fileNamesOffset):
create_dir(extract_dir)
# file_order.metadata
# Filename Size
fp.seek(fileNamesOffset)
fileNameSize = little_endian_to_int(fp.read(4))
# Filenames
fileNames = []
for i in range(FileCount):
fileNames.append(fp.read(fileNameSize))
# Extraction
filename = 'file_order.metadata'
extract_dir_sub = extract_dir + '\\' + filename
outfile = open(extract_dir_sub,'wb')
# Header
outfile.write(struct.pack('<i', FileCount))
outfile.write(struct.pack('<i', fileNameSize))
#Filenames
for fileName in fileNames:
outfile.write(fileName)
outfile.close()
# hash_data.metadata
# Header
fp.seek(hashMapOffset)
preHashShift = to_int(fp.read(4))
bucketOffsetsOffset = to_int(fp.read(4))
hashesOffset = to_int(fp.read(4))
fileIndicesOffset = to_int(fp.read(4))
# Bucket Offsets
fp.seek(hashMapOffset + bucketOffsetsOffset)
bucketOffsets = []
while fp.tell() < (hashMapOffset + hashesOffset):
bucketOffsets.append(to_int(fp.read(2)))
# Hashes
fp.seek(hashMapOffset + hashesOffset)
hashes = []
for i in range(FileCount):
hashes.append(fp.read(4))
# File Indices
fp.seek(hashMapOffset + fileIndicesOffset)
fileIndices = []
for i in range(FileCount):
fileIndices.append(to_int(fp.read(2)))
# Extraction
filename = 'hash_data.metadata'
extract_dir_sub = extract_dir + '\\' + filename
outfile = open(extract_dir_sub,'wb')
# Header
outfile.write(struct.pack('<i', preHashShift))
outfile.write(struct.pack('<i', bucketOffsetsOffset))
outfile.write(struct.pack('<i', hashesOffset))
outfile.write(struct.pack('<i', fileIndicesOffset))
# Bucket Offsets
for i in bucketOffsets:
#print(bucketOffsets)
outfile.write(struct.pack('<H', i))
# Hashes
for i in hashes:
outfile.write(i)
# File Indices
for i in fileIndices:
#print(i)
outfile.write(struct.pack('<H', i))
outfile.close()
def main(filename, extract_dir, ROOT_DIR):
fp = open(filename,"rb")
headers = read_header(fp)
if headers:
FileCount, FileTableOffset, ExtensionTableOffset,NameTableOffset,SizeTableOffset,hashMapOffset = headers
for i in range(FileCount):
extract_dir_sub = ''
index,Filename,FileOffset,Size,Extension = get_fileinfo(fp, i, FileTableOffset,ExtensionTableOffset, NameTableOffset,SizeTableOffset)
if extract_dir != '':
extract_dir_sub = extract_dir + '\\' + filename.replace(ROOT_DIR ,'')
extract_file(fp, Filename, FileOffset, Size, extract_dir_sub)
extract_hashes(fp, extract_dir, FileCount, hashMapOffset, NameTableOffset)
return Filename
if __name__ == '__main__':
extract_dir = ''
dirname = ''
useage = "\nUseage:\npython dat_unpacker.py your_dat_path your_extract_path"
useage1 = "\nUseage:\nblender --background --python dat_unpacker.py your_dat_path your_extract_path"
if len(sys.argv) < 3:
print(useage)
exit()
if len(sys.argv) > 2:
dir_name = sys.argv[1]
extract_dir = sys.argv[2]
print()
if os.path.split(sys.argv[0])[-1].lower().find("blender") >-1:
if len(sys.argv) < 6:
print(useage1)
exit()
dir_name = sys.argv[4]
extract_dir = sys.argv[5]
if not os.path.exists(extract_dir):
create_dir(extract_dir)
ROOT_DIR = dir_name
for dirpath,dirnames,filename in os.walk(dir_name):
for file in filename:
filename = "%s\%s"%(dirpath,file)
main(filename, extract_dir, ROOT_DIR)