fix issues

- revert nullptr check for BMap::MeshTrans pointer getter.
- fix pointer return error of BMap::MeshTrans
- fix type hint error and variable name error in PyBMap
- fix CKMesh flag init value error
- improve empty material slots saving. add a nullptr entry if no slots to make sure virtools can load it.
This commit is contained in:
yyc12345 2023-12-01 23:31:09 +08:00
parent 74d8f02588
commit a70e32a306
5 changed files with 47 additions and 31 deletions

View File

@ -256,7 +256,7 @@ bool BMMeshTrans_PrepareVertexCount(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_IN(Li
bool BMMeshTrans_PrepareVertex(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_OUT(LibCmo::VxMath::VxVector3*, out_mem)) { bool BMMeshTrans_PrepareVertex(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_OUT(LibCmo::VxMath::VxVector3*, out_mem)) {
if (!CheckBMMeshTrans(trans)) return false; if (!CheckBMMeshTrans(trans)) return false;
BMPARAM_OUT_ASSIGN(out_mem, trans->PrepareVertex()); BMPARAM_OUT_ASSIGN(out_mem, trans->PrepareVertex());
return BMPARAM_OUT_VAL(out_mem) != nullptr; return true;
} }
bool BMMeshTrans_PrepareNormalCount(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_IN(LibCmo::CKDWORD, count)) { bool BMMeshTrans_PrepareNormalCount(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_IN(LibCmo::CKDWORD, count)) {
if (!CheckBMMeshTrans(trans)) return false; if (!CheckBMMeshTrans(trans)) return false;
@ -265,7 +265,7 @@ bool BMMeshTrans_PrepareNormalCount(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_IN(Li
bool BMMeshTrans_PrepareNormal(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_OUT(LibCmo::VxMath::VxVector3*, out_mem)) { bool BMMeshTrans_PrepareNormal(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_OUT(LibCmo::VxMath::VxVector3*, out_mem)) {
if (!CheckBMMeshTrans(trans)) return false; if (!CheckBMMeshTrans(trans)) return false;
BMPARAM_OUT_ASSIGN(out_mem, trans->PrepareNormal()); BMPARAM_OUT_ASSIGN(out_mem, trans->PrepareNormal());
return BMPARAM_OUT_VAL(out_mem) != nullptr; return true;
} }
bool BMMeshTrans_PrepareUVCount(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_IN(LibCmo::CKDWORD, count)) { bool BMMeshTrans_PrepareUVCount(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_IN(LibCmo::CKDWORD, count)) {
if (!CheckBMMeshTrans(trans)) return false; if (!CheckBMMeshTrans(trans)) return false;
@ -274,7 +274,7 @@ bool BMMeshTrans_PrepareUVCount(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_IN(LibCmo
bool BMMeshTrans_PrepareUV(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_OUT(LibCmo::VxMath::VxVector2*, out_mem)) { bool BMMeshTrans_PrepareUV(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_OUT(LibCmo::VxMath::VxVector2*, out_mem)) {
if (!CheckBMMeshTrans(trans)) return false; if (!CheckBMMeshTrans(trans)) return false;
BMPARAM_OUT_ASSIGN(out_mem, trans->PrepareUV()); BMPARAM_OUT_ASSIGN(out_mem, trans->PrepareUV());
return BMPARAM_OUT_VAL(out_mem) != nullptr; return true;
} }
bool BMMeshTrans_PrepareMtlSlotCount(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_IN(LibCmo::CKDWORD, count)) { bool BMMeshTrans_PrepareMtlSlotCount(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_IN(LibCmo::CKDWORD, count)) {
if (!CheckBMMeshTrans(trans)) return false; if (!CheckBMMeshTrans(trans)) return false;
@ -283,7 +283,7 @@ bool BMMeshTrans_PrepareMtlSlotCount(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_IN(L
bool BMMeshTrans_PrepareMtlSlot(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_OUT(LibCmo::CK2::CK_ID*, out_mem)) { bool BMMeshTrans_PrepareMtlSlot(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_OUT(LibCmo::CK2::CK_ID*, out_mem)) {
if (!CheckBMMeshTrans(trans)) return false; if (!CheckBMMeshTrans(trans)) return false;
BMPARAM_OUT_ASSIGN(out_mem, trans->PrepareMtlSlot()); BMPARAM_OUT_ASSIGN(out_mem, trans->PrepareMtlSlot());
return BMPARAM_OUT_VAL(out_mem) != nullptr; return true;
} }
bool BMMeshTrans_PrepareFaceCount(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_IN(LibCmo::CKDWORD, count)) { bool BMMeshTrans_PrepareFaceCount(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_IN(LibCmo::CKDWORD, count)) {
if (!CheckBMMeshTrans(trans)) return false; if (!CheckBMMeshTrans(trans)) return false;
@ -292,22 +292,22 @@ bool BMMeshTrans_PrepareFaceCount(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_IN(LibC
bool BMMeshTrans_PrepareFaceVertexIndices(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_OUT(LibCmo::CKDWORD*, out_mem)) { bool BMMeshTrans_PrepareFaceVertexIndices(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_OUT(LibCmo::CKDWORD*, out_mem)) {
if (!CheckBMMeshTrans(trans)) return false; if (!CheckBMMeshTrans(trans)) return false;
BMPARAM_OUT_ASSIGN(out_mem, trans->PrepareFaceVertexIndices()); BMPARAM_OUT_ASSIGN(out_mem, trans->PrepareFaceVertexIndices());
return BMPARAM_OUT_VAL(out_mem) != nullptr; return true;
} }
bool BMMeshTrans_PrepareFaceNormalIndices(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_OUT(LibCmo::CKDWORD*, out_mem)) { bool BMMeshTrans_PrepareFaceNormalIndices(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_OUT(LibCmo::CKDWORD*, out_mem)) {
if (!CheckBMMeshTrans(trans)) return false; if (!CheckBMMeshTrans(trans)) return false;
BMPARAM_OUT_ASSIGN(out_mem, trans->PrepareFaceNormalIndices()); BMPARAM_OUT_ASSIGN(out_mem, trans->PrepareFaceNormalIndices());
return BMPARAM_OUT_VAL(out_mem) != nullptr; return true;
} }
bool BMMeshTrans_PrepareFaceUVIndices(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_OUT(LibCmo::CKDWORD*, out_mem)) { bool BMMeshTrans_PrepareFaceUVIndices(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_OUT(LibCmo::CKDWORD*, out_mem)) {
if (!CheckBMMeshTrans(trans)) return false; if (!CheckBMMeshTrans(trans)) return false;
BMPARAM_OUT_ASSIGN(out_mem, trans->PrepareFaceUVIndices()); BMPARAM_OUT_ASSIGN(out_mem, trans->PrepareFaceUVIndices());
return BMPARAM_OUT_VAL(out_mem) != nullptr; return true;
} }
bool BMMeshTrans_PrepareFaceMtlSlot(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_OUT(LibCmo::CKDWORD*, out_mem)) { bool BMMeshTrans_PrepareFaceMtlSlot(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_OUT(LibCmo::CKDWORD*, out_mem)) {
if (!CheckBMMeshTrans(trans)) return false; if (!CheckBMMeshTrans(trans)) return false;
BMPARAM_OUT_ASSIGN(out_mem, trans->PrepareFaceMtlSlot()); BMPARAM_OUT_ASSIGN(out_mem, trans->PrepareFaceMtlSlot());
return BMPARAM_OUT_VAL(out_mem) != nullptr; return true;
} }
bool BMMeshTrans_Parse(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_IN(BMap::BMFile*, bmfile), BMPARAM_IN(LibCmo::CK2::CK_ID, objid)) { bool BMMeshTrans_Parse(BMPARAM_MESHTRANS_DECL(trans), BMPARAM_IN(BMap::BMFile*, bmfile), BMPARAM_IN(LibCmo::CK2::CK_ID, objid)) {
if (!CheckBMMeshTrans(trans)) return false; if (!CheckBMMeshTrans(trans)) return false;

View File

@ -94,17 +94,17 @@ namespace BMap {
LibCmo::CKDWORD* BMMeshTransition::PrepareFaceNormalIndices() { LibCmo::CKDWORD* BMMeshTransition::PrepareFaceNormalIndices() {
if (m_IsParsed || !m_IsFaceOK) return nullptr; if (m_IsParsed || !m_IsFaceOK) return nullptr;
return m_FaceVertexs.data(); return m_FaceNormals.data();
} }
LibCmo::CKDWORD* BMMeshTransition::PrepareFaceUVIndices() { LibCmo::CKDWORD* BMMeshTransition::PrepareFaceUVIndices() {
if (m_IsParsed || !m_IsFaceOK) return nullptr; if (m_IsParsed || !m_IsFaceOK) return nullptr;
return m_FaceVertexs.data(); return m_FaceUVs.data();
} }
LibCmo::CKDWORD* BMMeshTransition::PrepareFaceMtlSlot() { LibCmo::CKDWORD* BMMeshTransition::PrepareFaceMtlSlot() {
if (m_IsParsed || !m_IsFaceOK) return nullptr; if (m_IsParsed || !m_IsFaceOK) return nullptr;
return m_FaceVertexs.data(); return m_FaceMtlSlotIdxs.data();
} }
bool BMMeshTransition::Parse(BMFile* bmfile, LibCmo::CK2::CK_ID mesh_id) { bool BMMeshTransition::Parse(BMFile* bmfile, LibCmo::CK2::CK_ID mesh_id) {
@ -148,8 +148,8 @@ namespace BMap {
// create one first // create one first
TransitionVertex tvec( TransitionVertex tvec(
m_Vertexs[m_FaceVertexs[faceid * 3 + j]], m_Vertexs[m_FaceVertexs[faceid * 3 + j]],
m_Normals[m_FaceVertexs[faceid * 3 + j]], m_Normals[m_FaceNormals[faceid * 3 + j]],
m_UVs[m_FaceVertexs[faceid * 3 + j]] m_UVs[m_FaceUVs[faceid * 3 + j]]
); );
// try insert it // try insert it

View File

@ -1,4 +1,5 @@
# my ban # my ban
.vscode/
*.dll *.dll
*.pdb *.pdb
*.so *.so

View File

@ -115,7 +115,11 @@ def _vxvector2_iterator(pvector: bmap.bm_VxVector2_p, count: int) -> typing.Iter
idx += 2 idx += 2
yield ret yield ret
def _ckfaceindices_assigner(pindices: bmap.bm_CKDWORD_p, count: int, itor: typing.Iterator[virtools_types.CKFaceIndices]) -> None: # bmap.bm_CKWORD_p | bmap.bm_CKDWORD_p is just a type hint
# wo do not need distinguish them in code.
# because the type of pindices is decided by runtime.
def _ckfaceindices_assigner(pindices: bmap.bm_CKWORD_p | bmap.bm_CKDWORD_p, count: int, itor: typing.Iterator[virtools_types.CKFaceIndices]) -> None:
idx: int = 0 idx: int = 0
for _ in range(count): for _ in range(count):
userindices: virtools_types.CKFaceIndices = next(itor) userindices: virtools_types.CKFaceIndices = next(itor)
@ -124,7 +128,7 @@ def _ckfaceindices_assigner(pindices: bmap.bm_CKDWORD_p, count: int, itor: typin
pindices[idx + 2] = userindices.i3 pindices[idx + 2] = userindices.i3
idx += 3 idx += 3
def _ckfaceindices_iterator(pindices: bmap.bm_CKDWORD_p, count: int) -> typing.Iterator[virtools_types.CKFaceIndices]: def _ckfaceindices_iterator(pindices: bmap.bm_CKWORD_p | bmap.bm_CKDWORD_p, count: int) -> typing.Iterator[virtools_types.CKFaceIndices]:
ret: virtools_types.CKFaceIndices = virtools_types.CKFaceIndices() ret: virtools_types.CKFaceIndices = virtools_types.CKFaceIndices()
idx: int = 0 idx: int = 0
for _ in range(count): for _ in range(count):
@ -175,12 +179,12 @@ class BMObject(_AbstractCKObject):
else: else:
return name.value.decode(g_BMapEncoding) return name.value.decode(g_BMapEncoding)
def set_name(self, name: str | None) -> None: def set_name(self, name_: str | None) -> None:
name: bmap.bm_CKSTRING name: bmap.bm_CKSTRING
if name is None: if name_ is None:
name = bmap.bm_CKSTRING(0) name = bmap.bm_CKSTRING(0)
else: else:
name = bmap.bm_CKSTRING(name.encode(g_BMapEncoding)) name = bmap.bm_CKSTRING(name_.encode(g_BMapEncoding))
bmap.BMObject_SetName(self._get_pointer(), self._get_ckid(), name) bmap.BMObject_SetName(self._get_pointer(), self._get_ckid(), name)
class BMTexture(BMObject): class BMTexture(BMObject):
@ -415,8 +419,8 @@ class BMMesh(BMObject):
bmap.BMMesh_GetLitMode(self._get_pointer(), self._get_ckid(), ctypes.byref(mode)) bmap.BMMesh_GetLitMode(self._get_pointer(), self._get_ckid(), ctypes.byref(mode))
return virtools_types.VXMESH_LITMODE(mode.value) return virtools_types.VXMESH_LITMODE(mode.value)
def set_lit_mode(self, mode: virtools_types.VXMESH_LITMODE) -> None: def set_lit_mode(self, mode_: virtools_types.VXMESH_LITMODE) -> None:
mode: bmap.bm_enum = bmap.bm_enum(mode.value) mode: bmap.bm_enum = bmap.bm_enum(mode_.value)
bmap.BMMesh_SetLitMode(self._get_pointer(), self._get_ckid(), mode) bmap.BMMesh_SetLitMode(self._get_pointer(), self._get_ckid(), mode)
def get_vertex_count(self) -> int: def get_vertex_count(self) -> int:
@ -535,9 +539,9 @@ class BM3dObject(BMObject):
ret.from_const(tuple(flat[i] for i in range(16))) ret.from_const(tuple(flat[i] for i in range(16)))
return ret return ret
def set_world_matrix(self, mat: virtools_types.VxMatrix) -> None: def set_world_matrix(self, mat_: virtools_types.VxMatrix) -> None:
# star syntax expand the tuple as the argument. # star syntax expand the tuple as the argument.
mat: bmap.bm_VxMatrix = bmap.bm_VxMatrix(*(mat.to_const())) mat: bmap.bm_VxMatrix = bmap.bm_VxMatrix(*(mat_.to_const()))
bmap.BM3dObject_SetWorldMatrix(self._get_pointer(), self._get_ckid(), mat) bmap.BM3dObject_SetWorldMatrix(self._get_pointer(), self._get_ckid(), mat)
def get_current_mesh(self) -> BMMesh | None: def get_current_mesh(self) -> BMMesh | None:
@ -819,7 +823,7 @@ class BMMeshTrans(_AbstractPointer):
idx: int = 0 idx: int = 0
for _ in range(count): for _ in range(count):
usermtl: BMMaterial = next(itor) usermtl: BMMaterial | None = next(itor)
if usermtl is None: if usermtl is None:
raw_ckid[idx] = g_InvalidCKID raw_ckid[idx] = g_InvalidCKID
else: else:

View File

@ -22,8 +22,8 @@ namespace LibCmo::CK2::ObjImpls {
m_LineIndices(), m_LineIndices(),
// init flags // init flags
m_Flags(EnumsHelper::Merge({ m_Flags(EnumsHelper::Merge({
VxMath::VXMESH_FLAGS::VXMESH_FORCETRANSPARENCY, VxMath::VXMESH_FLAGS::VXMESH_VISIBLE,
VxMath::VXMESH_FLAGS::VXMESH_HASTRANSPARENCY VxMath::VXMESH_FLAGS::VXMESH_RENDERCHANNELS
})) { })) {
// set visible in default // set visible in default
EnumsHelper::Add(m_ObjectFlags, CK_OBJECT_FLAGS::CK_OBJECT_VISIBLE); EnumsHelper::Add(m_ObjectFlags, CK_OBJECT_FLAGS::CK_OBJECT_VISIBLE);
@ -53,14 +53,25 @@ namespace LibCmo::CK2::ObjImpls {
} }
// write material slots // write material slots
if (GetMaterialSlotCount() != 0) { // MARK: due to virtools shit implement, we must make sure there is at least one material channel existed.
// so if the material slot is empty, we write a mullptr slot for it.
{
chunk->WriteIdentifier(CK_STATESAVEFLAGS_MESH::CK_STATESAVE_MESHMATERIALS); chunk->WriteIdentifier(CK_STATESAVEFLAGS_MESH::CK_STATESAVE_MESHMATERIALS);
chunk->WriteStruct(GetMaterialSlotCount());
for (auto& mtlSlot : m_MaterialSlot) { if (GetMaterialSlotCount() != 0) {
// write object id // write real slots
chunk->WriteObjectPointer(mtlSlot); chunk->WriteStruct(GetMaterialSlotCount());
// MARK: write a zero? idk what the fuck it is. for (auto& mtlSlot : m_MaterialSlot) {
// write object id
chunk->WriteObjectPointer(mtlSlot);
// MARK: write a zero? idk what the fuck it is.
chunk->WriteStruct(static_cast<CKDWORD>(0));
}
} else {
// write fake one like real one
chunk->WriteStruct(static_cast<CKDWORD>(1));
// write id and blank
chunk->WriteObjectPointer(nullptr);
chunk->WriteStruct(static_cast<CKDWORD>(0)); chunk->WriteStruct(static_cast<CKDWORD>(0));
} }
} }