2014-01-24 09:00:59 +01:00
|
|
|
|
#include "LevelParser.h"
|
|
|
|
|
|
2014-01-27 10:14:02 +01:00
|
|
|
|
#include "Loader.h"
|
2014-01-27 13:57:18 +01:00
|
|
|
|
#include "ParseFunctions.h"
|
2014-01-27 10:14:02 +01:00
|
|
|
|
|
2014-01-24 10:01:58 +01:00
|
|
|
|
using namespace GameLogic;
|
2014-01-24 10:42:19 +01:00
|
|
|
|
using namespace ::LevelFileLoader;
|
2014-01-24 10:01:58 +01:00
|
|
|
|
|
|
|
|
|
LevelParser::LevelParser()
|
|
|
|
|
{
|
2014-01-28 11:27:29 +01:00
|
|
|
|
formatVersion.formatVersionMajor = 1;
|
|
|
|
|
formatVersion.formatVersionMinor = 0;
|
2014-01-24 10:01:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LevelParser::~LevelParser()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<ObjectTypeHeader> LevelParser::Parse(std::string filename)
|
|
|
|
|
{
|
2014-01-28 11:27:29 +01:00
|
|
|
|
int bufferSize = 0;
|
2014-01-28 16:15:10 +01:00
|
|
|
|
int counter = 0;
|
2014-01-28 11:27:29 +01:00
|
|
|
|
|
|
|
|
|
std::vector<ObjectTypeHeader> objects;
|
|
|
|
|
|
2014-01-24 10:01:58 +01:00
|
|
|
|
//Read entire level file.
|
2014-01-27 10:14:02 +01:00
|
|
|
|
Loader loader;
|
2014-01-28 16:15:10 +01:00
|
|
|
|
char* buffer = (char*)loader.LoadFile(filename.c_str(), bufferSize);
|
2014-01-24 10:01:58 +01:00
|
|
|
|
|
2014-01-28 11:27:29 +01:00
|
|
|
|
//Read format version
|
|
|
|
|
FormatVersion levelFormatVersion;
|
2014-01-28 16:15:10 +01:00
|
|
|
|
//ParseObject(&buffer[counter], &levelFormatVersion, sizeof(formatVersion));
|
2014-01-28 11:27:29 +01:00
|
|
|
|
if(this->formatVersion != levelFormatVersion)
|
|
|
|
|
{
|
|
|
|
|
//Do something if it's not the same version
|
|
|
|
|
}
|
2014-01-24 10:01:58 +01:00
|
|
|
|
|
2014-01-28 11:27:29 +01:00
|
|
|
|
while(counter < bufferSize)
|
2014-01-24 10:01:58 +01:00
|
|
|
|
{
|
|
|
|
|
//Get typeID
|
2014-01-27 13:57:18 +01:00
|
|
|
|
ObjectTypeHeader typeID;
|
2014-01-28 16:15:10 +01:00
|
|
|
|
ParseObject(&buffer[counter], &typeID, sizeof(typeID));
|
2014-01-27 13:57:18 +01:00
|
|
|
|
switch((int)typeID.typeID)
|
2014-01-24 10:01:58 +01:00
|
|
|
|
{
|
2014-01-28 11:27:29 +01:00
|
|
|
|
case ObjectType_LevelMetaData:
|
2014-01-28 16:15:10 +01:00
|
|
|
|
{
|
2014-01-28 11:27:29 +01:00
|
|
|
|
LevelMetaData header;
|
2014-01-28 16:15:10 +01:00
|
|
|
|
ParseLevelMetaData(&buffer[counter], header, counter);
|
2014-01-28 11:27:29 +01:00
|
|
|
|
objects.push_back(header);
|
2014-01-24 10:01:58 +01:00
|
|
|
|
break;
|
2014-01-28 16:15:10 +01:00
|
|
|
|
}
|
2014-01-24 10:01:58 +01:00
|
|
|
|
|
2014-01-28 11:27:29 +01:00
|
|
|
|
case ObjectType_Dynamic:
|
2014-01-28 16:15:10 +01:00
|
|
|
|
{
|
2014-01-28 11:27:29 +01:00
|
|
|
|
ObjectHeader header;
|
2014-01-28 16:15:10 +01:00
|
|
|
|
ParseObject(&buffer[counter], &header, sizeof(header));
|
2014-01-28 11:27:29 +01:00
|
|
|
|
objects.push_back(header);
|
|
|
|
|
counter += sizeof(header);
|
2014-01-28 16:15:10 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-24 10:01:58 +01:00
|
|
|
|
default:
|
|
|
|
|
//Couldn't find typeID. FAIL!!!!!!
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return objects;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 11:27:29 +01:00
|
|
|
|
//f<>r meta information om leveln.
|
|
|
|
|
LevelMetaData LevelParser::ParseHeader(std::string filename)
|
2014-01-24 10:01:58 +01:00
|
|
|
|
{
|
2014-01-28 11:27:29 +01:00
|
|
|
|
int bufferSize = 0;
|
2014-01-28 16:15:10 +01:00
|
|
|
|
int counter = 0;
|
2014-01-28 11:27:29 +01:00
|
|
|
|
|
|
|
|
|
LevelMetaData levelHeader;
|
|
|
|
|
levelHeader.typeID = ObjectType::ObjectType_Unknown;
|
|
|
|
|
|
2014-01-24 10:01:58 +01:00
|
|
|
|
//Read entire level file.
|
2014-01-27 10:14:02 +01:00
|
|
|
|
Loader loader;
|
2014-01-28 16:15:10 +01:00
|
|
|
|
char* buffer = (char*)loader.LoadFile(filename.c_str(), bufferSize);
|
2014-01-24 10:01:58 +01:00
|
|
|
|
|
2014-01-28 11:27:29 +01:00
|
|
|
|
//Read format version
|
|
|
|
|
FormatVersion levelFormatVersion;
|
2014-01-28 16:15:10 +01:00
|
|
|
|
//ParseObject(&buffer[counter], &levelFormatVersion, sizeof(formatVersion));
|
2014-01-28 11:27:29 +01:00
|
|
|
|
if(this->formatVersion != levelFormatVersion)
|
|
|
|
|
{
|
|
|
|
|
//Do something if it's not the same version
|
|
|
|
|
}
|
2014-01-24 10:01:58 +01:00
|
|
|
|
|
2014-01-28 11:27:29 +01:00
|
|
|
|
//Find the header in the returned string.
|
|
|
|
|
while(counter < bufferSize)
|
2014-01-24 10:01:58 +01:00
|
|
|
|
{
|
2014-01-27 13:57:18 +01:00
|
|
|
|
ObjectTypeHeader typeID;
|
2014-01-28 16:15:10 +01:00
|
|
|
|
ParseObject(&buffer[counter], &typeID, sizeof(typeID));
|
2014-01-28 11:27:29 +01:00
|
|
|
|
|
2014-01-27 13:57:18 +01:00
|
|
|
|
switch(typeID.typeID)
|
2014-01-24 10:01:58 +01:00
|
|
|
|
{
|
2014-01-28 11:27:29 +01:00
|
|
|
|
case ObjectType_LevelMetaData:
|
2014-01-28 16:15:10 +01:00
|
|
|
|
ParseLevelMetaData(&buffer[counter], levelHeader, counter);
|
2014-01-28 11:27:29 +01:00
|
|
|
|
return levelHeader;
|
2014-01-24 10:01:58 +01:00
|
|
|
|
break;
|
2014-01-28 11:27:29 +01:00
|
|
|
|
case ObjectType_Dynamic:
|
|
|
|
|
//Do not call parse this object, since we are only interested in the LevelMetaData
|
|
|
|
|
//Only increase the counter size
|
|
|
|
|
counter += sizeof(ObjectHeader);
|
2014-01-24 10:01:58 +01:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
//Couldn't find typeID. FAIL!!!!!!
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 11:27:29 +01:00
|
|
|
|
return levelHeader;
|
2014-01-24 10:01:58 +01:00
|
|
|
|
}
|