From 0d1b97510d9e112cce8c1967332ed1c481daea8f Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Tue, 14 Jul 2015 18:20:05 +0200 Subject: [PATCH] Add exception handling when creating new objects on level loading --- src/graphics/model/model_manager.cpp | 7 ++++++- src/object/robotmain.cpp | 30 ++++++++++++++++++---------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/graphics/model/model_manager.cpp b/src/graphics/model/model_manager.cpp index 0a78438a..66912c5b 100644 --- a/src/graphics/model/model_manager.cpp +++ b/src/graphics/model/model_manager.cpp @@ -19,6 +19,7 @@ #include "graphics/model/model_manager.h" +#include "common/logger.h" #include "common/resources/inputstream.h" #include "graphics/model/model_input.h" @@ -32,8 +33,12 @@ CModel& CModelManager::GetModel(const std::string& modelName) if (it != m_models.end()) return it->second; + std::string modelFile = "models-new/" + modelName + ".txt"; + + GetLogger()->Debug("Loading new model: %s\n", modelFile.c_str()); + CInputStream stream; - stream.open("models-new/" + modelName + ".txt"); + stream.open(modelFile.c_str()); if (!stream.is_open()) throw CModelIOException(std::string("Could not open file '") + modelName + "'"); diff --git a/src/object/robotmain.cpp b/src/object/robotmain.cpp index 4f8a2e4c..a9acc5a2 100644 --- a/src/object/robotmain.cpp +++ b/src/object/robotmain.cpp @@ -64,6 +64,7 @@ #include "object/motion/motionhuman.h" #include "object/motion/motiontoto.h" #include "object/object.h" +#include "object/object_create_exception.h" #include "object/object_manager.h" #include "object/scene_conditions.h" #include "object/task/task.h" @@ -3523,16 +3524,25 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject) Math::Vector pos = line->GetParam("pos")->AsPoint()*g_unit; float dirAngle = line->GetParam("dir")->AsFloat(0.0f)*Math::PI; bool trainer; - CObject* obj = m_objMan->CreateObject( - pos, dirAngle, - type, - line->GetParam("power")->AsFloat(1.0f), - line->GetParam("z")->AsFloat(1.0f), - line->GetParam("h")->AsFloat(0.0f), - trainer = line->GetParam("trainer")->AsBool(false), - line->GetParam("toy")->AsBool(false), - line->GetParam("option")->AsInt(0) - ); + CObject* obj = nullptr; + try + { + obj = m_objMan->CreateObject( + pos, dirAngle, + type, + line->GetParam("power")->AsFloat(1.0f), + line->GetParam("z")->AsFloat(1.0f), + line->GetParam("h")->AsFloat(0.0f), + trainer = line->GetParam("trainer")->AsBool(false), + line->GetParam("toy")->AsBool(false), + line->GetParam("option")->AsInt(0) + ); + } + catch (const CObjectCreateException& e) + { + GetLogger()->Error("Error loading level object: %s\n", e.what()); + continue; + } if (m_fixScene && type == OBJECT_HUMAN) {