/
tool_TextureFinder.py
364 lines (298 loc) · 13.1 KB
/
tool_TextureFinder.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#by Durik256
#install:
#Download Noesis
#Download this script or copy as txt and rename to "tool_TextureFinder.py
#Drop this script to '..\Noesis\Plugins\Python'
#Tools>Display Plugin Tools (once)
#Tools>Texture Finder
from inc_noesis import *
import os
from ctypes import *
import noewin
def registerNoesisTypes():
handle = noesis.registerTool("&Texture Finder", TextureFinderMethod)
handle = noesis.register("Texture Finder", ".TextureFinder")
noesis.setHandlerTypeCheck(handle, CheckType)
noesis.setHandlerLoadRGBA(handle, LoadRGBA)
noesis.setHandlerWriteRGBA(handle, WriteRGBA)
return 1
FilePath = ""
def CheckType(data):
if data[:4] == b'TEX ' or data[:4] == b'TEMP':
return 1
return 0
def LoadRGBA(data, texList):
name = rapi.getInputName()
if data[:4] == b'TEX ':
bs = NoeBitStream(data)
bs.seek(4)#magic
w, h = bs.read('2I')
data = rapi.imageDecodeRaw(data[12:], w, h, 'r8g8b8a8')
else:
arg = data.decode().split(';')
ofs = toInt(arg[1], 'offset')
w = toInt(arg[4], 'width')
if arg[5] == 'max':
h = 2**32
else:
h = toInt(arg[5], 'height')
fw = int(arg[9])#9
fh = int(arg[10])#10
fmt = arg[6]
skw = toInt(arg[2], 'skew')
bits = parseFormat(fmt)
raw = bits
pal = int(arg[11])#11
pofs = toInt(arg[3], 'palette ofsset /or/ num MipMap')
bpp = toInt(arg[7], 'bpp')
mip = int(arg[12])#12
flg = int(arg[13])#13
swzl = int(arg[14])
pstp = arg[8]
dxt, texFmt = False, noesis.NOESISTEX_DXT1
if 'dxt' in fmt.lower():
dxt = True
if bits == 1:
bits = 4
elif bits == 3:
bits = 8
texFmt = noesis.NOESISTEX_DXT3
elif bits == 5:
bits = 8
texFmt = noesis.NOESISTEX_DXT5
else:
bits = 0
if pal:
bits = bpp
print('offset:',ofs,'\nsize: %ix%i'%(w,h),'\nformat:',fmt,'\nskew:',skw,'\nbits per pixel:',bits)
print('flip:\n Horizontal:',fw,'\n Vertical:',fh)
if pal:
print('palette offset /or/ mipmap:',pofs,'\nbpp: ', bpp,'\nflag:',flg)
with open(FilePath,'rb') as f:
bs = NoeFileStream(f)
bs.seek(ofs)
max = ceil((bs.fileSize - ofs)/(w*bits/8))
if dxt: max = max//4*4
print("max:",max)
if h > max:
h = max
size, eof, add = ceil(w*h*bits/8 + (skw*h*8)), bs.fileSize - ofs, 0
if eof < size:
add = size - eof
size = eof
print('size file:',bs.fileSize,'\nsize on prewiev:',size+add)
if not skw:
data = bs.readBytes(size) + b'\xFF'*add
else:
data = b''
for y in range(h):
data += bs.readBytes(ceil(w*bits/8))
bs.seek(skw,1)
data += b'\xFF'*add
if dxt:
data = rapi.imageDecodeDXT(data, w, h, texFmt)
elif pal:
if mip:
if pofs:
bs.seek((w*h)//(pofs*2),1)
else:
bs.seek(pofs)
size, eof, add = ceil(raw*256/8), bs.fileSize - bs.tell(), 0
if eof < size:
add = size - eof
size = eof
palette = bs.readBytes(size) + b'\xFF'*add
print('size palette:',len(palette))
if swzl:
if pstp == 'PSP':
data = rapi.imageUntwiddlePSP(data, w, h, bpp)
elif pstp == 'PS2':
data = rapi.imageUntwiddlePS2(data, w, h, bpp)
data = rapi.imageDecodeRawPal(data, palette, w, h, bpp, fmt, flg)
else:
data = rapi.imageDecodeRaw(data, w, h, fmt)
if fw or fh:
data = rapi.imageFlipRGBA32(data, w, h, fh, fw)
#if swizzle:
texList.append(NoeTexture(name, w, h, data, noesis.NOESISTEX_RGBA32))
return 1
def swizzle():
dest = [width * height]
destOffset = 0;
# Incorporate the bpp into the width
width = (width * 8) >> 3;
rowblocks = (width / 16);
for y in range(height):
for x in range(width):
blockX = x / 16
blockY = y / 8
blockIndex = blockX + ((blockY) * rowblocks)
blockAddress = blockIndex * 16 * 8
dest[destOffset] = data[blockAddress + (x - blockX * 16) + ((y - blockY * 8) * 16)]
destOffset+=1
# ---- end unswizzling code ---- \\
def ceil(i):
return int(-1 * (i) // 1 * -1)
def parseFormat(fmt):
try:
x = ''.join(c if c.isdigit() else ' ' for c in fmt)
return sum([int(i) for i in x.split()])
except:
print('Error parse format', fmt)
return 0
def toInt(s, n='value'):
try:
return int(s)
except:
print('Error convert', n)
return 0
def WriteRGBA(data, w, h, writer):
writer.writeBytes(b'TEX ')
writer.writeUInt(w)
writer.writeUInt(h)
#WRITE TEXTURE
data = rapi.imageEncodeRaw(data, w, h, 'r8g8b8a8')
writer.writeBytes(data)
return 1
#---------------------------------------------------
def dialogOpenFile(wind, controlId, wParam, lParam):
FileName = noesis.userPrompt(noesis.NOEUSERVAL_FILEPATH, "Open File", "Select any File", noesis.getSelectedFile())
global FilePath
wind.userControls[18].setText('')
FilePath = ''
if FileName != None:
FilePath = str(FileName)
wind.userControls[18].setText(os.path.basename(FileName))
OpenTemp(wind, id, wParam, lParam)
def OpenTemp(wind, id, wParam, lParam):
template = 'TEMP;'
for x in [0,4,9]:#textbox
template += str(wind.userControls[x].getText().strip()) + ';'
for x in [1,2,3,10,30]:#combobox
x = wind.userControls[x]
l = noewin.user32.GetWindowTextLengthW(x.hWnd) + 1
textBuffer = noewin.create_unicode_buffer(l)
noewin.user32.GetWindowTextW(x.hWnd, textBuffer, l)
template += textBuffer.value.strip() + ';'
for x in [5,6,7,8,11,29]:#checkbox
template += ('1' if wind.userControls[x].isChecked() else '0') + ';'
if os.path.exists(FilePath):
with open('temp_load.TextureFinder', 'w') as w:
w.write(template)
noesis.openAndRemoveTempFile('temp_load.TextureFinder')
else:
print('Error file exists!', FilePath)
print(template)
def ChekedMethod(wind, id, wParam, lParam):
checkBox = wind.getControlById(id)
checkBox.setChecked(noewin.BST_UNCHECKED if checkBox.isChecked() else noewin.BST_CHECKED)
if checkBox == wind.userControls[7]:
DisablePallete(wind, False)
if checkBox.isChecked():
DisablePallete(wind, True)
elif checkBox == wind.userControls[8]:
wind.userControls[9].setText('0')
noewin.user32.SetWindowTextW(wind.userControls[20].hWnd, "offset:")
if checkBox.isChecked():
noewin.user32.SetWindowTextW(wind.userControls[20].hWnd, "mipMap:")
def DisablePallete(wind, enabled):
for x in [8,9,10,11,12,13]:#29,30-swizzle; range(8,14)
noewin.user32.EnableWindow(wind.userControls[x].hWnd, enabled)
def TextureFinderMethod(toolIndex):
noesis.logPopup()
wind = noewin.NoeUserWindow("TextureFinder by(Durik256)", "TextureFinderClass", 235, 316, noewin.defaultWindowProc)
rect = noewin.getNoesisWindowRect()
if rect:
wind.x = rect[0] + 64
wind.y = rect[1] + 64
if not wind.createWindow():
print("Failed to create window.")
return 0
wind.setFont("Arial", 14)
ctrl = wind.userControls
wind.createEditBox(60, 40, 143, 20, text = '0', isMultiLine = False) #0-Offset
wind.createComboBox(60, 70, 70, 14, None, style = noewin.CBS_DROPDOWN | noewin.CBS_HASSTRINGS) #1-width
wind.createComboBox(149, 70, 70, 14, None, style = noewin.CBS_DROPDOWN | noewin.CBS_HASSTRINGS) #2-height
wind.createComboBox(60, 100, 159, 20, None, style = noewin.CBS_DROPDOWN | noewin.CBS_HASSTRINGS)#3-format
wind.createEditBox(60, 134, 54, 20, text = '0', isMultiLine = False) #4-skew
wind.createCheckBox("x", 173, 132, 30, 24, ChekedMethod) #5-flipX
wind.createCheckBox("", 203, 132, 16, 24, ChekedMethod) #6-flipY
wind.createCheckBox("palette:", 10, 162, 90, 24, ChekedMethod) #7-palette
wind.createCheckBox("mipMap", 140, 162, 90, 24, ChekedMethod) #8-mipMap
wind.createEditBox(60, 192, 143, 20, text = '0', isMultiLine = False) #9-OffsetPallete
wind.createComboBox(60, 222, 70, 20, None, style = noewin.CBS_DROPDOWN | noewin.CBS_HASSTRINGS) #10-BBP
wind.createCheckBox("flag", 140, 221, 90, 24, ChekedMethod) #11-flag
#fix height comboBox
noewin.user32.SendMessageW(wind.userControls[1].hWnd, noewin.CB_SETITEMHEIGHT, -1, 14)
noewin.user32.SendMessageW(wind.userControls[2].hWnd, noewin.CB_SETITEMHEIGHT, -1, 14)
noewin.user32.SendMessageW(wind.userControls[10].hWnd, noewin.CB_SETITEMHEIGHT, -1, 14)
#UpDownNumeric ▲▼▾▴
wind.createButton('▴', 203, 191, 16, 11, ChangeValue)#12 - pofs
wind.createButton('▾', 203, 202, 16, 11, ChangeValue)#13 - pofs
wind.createButton('▴', 114, 133, 16, 11, ChangeValue)#14 - skew
wind.createButton('▾', 114, 144, 16, 11, ChangeValue)#15 - skew
wind.createButton('▴', 203, 39, 16, 11, ChangeValue)#16 - gofs
wind.createButton('▾', 203, 50, 16, 11, ChangeValue)#17 - gofs
wind.createEditBox(10, 10, 188, 20, isMultiLine = False)#18 - field file
wind.createButton("...", 198, 9, 22, 22, dialogOpenFile)#19
#static
wind.createStatic("offset:", 10, 192, 40, 20)#20
wind.createStatic("offset:", 10, 41, 40, 20)#21
wind.createStatic("size:", 10, 71, 40, 20)#22
wind.createStatic("x", 136, 71, 10, 20)#23
wind.createStatic("format:", 10, 101, 45, 20)#24
wind.createStatic("skew:", 10, 135, 45, 20)#25
wind.createStatic("flip:", 140, 135, 45, 20)#26
wind.createStatic("BPP:", 10, 224, 40, 20)#27
wind.createButton("run", 170, 251, 50, 22, OpenTemp)#28
#add Swizzle
wind.createCheckBox("swizzle", 10, 249, 65, 24, ChekedMethod) #29-swizzle
wind.createComboBox(75, 251, 55, 20, None, style = noewin.CBS_DROPDOWNLIST | noewin.CBS_HASSTRINGS)#30
noewin.user32.SendMessageW(wind.userControls[30].hWnd, noewin.CB_SETITEMHEIGHT, -1, 14)
wind.userControls[30].addString('PSP')
wind.userControls[30].addString('PS2')
wind.userControls[30].selectString('PSP')
#add items in combobox
i = 2048
for x in range(12):
wind.userControls[1].addString(str(i))
wind.userControls[2].addString(str(i))
i //=2
wind.userControls[2].addString('max')
wind.userControls[1].selectString('512')
wind.userControls[2].selectString('512')
formats = ['r16g16b16a16', 'r16g16b16',
'r8g8b8a8' , 'r8g8b8',
'r5g6b5' , 'r5g5b5a1',
'r4g4b4a4' , 'r3g3b2',
'r2g2b2a2' , 'DXT1',
'DXT3' , 'DXT5']
for x in formats:
wind.userControls[3].addString(x)
wind.userControls[3].selectString('r8g8b8a8')
wind.userControls[10].addString('4')
wind.userControls[10].addString('8')
wind.userControls[10].selectString('8')
DisablePallete(wind, False)
return 0
#UpDownNumerick change value
def calc(id, i, wind):
s = ''.join(c for c in '0'+wind.userControls[id].getText() if c.isdigit())
s = int(s) + (i)
if s < 0: s = 0
wind.userControls[id].setText(str(s))
def ChangeValue(wind, id, wParam, lParam):
btn = wind.getControlById(id)
if btn == wind.userControls[12]:#12 - pofsUp
calc(9, 1, wind)
elif btn == wind.userControls[13]:#13 - pofsDown
calc(9, -1, wind)
elif btn == wind.userControls[14]:#14 - skewUp
calc(4, 1, wind)
elif btn == wind.userControls[15]:#15 - skewDown
calc(4, -1, wind)
elif btn == wind.userControls[16]:#16 - gofsUp
calc(0, 1, wind)
elif btn == wind.userControls[17]:#17 - gofsDown
calc(0, -1, wind)
OpenTemp(wind, id, wParam, lParam)