write garbage

- fix PathManager nullptr assign issue
- fix Context set encoding unexpected copy issue
- write some export function for BMap.
This commit is contained in:
yyc12345 2023-09-23 16:25:26 +08:00
parent 94dadbfb1f
commit a17b9deb4c
7 changed files with 81 additions and 15 deletions

View File

@ -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;
}
}

View File

@ -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

View File

@ -109,7 +109,7 @@ namespace BMap {
// do parse
DoRealParse();
// check vertex overflow
if (m_ProcVertexs.size() > std::numeric_limits<LibCmo::CKWORD>::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<LibCmo::XContainer::XString> 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

View File

@ -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;

View File

@ -309,7 +309,7 @@ namespace LibCmo::CK2 {
}
}
void CKContext::SetEncoding(const XContainer::XArray<XContainer::XString> encoding_series) {
void CKContext::SetEncoding(const XContainer::XArray<XContainer::XString>& encoding_series) {
// free all current series
for (const auto& encoding : this->m_NameEncoding) {
LibCmo::EncodingHelper::DestroyEncodingToken(encoding);

View File

@ -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<XContainer::XString> encoding_series);
void SetEncoding(const XContainer::XArray<XContainer::XString>& encoding_series);
protected:
XContainer::XArray<EncodingHelper::ENCODING_TOKEN> m_NameEncoding;

View File

@ -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)) {