From 03553616e74bb84e65cde0dabee8ee53b5aef082 Mon Sep 17 00:00:00 2001 From: Anakin Date: Fri, 21 Oct 2016 19:24:30 +0200 Subject: [PATCH] now the tool finds the msh2 chunk even if it is not the 2nd behind HEDR, hence restructured code, --- MshViewer/Header/Object.h | 3 +- MshViewer/Source/Object.cpp | 95 ++++++++++++++++++++++++++----------- 2 files changed, 68 insertions(+), 30 deletions(-) diff --git a/MshViewer/Header/Object.h b/MshViewer/Header/Object.h index a81c3e1..a584791 100644 --- a/MshViewer/Header/Object.h +++ b/MshViewer/Header/Object.h @@ -52,14 +52,15 @@ public: private: - std::list lChunkMsh2; std::list lModls; std::fstream fsMesh; + std::list lTextures; private: void setModlDefault(Modl* model); void loadChunks(std::list &destination, std::streampos start, const std::uint32_t end); + void analyseMsh2Chunks(std::list &chunkList); void analyseModlChunks(Modl* dataDestination, std::list &chunkList); void analyseGeomChunks(Modl* dataDestination, std::list &chunkList); void analyseSegmChunks(Modl* dataDestination, std::list &chunkList); diff --git a/MshViewer/Source/Object.cpp b/MshViewer/Source/Object.cpp index 296d416..bf0c5b8 100644 --- a/MshViewer/Source/Object.cpp +++ b/MshViewer/Source/Object.cpp @@ -14,48 +14,47 @@ 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); + // jump to file size information + fsMesh.seekg(4); - if (strcmp(tempChunkName, "MSH2")) - throw std::invalid_argument(std::string("corrupted file MSH2 expected instead of ") += tempChunkName); + std::uint32_t tempFileSize; + std::list tempMainChunks; - std::uint32_t tempSize; - fsMesh.read(reinterpret_cast(&tempSize), sizeof(tempSize)); + // get all chunks under HEDR + fsMesh.read(reinterpret_cast(&tempFileSize), sizeof(tempFileSize)); + loadChunks(tempMainChunks, fsMesh.tellg(), tempFileSize); - // 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++) + // evaluate sub chunks (= find MSH2) + for (std::list::iterator it = tempMainChunks.begin(); it != tempMainChunks.end(); it++) { - if (!strcmp("MODL", (*it)->name)) + if (!strcmp("MSH2", (*it)->name)) { - Modl* tempModl = new Modl; - setModlDefault(tempModl); - // get all subchunks - std::list tempChunks; - loadChunks(tempChunks, (*it)->position, (*it)->size); - - // evaluate MODL subchunks - analyseModlChunks(tempModl, tempChunks); + std::list tempMsh2Chunks; + loadChunks(tempMsh2Chunks, (*it)->position, (*it)->size); - //clean up - while (!tempChunks.empty()) + // evaluate MSH2 subchunks + analyseMsh2Chunks(tempMsh2Chunks); + + // clean up + while (!tempMsh2Chunks.empty()) { - ChunkHeader* tempCursor = tempChunks.front(); - tempChunks.pop_front(); + ChunkHeader* tempCursor = tempMsh2Chunks.front(); + tempMsh2Chunks.pop_front(); delete tempCursor; } - - // save Model data - lModls.push_back(tempModl); + continue; } } + // clean up + while (!tempMainChunks.empty()) + { + ChunkHeader* tempCursor = tempMainChunks.front(); + tempMainChunks.pop_front(); + delete tempCursor; + } + // close file fsMesh.close(); } @@ -132,6 +131,44 @@ void Object::loadChunks(std::list& destination, std::streampos sta } +void Object::analyseMsh2Chunks(std::list& chunkList) +{ + for (std::list::iterator it = chunkList.begin(); it != chunkList.end(); it++) + { + if (!strcmp("MATL", (*it)->name)) + { + + continue; + } + + if (!strcmp("MODL", (*it)->name)) + { + Modl* tempModl = new Modl; + setModlDefault(tempModl); + + // get all subchunks + std::list tempChunks; + loadChunks(tempChunks, (*it)->position, (*it)->size); + + // evaluate MODL subchunks + analyseModlChunks(tempModl, tempChunks); + + //clean up + while (!tempChunks.empty()) + { + ChunkHeader* tempCursor = tempChunks.front(); + tempChunks.pop_front(); + delete tempCursor; + } + + // save Model data + lModls.push_back(tempModl); + + continue; + } + } +} + void Object::analyseModlChunks(Modl* dataDestination, std::list& chunkList) { for (std::list::iterator it = chunkList.begin(); it != chunkList.end(); it++)