#include "Object.h" #include ///////////////////////////////////////////////////////////////////////// // public constructor/destructor Object::Object(const char* path) { // open file fsMesh.open(path, std::ios::in | std::ios::binary); 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); if (strcmp(tempChunkName, "MSH2")) throw std::invalid_argument(std::string("corrupted file MSH2 expected instead of ") += tempChunkName); std::uint32_t tempSize; fsMesh.read(reinterpret_cast(&tempSize), sizeof(tempSize)); // 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)) { std::list* tempModlList = new std::list; loadChunks(*tempModlList, (*it)->position, (*it)->size); lChunkModls.push_back(tempModlList); } } // close file fsMesh.close(); } Object::~Object() { //delete Chunk list; } ///////////////////////////////////////////////////////////////////////// // private functions void Object::loadChunks(std::list& destination, std::streampos start, const std::uint32_t end) { // jump to first chunk fsMesh.seekg(start); do { chunkHeader* tempHeader = new chunkHeader(); fsMesh.read(reinterpret_cast(&tempHeader->name[0]), sizeof(tempHeader->name) - 1); fsMesh.read(reinterpret_cast(&tempHeader->size), sizeof(tempHeader->size)); tempHeader->position = fsMesh.tellg(); destination.push_back(tempHeader); fsMesh.seekg(tempHeader->size, std::ios_base::cur); // reached end if (fsMesh.tellg() - start == end) break; // 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; } ///////////////////////////////////////////////////////////////////////// // public getter ///////////////////////////////////////////////////////////////////////// // public functions