SWBF2-Classic-Msh-Viewer/MshViewer/Source/Object.cpp

100 lines
2.4 KiB
C++
Raw Normal View History

#include "Object.h"
2016-09-12 14:49:05 +00:00
#include <iostream>
/////////////////////////////////////////////////////////////////////////
// public constructor/destructor
2016-09-12 14:49:05 +00:00
Object::Object(const char* path)
{
2016-09-12 14:49:05 +00:00
// open file
fsMesh.open(path, std::ios::in | std::ios::binary);
2016-09-12 14:49:05 +00:00
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<char*>(&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<char*>(&tempSize), sizeof(tempSize));
// get all sub chunks from MSH2
loadChunks(lChunkMsh2, fsMesh.tellg(), tempSize);
// search for all MODL Chunks
for (std::list<chunkHeader*>::iterator it = lChunkMsh2.begin(); it != lChunkMsh2.end(); it++)
{
if (!strcmp("MODL", (*it)->name))
{
std::list<chunkHeader*>* tempModlList = new std::list<chunkHeader*>;
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<chunkHeader*>& destination, std::streampos start, const std::uint32_t end)
{
// jump to first chunk
fsMesh.seekg(start);
2016-09-12 14:49:05 +00:00
do
{
chunkHeader* tempHeader = new chunkHeader();
fsMesh.read(reinterpret_cast<char*>(&tempHeader->name[0]), sizeof(tempHeader->name) - 1);
fsMesh.read(reinterpret_cast<char*>(&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)
2016-09-12 14:49:05 +00:00
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;
2016-09-12 14:49:05 +00:00
}
2016-09-12 14:49:05 +00:00
/////////////////////////////////////////////////////////////////////////
// public getter
/////////////////////////////////////////////////////////////////////////
// public functions