if (heapSize < 20000000) then
	heapSize = 200000000
fname = getOpenFileName \
caption:"Open .bmd6model from Mesh folder" \
types:"DOOM 2016 Rigged Meshes (*.bmd6model)|*.bmd6model" \
historyCategory:"DOOM_RObjectPresets"
f = fopen fname "rb"

clearlistener()	

fn ReadBEword fstream = (
return (bit.swapBytes (readshort fstream #unsigned) 1 2)
)

fn readBEshort fstream = (
short = readshort fstream #unsigned
short = bit.swapBytes short 1 2
return short
)

fn ReadBEfloat fstream = (
    fpt=readfloat fstream
    itger = bit.floatAsInt fpt
    hih = bit.intashex itger
    while hih.count < 8 do hih = "0" + hih
    shn = (substring hih 7 2) + \
    (substring hih 5 2) + \
    (substring hih 3 2) + \
    (substring hih 1 2)
    bit.intAsFloat (bit.hexasint shn)
    )

fn readBElong fstream = (
long = readlong fstream
long = bit.swapBytes long 1 4
long = bit.swapBytes long 2 3
return long
)

fn ReadBEHalfFloat fstream = 
	(
	hf=ReadBEword fstream
	sign = bit.get hf 16
	exponent = (bit.shift (bit.and hf (bit.hexasint "7C00")) -10) as integer - 16
	fraction = bit.and hf (bit.hexasint "03FF")
	if sign==true then sign = 1 else sign = 0
	exponentF = exponent + 127
	outputAsFloat = bit.or (bit.or (bit.shift fraction 13) \
	(bit.shift exponentF 23)) (bit.shift sign 31)
	return bit.intasfloat outputasfloat*2
)

fn readFixedString bstream fixedLen = (
local str = ""
for i = 1 to fixedLen do (
str += bit.intAsChar (ReadByte bstream #unsigned))
str
)

struct weight_data
(
   boneids,weights
)
	
struct Mesh_Info_Struct
(
VertCount, FaceCount
)	
	
clearlistener()

--------------------------------------------------------------------------------------------	
	
fseek f 0x14 #seek_set	
DataCount=readBEshort f

fseek f 0x24 #seek_set	
OffData=readBElong f

fseek f OffData #seek_set	

VertOff=#()	
MeshNameOff=#() 	
FaceOff=#() 
MeshInfoOff=#() 
for i=1 to DataCount do (
getPos = ftell f + 24
Unk_00=readBElong f
DataOffStart=readBElong f
Unk_01=readBElong f
DataSize=readBElong f
Hash=readBElong f
SecId=(readBEshort f+1)
if SecId==9 then (
append MeshNameOff DataOffStart	
)		
if SecId==10 then (
append MeshInfoOff DataOffStart	
)		
if SecId==13 then (
append VertOff DataOffStart		
)	
if SecId==14 then (
append FaceOff DataOffStart	
)	
fseek f getPos #seek_set	
)	

fseek f MeshNameOff[1] #seek_set

Print ("Mesh Names Start @ 0x"+((bit.intAsHex(ftell f))as string))	

MeshCount=readlong f
StringSize=readlong f

MeshNameArray=#()
for i=1 to MeshCount do (
MeshName=readstring f	
append MeshNameArray MeshName	
)	

fseek f MeshInfoOff[1] #seek_set

Print ("Mesh Info Start @ 0x"+((bit.intAsHex(ftell f))as string))	

Mesh_Info_Array=#()
for i=1 to MeshCount do (
getPos = ftell f + 52	
fseek f 0x1C #seek_cur
VertCount=readlong f
FaceCount=readlong f	
fseek f getPos #seek_set		
append Mesh_Info_Array (Mesh_Info_Struct VertCount:VertCount FaceCount:FaceCount )	
)	

print Mesh_Info_Array 

VOff=VertOff[1] 	
FOff=FaceOff[1]	

for i=1 to MeshCount do (
	
FaceArray=#()
vertArray=#()
UV_array=#()
Weight_array=#()		
	
fseek f VOff #seek_set	

Print ("Vertex Start @ 0x"+((bit.intAsHex(ftell f))as string))	
		
for x = 1 to Mesh_Info_Array[i].VertCount do(
getPos = ftell f + 48
	
vx=readfloat f
vy=readfloat f
vz=readfloat f

tu=readfloat f
tv=readfloat f*-1	
	
fseek f getPos #seek_set		
append VertArray([vy,-vx,vz])
append UV_array[tu,tv,0]	
)

Print ("Vertex End @ 0x"+((bit.intAsHex(ftell f))as string))
	
VOff+=(Mesh_Info_Array[i].VertCount*48)	
		
fseek f FOff #seek_set		
	
Print ("Face Start @ 0x"+((bit.intAsHex(ftell f))as string))	
	
for x = 1 to Mesh_Info_Array[i].FaceCount do (

fa=(readshort f +1) 
fb=(readshort f +1)
fc=(readshort f +1)
	
append FaceArray[fc,fb,fa]
)

Print ("Face End @ 0x"+((bit.intAsHex(ftell f))as string))

FOff+=(Mesh_Info_Array[i].FaceCount*6)

msh = mesh vertices:vertArray faces:faceArray
msh.name=MeshNameArray[i]
msh.numTVerts = UV_array.count
buildTVFaces msh
for j = 1 to UV_array.count do setTVert msh j UV_array[j]
for j = 1 to faceArray.count do setTVFace msh j faceArray[j]
)

fclose f