Implemented reading and writing of tex_idx and AltPivotInfo.

Fixed ModelHeader output to match GetVrml's output.
bugfix: Would include BoneInfo even if the model has no bones.
master
TigerKat 5 years ago
parent ca990f4bd3
commit 7a7dce51ed

@ -20,5 +20,4 @@ Dumps all the meshes contained in <file.geo> to <geo_name>/<model
##Known issues: ##Known issues:
- Not all structures are read from the .geo file. (AltPivotInfo)
- Not all structures are regenerated when writing a .geo file. (PolyGrid and Reductions) - Not all structures are regenerated when writing a .geo file. (PolyGrid and Reductions)

118
geo.py

@ -33,7 +33,7 @@ from bones import *
#16 4 i32: lodinfo_blocksize (only present in version 2 to 6) #16 4 i32: lodinfo_blocksize (only present in version 2 to 6)
#? texname_blocksize PackNames block of texture names #? texname_blocksize PackNames block of texture names
#? objname_blocksize objnames #? objname_blocksize objnames
#? texidx_blocksize texidx #? texidx_blocksize TexID[]: texidx_block,
#? lodinfo_blocksize lodinfo (only present in version 2 to 6) #? lodinfo_blocksize lodinfo (only present in version 2 to 6)
#? ModelHeader ??? #? ModelHeader ???
@ -84,12 +84,12 @@ from bones import *
# Offset Size Description # Offset Size Description
# 0 4 i32: size # 0 4 i32: size
# 4 4 f32: radius # 4 4 f32: radius
# 8 4 i32: tex_count # 8 4 i32: tex_count, The number of TexID entries in the texidx_block.
# 12 4 ptr(BoneInfo): boneinfo ? # 12 4 ptr(BoneInfo): boneinfo ?
# 16 4 i32: vert_count # 16 4 i32: vert_count
# 20 4 i32: tri_count # 20 4 i32: tri_count
#ver 8+ 24 4 i32: reflection_quad_count #ver 8+ 24 4 i32: reflection_quad_count
# +4 4 i32: tex_idx # +4 4 ptr(TexID): tex_idx, Byte offset in the texidx_block to the first TexID for this model.
# +4 32 PolyGrid: grid # +4 32 PolyGrid: grid
# +32 4 ptr(char): name # +32 4 ptr(char): name
# +4 4 ptr(AltPivotInfo): # +4 4 ptr(AltPivotInfo):
@ -129,6 +129,10 @@ from bones import *
#8 4 ptr(u8): data, The offset of this block of data inside the .geo's main data block. #8 4 ptr(u8): data, The offset of this block of data inside the .geo's main data block.
#12 #12
#TexID:
#Offset Size Description
#0 2 u16: id, Index to the texture name in the texture name list.
#2 2 u16: count, Number of tris using this texture.
#struct PolyGrid: "libs/UtilitiesLib/components/gridpoly.h" #struct PolyGrid: "libs/UtilitiesLib/components/gridpoly.h"
#Offset Size Description #Offset Size Description
@ -481,7 +485,12 @@ class Data:
if offset is None: if offset is None:
offset = self.offset offset = self.offset
self.data[0 : offset] self.data[0 : offset]
def __len__(self):
return len(self.data)
def __str__(self):
return str(self.data)
def __repr__(self):
return repr(self.data)
class Reductions: class Reductions:
def __init__(self, model): def __init__(self, model):
@ -630,25 +639,25 @@ class Model:
self.pack_reflection_quads = self.geo.getHeaderElement("<iii") self.pack_reflection_quads = self.geo.getHeaderElement("<iii")
else: else:
self.pack_reflection_quads = (0, 0, 0) self.pack_reflection_quads = (0, 0, 0)
pack_list = [ #pack_list = [
self.pack_tris, # self.pack_tris,
self.pack_verts, # self.pack_verts,
self.pack_norms, # self.pack_norms,
self.pack_sts, # self.pack_sts,
self.pack_sts3, # self.pack_sts3,
self.pack_weights, # self.pack_weights,
self.pack_matidxs, # self.pack_matidxs,
self.pack_grid, # self.pack_grid,
#pack.lmap_utransforms, pack.lmap_vtransforms # #pack.lmap_utransforms, pack.lmap_vtransforms
self.pack_reductions, # self.pack_reductions,
self.pack_reflection_quads, # self.pack_reflection_quads,
] #]
other_list = [ #other_list = [
self.boneinfo_ptr, # self.boneinfo_ptr,
self.name_ptr, # self.name_ptr,
self.api_ptr, # self.api_ptr,
] #]
(self.pack_boneinfo, self.pack_name, self.pack_api) = inferSizes(self.geo.main_data_size, pack_list, other_list) #(self.pack_boneinfo, self.pack_name, self.pack_api) = inferSizes(self.geo.main_data_size, pack_list, other_list)
self.name = extractString(self.geo.header_objname_data, self.name_ptr) self.name = extractString(self.geo.header_objname_data, self.name_ptr)
#self.geo.header_offset = final_offset - 12 - 2 #self.geo.header_offset = final_offset - 12 - 2
@ -665,7 +674,27 @@ class Model:
#print("automatic offset: %d final offset: %d" % (self.geo.header_offset, final_offset)) #print("automatic offset: %d final offset: %d" % (self.geo.header_offset, final_offset))
self.skipped_data = self.geo.header_data[self.geo.header_offset : final_offset] self.skipped_data = self.geo.header_data[self.geo.header_offset : final_offset]
self.geo.header_offset = final_offset self.geo.header_offset = final_offset
def parseData(self): def parseData(self):
self.tex_idx = []
texidx_data = self.geo.header_texidx_data
texidx_data.seek(self.tex_idx_ptr)
c = 0
for i in range(self.tex_count):
self.tex_idx.append(texidx_data.decode("<HH"))
c += self.tex_idx[-1][1]
#todo: assert(c == self.tri_count)
self.altpivotinfo = []
if self.api_ptr != 0:
self.geo.seekMainData(self.api_ptr)
(self.altpivotinfo_count, ) = self.geo.decode("<i")
for i in range(self.altpivotinfo_count):
self.altpivotinfo.append([])
for j in range(4):
self.altpivotinfo[i].append(list(self.geo.getMainElement("<fff")))
self.altpivotinfo_count = len(self.altpivotinfo)
self.tris_data = self.geo.getDataBlock(self.pack_tris) self.tris_data = self.geo.getDataBlock(self.pack_tris)
self.verts_data = self.geo.getDataBlock(self.pack_verts) self.verts_data = self.geo.getDataBlock(self.pack_verts)
self.norms_data = self.geo.getDataBlock(self.pack_norms) self.norms_data = self.geo.getDataBlock(self.pack_norms)
@ -793,18 +822,36 @@ class Model:
self.pack_grid = self.geo.encodeMainDataPacked(self.grid_data) self.pack_grid = self.geo.encodeMainDataPacked(self.grid_data)
self.pack_reductions = self.geo.encodeMainDataPacked(self.reductions_data) self.pack_reductions = self.geo.encodeMainDataPacked(self.reductions_data)
self.pack_reflection_quads = self.geo.encodeMainDataPacked(self.reflection_quads_data) self.pack_reflection_quads = self.geo.encodeMainDataPacked(self.reflection_quads_data)
if self.bone_count > 0:
bone_data = struct.pack("<" + "i" * (1 + 15), self.bone_count, *self.bone_ids) bone_data = struct.pack("<" + "i" * (1 + 15), self.bone_count, *self.bone_ids)
bone_data += struct.pack("<ii", 0, 0) #weights_ptr and matidx_ptr place holders, needed by the game bone_data += struct.pack("<ii", 0, 0) #weights_ptr and matidx_ptr place holders, needed by the game
self.boneinfo_ptr = self.geo.encodeMainData(bone_data) self.boneinfo_ptr = self.geo.encodeMainData(bone_data)
else:
self.boneinfo_ptr
api_data = b""
if len(self.altpivotinfo) > 0:
api_data = struct.pack("<i", len(self.altpivotinfo))
for i in range(len(self.altpivotinfo)):
for j in range(len(self.altpivotinfo[i])):
api_data += struct.pack("<fff", *self.altpivotinfo[i][j])
for i in range(15 - len(self.altpivotinfo)):
api_data += struct.pack("<fff", 0, 0, 0) * 4
if len(api_data) > 0:
self.api_ptr = self.geo.encodeMainData(api_data)
else:
self.api_ptr = 0
#Encode shared header data #Encode shared header data
print("unhandled: model.texidx_ptr !") self.texidx_ptr = len(self.geo.header_texidx_data)
self.texidx_ptr = 0 texidx_data = self.geo.header_texidx_data
texidx_data.seekEnd()
for t in self.tex_idx:
texidx_data.encode("<HH", *t)
self.tex_count = len(self.tex_idx)
self.name_ptr = len(self.geo.header_objname_data) self.name_ptr = len(self.geo.header_objname_data)
self.geo.header_objname_data += self.name + ZERO_BYTE self.geo.header_objname_data += self.name + ZERO_BYTE
self.geo.header_objnames.append(self.name) self.geo.header_objnames.append(self.name)
print("unhandled: model.api_ptr !")
self.api_ptr = 0
#Encode the header #Encode the header
self.header_data = b"" self.header_data = b""
@ -895,6 +942,7 @@ class Model:
#print(" reductions_data: %s" % ([self.reductions_data], )) #print(" reductions_data: %s" % ([self.reductions_data], ))
print(" reflection_quads_data: %s" % ([self.reflection_quads_data], )) print(" reflection_quads_data: %s" % ([self.reflection_quads_data], ))
print(" tex_idx: %s" % ([self.tex_idx]))
print(" tris: %s" % ([self.tris], )) print(" tris: %s" % ([self.tris], ))
print(" verts: %s" % ([self.verts], )) print(" verts: %s" % ([self.verts], ))
print(" norms: %s" % ([self.norms], )) print(" norms: %s" % ([self.norms], ))
@ -915,6 +963,8 @@ class Model:
print(" bone_count: %d" % self.bone_count) print(" bone_count: %d" % self.bone_count)
print(" bone_ids: %s" % (self.bone_ids, )) print(" bone_ids: %s" % (self.bone_ids, ))
print(" altpivotinfo: %s" % (self.altpivotinfo, ))
for i in range(self.bone_count): for i in range(self.bone_count):
print(" : %s" % (BONES_LIST[self.bone_ids[i]], )) print(" : %s" % (BONES_LIST[self.bone_ids[i]], ))
@ -972,7 +1022,7 @@ class Geo:
self.header_offset += self.texname_blocksize self.header_offset += self.texname_blocksize
self.header_objname_data = self.header_data[self.header_offset : self.header_offset + self.objname_blocksize] self.header_objname_data = self.header_data[self.header_offset : self.header_offset + self.objname_blocksize]
self.header_offset += self.objname_blocksize self.header_offset += self.objname_blocksize
self.header_texidx_data = self.header_data[self.header_offset : self.header_offset + self.texidx_blocksize] self.header_texidx_data = Data(self.header_data[self.header_offset : self.header_offset + self.texidx_blocksize])
self.header_offset += self.texidx_blocksize self.header_offset += self.texidx_blocksize
self.header_lodinfo_data = self.header_data[self.header_offset : self.header_offset + self.lodinfo_blocksize] self.header_lodinfo_data = self.header_data[self.header_offset : self.header_offset + self.lodinfo_blocksize]
self.header_offset += self.lodinfo_blocksize self.header_offset += self.lodinfo_blocksize
@ -1028,22 +1078,22 @@ class Geo:
self.header_objnames = [] self.header_objnames = []
self.header_objname_data = b"" self.header_objname_data = b""
self.lodinfo_data = b"" self.lodinfo_data = b""
self.header_texidx_data = Data()
#Encode models into main data #Encode models into main data
for m in self.models: for m in self.models:
m.encode() m.encode()
#todo: convert data #Convert remaining data
print("texname unhandled!") self.header_texname_data = packNames(self.header_texnames)
print("texidx unhandled!")
#Encode information into header data. #Encode information into header data.
self.header_data += struct.pack("<iiii", len(self.main_data), len(self.header_texname_data), len(self.header_objname_data), len(self.header_texidx_data)) self.header_data += struct.pack("<iiii", len(self.main_data), len(self.header_texname_data), len(self.header_objname_data), len(self.header_texidx_data))
if self.version >= 2 and self.version <= 6: if self.version >= 2 and self.version <= 6:
self.header_data += struct.pack("<i", len(self.lodinfo_data)) self.header_data += struct.pack("<i", len(self.lodinfo_data))
self.header_data += self.header_texname_data + self.header_objname_data + self.header_texidx_data self.header_data += self.header_texname_data + self.header_objname_data + self.header_texidx_data.data
if self.version >= 2 and self.version <= 6: if self.version >= 2 and self.version <= 6:
self.header_data += self.lodinfo_data self.header_data += self.lodinfo_data
#Encode the main model header. #Encode the main model header.
self.header_data += storeString(self.header_modelheader_name, 124) self.header_data += storeString(self.header_modelheader_name, 124)
self.header_data += struct.pack("<ifii", 0, self.header_modelheader_tracklength, 0, len(self.models)) self.header_data += struct.pack("<ifii", 0, self.header_modelheader_tracklength, -1, len(self.models))
#Encode model headers into header data. #Encode model headers into header data.
for m in self.models: for m in self.models:
m.encodeHeader() m.encodeHeader()
@ -1130,7 +1180,7 @@ if __name__ == "__main__":
print("Test loads a .geo file, dumps its content, and optionally writes its content out.") print("Test loads a .geo file, dumps its content, and optionally writes its content out.")
exit(0) exit(0)
fh = open(sys.argv[1], "rb") fh = open(sys.argv[1], "rb")
print(sys.argv) #print(sys.argv)
if len(sys.argv) <= 2: if len(sys.argv) <= 2:
fho = None fho = None
else: else:

Loading…
Cancel
Save