fname = GetOpenFileName caption:"Dead Space Skeleton" types:"(*.rcb.win)|*.rcb.win"
If (fname!=undefined) and ((DoesFileExist fname)==true) then (--)
g = fopen fname "rb"   --open file in read only format
clearlistener()
fscale=100
-- delete $*

fn ReadFixedString bstream fixedLen = (
   local str = ""
   for i = 1 to fixedLen do
   (
      str += bit.intAsChar (ReadByte bstream #unsigned)
   )
   str
)
fn GetName bstream Offset = (
	fseek bstream Offset #seek_set
	MeshName = readstring bstream
	return MeshName
)

BoneNameOffset=#()
BoneNameLength=#()
BoneName=#()
BoneParentID_Array=#()
BoneArrayNameOffset=#()
BoneName=#()
BoneCount=#()
BoneIDTable=#()
BoneStart=#()

FileLength = readlong g
ukw =readlong g	--always 0x1a
ukw2 = readlong g
fseek g 0xc #seek_cur

SkelCount = readlong g	-- always 1, prob count
DataOffset = readlong g
ukw4 = readlong g	-- same as ukw 3
DataOffset2 = readlong g
fseek g 0x10 #seek_cur
ukw5 = readlong g
ukw6 = readlong g	-- same as ukw 4 and 3
DataOffset3 = readlong g

fseek g DataOffset #seek_set

for x=1 to SkelCount do (
	ukw7 = readlong g
	BoneArrayNameOffset[x] = readlong g
	BoneCount[x] = readlong g
	BoneIDTable[x] = readlong g
	BoneStart[x] = readlong g
	ukwtableoffset = readlong g
	
	Print ("BoneStart @ 0x"+((bit.intAsHex(BoneStart[x]))as string))
	Print ("BoneCount @ 0x"+((bit.intAsHex(BoneCount[x]))as string))

)

for x=1 to SkelCount do (
	BoneName[x] = GetName g BoneArrayNameOffset[x]
	print BoneName[x]
)


For x=1 to SkelCount do (
	BoneParentID=#()

	fseek g BoneIDTable[x] #seek_set
	For y=1 to BoneCount[x] do (
		BoneParentID[y] = Readlong g + 1
		fseek g 0xc #seek_cur
	)
	BoneParentID_Array[x] = BoneParentID
)

For x=1 to SkelCount do (
	fseek g bonestart[x] #seek_set
	
	BoneColor = random black white

	BNArr = #()
	for y=1 to BoneCount[x] do (
		
		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,m21,m31,m41] [m12,m22,m32,m42] [m13,m23,m33,m43] [m14,m24,m34,m44]	
		
		newBone = bonesys.createbone	\
		tfm.row4	\
		(tfm.row4 + 0.01 * (normalize tfm.row1)) \
		(normalize tfm.row3)
		newBone.width = 0.1
		newBone.height = 0.1
		If SkelCount == 1 then newBone.wirecolor = yellow
		Else newBone.wirecolor = BoneColor
		
		newbone.showlinks = true
		newBone.transform = inverse tfm

		
		pos = [m41,m42,m43]
		pos = pos * tfm

		newBone.pos.x += ((fscale)*pos.x)
		newBone.pos.y += (-1*(fscale)*pos.z)
		newBone.pos.z += (-1*(fscale)*pos.y)	

		newBone.setBoneEnable false 0
		newBone.pos.controller = TCB_position ()
		newBone.rotation.controller = TCB_rotation ()
		
		
		if (BoneParentID_Array[x][y] != 0) then (
		newBone.parent = BNArr[BoneParentID_Array[x][y]]
	   )
	   else (
			newbone.name = BoneName[x]
		)
		append BNArr newBone
	)
)




select $*
deselect $*

Print ("Last Read @ 0x"+((bit.intAsHex(ftell g))as string))
)

gc()
fclose g