if (heapSize < 20000000) then
	heapSize = 200000000
gname = getOpenFileName \
caption:"Open .sm3 from Bones folder" \
types:"LOS2 (*.sm3)|*.sm3" \
historyCategory:"LOS2ObjectPresets"
g = fopen gname "rb"
fname = getOpenFileName \
caption:"Open .msh from Mesh folder" \
types:"L0S2  (*.msh)|*.msh" \
historyCategory:"LOS2ObjectPresets"
f = fopen fname "rb"

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
)
struct Weight_Info_Struct
(
        weight1,weight2,weight3,weight4
)
struct Bone_Info_Struct
(
        bone1,bone2,bone3,bone4
)
clearlistener()

fseek g 0x38 #seek_set
check=readlong g
if check==0 then 
for i=1 to 1 do (	
Data1Count = readlong g
fseek g Data1Count #seek_cur	
fseek g 0x49 #seek_cur
DataCount1 = readlong g
print DataCount1
for x = 1 to DataCount1 do (
DataNameSize1=readlong g
DataName1=ReadFixedString g DataNameSize1
)		
)

if check!=0 then 	
for a = 1 to 1 do (		
for i = 1 to 1 do (	
fseek g 0xC #seek_cur		
DataCount = readlong g
print DataCount
for x = 1 to DataCount do (
DataNameSize=readlong g
DataName=ReadFixedString g DataNameSize
)	
)

Pos=ftell g
check2=readlong g	
if check2==0 then 
for a=1 to 1 do (		
fseek g Pos #seek_set
fseek g 0x4D #seek_cur
DataCount2 = readlong g
print DataCount2
for x = 1 to DataCount2 do (
DataNameSize2=readlong g
DataName2=ReadFixedString g DataNameSize2
)
)

if check2!=0 then 
for a=1 to 1 do (	
fseek g Pos #seek_set
Data2Count = readlong g
fseek g Data2Count #seek_cur	
fseek g 0x49 #seek_cur
DataCount2 = readlong g
print DataCount2
for x = 1 to DataCount2 do (
DataNameSize2=readlong g
DataName2=ReadFixedString g DataNameSize2
)	
)	
)

Print ("Skel Info End @ 0x"+((bit.intAsHex(ftell g))as string))		
	
BNArr = #()
BoneIdsArray = #()	
	
BoneCount = readlong g

for a = 1 to BoneCount do (
BoneNameSize = readlong g; BoneName = ReadFixedString g BoneNameSize
BoneParentnameSize = readlong g; BoneParentname = ReadFixedString g BoneParentnameSize
BoneParent = readlong g
unk0 = readlong g
if unk0==0 then fseek g 0x0 #seek_cur	
if unk0!=0 then 
for x=1 to 1 do (	
fseek g 0xC #seek_cur		
DataCount = readlong g
for x = 1 to DataCount do (
DataNameSize=readlong g
DataName=ReadFixedString g DataNameSize
)	
)
BoneId = readlong g +1
unk2 = readlong g
fseek g 0x30 #seek_cur
m11 = readFloat g; m12 = readFloat g; m13 = readFloat g; m14 = readFloat g
m21 = readFloat g; m22 = readFloat g; m23 = readFloat g; m24 = readFloat g
m31 = readFloat g; m32 = readFloat g; m33 = readFloat g; m34 = readFloat g
m41 = readFloat g; m42 = readFloat g; m43 = readFloat g; m44 = readFloat g
tfm = matrix3 [m11,m12,m13] [m21,m22,m23] [m31,m32,m33] [m41,m42,m43]
fseek g 0x1C #seek_cur

if (getNodeByName BoneName) != undefined do (
append BNArr (getNodeByName BoneName)
)
if (getNodeByName BoneName) == undefined do (

 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 ()
 if (BoneParent != -1) then
 newBone.parent = BNArr[(BoneParent + 1)]					  
append BNArr newBone
append BoneIdsArray BoneId					  					  
)					  
)
Print ("Last Read @ 0x"+((bit.intAsHex(ftell g))as string))	
	
 fseek f 0xc #seek_set
meshcount = readlong f
	
for x = 1 to meshcount do (

	
MainMeshCount = readlong f
meshExt=ReadFixedString f 5	
fseek f 0x20 #seek_cur
SubMeshCount = readlong f
for i = 1 to SubMeshCount do (	
	
Vert_array=#()
Face_array=#()
UV_array=#()	
Weight_array =#()
UsedBonesArray =#()	
	
fseek f 0x10 #seek_cur	
print SubMeshCount	
	
facecount = readlong f	
faceSize = readlong f


for x = 1 to facecount do(
fa=readshort f +1
fb=readshort f +1
fc=readshort f +1
append Face_array[fa,fb,fc]
)

ext=ReadFixedString f 4	
unk = readlong f
subCount = readlong f
Count = readlong f
stringSize = readlong f
extSTR=ReadFixedString f stringSize
print extSTR
null = readlong f
vertexSize = readlong f	
print vertexSize
vertexcount = readlong f	
if vertexSize==44 then (
for x = 1 to vertexcount do(
getPos = ftell f + vertexSize	
dx=readFloat f
dy=readFloat f
dz=readFloat f
x1=readHalfFloat f
x2=readHalfFloat f	
x3=readHalfFloat f
x4=readHalfFloat f
x5=readHalfFloat f
x6=readHalfFloat f	
x7=readHalfFloat f
x8=readHalfFloat f*-1	
x9=readHalfFloat f
x10=readHalfFloat f	
x11=readHalfFloat f
x12=readHalfFloat f	
bone1 = readbyte f #unsigned
bone2 = readbyte f #unsigned
bone3 = readbyte f #unsigned
bone4 = readbyte f #unsigned	
weight1 = readbyte f #unsigned
weight2 = readbyte f #unsigned
weight3 = readbyte f #unsigned
weight4 =readbyte f #unsigned	
	
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 / 255.0)
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2 / 255.0)
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3 / 255.0)
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
         append w.weights (w4 / 255.0)
      )      
   )
append Weight_array w			
fseek f getPos #seek_set
append Vert_array[dx,dy,dz] 
append UV_array[x7,x8,0]
)
)
if vertexSize==36 then (
for x = 1 to vertexcount do(
getPos = ftell f + vertexSize	
bone1 = -1
bone2 = -1
bone3 = -1
bone4 = -1	
weight1 = 0
weight2 = 0
weight3 = 0
weight4 = 0		
dx=readFloat f
dy=readFloat f
dz=readFloat f
x1=readHalfFloat f
x2=readHalfFloat f
x3=readHalfFloat f
x4=readHalfFloat f
x5=readHalfFloat f*-1
x6=readHalfFloat f	
x7=readHalfFloat f
x8=readHalfFloat f	
x9=readHalfFloat f
x10=readHalfFloat f	
x11=readHalfFloat f
x12=readHalfFloat f	

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
      )      
   )
	
fseek f getPos #seek_set
append Weight_array w		 
append Vert_array[dx,dy,dz] 
append UV_array[x7,x8,0]
)
)
if vertexSize==32 then (
for x = 1 to vertexcount do(
getPos = ftell f + vertexSize			
dx=readFloat f
dy=readFloat f
dz=readFloat f
x1=readHalfFloat f
x2=readHalfFloat f
x3=readFloat f
x4=readFloat f*-1
bone1 = readbyte f #unsigned
bone2 = readbyte f #unsigned
bone3 = readbyte f #unsigned
bone4 = readbyte f #unsigned	
weight1 = readbyte f #unsigned
weight2 = readbyte f #unsigned
weight3 = readbyte f #unsigned
weight4 =readbyte f #unsigned	

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
      )      
   )
	
fseek f getPos #seek_set
append Weight_array w		 
append Vert_array[dx,dy,dz] 
append UV_array[x3,x4,0]
)
)
if vertexSize==24 then (
for x = 1 to vertexcount do(
getPos = ftell f + vertexSize	
bone1 = -1
bone2 = -1
bone3 = -1
bone4 = -1	
weight1 = 0
weight2 = 0
weight3 = 0
weight4 = 0		
dx=readFloat f
dy=readFloat f
dz=readFloat f
x1=readHalfFloat f
x2=readHalfFloat f
x3=readFloat f
x4=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
      )      
   )
	
fseek f getPos #seek_set
append Weight_array w		 
append Vert_array[dx,dy,dz] 
append UV_array[x3,x4,0]
)
)
if vertexSize==20 then (
for x = 1 to vertexcount do(
getPos = ftell f + vertexSize	
bone1 = -1
bone2 = -1
bone3 = -1
bone4 = -1	
weight1 = 0
weight2 = 0
weight3 = 0
weight4 = 0		
dx=readFloat f
dy=readFloat f
dz=readFloat f
x1=readFloat f
x2=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
      )      
   )
	
fseek f getPos #seek_set
append Weight_array w		 
append Vert_array[dx,dy,dz] 
append UV_array[x1,x2,0]
)
)
if vertexSize==12 then (
for x = 1 to vertexcount do(
getPos = ftell f + vertexSize	
bone1 = -1
bone2 = -1
bone3 = -1
bone4 = -1	
weight1 = 0
weight2 = 0
weight3 = 0
weight4 = 0		
dx=readFloat f
dy=readFloat f
dz=readFloat f

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
      )      
   )
	
fseek f getPos #seek_set
append Weight_array w		 
append Vert_array[dx,dy,dz] 
)
)
unk1 = readlong f	

facecount1 = readlong f	
faceSize1 = readlong f	
	
for x = 1 to facecount1 do(
ca=readshort f +1
cb=readshort f +1
cc=readshort f +1
)

ext1=ReadFixedString f 4	
unk2 = readlong f
subCount1 = readlong f
Count1 = readlong f
stringSize1 = readlong f
extSTR1=ReadFixedString f stringSize1
print extSTR1
null1 = readlong f
vertexSize1 = readlong f	
vertexcount1 = readlong f	

for x = 1 to vertexcount1 do(
getPos = ftell f + vertexSize1	
xx=readFloat f
xy=readFloat f
xz=readFloat f
fseek f getPos #seek_set
)
	
unit = readlong f
unitcount= readlong f
longcount= readlong f	
for x = 1 to unitcount do(
short=readshort f +1
Append UsedBonesArray short	
)
longNull1= readlong f	
longNull2= readlong f	
ffffffff= readlong f
fseek f 0x20 #seek_cur
Print ("Last Read @ 0x"+((bit.intAsHex(ftell f))as string))	
	
msh = mesh vertices:Vert_array faces:Face_array
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 Face_array.count do setTVFace msh j Face_array[j]

max modify mode
select msh
skinMod = skin ()
addModifier msh skinMod
if unitcount== 0 then (
for i = 1 to UsedBonesArray.Count+1 do
(
	maxbone =  BNArr[i]
	if i != BNArr.Count then 
		skinOps.addBone skinMod maxbone 0
	else
		skinOps.addBone skinMod maxbone 1
)
)
if unitcount != 0 then (
for i = 1 to UsedBonesArray.Count do
(
	maxbone =  BNArr[(UsedBonesArray[i])]
	if i != BNArr.Count then 
		skinOps.addBone skinMod maxbone 0
	else
		skinOps.addBone skinMod maxbone 1
)
)
modPanel.setCurrentObject skinMod
for i = 1 to vertexcount 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

)
)

)

fclose g
fclose f
