
scale_value = 0
	
	
if ( (getfiles "D:\logger_maxscript.txt").count != 0 ) then (
	logfile="D:\logger_maxscript.txt"
	openLog "D:\logger_maxscript.txt" mode:"a" outputOnly:true
	)	
	
	
	
fname = getOpenFileName \
caption:"Open .mesh from Mesh folder" \
types:"Halo5Mesh (*.*)|*.*" \
historyCategory:"Halo5MeshPresets"	
	

-- f__model = fopen fname "rb"   --"D:\\needler\\needler.model" "rb"
-- f__model = fopen "D:\\plasma_rifle_h2\\plasma_rifle_h2.model" "rb"

f_header = fopen fname "rb"  

fseek f_header -64 #seek_end
--values.X * (cinfo.x_max - cinfo.x_min) + cinfo.x_min

if((findString fname "storm_masterchief.render_model" ) != undefined  ) do ( 
	fseek f_header 0x06d39ca #seek_set
	)


X_MIN = readfloat f_header
X_MAX = readfloat f_header
X_VALUE = X_MAX - X_MIN
print("X_VALUE: " + (X_VALUE as string)  + "		X_MIN: "+  (X_MIN as string) + "		X_MAX: "  + (X_MAX as string))
Y_MIN = readfloat f_header
Y_MAX = readfloat f_header
Y_VALUE = Y_MAX - Y_MIN
print("Y_VALUE: " + (Y_VALUE as string)  + "		Y_MIN: "+  (Y_MIN as string) + "		Y_MAX: "  + (Y_MAX as string))
Z_MIN = readfloat f_header
Z_MAX = readfloat f_header
Z_VALUE = Z_MAX - Z_MIN
print("Z_VALUE: " + (Z_VALUE as string)  + "		Z_MIN: "+  (Z_MIN as string) + "		Z_MAX: "  + (Z_MAX as string))
U_MIN = readfloat f_header
U_MAX = readfloat f_header
U_VALUE = U_MAX - U_MIN
print("U_VALUE: " + (U_VALUE as string)  + "		U_MIN: "+  (U_MIN as string) + "		U_MAX: "  + (U_MAX as string))
V_MIN = readfloat f_header
V_MAX = readfloat f_header
V_VALUE = V_MAX - V_MIN
print("V_VALUE: " + (V_VALUE as string)  + "		V_MIN: "+  (V_MIN as string) + "		V_MAX: "  + (V_MAX as string))
W_MIN = readfloat f_header
W_MAX = readfloat f_header
W_VALUE = W_MAX - W_MIN
print("W_VALUE: " + (W_VALUE as string)  + "		W_MIN: "+  (W_MIN as string) + "		W_MAX: "  + (W_MAX as string))

scale_value =  V_VALUE * U_VALUE * X_VALUE * Y_VALUE * Z_VALUE

if(scale_value == 0) do (
	X_VALUE = 1
	Y_VALUE = 1
	Z_VALUE = 1
	U_VALUE = 1
	V_VALUE = 1
	)


fclose f_header

file_array = getFiles (fname + "[*")



for i = 1 to file_array.count do (
print("FILENAME:	" + file_array[i])

try (

f__mesh = fopen (file_array[i])  "rb"                    --                fopen "D:\\needler\\needler.render_model[0_mesh resource!_]" "rb"

struct data_chunk1 (
	public
	offset_block = 0,
	size_block = 0,	
	types= 0,
	offset_block2 = 0,
	data_coding = 0,  
	element_number=0,
	m = 0
	)
	
data_chunk1_array = #()

vert_arrays = #()
UV_arrays = #()
face_arrays = #()
m_array = #()
ms = #()
	
vert_array = #()
UV_array = #()
face_array = #()


print f__mesh

fseek f__mesh 0x20 #seek_set

v1 = #()
for i = 1 to 0x0C do (
	val = (readlong f__mesh #unsigned)
	append v1 val
	)
	
	
	
print v1

print("")
print("offset: 0x0" + (bit.intAsHex (ftell f__mesh)) +"   ")
print("")



for i = 1 to v1[1] do (
	vv1 = #()
	
	vv1s = (i as string + "	||	")
	for i = 1 to 0x010 do (
		val = readbyte f__mesh #unsigned
		vv1s = vv1s + "	" + (bit.intAsHex val)
		append vv1 val
		)
	print vv1s
	size1 = vv1[1] + vv1[2] * 0x100 + vv1[3] * 0x10000 + vv1[4] * 0x1000000
	offset1 = vv1[9] + vv1[10] * 0x100 + vv1[11] * 0x10000 + vv1[12] * 0x1000000 + (v1[8] + v1[9])
	vd = data_chunk1()
	vd.offset_block = offset1
	vd.size_block = size1
	append data_chunk1_array vd
	print("offset block: 0x0" + (bit.intAsHex (vd.offset_block )) +"	size block: 0x0" + (bit.intAsHex (vd.size_block )) + "	offset next block: 0x0" + (bit.intAsHex (vd.offset_block + vd.size_block)))

		
	)

print("")
print("offset: 0x0" + (bit.intAsHex (ftell f__mesh)) +"   ")
print("")

	
for i = 1 to v1[2] do (
	vv1 = #()
	vv1s = (i as string + "	||	")
	for i = 1 to 0x020 do (
		val = readbyte f__mesh #unsigned
		vv1s = vv1s + "	" + (bit.intAsHex val)
		)
	print vv1s
		
	)
	
print("")
print("offset: 0x0" + (bit.intAsHex (ftell f__mesh)) +"   ")
print("")

for i = 1 to v1[3] do (
	vv1 = #()
	vv1s = (i as string + "	||	")
	for i = 1 to 0x014 do (
		val = readbyte f__mesh #unsigned
		append vv1 val
		vv1s = vv1s + "	" + (bit.intAsHex val)
		)
	print vv1s
		
	data_chunk1_array[i + v1[2]].types = vv1[1]
	offset1 = vv1[17] + vv1[18] * 0x100 + vv1[19] * 0x10000 + vv1[20] * 0x1000000

	
	)

	
print("")
print("offset: 0x0" + (bit.intAsHex (ftell f__mesh)) +"   ")
print("")

offset_3 = ftell f__mesh
fseek f__mesh (0x18 + 32) #seek_cur

for i = (v1[2] + 1) to data_chunk1_array.count do (
	if(data_chunk1_array[i].types == 1) then (
		vv1 = #()
		vv1s = (i as string + "	||	")
		for i = 1 to 0x050 do (
			val = readbyte f__mesh #unsigned
			append vv1 val
			vv1s = vv1s + "	" + (bit.intAsHex val)
		)
		print vv1s
		data_chunk1_array[i].data_coding = vv1[1] + vv1[2] * 0x100
		data_chunk1_array[i].element_number = vv1[5] + vv1[6] * 0x100 + vv1[7] * 0x10000 + vv1[8] * 0x1000000

		)
	else if (data_chunk1_array[i].types == 2) then (
		vv1 = #()
		vv1s = (i as string + "	||	")
		for i = 1 to 0x048 do (
			val = readbyte f__mesh #unsigned
			append vv1 val
			vv1s = vv1s + "	" + (bit.intAsHex val)
		)
		print vv1s
		--print(vv1 as string)
		data_chunk1_array[i].data_coding = vv1[1] + vv1[2] * 0x100
		data_chunk1_array[i].element_number = vv1[5] + vv1[6] * 0x100 + vv1[7] * 0x10000 + vv1[8] * 0x1000000
		)
	else (print "!!!ERROR!!!")
	)



















submesh_number = (data_chunk1_array.count - v1[2])/2
print("submesh_number: " + (submesh_number as string))
	
		print("")

			print("")

				print("")

				
				
for i = v1[2] + 1 to (v1[2] +  submesh_number) do (
	print ("vertex mesh number: " + (i as string ) + "	//	" + ((vert_arrays.count + 1) as string)) 
	print("offset block: 0x0" + (bit.intAsHex (data_chunk1_array[i].offset_block )) +"	size block: 0x0" + (bit.intAsHex (data_chunk1_array[i].size_block )) + "	offset next block: 0x0" + (bit.intAsHex (data_chunk1_array[i].offset_block + data_chunk1_array[i].size_block)))
	vn = data_chunk1_array[i].size_block / 0x01c
	--print("vertex number: " + (vn as string))
	print("data coding: 0x0" + (bit.intAsHex (data_chunk1_array[i].data_coding )))
	print("vertex number: " + (data_chunk1_array[i].element_number as string) +  "	(0x0" + (bit.intAsHex (data_chunk1_array[i].element_number )) + ")")
	
	fseek f__mesh data_chunk1_array[i].offset_block  #seek_set
	
	print("offset: 0x0" + (bit.intAsHex (ftell f__mesh)) +"   ")

	vert_array = #()
	UV_array = #()
	
	
	if data_chunk1_array[i].data_coding == 0x01c24 then (
		for j = 1 to data_chunk1_array[i].element_number do (
			offs = ftell f__mesh
			x = readshort f__mesh #unsigned
			y = readshort f__mesh #unsigned
			z = readshort f__mesh #unsigned		
			fseek f__mesh 0x2 #seek_cur		
			u = readshort f__mesh #unsigned
			w = readshort f__mesh #unsigned	
			u_f = u
			w_f = w
			
			fseek f__mesh 0x10 #seek_cur	
			append vert_array [x,y,z]
			append UV_array [u_f,-w_f+1,0]
			--print((j as string)+"||"+((offs) as string) + "	||	" + ((x) as string) +"	"+((y) as string)+"	"+((z) as string)+"	||	"+((u) as string)+"	"+((w) as string))
			)
		)
	else if  data_chunk1_array[i].data_coding == 0x01c01 then (	
		for j = 1 to data_chunk1_array[i].element_number do (
			offs = ftell f__mesh
			x = readshort f__mesh #unsigned
			y = readshort f__mesh #unsigned
			z = readshort f__mesh #unsigned		
			fseek f__mesh 0x2 #seek_cur		
			u = readshort f__mesh #unsigned
			w = readshort f__mesh #unsigned	
			u_f = u
			w_f = w
			
			fseek f__mesh 0x10 #seek_cur	
			append vert_array [x,y,z]
			append UV_array [u_f,-w_f+1,0]
			--print((j as string)+"||"+((offs) as string) + "	||	" + ((x) as string) +"	"+((y) as string)+"	"+((z) as string)+"	||	"+((u) as string)+"	"+((w) as string))
			)
		)
	else if  data_chunk1_array[i].data_coding == 0x01c05 then (	
		for j = 1 to data_chunk1_array[i].element_number do (
			offs = ftell f__mesh
			x = readshort f__mesh #unsigned
			y = readshort f__mesh #unsigned
			z = readshort f__mesh #unsigned		
			fseek f__mesh 0x2 #seek_cur		
			u = readshort f__mesh #unsigned
			w = readshort f__mesh #unsigned	
			u_f = u
			w_f = w
			
			fseek f__mesh 0x10 #seek_cur	
			append vert_array [x,y,z]
			append UV_array [u_f,-w_f+1,0]
			--print((j as string)+"||"+((offs) as string) + "	||	" + ((x) as string) +"	"+((y) as string)+"	"+((z) as string)+"	||	"+((u) as string)+"	"+((w) as string))
			)
		)
	else if  data_chunk1_array[i].data_coding == 0x01802 then (	
		for j = 1 to data_chunk1_array[i].element_number do (
			offs = ftell f__mesh
			x = readshort f__mesh #unsigned
			y = readshort f__mesh #unsigned
			z = readshort f__mesh #unsigned		
			fseek f__mesh 0x2 #seek_cur		
			u = readshort f__mesh #unsigned
			w = readshort f__mesh #unsigned	
			u_f = u
			w_f = w
			
			fseek f__mesh 0xC #seek_cur	
			append vert_array [x,y,z]
			append UV_array [u_f,-w_f+1,0]
			--print((j as string)+"||"+((offs) as string) + "	||	" + ((x) as string) +"	"+((y) as string)+"	"+((z) as string)+"	||	"+((u) as string)+"	"+((w) as string))
			)
		)	
	else if  data_chunk1_array[i].data_coding == 0x02003 then (	
		for j = 1 to data_chunk1_array[i].element_number do (
			offs = ftell f__mesh
			x = readshort f__mesh #unsigned
			y = readshort f__mesh #unsigned
			z = readshort f__mesh #unsigned		
			fseek f__mesh 0x2 #seek_cur		
			u = readshort f__mesh #unsigned
			w = readshort f__mesh #unsigned	
			u_f = u
			w_f = w
			
			fseek f__mesh 0x14 #seek_cur	
			append vert_array [x,y,z]
			append UV_array [u_f,-w_f+1,0]
			--print((j as string)+"||"+((offs) as string) + "	||	" + ((x) as string) +"	"+((y) as string)+"	"+((z) as string)+"	||	"+((u) as string)+"	"+((w) as string))
			)
		)
		else if  data_chunk1_array[i].data_coding == 0x02025 then (	
		for j = 1 to data_chunk1_array[i].element_number do (
			offs = ftell f__mesh
			x = readshort f__mesh #unsigned
			y = readshort f__mesh #unsigned
			z = readshort f__mesh #unsigned		
			fseek f__mesh 0x2 #seek_cur		
			u = readshort f__mesh #unsigned
			w = readshort f__mesh #unsigned	
			u_f = u
			w_f = w
			
			fseek f__mesh 0x14 #seek_cur	
			append vert_array [x,y,z]
			append UV_array [u_f,-w_f+1,0]
			--print((j as string)+"||"+((offs) as string) + "	||	" + ((x) as string) +"	"+((y) as string)+"	"+((z) as string)+"	||	"+((u) as string)+"	"+((w) as string))
			)
		)
		else if  data_chunk1_array[i].data_coding == 0x02c2f then (	
		for j = 1 to data_chunk1_array[i].element_number do (
			offs = ftell f__mesh
			x = readshort f__mesh #unsigned
			y = readshort f__mesh #unsigned
			z = readshort f__mesh #unsigned		
			fseek f__mesh 0x2 #seek_cur		
			u = readshort f__mesh #unsigned
			w = readshort f__mesh #unsigned	
			u_f = u
			w_f = w
			
			fseek f__mesh 0x020 #seek_cur	
			append vert_array [x,y,z]
			append UV_array [u_f,-w_f+1,0]
			--print((j as string)+"||"+((offs) as string) + "	||	" + ((x) as string) +"	"+((y) as string)+"	"+((z) as string)+"	||	"+((u) as string)+"	"+((w) as string))
			)
		)

	else if  data_chunk1_array[i].data_coding == 0x0240e then (	
		for j = 1 to data_chunk1_array[i].element_number do (
			offs = ftell f__mesh
			x = readshort f__mesh #unsigned
			y = readshort f__mesh #unsigned
			z = readshort f__mesh #unsigned		
			fseek f__mesh 0x2 #seek_cur		
			u = readshort f__mesh #unsigned
			w = readshort f__mesh #unsigned	
			u_f = u
			w_f = w
			
			fseek f__mesh 0x18 #seek_cur	
			append vert_array [x,y,z]
			append UV_array [u_f,-w_f+1,0]
			--print((j as string)+"||"+((offs) as string) + "	||	" + ((x) as string) +"	"+((y) as string)+"	"+((z) as string)+"	||	"+((u) as string)+"	"+((w) as string))
			)
		)
	else if  data_chunk1_array[i].data_coding == 0x02428 then (	
		for j = 1 to data_chunk1_array[i].element_number do (
			offs = ftell f__mesh
			x = readshort f__mesh #unsigned
			y = readshort f__mesh #unsigned
			z = readshort f__mesh #unsigned		
			fseek f__mesh 0x2 #seek_cur		
			u = readshort f__mesh #unsigned
			w = readshort f__mesh #unsigned	
			u_f = u
			w_f = w
			
			fseek f__mesh 0x18 #seek_cur	
			append vert_array [x,y,z]
			append UV_array [u_f,-w_f+1,0]
			--print((j as string)+"||"+((offs) as string) + "	||	" + ((x) as string) +"	"+((y) as string)+"	"+((z) as string)+"	||	"+((u) as string)+"	"+((w) as string))
			)
		)
		
	else(
		print("error reading vertex! unknown coding format:  0x0" + (bit.intAsHex (data_chunk1_array[i].data_coding)) )
		)
		
	for j = 1 to data_chunk1_array[i].element_number do (
		vert_array[j] = [vert_array[j].x * X_VALUE , vert_array[j].y * Y_VALUE , vert_array[j].z * Z_VALUE ]
		UV_array[j] = [UV_array[j].x * U_VALUE / 65536 + U_MIN, UV_array[j].y * V_VALUE / 65536 - V_MIN, 0]
		)


	append UV_arrays UV_array
	append vert_arrays vert_array
	print("offset: 0x0" + (bit.intAsHex (ftell f__mesh)) +"   ")
	print("")
	)
	
	print("")
		print("FACES")
			print("")
	
for i = v1[2] + 1 + submesh_number  to (v1[2]  + submesh_number + submesh_number) do (
	print ("faces mesh number: " + i as string  + "	//	" + ((face_arrays.count + 1 )as string))
	print("offset block: 0x0" + (bit.intAsHex (data_chunk1_array[i].offset_block )) +"	size block: 0x0" + (bit.intAsHex (data_chunk1_array[i].size_block )) + "	offset next block: 0x0" + (bit.intAsHex (data_chunk1_array[i].offset_block + data_chunk1_array[i].size_block)))
	fnum = data_chunk1_array[i].size_block / 6
	print("faces number: " + (fnum as string))
		print("data coding: 0x0" + (bit.intAsHex (data_chunk1_array[i].data_coding )))

	fseek f__mesh data_chunk1_array[i].offset_block  #seek_set
	
	print("offset: 0x0" + (bit.intAsHex (ftell f__mesh)) +"   ")

	face_array = #()
	m = 0
	for j = 1 to fnum do (
		offs = ftell f__mesh
		x = (readshort f__mesh #unsigned) + 1
		y = (readshort f__mesh #unsigned) + 1
		z = (readshort f__mesh #unsigned) + 1	
		if x > m do m = x
		if y > m do m = y
		if z > m do m = z
		--print((j as string) + "		" +([x,y,z] as string) )
		append face_array [x,y,z]
		)

	append face_arrays face_array
	print ("m: " + (m as string))
	append ms m
	data_chunk1_array[i].m = m
	print("offset: 0x0" + (bit.intAsHex (ftell f__mesh)) +"   ")
	print("")
	)	
	
	
	for i = 1 to submesh_number do (
		try(
			msh = mesh name:((i as string) + "__f" + (face_arrays[i].count as string) + "__v" + (vert_arrays[i].count as string)) vertices:vert_arrays[i] faces:face_arrays[i]	
			setNumTVerts msh UV_arrays[i].count		
			for j = 1 to UV_arrays[i].count do (setTVert msh j UV_arrays[i][j]) 
			buildTVFaces msh false
			for j = 1 to face_arrays[i].count do (setTVFace msh j face_arrays[i][j])
			)
		catch(
			print("ERROR CREATING MESH " + (i as string) + "		number faces: " + (face_arrays[i].count as string) + "		number vertex: " + (vert_arrays[i].count as string) + "		found vertex: " + (ms[i] as string))
			
			)
	)
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	fclose f__mesh

	)
	catch (
	print("FILE_IMPORT_ERROR: " + file_array[i] + "   size:  " + ((getFileSize(file_array[i])) as string))
	)
	
)