LuaAI: fixed bug #19782 and made the code more safe.

This commit is contained in:
Dmitry Kovalenko 2012-06-21 11:49:40 +00:00
parent c578a27fba
commit 552b6b0c7e
4 changed files with 33 additions and 9 deletions

View file

@ -45,6 +45,10 @@ engine::~engine()
{
}
bool engine::is_ok() const
{
return true;
}
void engine::parse_aspect_from_config( readonly_context &context, const config &cfg, const std::string &id, std::back_insert_iterator< std::vector< aspect_ptr > > b )
{

View file

@ -43,7 +43,8 @@ public:
virtual ~engine();
virtual bool is_ok() const;
static void parse_aspect_from_config( readonly_context &context, const config &cfg, const std::string &id, std::back_insert_iterator<std::vector< aspect_ptr > > b );
@ -151,7 +152,11 @@ public:
}
virtual engine_ptr get_new_instance( readonly_context &ai, const config &cfg ){
return engine_ptr(new ENGINE(ai,cfg));
engine_ptr e = engine_ptr(new ENGINE(ai,cfg));
if (!e->is_ok()) {
return engine_ptr();
}
return e;
}
virtual engine_ptr get_new_instance( readonly_context &ai, const std::string& name ){

View file

@ -75,8 +75,10 @@ public:
} else {
return BAD_SCORE;
}
return *(l_obj->get());
boost::shared_ptr<int> result = l_obj->get();
return result ? *result : 0.0;
}
@ -239,7 +241,10 @@ engine_lua::engine_lua( readonly_context &context, const config &cfg )
{
name_ = "lua";
config data(cfg.child_or_empty("data"));
lua_ai_context_->set_persistent_data(data);
if (lua_ai_context_) { // The context might be NULL if the config contains errors
lua_ai_context_->set_persistent_data(data);
}
}
std::string engine_lua::get_engine_code(const config &cfg) const
@ -256,6 +261,12 @@ engine_lua::~engine_lua()
{
}
bool engine_lua::is_ok() const
{
return lua_ai_context_ ? true : false;
}
void engine_lua::do_parse_candidate_action_from_config( rca_context &context, const config &cfg, std::back_insert_iterator<std::vector< candidate_action_ptr > > b ){
if (!cfg) {
return;
@ -352,10 +363,12 @@ config engine_lua::to_config() const
cfg["id"] = get_id();
cfg["code"] = this->code_;
config data = config();
lua_ai_context_->get_persistent_data(data);
cfg.add_child("data") = data;
if (lua_ai_context_) {
config data = config();
lua_ai_context_->get_persistent_data(data);
cfg.add_child("data") = data;
}
return cfg;
}

View file

@ -33,6 +33,8 @@ public:
engine_lua( readonly_context &context, const config &cfg );
virtual ~engine_lua();
bool is_ok() const;
/**
* Taka a config (with engine=lua in it)