if (heapSize < 20000000) then
	heapSize = 200000000
gname = getOpenFileName \
caption:"Open .x.1 from Bones folder" \
types:"DSector_Bones (*.x.1)|*.x.1" \
historyCategory:"DSectorObjectPresets"
g = fopen gname "rb"
fname = getOpenFileName \
caption:"Open .fbx from Mesh folder" \
types:"DSector (*.x.1)|*.x.1" \
historyCategory:"DSectorObjectPresets"
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
)	

clearlistener()

struct weight_data
(
   boneids,weights
)
struct face_info_struct
(
FacePos,FaceCount
)
clearlistener()

BoneNamesArray = #()
BoneNamesArray1 = #()
boneparent_array = #()
UsedBoneCountArray = #()
FaceArray = #()
BNArr1 = #()

fseek g 0xb #seek_cur
modelPathS = readlong g
modelPath = ReadFixedString g modelPathS
fseek g 0x8 #seek_cur
materialS = readlong g
materialname = ReadFixedString g materialS
Print ("Last Read @ 0x"+((bit.intAsHex(ftell g))as string))
fseek g 0x28 #seek_cur
BoneCount = readlong g
for a = 1 to BoneCount do (
BoneNameSize = readlong g	
BoneName = ReadFixedString g BoneNameSize	
append BoneNamesArray BoneName
)	

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

	
fseek f 0x0 #seek_set	
BNArr1 = #()
for a = 1 to BoneCount do ( --second bones section
c11 = Readfloat f; c12 = Readfloat f; c13 = Readfloat f; c14 = Readfloat f
c21 = Readfloat f; c22 = Readfloat f; c23 = Readfloat f; c24 = Readfloat f
c31 = Readfloat f; c32 = Readfloat f; c33 = Readfloat f; c34 = Readfloat f
c41 = Readfloat f; c42 = Readfloat f; c43 = Readfloat f; c44 = Readfloat f

tfm2 = matrix3 [c11,c12,c13] [c21,c22,c23] [c31,c32,c33] ([c41,c42,c43]*100)	


if (getNodeByName BoneNamesArray[a]) != undefined do (
append BNArr1 (getNodeByName  BoneNamesArray[a])
)
if (getNodeByName  BoneNamesArray[a]) == undefined do (

	newBone2 = bonesys.createbone	\
				  tfm2.row4	\
				  (tfm2.row4 + 0.01 * (normalize tfm2.row1)) \
				  (normalize tfm2.row3)
			newBone2.name = BoneNamesArray[a]		  
			newBone2.width  = 0.01
			newBone2.height = 0.01
			newBone2.transform = inverse tfm2		  
			newBone2.setBoneEnable false 0
			newBone2.wirecolor = white
			newbone2.showlinks = true
			newBone2.pos.controller      = TCB_position ()
			newBone2.rotation.controller = TCB_rotation ()						  
append BNArr1 newBone2
)	
)

Print ("Last Read @ 0x"+((bit.intAsHex(ftell f))as string))
	
rootRT = dummy name:"Main_Root" boxsize:[.1,.1,.1]
for b in BNArr1 where b.parent==undefined do b.parent = rootRT
rootRT.transform = (matrix3 [-1,0,0] [0,0,1] [0,-1,0] [0,0,0])	

fclose g	
fclose f