if (heapSize < 200000) then
        heapSize = 2000000 -- allow ~ 40 MB instead of just 7.5 MB. Prevents "Runtime Error: Out of scripter memory"
hname = getOpenFileName \
caption:"Open .* from Mesh folder" \
types:"Survarium export_properties Info (*.*)|*.*" \
historyCategory:"SurvariumInfoObjectPresets"
h = fopen hname "rb"		
gname = getOpenFileName \
caption:"Open .* from Bones folder" \
types:"Survarium bind_pose Bones (*.*)|*.*" \
historyCategory:"SurvariumBonesObjectPresets"
g = fopen gname "rb"
fname = getOpenFileName \
caption:"Open .* from Mesh folder" \
types:"Survarium converted_model Meshes (*.*)|*.*" \
historyCategory:"SurvariumInfoObjectPresets"
f = fopen fname "rb"

clearlistener()
fn readHalfFloat fstream = (
    hf=readshort fstream #unsigned
    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 h 0x30 #seek_set
Version = readlong h
fseek h 0xA8 #seek_set
MeshNameOff = readlong h
fseek h MeshNameOff #seek_set
MeshName = readstring h

BoneCount = #()

fseek g 0x0 #seek_set
BoneCount = readshort g

BNArr = #()

for i = 1 to BoneCount do (

BoneName=readString g
				
c11 = ReadFloat g; c12 = ReadFloat g; c13 = ReadFloat g
c21 = ReadFloat g; c22 = ReadFloat g; c23 = ReadFloat g
c31 = ReadFloat g; c32 = ReadFloat g; c33 = ReadFloat g
		
tfm = (quat 0 0 0 1) as matrix3
tfm.row4 = ([c11,c12,c13]*100)
	
newBone = bonesys.createbone	\
				  tfm.row4	\
				  (tfm.row4 + 0.01 * (normalize tfm.row1)) \
				  (normalize tfm.row3)
			newBone.name = BoneName
			newBone.width  = 0.01
			newBone.height = 0.01
			newBone.transform = inverse tfm
			newBone.setBoneEnable false 0
			newBone.wirecolor = white
			newbone.showlinks = true
			newBone.pos.controller      = TCB_position ()
			newBone.rotation.controller = TCB_rotation ()				  
append BNArr newBone				  
)

rootRT = dummy name:"Main_Root" boxsize:[.1,.1,.1]
for b in BNArr where b.parent==undefined do b.parent = rootRT
rootRT.transform = (matrix3 [1,0,0] [0,0,-1] [0,1,0] [0,0,0])


fseek f 0x0 #seek_set
VertexId = ReadLong f
VertexSecSize = ReadLong f
numVerts = ReadLong f

vertArray = #()
Facearray = #()	
UV_array = #()
Weight_array =#()

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

if Version==44 then	
for x = 1 to numVerts Do (
vx = Readfloat f
vy = Readfloat f
vz = Readfloat f

bone1 = -1	
bone2 = -1	
bone3 = -1
bone4 = -1
	
weight1 = 0
weight2 = 0
weight3 = 0
weight4 = 0
	
n1 = ReadLong f
n2 = ReadLong f
n3 = ReadLong f	
	
tu=ReadFloat f	
tv=ReadFloat f*-1	

w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2
if(weight3 != 0) then
   maxweight = maxweight + weight3
if(weight4 != 0) then
   maxweight = maxweight + weight4


if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1)
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2)
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3)
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
        append w.weights (w4)
      )      
   )
append Weight_array w			   
append vertArray ([-vx,-vz,vy]*100)
append UV_array [tu,tv,0]	 
)
	
if Version==45 then	
for x = 1 to numVerts Do (
vx = Readfloat f
vy = Readfloat f
vz = Readfloat f

bone1 = readbyte f #unsigned	
bone2 = readbyte f #unsigned	
bone3 = readbyte f #unsigned	
bone4 = readbyte f #unsigned	
	
weight1 = Readfloat f
weight2 = 0
weight3 = 0
weight4 = 0
	
n1 = ReadLong f
n2 = ReadLong f
n3 = ReadLong f	
	
tu=ReadFloat f	
tv=ReadFloat f*-1	

w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2
if(weight3 != 0) then
   maxweight = maxweight + weight3
if(weight4 != 0) then
   maxweight = maxweight + weight4


if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1)
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2)
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3)
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
        append w.weights (w4)
      )      
   )
append Weight_array w			   
append vertArray ([-vx,-vz,vy]*100)
append UV_array [tu,tv,0]	 
)		
	
if Version==46 then	
for x = 1 to numVerts Do (
vx = Readfloat f
vy = Readfloat f
vz = Readfloat f

bone1 = readbyte f #unsigned	
bone2 = readbyte f #unsigned	
bone3 = readbyte f #unsigned	
bone4 = readbyte f #unsigned	
	
weight1 = Readfloat f
weight2 = Readfloat f
weight3 = 0
weight4 = 0
	
n1 = ReadLong f
n2 = ReadLong f
n3 = ReadLong f	
	
tu=ReadFloat f	
tv=ReadFloat f*-1	

w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2
if(weight3 != 0) then
   maxweight = maxweight + weight3
if(weight4 != 0) then
   maxweight = maxweight + weight4


if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1)
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2)
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3)
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
        append w.weights (w4)
      )      
   )
append Weight_array w			   
append vertArray ([-vx,-vz,vy]*100)
append UV_array [tu,tv,0]	 
)	

if Version==47 then	
for x = 1 to numVerts Do (
vx = Readfloat f
vy = Readfloat f
vz = Readfloat f

bone1 = readbyte f #unsigned	
bone2 = readbyte f #unsigned	
bone3 = readbyte f #unsigned	
bone4 = readbyte f #unsigned	
	
weight1 = Readfloat f
weight2 = Readfloat f
weight3 = Readfloat f
weight4 = 0
	
n1 = ReadLong f
n2 = ReadLong f
n3 = ReadLong f	
	
tu=ReadFloat f	
tv=ReadFloat f*-1	

w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2
if(weight3 != 0) then
   maxweight = maxweight + weight3
if(weight4 != 0) then
   maxweight = maxweight + weight4


if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1)
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2)
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3)
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
        append w.weights (w4)
      )      
   )
append Weight_array w			   
append vertArray ([-vx,-vz,vy]*100)
append UV_array [tu,tv,0]	 
)

if Version==48 then	
for x = 1 to numVerts Do (
vx = Readfloat f
vy = Readfloat f
vz = Readfloat f

bone1 = readbyte f #unsigned	
bone2 = readbyte f #unsigned	
bone3 = readbyte f #unsigned	
bone4 = readbyte f #unsigned	
	
weight1 = Readfloat f
weight2 = Readfloat f
weight3 = Readfloat f
weight4 = Readfloat f
	
n1 = ReadLong f
n2 = ReadLong f
n3 = ReadLong f	
	
tu=ReadFloat f	
tv=ReadFloat f*-1	

w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2
if(weight3 != 0) then
   maxweight = maxweight + weight3
if(weight4 != 0) then
   maxweight = maxweight + weight4


if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1)
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2)
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3)
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
        append w.weights (w4)
      )      
   )
append Weight_array w			   
append vertArray ([-vx,-vz,vy]*100)
append UV_array [tu,tv,0]	 
)

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

FaceId = ReadLong f
FaceSecSize = ReadLong f
numFaces = ReadLong f

for x = 1 to (numFaces/3) do 
(
f1 = (Readshort f #unsigned + 1) 
f2 = (Readshort f #unsigned + 1) 
f3 = (Readshort f #unsigned + 1) 
append Facearray [f3,f2,f1]
)

try(
msh = mesh vertices:vertArray faces:faceArray
msh.numTVerts = UV_array.count
buildTVFaces msh
msh.name = MeshName
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.displayByLayer = false
msh.backfacecull = true
msh.wirecolor = (color 230 200 210)
			

max modify mode
select msh
skinMod = skin ()
addModifier msh skinMod
for i = 1 to BoneCount do
(
	maxbone = BNArr[i] 
	if i != BoneCount then 
		skinOps.addBone skinMod maxbone 0
	else
		skinOps.addBone skinMod maxbone 1
)
modPanel.setCurrentObject skinMod
for i = 1 to (numVerts) do (
   w = Weight_array[i]
   bi = #() --bone index array
   wv = #() --weight value array
   
   for j = 1 to w.boneids.count do
   (
      boneid = w.boneids[j]
      weight = w.weights[j]
      append bi boneid
      append wv weight
   )   
   
   skinOps.ReplaceVertexWeights skinMod i bi wv

)
)catch(format "Failed!!!\n")

fclose h
fclose g
fclose f	