if (heapSize < 200000) then
        heapSize = 2000000 -- allow ~ 40 MB instead of just 7.5 MB. Prevents "Runtime Error: Out of scripter memory"
gname = getOpenFileName \
caption:"Open .bone from Mesh folder" \
types:"Red Faction Info (*.ccmesh_pc)|*.ccmesh_pc" \
historyCategory:"RedFactionObjectPresets"
g = fopen gname "rb"
fname = getOpenFileName \
caption:"Open .mesh from Mesh folder" \
types:"Red Faction Models (*.gcmesh_pc)|*.gcmesh_pc" \
historyCategory:"RedFactionObjectPresets"
f = fopen fname "rb"

clearlistener()

fn readBElong fstream = (bit.swapBytes (bit.swapBytes (readlong fstream #unsigned) 1 4) 2 3)

fn readBEfloat fstream = (bit.intAsFloat (bit.swapBytes (bit.swapBytes (readlong fstream #unsigned) 1 4) 2 3))	

fn ReadHalfFloat fstream = 
	(
	hf=readshort 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
)

clearlistener()

fseek g (0x9C) #seek_set
VertCount=readlong g
fseek g (0x1C) #seek_cur
VerOffset=readlong g
FaceCount=readlong g

fclose g

vertArray = #()
faceArray = #()
UV_array = #()
Weight_array=#()	

fseek f (0x10) #seek_set

StartDirection = 1
f1 = readshort f #unsigned +1
f2 = readshort f #unsigned +1
FaceDirection = StartDirection
for i = 3 to FaceCount do (   
f3 = (ReadShort f)
if (f3==0xFFFF) then (
f1 = readshort f #unsigned +1
f2 = readshort f #unsigned +1
FaceDirection = StartDirection 
) else (
f3 += (1)
FaceDirection *= -1
if (f1!=f2)AND(f2!=f3)AND(f3!=f1) then (
if FaceDirection > 0 then append Facearray [f1,f2,f3]
else append Facearray [f1,f3,f2]
)
f1 = f2
f2 = f3
) 
) 

fseek f (VerOffset) #seek_set

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

for x = 1 to VertCount do (
	
vx = ReadFloat f
vy = ReadFloat f
vz = ReadFloat f	
	
fseek f (0x10) #seek_cur
	
tu = (ReadShort f /1024.00)
tv = (ReadShort f /1024.00)*-1	
	
unk00 = ReadShort f
unk01 = ReadShort f
	
append UV_array [tu,tv,0]		
append vertArray ([-vx,-vz,vy]*100)
)	

msh = mesh vertices:vertArray faces:faceArray
msh.name = ("Model")
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]
msh.wirecolor = (color 230 200 210)	

fclose f