cho tớ hỏi hàm này bị sai gì sao open file 2-5MB là crash thế 

Mã:
if (heapSize < 20000000) then
heapSize = 200000000 -- allow ~ 40 MB instead of just 7.5 MB. Prevents "Runtime Error: Out of scripter memory"
fname = getOpenFileName \
caption:"Open Heroes of Newerth Model File" \
types:"Heroes of Newerth Model File(*.model)|*.model" \
historyCategory:"Heroes of NewerthObjectPresets"
f = fopen fname "rb"
fn ReadFixedString bstream fixedLen =
(
local str = ""
for i = 1 to fixedLen do
(
str += bit.intAsChar (ReadByte bstream #unsigned)
)
str
)
SMDLhead = ReadFixedString f 8
unk01 = readlong f
version = readlong f
num_meshes = readlong f
num_sprites = readlong f
num_surfs = readlong f
BoneCount = readlong f
fseek f 0x18#seek_cur
bone1 = ReadFixedString f 4
bonesize = readlong f
BNArr = #()
for i = 1 to BoneCount Do (
BoneParentID = readlong f
BoneParentID = (BoneParentID + 1)
m11 = readfloat f; m12 = readfloat f; m13 = readfloat f
m21 = readfloat f; m22 = readfloat f; m23 = readfloat f
m31 = readfloat f; m32 = readfloat f; m33 = readfloat f
m41 = readfloat f; m42 = readfloat f; m43 = readfloat f
tfm = matrix3 [m11,m12,m13] [m21,m22,m23] [m31,m32,m33] [m41,m42,m43]
tfm = inverse tfm
m11 = readfloat f; m12 = readfloat f; m13 = readfloat f
m21 = readfloat f; m22 = readfloat f; m23 = readfloat f
m31 = readfloat f; m32 = readfloat f; m33 = readfloat f
m41 = readfloat f; m42 = readfloat f; m43 = readfloat f
tfm2 = matrix3 [m11,m12,m13] [m21,m22,m23] [m31,m32,m33] [m41,m42,m43]
bnamesize = readbyte f#unsigned
BoneName = readstring f
newBone = bonesys.createbone \
tfm.row4 \
(tfm.row4 + 0.01 * (normalize tfm.row1)) \
(normalize tfm.row3)
newBone.name = BoneName
newBone.width = 0.3
newBone.height = 0.3
newBone.wirecolor = yellow
newbone.showlinks = true
newBone.setBoneEnable false 0
newBone.pos.controller = TCB_position ()
newBone.rotation.controller = TCB_rotation ()
append BNArr newBone
if i != 1 then
newBone.parent = BNArr[BoneParentID]
)
--fseek f bonesize#seek_cur -- skip bones
mesh1 = ReadFixedString f 4
unk07 = readlong f
unk08 = readlong f
unk09 = readlong f
VertexCount = readlong f
fseek f 0x1C#seek_cur
nsize1 = readbyte f#unsigned
nsize2 = readbyte f#unsigned
nname1 = readstring f
nname2 = readstring f
Vert_array = #()
Normal_array = #()
UV_array = #()
Face_array = #()
vrts = ReadFixedString f 4
vrts_size = readlong f
null = readlong f
for a = 1 to VertexCount Do (
vx = readfloat f --read xyz coordinates
vy = readfloat f
vz = readfloat f
append Vert_array [vx,vy,vz] --save verts to Vert_array
)
lnk1 = ReadFixedString f 4
lnk1_size = readlong f
null1 = readlong f
weightc1 = readlong f
struct weight_data
(
boneids,weights
)
Weight_array = #()
used_id_array = #()
for a = 1 to weightc1 Do (
w = (weight_data boneids:#() weights:#())
maxweight = 0
WeightCount = readlong f
if WeightCount == 1 do (
weight1 = readfloat f
weight2 = 0
weight3 = 0
weight4 = 0
weight5 = 0
weight6 = 0
weight7 = 0
bone1 = readlong f
)
if WeightCount == 2 do (
weight1 = readfloat f
weight2 = readfloat f
weight3 = 0
weight4 = 0
weight5 = 0
weight6 = 0
weight7 = 0
bone1 = readlong f
bone2 = readlong f
)
if WeightCount == 3 do (
weight1 = readfloat f
weight2 = readfloat f
weight3 = readfloat f
weight4 = 0
weight5 = 0
weight6 = 0
weight7 = 0
bone1 = readlong f
bone2 = readlong f
bone3 = readlong f
)
if WeightCount == 4 do (
weight1 = readfloat f
weight2 = readfloat f
weight3 = readfloat f
weight4 = readfloat f
weight5 = 0
weight6 = 0
weight7 = 0
bone1 = readlong f
bone2 = readlong f
bone3 = readlong f
bone4 = readlong f
)
if WeightCount == 5 do (
weight1 = readfloat f
weight2 = readfloat f
weight3 = readfloat f
weight4 = readfloat f
weight5 = readfloat f
weight6 = 0
weight7 = 0
bone1 = readlong f
bone2 = readlong f
bone3 = readlong f
bone4 = readlong f
bone5 = readlong f
)
if WeightCount == 6 do (
weight1 = readfloat f
weight2 = readfloat f
weight3 = readfloat f
weight4 = readfloat f
weight5 = readfloat f
weight6 = readfloat f
weight7 = 0
bone1 = readlong f
bone2 = readlong f
bone3 = readlong f
bone4 = readlong f
bone5 = readlong f
bone6 = readlong f
)
if WeightCount == 7 do (
weight1 = readfloat f
weight2 = readfloat f
weight3 = readfloat f
weight4 = readfloat f
weight5 = readfloat f
weight6 = readfloat f
weight7 = readfloat f
bone1 = readlong f
bone2 = readlong f
bone3 = readlong f
bone4 = readlong f
bone5 = readlong f
bone6 = readlong f
bone7 = readlong f
)
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(weight5 != 0) then
maxweight = maxweight + weight5
if(weight6 != 0) then
maxweight = maxweight + weight6
if(weight7 != 0) then
maxweight = maxweight + weight7
if(maxweight != 0) then
(
if(weight1 != 0) then
(
w1 = weight1 as float
append w.boneids (bone1+1) --BoneID_array[bone1+1])
append w.weights (w1 / 100.0)
append used_id_array (bone1+1)
)
if(weight2 != 0) then
(
w2 = weight2 as float
append w.boneids (bone2+1) --(BoneID_array[bone2+1])
append w.weights (w2 / 100.0)
append used_id_array (bone2+1)
)
if(weight3 != 0) then
(
w3 = weight3 as float
append w.boneids (bone3+1)--(BoneID_array[bone3+1])
append w.weights (w3 / 100.0)
append used_id_array (bone3+1)
)
if(weight4 != 0) then
(
w4 = weight4 as float
append w.boneids (bone4+1)--(BoneID_array[bone4+1])
append w.weights (w4 / 100.0)
append used_id_array (bone4+1)
)
if(weight5 != 0) then
(
w5 = weight5 as float
append w.boneids (bone5+1)--(BoneID_array[bone4+1])
append w.weights (w5 / 100.0)
append used_id_array (bone5+1)
)
if(weight6 != 0) then
(
w6 = weight6 as float
append w.boneids (bone6+1)--(BoneID_array[bone6+1])
append w.weights (w6 / 100.0)
append used_id_array (bone6+1)
)
if(weight7 != 0) then
(
w7 = weight7 as float
append w.boneids (bone7+1)--(BoneID_array[bone6+1])
append w.weights (w7 / 100.0)
append used_id_array (bone7+1)
)
)
append Weight_array w
)
--fseek f lnk1_size#seek_cur
face = ReadFixedString f 4
face_size = readlong f
null = readlong f
face_count = readlong f
facesize2 = readbyte f#unsigned
for a = 1 to face_count Do (
f1 = (readshort f) + 1 --read face indices, games are start form 0, but Max start from 1
f2 = (readshort f) + 1 --so we add 1 to each index
f3 = (readshort f) + 1
append Face_array [f1,f2,f3] --save faces to Face_array
)
texc = ReadFixedString f 4
texc_size = readlong f
null = readlong f
null = readlong f
for a = 1 to VertexCount Do (
tu = readfloat f --read UV float value
tv = (readfloat f) * -1
append UV_array [tu,tv,0] --save UVs to UV_array
)
tang = ReadFixedString f 4
tang_size = readlong f
fseek f tang_size#seek_cur
sign = ReadFixedString f 4
sign_size = readlong f
if sign == "nrml" do (
null = readlong f
for a = 1 to VertexCount Do (
nx = readfloat f --read xyz coordinates
ny = readfloat f
nz = readfloat f
append Normal_array [nx,ny,nz] --save verts to Vert_array
)
)
if sign == "sign" do (
fseek f sign_size#seek_cur
)
total_size = getFileSize fname
if total_size != (ftell f) do (
nrml = ReadFixedString f 4
nrml_size = readlong f
null = readlong f
for a = 1 to VertexCount Do (
nx = readfloat f --read xyz coordinates
ny = readfloat f
nz = readfloat f
append Normal_array [nx,ny,nz] --save verts to Vert_array
)
)
msh = mesh vertices:Vert_array faces:Face_array --build mesh
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]
for j = 1 to Normal_array.count do setNormal msh j Normal_array[j]
max modify mode
select msh
skinMod = skin ()
addModifier msh skinMod
for i = 1 to BNArr.count do
(
maxbone = getnodebyname BNArr[i].name
if i != BNArr.count 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
)
for i = 1 to BNArr.count do (
j = i + 1
if i == j do (
if used_id_array[i] != used_id_array[j] do (
skinOps.removebone skinMod used_id_array[i]
)
)
)
max create mode
fclose f