if (heapSize < 20000000) then
	heapSize = 200000000
fname = getOpenFileName \
caption:"Open .bmodel from Mesh folder" \
types:"DOOM 2016 Static Meshes (*.bmodel)|*.bmodel" \
historyCategory:"DOOM_SObjectPresets"
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, FaceStride, UVType
)	
	
struct Mesh_Info_Struct2
(
VertStride, VertOff, WStride, WOff 	
)	

clearlistener()

--------------------------------------------------------------------------------------------	
	
FaceArray=#()
vertArray=#()
UV_array=#()
Weight_array=#()	

fseek f 0x8 #seek_set	
MeshCount=readBElong f
NameMSHSize=readlong f	
NameMSH = readFixedString f NameMSHSize

fseek f 0x10 #seek_cur

VertCount=readBElong f
FaceCount=readBElong f
UnkCount=readBElong f

fseek f 0x28 #seek_cur

Print ("Mesh Info Start @ 0x"+((bit.intAsHex(ftell f))as string))	
		
for x = 1 to VertCount do(
getPos = ftell f + 48
	
vx=readBEfloat f
vy=readBEfloat f
vz=readBEfloat f

tu=readBEfloat f
tv=readBEfloat 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))	

for x = 1 to FaceCount/3 do (

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

msh = mesh vertices:vertArray faces:faceArray
msh.name=NameMSH
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