-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsort.py
223 lines (205 loc) · 9.95 KB
/
sort.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
import os
import shutil
import subprocess
import re
import platform
def sort(in_string, validType):
in_file_sb = open("tmp", "wb")
out_file_txt = open("tmp2", "w")
if validType == True:
in_file_sb.close()
out_file_txt.close()
os.remove("tmp")
os.remove("tmp2")
in_file_sb = open("soundbank." + in_string, "rb")
out_file_txt = open("list." + in_string, "w")
else:
in_file_sb.close()
out_file_txt.close()
os.remove("tmp")
os.remove("tmp2")
in_file_sb = open("soundbank", "rb")
out_file_txt = open("list", "w")
in_file_sb.seek(0,2)
sbSize = in_file_sb.tell()
in_file_sb.seek(0)
sbNames = []
sNames = []
sbStart = []
objIDs = []
sCounts = []
in_file_sb.seek(0)
RTableOffset = []
objID = 0
sCount = 0
objIDs = []
objNames = []
sbNamesinBank = []
while in_file_sb.tell() < sbSize - 1024:
readTest = in_file_sb.read(4)
sNamesinBank = []
in_file_sb.seek(-4,1)
if readTest == b'1TAD':
sbStart.append(in_file_sb.tell())
in_file_sb.seek(12,1)
sizeCheck = int.from_bytes(in_file_sb.read(4), "little")
in_file_sb.seek(sbStart[len(sbStart) - 1] + 32)
if sizeCheck == 3:
in_file_sb.seek(-12,1)
NTStart = int.from_bytes(in_file_sb.read(4), "little") + sbStart[len(sbStart) - 1]
NTEnd = int.from_bytes(in_file_sb.read(4), "little") + sbStart[len(sbStart) - 1] + NTStart
#sb_search = in_file_sb.read(4096)
#in_file_sb.seek(-4096, 1)
#if sb_search.find(b'WAVEfmt') < 0:
#continue
in_file_sb.seek(NTStart + 12)
sbName = ''
sbRead = in_file_sb.read(1)
while sbRead != b'\x00':
sbStr = str(sbRead, 'UTF-8')
sbName += sbStr
sbRead = in_file_sb.read(1)
sbNames.append(sbName.split(".")[0])
#print(sbName)
#print(in_file_sb.tell())
sCount = 0
sCounts.append(0)
in_file_sb.seek(NTStart)
while in_file_sb.tell() < NTEnd:
sName = ""
sRead = in_file_sb.read(1)
if in_file_sb.tell() >= NTEnd:
break
if sRead == b'\x00':
while sRead == b'\x00':
sRead = in_file_sb.read(1)
if in_file_sb.tell() >= NTEnd:
break
if not sRead.isupper() and not sRead.islower():
#print("#1 : " + str(sRead))
break
while sRead != b'\x00' and int.from_bytes(sRead, "little") < 123:
try:
sStr = str(sRead, 'UTF-8')
except:
break
sName += sStr
sRead = in_file_sb.read(1)
if in_file_sb.tell() >= NTEnd:
break
#print(sName)
if len(sName) < 6:
#print("#2 + " + sName)
#print(sbName.split(".")[0])
in_file_sb.seek(-1,1)
break
#if sName.find("VFX_SWITCH_LOADER") >= 0:
#break
#if sName.startswith("Play") == True:
#break
#if sName.startswith("Stop") == True:
#break
sNames.append(sName)
sNamesinBank.append(sName)
out_file_txt.write(sName + "\n")
#print(sName)
sCount += 1
sCounts[len(sCounts) - 1] = sCount
in_file_sb.seek(sbStart[len(sbStart) - 1] + 20)
RTableOffset = (int.from_bytes(in_file_sb.read(4), "little") + sbStart[len(sbStart) - 1])
RTableSize = int(int.from_bytes(in_file_sb.read(4), "little") / 16)
in_file_sb.seek(RTableOffset)
for j in range(RTableSize):
in_file_sb.seek(12,1)
objID = int.from_bytes(in_file_sb.read(2), "little")
if len(sNamesinBank) > j:
objIDs.append(objID)
objNames.append(sNamesinBank[j])
in_file_sb.seek(sbStart[len(sbStart) - 1] + 8)
finalJump = int.from_bytes(in_file_sb.read(4), "little")
in_file_sb.seek(sbStart[len(sbStart) - 1] + finalJump - 4)
sbNamesinBank.append(sNamesinBank)
in_file_sb.seek(1,1)
in_file_sb.seek(0)
in_file_sb.close()
sbOff = 0
RIFFOff = 0
nameCount = 0
bankPath = "banks"
if validType == True:
bankPath = "banks_" + in_string
print(str(len(sbNames)) + " subbanks")
print(str(len(sNames)) + " sounds")
out_file_txt.close()
if os.path.exists(bankPath + os.path.sep + "txtp_sorted"):
shutil.rmtree(bankPath + os.path.sep + "txtp_sorted")
checked = []
for j in range(len(sbNames)):
if sbNames[j] not in os.listdir(bankPath + os.path.sep + "txtp"):
continue
for i in range(len(sbNamesinBank[j])):
#print(sbNamesinBank[j][i])
for file in sorted(os.listdir(bankPath + os.path.sep + "txtp" + os.path.sep + sbNames[j])):
if not file.endswith(".txtp"):
continue
fileName = file.split(" ")[0].replace("~", "-")[0].split(" (")[0]
fileName2 = file.replace(" {m}", "").replace(" {r}", "").replace(" {s}", "").replace(" ", "").split(".txtp")[0]
if platform.system() == "Windows":
fileName2 = fileName2.replace('/', '\\')
#fileName2 = re.sub('[(]MUSIC_STATE_GROUP[=]\S+[)]\s?[(]', '(', fileName2)
fileName2 = re.sub('[(]\\S+[=]', '(', fileName2)
fileName2 = fileName2.replace("~", "")
if sbNamesinBank[j][i] in fileName:
fileName = sbNamesinBank[j][i]
#Dupes = False
print("Extracting " + fileName)
bankCheck = open(bankPath + os.path.sep + "txtp" + os.path.sep + sbNames[j] + os.path.sep + file, "r")
bankCheckRead = bankCheck.read()
bankEmbedSearch = re.search("wem/.+[.]bnk", bankCheckRead)
bankCheck.close()
if not os.path.exists(bankPath + os.path.sep + "txtp_sorted" + os.path.sep + sbNames[j]):
os.makedirs(bankPath + os.path.sep + "txtp_sorted" + os.path.sep + str(sbNames[j]))
fileOpen = open(bankPath + os.path.sep + "txtp" + os.path.sep + sbNames[j] + os.path.sep + file, "r")
fileRead = fileOpen.read()
fileOpen.close()
bankDeps = re.findall('[.][.]/[.][.]/wem.+[.]wem', fileRead)
if len(bankDeps) > 1:
for rcount in range(len(bankDeps)):
if ".bnk" in bankDeps[rcount]:
bankDeps[rcount] = bankDeps[rcount].split(" ")[0] + " " + bankDeps[rcount].split(" ")[1] + "\n##" + bankDeps[rcount].split("##")[1]
if platform.system() == "Windows":
bankDeps[rcount] = bankDeps[rcount].replace("/", "\\")
if bankDeps[rcount] in checked:
continue
offset = 1
if os.path.exists(bankPath + os.path.sep + 'txtp_sorted' + os.path.sep + sbNames[j] + os.path.sep + fileName + '_' + str(rcount + offset) + '.txtp'):
while os.path.exists(bankPath + os.path.sep + 'txtp_sorted' + os.path.sep + sbNames[j] + os.path.sep + fileName + '_' + str(rcount + offset) + '.txtp'):
offset += 1
if os.path.exists(bankPath + os.path.sep + 'txtp_sorted' + os.path.sep + sbNames[j] + os.path.sep + fileName2 + '_' + str(rcount + offset) + ".txtp"):
continue
if platform.system() == "Windows":
os.system('echo ' + fr"{bankDeps[rcount]}" + ' > "' + bankPath + os.path.sep + 'txtp_sorted' + os.path.sep + sbNames[j] + os.path.sep + fr"{fileName2}" + '_' + str(rcount + offset) + '.txtp"')
else:
os.system('echo "' + fr"{bankDeps[rcount]}" + '" > "' + bankPath + os.path.sep + 'txtp_sorted' + os.path.sep + sbNames[j] + os.path.sep + fr"{fileName2}" + '_' + str(rcount + offset) + '.txtp"')
#checked.append(bankDeps[rcount])
elif len(bankDeps) == 1:
if ".bnk" in bankDeps[0]:
bankDeps[0] = bankDeps[0].split(" ")[0] + " " + bankDeps[0].split(" ")[1] + "\n##" + bankDeps[0].split("##")[1].split(".")[0]
if platform.system() == "Windows":
bankDeps[0] = bankDeps[0].replace("/", "\\")
if bankDeps[0] in checked:
continue
if os.path.exists(bankPath + os.path.sep + 'txtp_sorted' + os.path.sep + sbNames[j] + os.path.sep + fileName2 + ".txtp"):
continue
if platform.system() == "Windows":
os.system('echo ' + fr"{bankDeps[0]}" + ' > "' + bankPath + os.path.sep + 'txtp_sorted' + os.path.sep + sbNames[j] + os.path.sep + fr"{fileName2}" + '.txtp"')
else:
os.system('echo "' + fr"{bankDeps[0]}" + '" > "' + bankPath + os.path.sep + 'txtp_sorted' + os.path.sep + sbNames[j] + os.path.sep + fr"{fileName2}" + '.txtp"')
#checked.append(bankDeps[0])
#Dupes = True
if validType == True:
os.remove("list." + in_string)
else:
os.remove("list")
#in_string = input("Rift Apart WWise Bank Language Suffix: ")
#sort(in_string)