From 59446f088f79d0ab885f39eb8b69e0b3df18307d Mon Sep 17 00:00:00 2001 From: Anakin Date: Mon, 10 Oct 2016 12:34:01 +0200 Subject: [PATCH] now all MODL chunks from MSH2 are saved, searching only subchunks and not all behind, finding corrupted size information and trying to continue --- MshViewer/Header/Object.h | 7 ++++--- MshViewer/Source/Object.cpp | 30 +++++++++++++++++++++++------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/MshViewer/Header/Object.h b/MshViewer/Header/Object.h index c513892..2b3a2c3 100644 --- a/MshViewer/Header/Object.h +++ b/MshViewer/Header/Object.h @@ -2,10 +2,11 @@ #include #include #include +//#include struct chunkHeader { char name[5]; - int size; + std::uint32_t size; std::streampos position; }; @@ -18,12 +19,12 @@ public: private: std::list lChunkMsh2; - std::list lChunkModl; + std::list*> lChunkModls; std::fstream fsMesh; private: - void loadChunks(std::list &destination, std::streampos start, const char end[5]); + void loadChunks(std::list &destination, std::streampos start, const std::uint32_t end); public: diff --git a/MshViewer/Source/Object.cpp b/MshViewer/Source/Object.cpp index 270940a..216d9fe 100644 --- a/MshViewer/Source/Object.cpp +++ b/MshViewer/Source/Object.cpp @@ -14,6 +14,7 @@ Object::Object(const char* path) if (!fsMesh.is_open()) throw std::invalid_argument(std::string("file not found: ") += path); + // jump into msh2 todo: search for MSH2 if there is a shadowvolume fsMesh.seekg(8); char tempChunkName[5] = { 0 }; fsMesh.read(reinterpret_cast(&tempChunkName[0]), sizeof(tempChunkName) - 1); @@ -21,15 +22,20 @@ Object::Object(const char* path) if (strcmp(tempChunkName, "MSH2")) throw std::invalid_argument(std::string("corrupted file MSH2 expected instead of ") += tempChunkName); - fsMesh.seekg(4, std::ios_base::cur); + std::uint32_t tempSize; + fsMesh.read(reinterpret_cast(&tempSize), sizeof(tempSize)); - loadChunks(lChunkMsh2, fsMesh.tellg(), "CL1L"); + // get all sub chunks from MSH2 + loadChunks(lChunkMsh2, fsMesh.tellg(), tempSize); + // search for all MODL Chunks for (std::list::iterator it = lChunkMsh2.begin(); it != lChunkMsh2.end(); it++) { if (!strcmp("MODL", (*it)->name)) { - loadChunks(lChunkModl, (*it)->position, "CL1L"); + std::list* tempModlList = new std::list; + loadChunks(*tempModlList, (*it)->position, (*it)->size); + lChunkModls.push_back(tempModlList); } } @@ -47,9 +53,9 @@ Object::~Object() ///////////////////////////////////////////////////////////////////////// // private functions -void Object::loadChunks(std::list& destination, std::streampos start, const char end[5]) +void Object::loadChunks(std::list& destination, std::streampos start, const std::uint32_t end) { - // Jump into Mesh2 + // jump to first chunk fsMesh.seekg(start); do @@ -64,9 +70,19 @@ void Object::loadChunks(std::list& destination, std::streampos sta fsMesh.seekg(tempHeader->size, std::ios_base::cur); - if (!std::strcmp(tempHeader->name, end)) + // reached end + if (fsMesh.tellg() - start == end) break; - } while (fsMesh.good()); + + // error. Maybe the size information is corrupted + if (!fsMesh.good()) + { + std::cout << "WARNING: corrupted file. Trying to continue" << std::endl; + fsMesh.clear(); + break; + } + + } while (true); std::cout << "got all chunks, totaly found: " << destination.size() << std::endl;