-
Notifications
You must be signed in to change notification settings - Fork 0
/
fbone_txtcon.py
289 lines (225 loc) · 10.2 KB
/
fbone_txtcon.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env python3.9
import sys
import os
import os.path
import time
import shutil
## Frostbite 1 Texture Converter by Heico
## Requires Python 3.9.6! Other versions may not work.
## Use command line or drag n drop file onto the script to convert
## Supported file formats: .dds .itexture .ps3texture .xenontexture .terrainheightfield
def main():
print("Frostbite 1 Texture Converter by Heico")
if len(sys.argv) != 2:
print("Wrong params! Usage: python3 fbone_txtcon.py sample.itexture")
time.sleep(3)
exit()
file = sys.argv[1]
if not os.path.exists(file):
print("File does not exist! Make sure to enter the path correctly.")
time.sleep(3)
exit()
if not file.endswith(".dds") and not file.endswith(".itexture") and not file.endswith(".ps3texture") and not file.endswith(".xenontexture") and not file.endswith(".terrainheightfield"):
print("Wrong file format! Supported: .dds .itexture .ps3texture .xenontexture .terrainheightfield")
time.sleep(3)
exit()
print("Converting file...")
convertFile(file)
print("DONE")
def convertFile(file: str):
if file.endswith(".dds"):
fileLocation = file.replace(".dds", ".itexture")
shutil.copyfile(file, fileLocation)
convertFileToITexture(fileLocation)
elif file.endswith(".itexture"):
fileLocation = file.replace(".itexture", ".dds")
shutil.copyfile(file, fileLocation)
convertFileToDDS(fileLocation, False)
elif file.endswith(".ps3texture"):
fileLocation = file.replace(".ps3texture", ".dds")
shutil.copyfile(file, fileLocation)
convertFileToDDS(fileLocation, True)
elif file.endswith(".xenontexture"):
fileLocation = file.replace(".xenontexture", ".dds")
shutil.copyfile(file, fileLocation)
convertFileToDDS(fileLocation, True)
elif file.endswith(".terrainheightfield"):
fileLocation = file.replace(".terrainheightfield", ".raw")
shutil.copyfile(file, fileLocation)
convertFileToRaw(fileLocation)
def convertFileToITexture(file: str):
reader = open(file, "rb")
dataMain = reader.read()
reader.close()
imgFormat = dataMain[87]
imgWidth = int.from_bytes(dataMain[16:20], "little")
imgHeight = int.from_bytes(dataMain[12:16], "little")
mipmapCount = int.from_bytes(dataMain[28:32], "little")
mipmapMinSize = 16
headerLength = 128
if imgFormat == 49:
dataHeader = iTextureDXT1
mipmapMinSize = 8
imgFormat = "DXT1 BC1"
elif imgFormat == 51:
dataHeader = iTextureDXT3
imgFormat = "DXT3 BC2"
elif imgFormat == 53:
dataHeader = iTextureDXT5
imgFormat = "DXT5 BC3"
elif imgFormat == 32:
if dataMain[88] == 32:
dataHeader = iTextureARGB
imgFormat = "ARGB8888"
elif dataMain[88] == 8:
dataHeader = iTextureGray
imgFormat = "Grayscale"
if len(dataHeader) > 0:
dataHeader[16] = dataMain[16]
dataHeader[17] = dataMain[17]
dataHeader[18] = dataMain[18]
dataHeader[19] = dataMain[19]
dataHeader[20] = dataMain[12]
dataHeader[21] = dataMain[13]
dataHeader[22] = dataMain[14]
dataHeader[23] = dataMain[15]
dataHeader[28] = dataMain[28]
dataHeader[29] = dataMain[29]
dataHeader[30] = dataMain[30]
dataHeader[31] = dataMain[31]
offset = 32
mipmapSizes = calcMipMapSizes(imgHeight, imgWidth, mipmapCount, mipmapMinSize)
for i in range(mipmapCount):
size = mipmapSizes[i].to_bytes(4, "little")
dataHeader[offset] = size[0]
dataHeader[offset + 1] = size[1]
dataHeader[offset + 2] = size[2]
dataHeader[offset + 3] = size[3]
offset += 4
dataMain = dataMain[headerLength:]
dataMain = dataHeader + dataMain
writer = open(file, "wb")
writer.write(dataMain)
writer.close()
else:
if os.path.exists(file):
os.remove(file)
def convertFileToDDS(file: str, isConsoleTexture: bool):
reader = open(file, "rb")
dataMain = reader.read()
reader.close()
res = dataMain[:3].decode("ascii")
headerLength = 92
if res == "RES":
headerLength = 156
if isConsoleTexture:
dataMain = reverseHeader(dataMain, headerLength)
imgFormat = int.from_bytes(dataMain[8:12], "little")
if res == "RES":
imgFormat = int.from_bytes(dataMain[72:76], "little")
if imgFormat == 0 or imgFormat == 18:
dataHeader = ddsDXT1
imgFormat = "DXT1 BC1"
elif imgFormat == 1:
dataHeader = ddsDXT3
imgFormat = "DXT3 BC2"
elif imgFormat == 2 or imgFormat == 19 or imgFormat == 20 or imgFormat == 13:
dataHeader = ddsDXT5
imgFormat = "DXT5 BC3"
elif imgFormat == 9:
dataHeader = ddsARGB
imgFormat = "ARGB8888"
elif imgFormat == 10:
dataHeader = ddsGray
imgFormat = "Grayscale"
if len(dataHeader) > 0:
if res == "RES":
dataHeader[16] = dataMain[80]
dataHeader[17] = dataMain[81]
dataHeader[18] = dataMain[82]
dataHeader[19] = dataMain[83]
dataHeader[12] = dataMain[84]
dataHeader[13] = dataMain[85]
dataHeader[14] = dataMain[86]
dataHeader[15] = dataMain[87]
dataHeader[28] = dataMain[92]
dataHeader[29] = dataMain[93]
dataHeader[30] = dataMain[94]
dataHeader[31] = dataMain[95]
dataHeader[20] = dataMain[96]
dataHeader[21] = dataMain[97]
dataHeader[22] = dataMain[98]
dataHeader[23] = dataMain[99]
else:
dataHeader[16] = dataMain[16]
dataHeader[17] = dataMain[17]
dataHeader[18] = dataMain[18]
dataHeader[19] = dataMain[19]
dataHeader[12] = dataMain[20]
dataHeader[13] = dataMain[21]
dataHeader[14] = dataMain[22]
dataHeader[15] = dataMain[23]
dataHeader[28] = dataMain[28]
dataHeader[29] = dataMain[29]
dataHeader[30] = dataMain[30]
dataHeader[31] = dataMain[31]
dataHeader[20] = dataMain[32]
dataHeader[21] = dataMain[33]
dataHeader[22] = dataMain[34]
dataHeader[23] = dataMain[35]
dataMain = dataMain[headerLength:]
dataMain = dataHeader + dataMain
writer = open(file, "wb")
writer.write(dataMain)
writer.close()
else:
if os.path.exists(file):
os.remove(file)
def convertFileToRaw(file: str):
reader = open(file, "rb")
data = reader.read()
reader.close()
headerLength = 49
if data[0] != 0:
headerLength = 45
data = data[headerLength:]
writer = open(file, "wb")
writer.write(data)
writer.close()
def calcMipMapSizes(height, width, mipmapCount, minSize):
mipmapSize = int(max(1, ((width + 3) / 4))) * int(max(1, ((height + 3) / 4))) * minSize
mipmaps = [mipmapSize]
for i in range(mipmapCount - 1):
if mipmapSize != minSize:
mipmapSize /= 4
mipmaps.append(int(mipmapSize))
return mipmaps
def reverseHeader(data, length):
offset = 0
while (True):
if offset >= length:
break
data = reverseFourByteBlock(data, offset)
offset += 4
return data
def reverseFourByteBlock(data, offset):
temp0 = data[offset]
temp1 = data[offset + 1]
temp2 = data[offset + 2]
temp3 = data[offset + 3]
data[offset] = temp3
data[offset + 1] = temp2
data[offset + 2] = temp1
data[offset + 3] = temp0
return data
ddsDXT1 = bytearray.fromhex("444453207C00000007100A00000400000004000000000800000000000B000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000040000004458543100000000000000000000000000000000000000000810400000000000000000000000000000000000")
ddsDXT3 = bytearray.fromhex("444453207C00000007100A00000200000004000000000800000000000B000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000040000004458543300000000000000000000000000000000000000000810400000000000000000000000000000000000")
ddsDXT5 = bytearray.fromhex("444453207C00000007100A00000200000002000000000400000000000A000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000040000004458543500000000000000000000000000000000000000000810400000000000000000000000000000000000")
ddsGray = bytearray.fromhex("444453207C00000007100200000200000002000000000000000000000A000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000020000002020202008000000FF0000000000000000000000000000000810400000000000000000000000000000000000")
ddsARGB = bytearray.fromhex("444453207C00000007100A0000010000000100000000040000000000090000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000004100000020202020200000000000FF0000FF0000FF000000000000FF0810400000000000000000000000000000000000")
iTextureDXT1 = bytearray.fromhex("030000000000000000000000010000000004000000040000000000000B000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
iTextureDXT3 = bytearray.fromhex("030000000000000001000000030000000004000000020000000000000B000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
iTextureDXT5 = bytearray.fromhex("030000000000000002000000010000000004000000020000000000000B000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
iTextureGray = bytearray.fromhex("03000000000000000A000000020000000002000000020000000000000A000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
iTextureARGB = bytearray.fromhex("0300000000000000090000000100000080000000400000000000000001000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
main()