From c0f1858c69b6b8f5567c951bc2c5c417e4df909c Mon Sep 17 00:00:00 2001 From: William Breathitt Gray Date: Wed, 24 Feb 2016 10:30:11 -0500 Subject: [PATCH] Add sanity checks for level file values --- src/World.cpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/World.cpp b/src/World.cpp index b450a84..efae588 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -135,6 +135,9 @@ void World::loadFiles(const std::string& mapFilePath){ std::getline(mapFile, buffer); unsigned numCharacterPoses; std::istringstream(buffer) >> numCharacterPoses; + if(!numCharacterPoses){ + throw std::runtime_error("Number of character poses sets must be greater than 0"); + } ImageSystem imgsys; std::vector>> characterPoses; for(unsigned i = 0; i < numCharacterPoses; i++){ @@ -143,6 +146,9 @@ void World::loadFiles(const std::string& mapFilePath){ std::getline(mapFile, buffer); unsigned numPoses; std::istringstream(buffer) >> numPoses; + if(numPoses < 2){ + throw std::runtime_error("Number of poses in a set must be greater than 1"); + } for(unsigned j = 0; j < numPoses; j++){ std::string poseFilePath; std::getline(mapFile, poseFilePath); @@ -156,6 +162,9 @@ void World::loadFiles(const std::string& mapFilePath){ std::getline(mapFile, buffer); unsigned numWeapons; std::istringstream(buffer) >> numWeapons; + if(!numWeapons){ + throw std::runtime_error("Number of weapons must be greater than 0"); + } std::vector> weapons; for(unsigned i = 0; i < numWeapons; i++){ std::string weaponFilePath; @@ -167,6 +176,9 @@ void World::loadFiles(const std::string& mapFilePath){ std::getline(mapFile, buffer); unsigned numTracers; std::istringstream(buffer) >> numTracers; + if(!numTracers){ + throw std::runtime_error("Number of tracers must be greater than 0"); + } std::vector> tracers; for(unsigned i = 0; i < numTracers; i++){ std::string tracerFilePath; @@ -184,6 +196,9 @@ void World::loadFiles(const std::string& mapFilePath){ std::getline(mapFile, buffer); unsigned numEnemies; std::istringstream(buffer) >> numEnemies; + if(!numEnemies){ + throw std::runtime_error("Number of enemies must be greater than 0"); + } for(unsigned i = 0; i < numEnemies; i++){ std::getline(mapFile, buffer); unsigned poses; @@ -191,6 +206,10 @@ void World::loadFiles(const std::string& mapFilePath){ unsigned tracer; std::istringstream(buffer) >> x >> y >> poses >> weapon >> tracer; + poses = (poses >= characterPoses.size()) ? 0 : poses; + weapon = (weapon >= weapons.size()) ? 0 : weapon; + tracer = (tracer >= tracers.size()) ? 0 : tracer; + enemies.emplace_back(new Enemy(x, y, characterPoses[poses], weapons[weapon], tracers[tracer])); } @@ -224,7 +243,7 @@ void World::loadFiles(const std::string& mapFilePath){ for(x = 0; x < width; x++){ unsigned id; iss >> id; - if(id){ + if(id && id <= tile_textures.size()){ tiles.emplace_back(new Entity(x*32, y*32, tile_textures[id-1], 0, id >= solidTiles)); } }