--1 -6 verts
--7 - a uv
-- datapc_whiteroom.forge - animals (ce_a)
-- datapc_sharedgroup_01.forge animals textures (ce_a)
-- datapc_ACE_EGYPT.forge animals models (ce_a)

--f__mesh = fopen "D:\\ACorig\\_unp\\1096652136\\CE_W_DLC02_Akhenaten_Sword_LOD0\\1.dat" "rb"  
--dt = 5
		-- 4 - most weapons
		-- 5
		
		
		
		
		
		
		
		
		

if (heapSize < 200000) then
        heapSize = 2000000 -- allow ~ 40 MB instead of just 7.5 MB. Prevents "Runtime Error: Out of scripter memory"
fname = getOpenFileName \
caption:"Open LOD file" \
types:"Assassin's Creed Origins (*.*)|*.*" \
historyCategory:"AssassinsCreedOrigins"

f__mesh = fopen fname "rb"


struct submeshOrig
(
num = 0,
id = 0,
vert_count = 0,
face_count = 0,
max_face_index = 0,
min_face_index = 0x010000,
name = "",
vert_arr = #(),
face_arr = #(),
uv_arr = #(),
uv_arr2 = #(),

face_offset = 0,
vert_offset = 0,
uv_offset = 0

)

face_count = 0
vert_count = 0
vert_size = 0x14
v_count_offset = 0
UV_arrays = #()

glob_vert_array = #()
glob_face_array = #()
glob_UV_array = #()

stp = 0

	size_vert_buffer_offset = 0
vert_buffer_offset = size_vert_buffer_offset + 4




fseek f__mesh 0x08 #seek_set 
name_len = readlong f__mesh #unsigned
str_name = ""


for i = 1 to name_len do (
	str_name = str_name + bit.intaschar(readbyte f__mesh #unsigned)
	)
print("name: " + str_name)




fseek f__mesh 0x1d #seek_cur
ch1_number = readlong f__mesh #unsigned
print("block_chunks1 offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) - 4)) + "	|| value: 0x" + toUpper(bit.intAsHex(ch1_number)) + "(" + (ch1_number as string) +")")




for i = 1 to ch1_number do (
    str22 = "blk:: "
	
		
		for qq = 1 to 0x51 do (
			tmp_bt = readbyte f__mesh #unsigned
			str2 = toUpper(bit.intAsHex((tmp_bt) ))
			if(str2.count == 1) do str2 = "0" + str2
			if(str2.count == 8) do str2 = str2[7] + str2[8]
			str22 = str22 + str2 + "	"
			)	
	print(str22)			
	)
print("block_chunks1-0 end offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) )))



for i = 1 to 2 do (
    str22 = ""
	
		
		for qq = 1 to 0x10 do (
			tmp_bt = readbyte f__mesh #unsigned
			str2 = toUpper(bit.intAsHex((tmp_bt) ))
			if(str2.count == 1) do str2 = "0" + str2
			if(str2.count == 8) do str2 = str2[7] + str2[8]
			str22 = str22 + str2 + "	"
			)	
	print(str22)			
	)
	
readbyte f__mesh #unsigned
print("block_chunks1-1 end offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) )))

for i = 1 to 2 do (
    str22 = "blc:: "
	
		
		for qq = 1 to 0x11 do (
			tmp_bt = readbyte f__mesh #unsigned
			str2 = toUpper(bit.intAsHex((tmp_bt) ))
			if(str2.count == 1) do str2 = "0" + str2
			if(str2.count == 8) do str2 = str2[7] + str2[8]
			str22 = str22 + str2 + "	"
			)	
	print(str22)			
	)
print("block_chunks1-2 end offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) )))




fseek f__mesh 0x29 #seek_cur
ch1000_number = readlong f__mesh #unsigned
print("ch1000_number offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) - 4)) + "	|| value: 0x" + toUpper(bit.intAsHex(ch1000_number)) + "(" + (ch1000_number as string)+")")
--fseek f__mesh (ch1000_number * 0x18) #seek_cur
for i = 1 to ch1000_number do (
	str22 = "blk:: "

	
	for qq = 1 to 0x18 do (
		tmp_bt = readbyte f__mesh #unsigned
		str2 = toUpper(bit.intAsHex((tmp_bt) ))
		if(str2.count == 1) do str2 = "0" + str2
		if(str2.count == 8) do str2 = str2[7] + str2[8]
		str22 = str22 + str2 + "	"
		)	
	print(str22)
	)
print("ch1000_number end offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) )))





ch20_number = readlong f__mesh #unsigned
ch20_offset = ftell f__mesh
print("block_chunks20 offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) - 4)) + "	|| value: 0x" + toUpper(bit.intAsHex(ch20_number)) + "(" + (ch20_number as string)+")")
fseek f__mesh ch20_number #seek_cur
print("block_chunks20 end offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) )))


ch30_number = readlong f__mesh #unsigned
ch30_offset = ftell f__mesh
print("ch30_number offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) - 4)) + "	|| value: 0x" + toUpper(bit.intAsHex(ch30_number)) + "(" + (ch30_number as string)+")")
fseek f__mesh ch30_number #seek_cur
print("ch30_number end offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) )))

ch40_number = readlong f__mesh #unsigned
print("ch40_number offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) - 4)) + "	|| value: 0x" + toUpper(bit.intAsHex(ch40_number)) + "(" + (ch40_number as string)+")")
fseek f__mesh ch40_number #seek_cur
print("ch40_number end offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) )))


ch50_number = readlong f__mesh #unsigned   -- faces
ch50_offset = ftell f__mesh
print("ch50_number offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) - 4)) + "	|| value: 0x" + toUpper(bit.intAsHex(ch50_number)) + "(" + (ch50_number as string)+")")
fseek f__mesh ch50_number #seek_cur
print("ch50_number end offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) )))


ch60_number = readlong f__mesh #unsigned
print("ch60_number offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) - 4)) + "	|| value: 0x" + toUpper(bit.intAsHex(ch60_number)) + "(" + (ch60_number as string)+")")
fseek f__mesh ch60_number #seek_cur
print("ch60_number end offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) )))


ch70_number = readlong f__mesh #unsigned
print("ch70_number offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) - 4)) + "	|| value: 0x" + toUpper(bit.intAsHex(ch70_number)) + "(" + (ch70_number as string)+")")
fseek f__mesh ch70_number #seek_cur
print("ch70_number end offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) )))


ch80_number = readlong f__mesh #unsigned
print("ch80_number offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) - 4)) + "	|| value: 0x" + toUpper(bit.intAsHex(ch80_number)) + "(" + (ch80_number as string)+")")
fseek f__mesh ch80_number #seek_cur
print("ch80_number end offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) )))


for i = 1 to 1 do (
	
	    str22 = "blk:: "
	
		
		for qq = 1 to 0x12 do (
			tmp_bt = readbyte f__mesh #unsigned
			str2 = toUpper(bit.intAsHex((tmp_bt) ))
			if(str2.count == 1) do str2 = "0" + str2
			if(str2.count == 8) do str2 = str2[7] + str2[8]
			str22 = str22 + str2 + "	"
			)	
	print(str22)
	)
	
ch90_number = readlong f__mesh #unsigned
print("ch90_number offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) - 4)) + "	|| value: 0x" + toUpper(bit.intAsHex(ch90_number)) + "(" + (ch90_number as string)+")")


mesh_array = #()
for i = 1 to ch90_number do (
	cur_submesh = submeshOrig()
	cur_submesh.num = i
	fseek f__mesh 0x14 #seek_cur
	cur_submesh.vert_count = readlong f__mesh #unsigned
	fseek f__mesh 0x04 #seek_cur
	cur_submesh.face_count = readlong f__mesh #unsigned
	append mesh_array cur_submesh
	
	fseek f__mesh 0x04 #seek_cur
	
	fseek f__mesh -0x24 #seek_cur
	cur_submesh.id = readshort f__mesh #unsigned
	fseek f__mesh -0x2 #seek_cur

	    str22 = "blk:: "
	
		
		for qq = 1 to 0x24 do (
		
			tmp_bt = readbyte f__mesh #unsigned
			str2 = toUpper(bit.intAsHex((tmp_bt) ))
			if(str2.count == 1) do str2 = "0" + str2
			if(str2.count == 8) do str2 = str2[7] + str2[8]
			str22 = str22 + str2 + "	"
			)	
	print(str22)
	)
	
for cur_submesh in mesh_array do (
	print("submesh: vert_count: " + (cur_submesh.vert_count as string )   + "  face_count: " +  (cur_submesh.face_count as string ))
	)

print("ch90_number end offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) )))

fseek f__mesh 0x10 #seek_cur

ch100_number = readlong f__mesh #unsigned
print("ch100_number offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) - 4)) + "	|| value: 0x" + toUpper(bit.intAsHex(ch100_number)) + "(" + (ch100_number as string)+")")


for i = 1 to ch100_number do (


	
	    str22 = "blk:: "
	
		
		for qq = 1 to 0x1e do (
			tmp_bt = readbyte f__mesh #unsigned
			str2 = toUpper(bit.intAsHex((tmp_bt) ))
			if(str2.count == 1) do str2 = "0" + str2
			if(str2.count == 8) do str2 = str2[7] + str2[8]
			str22 = str22 + str2 + "	"
			)	
	print(str22)
	)
print("ch90_number end offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) )))






fseek f__mesh 0x16 #seek_cur

ch110_number = readlong f__mesh #unsigned
print("ch110_number offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) - 4)) + "	|| value: 0x" + toUpper(bit.intAsHex(ch110_number)) + "(" + (ch110_number as string)+")")


for i = 1 to ch110_number do (


	
	    str22 = ":: "
	
		
		for qq = 1 to 0x0a do (
			tmp_bt = readbyte f__mesh #unsigned
			str2 = toUpper(bit.intAsHex((tmp_bt) ))
			if(str2.count == 1) do str2 = "0" + str2
			if(str2.count == 8) do str2 = str2[7] + str2[8]
			str22 = str22 + str2 + "	"
			)	
	print(str22)
	)
print("ch90_number end offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh) )))




print("#################################################################################################################################")
print("###############################################################   FINAL     #####################################################")
print("#################################################################################################################################")











print("mesh_array_count:" + (mesh_array.count as string))
print("")
print("")

print("reading faces::")
fseek f__mesh ch50_offset #seek_set
print("faces offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh))) + "	|| value: 0x" + toUpper(bit.intAsHex(ch50_number / 6)) + "(" + ((ch50_number / 6) as string)+")")




for cur_submesh in mesh_array do (
	for i = 1 to cur_submesh.face_count do (
		x = (readshort f__mesh #unsigned)  + 1
		y = (readshort f__mesh #unsigned)  + 1
		z = (readshort f__mesh #unsigned)  + 1
		
		if (x != y) AND (y != z) AND (x != z) do (
		
		append cur_submesh.face_arr [x,y,z] 

		if x > cur_submesh.max_face_index do cur_submesh.max_face_index = x
		if y > cur_submesh.max_face_index do cur_submesh.max_face_index = y
		if z > cur_submesh.max_face_index do cur_submesh.max_face_index = z

		if x < cur_submesh.min_face_index do cur_submesh.min_face_index = x
		if y < cur_submesh.min_face_index do cur_submesh.min_face_index = y
		if z < cur_submesh.min_face_index do cur_submesh.min_face_index = z)
		
		)
	)	
print("end of face buffer")
print("current offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh)) ))	


for cur_submesh in mesh_array do (
	print("submesh: "+ (cur_submesh.num as string )+ "  id: 0x"+ toUpper(bit.intAsHex((cur_submesh.id)))+ " vert_count: " + (cur_submesh.vert_count as string )   + "  face_count: " +  (cur_submesh.face_count as string )  + "  min_face_index: " +  (cur_submesh.min_face_index as string ) + "  max_face_index: " +  (cur_submesh.max_face_index as string ) )
	)

--#################################################################################################################################
--############################################################### External UV #####################################################
--#################################################################################################################################

if((ch30_number > 0) AND (ch20_number > 0)) then (
print("EXTERNAL UV")
fseek f__mesh ch30_offset #seek_set
print("vertex offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh))) + "	|| value: 0x" + toUpper(bit.intAsHex(ch50_number / 6)) + "(" + ((ch50_number / 6) as string)+")")

global_vertex_count = 0

for cur_submesh in mesh_array do (
	global_vertex_count  = global_vertex_count + cur_submesh.vert_count
	for i = 1 to cur_submesh.vert_count do (
		voff = ftell f__mesh
		x = readshort  f__mesh #signed
		y = readshort  f__mesh #signed
		z = readshort  f__mesh #signed
		append cur_submesh.vert_arr [x,y,z]
		fseek f__mesh (voff + 0x0c)  #seek_set	
		)
	)	
print("end of vertex buffer")
print("current offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh)) ))	

uv_size = (ch20_number / global_vertex_count)
print ("uv_size " + ( uv_size as string))
fseek f__mesh ch20_offset #seek_set

fseek f__mesh ch20_offset #seek_set
print("UV offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh))) + "	|| value: 0x" + toUpper(bit.intAsHex(ch50_number / 6)) + "(" + ((ch50_number / 6) as string)+")")

if(uv_size >= 16) then (
for cur_submesh in mesh_array do (
	for i = 1 to cur_submesh.vert_count do (
		voff = ftell f__mesh
		fseek f__mesh 4  #seek_cur
		u = (readshort f__mesh #unsigned)
		v = (readshort f__mesh #unsigned)
		append cur_submesh.uv_arr [((u as float) / 65536 * 32), 1 - ((v as float) / 65536 * 32), 0]
		u2 = (readshort f__mesh #unsigned)
		v2 = (readshort f__mesh #unsigned)
		append cur_submesh.uv_arr2 [((u2 as float) / 65536 * 32), 1 - ((v2 as float) / 65536 * 32), 0]

		fseek f__mesh (voff + 0x10)  #seek_set	
		)
	)	
	)
else if(uv_size >= 8) then (
for cur_submesh in mesh_array do (
	for i = 1 to cur_submesh.vert_count do (
		voff = ftell f__mesh
		u2 = (readshort f__mesh #unsigned)
		v2 = (readshort f__mesh #unsigned)
		u = (readshort f__mesh #unsigned)
		v = (readshort f__mesh #unsigned)
		append cur_submesh.uv_arr [((u as float) / 65536 * 32), 1 - ((v as float) / 65536 * 32), 0]
		append cur_submesh.uv_arr2 [((u2 as float) / 65536 * 32), 1 - ((v2 as float) / 65536 * 32), 0]

		--fseek f__mesh (voff + 0x0c)  #seek_set	
		)
	)	
	)
	
print("end of UV buffer")
print("current offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh)) ))	

)





--#################################################################################################################################
--############################################################### Internal UV #####################################################
--#################################################################################################################################


if((ch30_number == 0) AND (ch20_number > 0)) then (

print("INTERNAL UV")

fseek f__mesh ch20_offset #seek_set
print("vertex offset:  0x"  + toUpper(bit.intAsHex((ftell f__mesh))) + "	|| value: 0x" + toUpper(bit.intAsHex(ch50_number / 6)) + "(" + ((ch50_number / 6) as string)+")")



for cur_submesh in mesh_array do (
		for i = 1 to (amax cur_submesh.vert_count cur_submesh.max_face_index) do (
		voff = ftell f__mesh
		x = (readshort f__mesh #signed)
		y = (readshort f__mesh #signed)
		z = (readshort f__mesh #signed)		
		tmp_buf = #()
		scl = 1
		str1 = ""
		
		for qq = 1 to (vert_size - 10) do (
			tmp_bt = readbyte f__mesh #usigned
			append tmp_buf tmp_bt
			str2 = toUpper(bit.intAsHex((tmp_bt) ))
			if(str2.count == 1) do str2 = "0" + str2
			if(str2.count == 8) do str2 = str2[7] + str2[8]
			str1 = str1 + str2 + "	"
			)		
		if(tmp_buf[1] < 0) do scl = -1
		u = (readshort f__mesh #unsigned)
		v = (readshort f__mesh #unsigned)

		str1 = str1 + " || scl: " + (scl as string)
		--print str1
		append cur_submesh.uv_arr [((u as float) / 65536 * 32), 1 - ((v as float) / 65536 * 32), 0]
		append cur_submesh.uv_arr2 [((u as float) / 65536 * 32), 1 - ((v as float) / 65536 * 32), 0]

		append cur_submesh.vert_arr ([x,y,z] * scl)
		
		
		fseek f__mesh (voff + 0x14) #seek_set
		)
	)
	)
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
		
for cur_submesh in mesh_array do (
	print("submesh: "+ (cur_submesh.num as string )+ "  id: 0x"+ toUpper(bit.intAsHex((cur_submesh.id)))+ " vert_count: " + (cur_submesh.vert_count as string ) +"||"+  (cur_submesh.vert_arr.count as string ) + " UV_count: " + "||"+  (cur_submesh.vert_arr.count as string ) + "  face_count: " +  (cur_submesh.face_count as string ) + "||" +  (cur_submesh.face_arr.count as string )  + "  min_face_index: " +  (cur_submesh.min_face_index as string ) + "  max_face_index: " +  (cur_submesh.max_face_index as string ) )
	)	
	
	
	
	
	
	
	
	
	
	
	
	
	
for cur_submesh in mesh_array do (
	--try(
	msh = mesh name:(str_name + "_num" + (cur_submesh.num as string)) vertices:cur_submesh.vert_arr faces:cur_submesh.face_arr
	
	meshop.setNumMaps msh 3

	
	meshop.setMapSupport msh 1 true 
	for i=1 to cur_submesh.vert_count do (
		meshop.setMapVert msh 1 i cur_submesh.uv_arr[i])
	meshop.buildMapFaces msh 1
	update msh
	
	meshop.setMapSupport msh 2 true 
	for i=1 to cur_submesh.vert_count do (
		meshop.setMapVert msh 2 i cur_submesh.uv_arr2[i])
	meshop.buildMapFaces msh 2
	update msh
	
	
	
	
	print ("SUCCESSFUL CREATING SUBMESH num: " + (cur_submesh.num as string))
	--)
	--catch (
	--print("error creating submesh")
	--)
	)



fclose f__mesh
