diff --git a/BMap/BMExports.cpp b/BMap/BMExports.cpp index 347e04f..c0e181b 100644 --- a/BMap/BMExports.cpp +++ b/BMap/BMExports.cpp @@ -22,3 +22,42 @@ void BMDispose() { // unregister iron pad IronPad::IronPadUnregister(); } + +BMap::BMFile* BMFile_Load(LibCmo::CKSTRING file_name, LibCmo::CKSTRING temp_folder, LibCmo::CKSTRING texture_folder, LibCmo::CKDWORD encoding_count, LibCmo::CKSTRING encodings[]) { + auto file = new BMap::BMFile(temp_folder, texture_folder, encoding_count, encodings, false); + if (file->IsFailed()) { + delete file; + file = nullptr; + } + if (!file->Load(file_name)) { + delete file; + file = nullptr; + } + + g_AllBMFiles.emplace(file); + return file; +} + +BMap::BMFile* BMFile_Create(LibCmo::CKSTRING temp_folder, LibCmo::CKSTRING texture_folder, LibCmo::CKDWORD encoding_count, LibCmo::CKSTRING encodings[]) { + auto file = new BMap::BMFile(temp_folder, texture_folder, encoding_count, encodings, false); + if (file->IsFailed()) { + delete file; + file = nullptr; + } + + g_AllBMFiles.emplace(file); + return file; +} + +bool BMFile_Save(BMap::BMFile* map_file, LibCmo::CKSTRING file_name, LibCmo::CKINT compreess_level) { + if (!g_AllBMFiles.contains(map_file)) return false; + return map_file->Save(file_name, compreess_level); +} + +void BMFile_Free(BMap::BMFile* map_file) { + // only free correct pointer + if (map_file == nullptr) return; + if (g_AllBMFiles.erase(map_file) != 0) { + delete map_file; + } +} diff --git a/BMap/BMExports.hpp b/BMap/BMExports.hpp index 0665e47..3593795 100644 --- a/BMap/BMExports.hpp +++ b/BMap/BMExports.hpp @@ -11,10 +11,10 @@ LIBCMO_EXPORT void BMDispose(); #pragma region BMFile -//LIBCMO_EXPORT BMap::BMFile* BMFile_Load(const char* file_name, const char* temp_folder, const char* texture_folder, const char* encoding); -//LIBCMO_EXPORT BMap::BMFile* BMFile_Create(); -//LIBCMO_EXPORT bool BMFile_Save(BMap::BMFile* map_file, const char* file_name, int compreess_level); -//LIBCMO_EXPORT void BMFile_Free(BMap::BMFile* map_file); +LIBCMO_EXPORT BMap::BMFile* BMFile_Load(LibCmo::CKSTRING file_name, LibCmo::CKSTRING temp_folder, LibCmo::CKSTRING texture_folder, LibCmo::CKDWORD encoding_count, LibCmo::CKSTRING encodings[]); +LIBCMO_EXPORT BMap::BMFile* BMFile_Create(LibCmo::CKSTRING temp_folder, LibCmo::CKSTRING texture_folder, LibCmo::CKDWORD encoding_count, LibCmo::CKSTRING encodings[]); +LIBCMO_EXPORT bool BMFile_Save(BMap::BMFile* map_file, LibCmo::CKSTRING file_name, LibCmo::CKINT compreess_level); +LIBCMO_EXPORT void BMFile_Free(BMap::BMFile* map_file); #pragma endregion diff --git a/BMap/BMap.cpp b/BMap/BMap.cpp index 3cc861d..47c46f8 100644 --- a/BMap/BMap.cpp +++ b/BMap/BMap.cpp @@ -109,7 +109,7 @@ namespace BMap { // do parse DoRealParse(); - + // check vertex overflow if (m_ProcVertexs.size() > std::numeric_limits::max()) { return false; @@ -216,11 +216,29 @@ namespace BMap { #pragma region BMfile - BMFile::BMFile(LibCmo::CKSTRING temp_folder, LibCmo::CKSTRING texture_folder, LibCmo::CKDWORD encoding_count, LibCmo::CKSTRING encodings[], bool is_reader) { - + BMFile::BMFile(LibCmo::CKSTRING temp_folder, LibCmo::CKSTRING texture_folder, LibCmo::CKDWORD encoding_count, LibCmo::CKSTRING encodings[], bool is_reader) : + m_IsReader(is_reader), m_IsFailed(false) { + m_Context = new LibCmo::CK2::CKContext(); + // set temp folder and texture folder + auto pm = m_Context->GetPathManager(); + m_IsFailed = m_IsFailed || !pm->AddPath(texture_folder); + m_IsFailed = m_IsFailed || !pm->SetTempFolder(temp_folder); + // set encoding + LibCmo::XContainer::XArray cache; + for (LibCmo::CKDWORD i = 0; i < encoding_count; ++i) { + if (encodings[i] != nullptr) + cache.emplace_back(encodings[i]); + } + m_Context->SetEncoding(cache); } - BMFile::~BMFile() {} + BMFile::~BMFile() { + delete m_Context; + } + + bool BMFile::IsFailed() { + return m_IsFailed; + } bool BMFile::Load(LibCmo::CKSTRING filename) { return false; @@ -247,12 +265,12 @@ LibCmo::CK2::ObjImpls::CK ## namepart * BMFile::Create ## namepart (LibCmo::CKST if (obj != nullptr) m_Obj ## namepart ## s.emplace_back(obj); \ return obj; \ } - + VISITOR_IMPL(Group, GROUP) - VISITOR_IMPL(3dObject, 3DOBJECT) - VISITOR_IMPL(Mesh, MESH) - VISITOR_IMPL(Material, MATERIAL) - VISITOR_IMPL(Texture, TEXTURE) + VISITOR_IMPL(3dObject, 3DOBJECT) + VISITOR_IMPL(Mesh, MESH) + VISITOR_IMPL(Material, MATERIAL) + VISITOR_IMPL(Texture, TEXTURE) #undef VISITOR_IMPL diff --git a/BMap/BMap.hpp b/BMap/BMap.hpp index 8698f36..00fb988 100644 --- a/BMap/BMap.hpp +++ b/BMap/BMap.hpp @@ -30,6 +30,7 @@ namespace BMap { public: BMMeshTransition(); ~BMMeshTransition(); + LIBCMO_DISABLE_COPY_MOVE(BMMeshTransition); void PrepareVertexCount(LibCmo::CKDWORD count); LibCmo::VxMath::VxVector3* PrepareVertex(); @@ -75,7 +76,9 @@ namespace BMap { public: BMFile(LibCmo::CKSTRING temp_folder, LibCmo::CKSTRING texture_folder, LibCmo::CKDWORD encoding_count, LibCmo::CKSTRING encodings[], bool is_reader); ~BMFile(); + LIBCMO_DISABLE_COPY_MOVE(BMFile); + bool IsFailed(); bool Load(LibCmo::CKSTRING filename); bool Save(LibCmo::CKSTRING filename, LibCmo::CKINT compress_level); @@ -93,6 +96,7 @@ LibCmo::CK2::ObjImpls::CK ## namepart * Create ## namepart (LibCmo::CKSTRING nam #undef VISITOR_DECL private: + bool m_IsFailed; bool m_IsReader; LibCmo::CK2::CKContext* m_Context; diff --git a/LibCmo/CK2/CKContext.cpp b/LibCmo/CK2/CKContext.cpp index aea1ed6..915b9bd 100644 --- a/LibCmo/CK2/CKContext.cpp +++ b/LibCmo/CK2/CKContext.cpp @@ -309,7 +309,7 @@ namespace LibCmo::CK2 { } } - void CKContext::SetEncoding(const XContainer::XArray encoding_series) { + void CKContext::SetEncoding(const XContainer::XArray& encoding_series) { // free all current series for (const auto& encoding : this->m_NameEncoding) { LibCmo::EncodingHelper::DestroyEncodingToken(encoding); diff --git a/LibCmo/CK2/CKContext.hpp b/LibCmo/CK2/CKContext.hpp index 67e7eac..12a0f6b 100644 --- a/LibCmo/CK2/CKContext.hpp +++ b/LibCmo/CK2/CKContext.hpp @@ -102,7 +102,7 @@ namespace LibCmo::CK2 { public: void GetUtf8String(const XContainer::XString& native_name, XContainer::XString& u8_name); void GetNativeString(const XContainer::XString& u8_name, XContainer::XString& native_name); - void SetEncoding(const XContainer::XArray encoding_series); + void SetEncoding(const XContainer::XArray& encoding_series); protected: XContainer::XArray m_NameEncoding; diff --git a/LibCmo/CK2/MgrImpls/CKPathManager.cpp b/LibCmo/CK2/MgrImpls/CKPathManager.cpp index 31bfee9..bef6e4a 100644 --- a/LibCmo/CK2/MgrImpls/CKPathManager.cpp +++ b/LibCmo/CK2/MgrImpls/CKPathManager.cpp @@ -22,6 +22,8 @@ namespace LibCmo::CK2::MgrImpls { CKPathManager::~CKPathManager() {} bool CKPathManager::SetTempFolder(CKSTRING u8_temp) { + if (u8_temp == nullptr) return false; + std::filesystem::path cache; EncodingHelper::U8PathToStdPath(cache, u8_temp); if (std::filesystem::is_directory(cache)) { @@ -39,6 +41,8 @@ namespace LibCmo::CK2::MgrImpls { } XContainer::XString CKPathManager::GetTempFilePath(CKSTRING u8_filename) { + if (u8_filename == nullptr) return XContainer::XString(); + std::filesystem::path stdfilename; EncodingHelper::U8PathToStdPath(stdfilename, u8_filename); auto realfile = this->m_TempFolder / stdfilename; @@ -50,6 +54,7 @@ namespace LibCmo::CK2::MgrImpls { bool CKPathManager::AddPath(CKSTRING u8path) { if (u8path == nullptr) return false; + std::filesystem::path newpath; EncodingHelper::U8PathToStdPath(newpath, u8path); if (std::filesystem::is_directory(newpath)) {