if (heapSize < 20000000) then
	heapSize = 200000000
fname = getOpenFileName \
caption:"Open .Mesh from Mesh folder" \
types:"Ring of Elysium Models (*.*)|*.dat;*.dfont" \
historyCategory:"RoE_ObjectPresets"
f = fopen fname "rb"

clearlistener()	

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

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

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

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 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()

BoneHashArray=#()	

clearlistener()	

filename=filenameFromPath fname

filename2=getFilenameFile filename

print ("This is FileName "+filename2)

fseek f (0x0) #seek_set

Unk1=readshort f
MeshCount=Readshort f
Unk2=readshort f

for i=1 to MeshCount do (

MatCount=readshort f	
	
vertArray=#()
faceArray =#()
UV_array=#()
Weight_array=#()	
	
FaceCount=readlong f
FaceSize=readlong f

Print ("Face Start @ 0x"+((bit.intAsHex(ftell f))as string))
	
	
if FaceSize==2 then  	
for y = 1 to (FaceCount/6) do ( --Faces
fa=(readshort f #unsigned+ 1) 
fb=(readshort f #unsigned+ 1) 
fc=(readshort f #unsigned+ 1) 
append faceArray [fa,fb,fc] 
)	

if FaceSize==4 then  	
for y = 1 to (FaceCount/12) do ( --Faces
fa=(readlong f + 1) 
fb=(readlong f + 1) 
fc=(readlong f + 1) 
append faceArray [fa,fb,fc] 
)	

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


print ("This is Vertex Size: "+(VertexSize as string))	

Print ("Vertex Start @ 0x"+((bit.intAsHex(ftell f))as string))	
	
if VertexSize==96 then 	
for x =1 to VertexSecSize/VertexSize do (
	
getPos = ftell f + VertexSize
	
vx=readfloat f
vy=readfloat f
vz=readfloat f
	
tu=readfloat f
tv=1.0-readfloat f	
	
fseek f 44 #seek_cur
	
bone1 = readbyte f #unsigned	
bone2 = readbyte f #unsigned	
bone3 = readbyte f #unsigned	
readbyte f

weight1 = readfloat f
weight2 = readfloat f
weight3 = 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(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)
      )   
   )
fseek f getPos #seek_set       
append Weight_array w	
append vertArray ([vx,vy,vz])	
append UV_array [tu,tv,0]	
)		
	
if VertexSize==88 then 	
for x =1 to VertexSecSize/VertexSize do (
	
getPos = ftell f + VertexSize
	
vx=readfloat f
vy=readfloat f
vz=readfloat f
	
tu=readfloat f
tv=1.0-readfloat f	
	
fseek f 52 #seek_cur
	
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 = 1-(weight1+weight2+weight3)	

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 vertArray ([vx,vy,vz])	
append UV_array [tu,tv,0]	
)	

if VertexSize==80 then 	
for x =1 to VertexSecSize/VertexSize do (
	
getPos = ftell f + VertexSize
	
vx=readfloat f
vy=readfloat f
vz=readfloat f
	
tu=readfloat f
tv=1.0-readfloat f	
	
fseek f 44 #seek_cur
	
bone1 = readbyte f #unsigned	
bone2 = readbyte f #unsigned	
bone3 = readbyte f #unsigned	
bone4 = readbyte f #unsigned
	
-- if bone4!=0 then print bone4	

weight1 = readfloat f
weight2 = readfloat f
weight3 = readfloat f
weight_comb=(weight1+weight2+weight3)
if weight_comb>=1.0 then weight_comb==1.0
if weight_comb<=0 then weight_comb==0.0	
weight4 = 0
-- print weight4

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 vertArray ([vx,vy,vz])	
append UV_array [tu,tv,0]	
)		

if VertexSize==72 then 	
for x =1 to VertexSecSize/VertexSize do (
	
getPos = ftell f + 72
	
vx=readfloat f
vy=readfloat f
vz=readfloat f
	
tu=readfloat f
tv=1.0-readfloat f	
	
fseek f 36 #seek_cur
	
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

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 vertArray ([vx,vy,vz])	
append UV_array [tu,tv,0]	
)	

if VertexSize==40 then 	
for x =1 to VertexSecSize/VertexSize do (
	
getPos = ftell f + VertexSize
	
vx=readHalfFloat f
vy=readHalfFloat f
vz=readHalfFloat f
	
readshort f	
	
tu=readHalfFloat f
tv=1.0-readHalfFloat f
	
fseek f 16 #seek_cur	
			
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)
      )      
   )
fseek f getPos #seek_set      
append Weight_array w		
append vertArray ([vx,vy,vz])	
append UV_array [tu,tv,0]	
)	

if VertexSize==36 then 	
for x =1 to VertexSecSize/VertexSize do (
	
getPos = ftell f + VertexSize
	
vx=readHalfFloat f
vy=readHalfFloat f
vz=readHalfFloat f
	
readshort f	
	
tu=readHalfFloat f
tv=1.0-readHalfFloat f
	
fseek f 16 #seek_cur	
			
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)
      )      
   )
fseek f getPos #seek_set      
append Weight_array w		
append vertArray ([vx,vy,vz])	
append UV_array [tu,tv,0]	
)

if VertexSize==32 then 	
for x =1 to VertexSecSize/VertexSize do (
	
getPos = ftell f + VertexSize
	
vx=readHalfFloat f
vy=readHalfFloat f
vz=readHalfFloat f
	
readshort f	
	
tu=readHalfFloat f
tv=1.0-readHalfFloat f
	
fseek f 12 #seek_cur	
			
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)
      )      
   )
fseek f getPos #seek_set      
append Weight_array w		
append vertArray ([vx,vy,vz])	
append UV_array [tu,tv,0]	
)

if VertexSize==28 then 	
for x =1 to VertexSecSize/VertexSize do (
	
getPos = ftell f + VertexSize
	
vx=readHalfFloat f
vy=readHalfFloat f
vz=readHalfFloat f
	
readshort f	
	
tu=readHalfFloat f
tv=1.0-readHalfFloat f
	
fseek f getPos #seek_set      
append vertArray ([vx,vy,vz])	
append UV_array [tu,tv,0]	
)

if VertexSize==24 then 	
for x =1 to VertexSecSize/VertexSize do (
	
getPos = ftell f + VertexSize
	
vx=readHalfFloat f
vy=readHalfFloat f
vz=readHalfFloat f
	
readshort f	
	
tu=readHalfFloat f
tv=1.0-readHalfFloat f
	
fseek f getPos #seek_set      
append vertArray ([vx,vy,vz])	
append UV_array [tu,tv,0]	
)

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

readlong f
	
MNameSize=((readbyte f #unsigned)-192)
MeshName=readFixedString f (MNameSize)	

readshort f

LongCount1=readlong f

for i=1 to (LongCount1-1) do (
readlong f	
)	

LongCount2=readlong f

for i=1 to LongCount2 do (
fseek f 12 #seek_cur
)	

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

BoneCount=readlong f

Print ("Bones Name Start @ 0x"+((bit.intAsHex(ftell f))as string))		
	
BoneNameArray=#()	
for i=1 to BoneCount do (
NameSize=(readbyte f #unsigned)	
BoneName=readFixedString f (NameSize-192)	
append BoneNameArray BoneName
)	
	
Print ("Bones Name End @ 0x"+((bit.intAsHex(ftell f))as string))	
	
BoneCount=readlong f	
	
Print ("Bones Start @ 0x"+((bit.intAsHex(ftell f))as string))		
	
BNArr=#()	
for i=1 to BoneCount do (
	
getPos = ftell f + 60
	
c11 = ReadFloat f; c12 = ReadFloat f; c13 = ReadFloat f
c21 = ReadFloat f; c22 = ReadFloat f; c23 = ReadFloat f
c31 = ReadFloat f; c32 = ReadFloat f; c33 = ReadFloat f
c41 = ReadFloat f; c42 = ReadFloat f; c43 = ReadFloat f
	
fseek f getPos #seek_set	
	
tfm = matrix3 [c11,c12,c13] [c21,c22,c23] [c31,c32,c33] ([c41,c42,c43])	
)	
	
Print ("Bones End @ 0x"+((bit.intAsHex(ftell f))as string))		

fseek f 0x28 #seek_cur

Print ("Mesh End @ 0x"+((bit.intAsHex(ftell f))as string))			
	
try(	
msh = mesh vertices:vertArray faces:faceArray
msh.name = (MeshName+"_"+filename2+"_"+i as string)
	
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.backfacecull = false	

max modify mode
select msh
skinMod = skin ()
addModifier msh skinMod
for i = 1 to  BoneNameArray.count do
(
	maxbone = getNodeByName  BoneNameArray[i] 
	if i != 72 then 
		skinOps.addBone skinMod maxbone 0
	else
		skinOps.addBone skinMod maxbone 1
)
modPanel.setCurrentObject skinMod
for i = 1 to Weight_array.count 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 "Check LOGs!\n")
)	

fclose f