diff --git a/LibCmo/CK2/CKFile.hpp b/LibCmo/CK2/CKFile.hpp index 43ed04c..6821a60 100644 --- a/LibCmo/CK2/CKFile.hpp +++ b/LibCmo/CK2/CKFile.hpp @@ -240,7 +240,27 @@ namespace LibCmo::CK2 { CKERROR Save(CKSTRING u8_filename); protected: - bool m_Done; + /** + * @brief A helper function to check whether given file can be written. + * @param filename[in] The name of file to be wriiten + * @return CKERROR::CK_OK if can write. + */ + CKERROR PrepareFile(CKSTRING filename); + /** + * @brief Internal used Object Adder. + * + * This function is used by AddSavedObject() and Copy from reader constructor. + * This function accept a object pointer, and try adding them. + * And set m_IncludedFiles and m_ObjectsHashTable properly. + * + * @param obj The pointer to added object. + * @param flags The flag used when saving this object. + * @return True if success. + */ + bool InternalObjectAdder(ObjImpls::CKObject* obj, CKDWORD flags = CK_STATESAVE_ALL); + + protected: + bool m_Done; /**< True if this writer is already written into file. A written CKFileWriter can no be written again. */ /** * @brief True if this writer is not allowed to add objects. * @remark @@ -269,8 +289,6 @@ namespace LibCmo::CK2 { XContainer::XBitArray m_AlreadySavedMask; /**< Field recording saved object id. If this object is saved, set m_AlreadySavedMask[id] to true. Also used to check whether object already is in save list. */ CKFileInfo m_FileInfo; /**< Headers summary */ - CKERROR PrepareFile(CKSTRING filename); - CKContext* m_Ctx; CKFileVisitor m_Visitor; }; diff --git a/LibCmo/CK2/CKFileOthers.cpp b/LibCmo/CK2/CKFileOthers.cpp index edd1d15..7b1380a 100644 --- a/LibCmo/CK2/CKFileOthers.cpp +++ b/LibCmo/CK2/CKFileOthers.cpp @@ -244,35 +244,17 @@ namespace LibCmo::CK2 { #pragma region Deep Assign - // copy object and calc max id - CK_ID maxid = 0; + // call internal object adder one by one for (const auto& item : reader->GetFileObjects()) { CKFileObject obj; // skip if invalid if (item.ObjPtr == nullptr) continue; - // set obj ptr - obj.ObjPtr = item.ObjPtr; - - // set other data - obj.ObjectId = obj.ObjPtr->GetID(); - obj.ObjectCid = obj.ObjPtr->GetClassID(); - obj.Data = nullptr; // blank statechunk - obj.SaveFlags = CK_STATESAVE_ALL; - XContainer::NSXString::FromCKSTRING(obj.Name, obj.ObjPtr->GetName()); - - // update max id - maxid = std::max(maxid, obj.ObjectId); - - // insert - m_FileObjects.emplace_back(std::move(obj)); - + // try add + InternalObjectAdder(item.ObjPtr); } - // set max id - m_SaveIDMax = maxid; - #pragma endregion } @@ -280,8 +262,7 @@ namespace LibCmo::CK2 { CKFileWriter::~CKFileWriter() {} - bool CKFileWriter::AddSavedObject(ObjImpls::CKObject* obj, CKDWORD flags) { - if (m_Done || m_DisableAddingObject) return false; + bool LibCmo::CK2::CKFileWriter::InternalObjectAdder(ObjImpls::CKObject * obj, CKDWORD flags) { if (obj == nullptr) return false; // check whether is saved. @@ -306,11 +287,19 @@ namespace LibCmo::CK2 { return true; } + bool CKFileWriter::AddSavedObject(ObjImpls::CKObject* obj, CKDWORD flags) { + if (m_Done || m_DisableAddingObject) return false; + + // call internal adder + return InternalObjectAdder(obj, flags); + } + bool CKFileWriter::AddSavedObjects(const XContainer::XObjectPointerArray& objarray, CKDWORD flags) { if (m_Done || m_DisableAddingObject) return false; bool ret = true; for (auto obj : objarray) { + // call AddSavedObject one by one if (!AddSavedObject(obj, flags)) { ret = false; } diff --git a/LibCmo/CK2/ObjImpls/CKMesh.cpp b/LibCmo/CK2/ObjImpls/CKMesh.cpp index d3762c5..8c49e02 100644 --- a/LibCmo/CK2/ObjImpls/CKMesh.cpp +++ b/LibCmo/CK2/ObjImpls/CKMesh.cpp @@ -130,7 +130,7 @@ namespace LibCmo::CK2::ObjImpls { CKBYTE* rawbuf = static_cast(buf.get()); // reserve length data - CKDWORD* reservedBufSize = reinterpret_cast(rawbuf); + CKDWORD* reservedBufDwordSize = reinterpret_cast(rawbuf); rawbuf += CKSizeof(CKDWORD); // write vertex position @@ -188,7 +188,7 @@ namespace LibCmo::CK2::ObjImpls { CKDWORD realConsumedSize = rawbuf - static_cast(buf.get()); // assign to reserved length field // length also include length indicator it self - *reservedBufSize = realConsumedSize; + *reservedBufDwordSize = realConsumedSize / CKSizeof(CKDWORD); // notify buffer real consumed size buf.get_deleter().SetConsumedSize(realConsumedSize); diff --git a/Unvirt/StructFormatter.cpp b/Unvirt/StructFormatter.cpp index a6e07ec..ca7dc61 100644 --- a/Unvirt/StructFormatter.cpp +++ b/Unvirt/StructFormatter.cpp @@ -324,8 +324,6 @@ namespace Unvirt::StructFormatter { fprintf(stdout, "\t0x%" PRIxCKDWORD " bytes\tColors\n", obj->GetVertexCount() * CKSizeof(LibCmo::CKDWORD)); PrintPointer(obj->GetVertexSpecularColors()); fprintf(stdout, "\t0x%" PRIxCKDWORD " bytes\tSpecularColors\n", obj->GetVertexCount() * CKSizeof(LibCmo::CKDWORD)); - PrintPointer(obj->GetVertexWeights()); - fprintf(stdout, "\t0x%" PRIxCKDWORD " bytes\tWeights\n", obj->GetVertexCount() * CKSizeof(LibCmo::CKFLOAT)); // face data fputs("== Face ==\n", stdout);