示例#1
28
xr_string to_string					(luabind::object const& o)
{
	using namespace luabind;
	if (o.type() == LUA_TSTRING) return object_cast<xr_string>(o);
	lua_State* L = o.lua_state();
	LUABIND_CHECK_STACK(L);

#ifdef BOOST_NO_STRINGSTREAM
	std::strstream s;
#else
	std::stringstream s;
#endif

	if (o.type() == LUA_TNUMBER)
	{
		s << object_cast<float>(o);
		return xr_string(s.str().c_str());
	}

	s << "<" << lua_typename(L, o.type()) << ">";
#ifdef BOOST_NO_STRINGSTREAM
	s << std::ends;
#endif
	return s.str().c_str();
}
void smart_cover::action::add_animation(LPCSTR type, luabind::object const &table)
{	
	VERIFY						( table.type() == LUA_TTABLE );
	luabind::object::iterator I	= table.begin();
	luabind::object::iterator E	= table.end();
	Animations* animations		= xr_new<Animations>( );
	for ( ; I != E; ++I) {
		luabind::object	string	= *I;
		if (string.type() != LUA_TSTRING) {
			VERIFY				( string.type() != LUA_TNIL );
			continue;
		}

		shared_str animation	= luabind::object_cast<LPCSTR>(string);
		VERIFY2					(
			std::find(
				animations->begin(),
				animations->end(),
				animation
			) == 
			animations->end(),
			make_string(
				"duplicated_animation found: %s",
				animation.c_str()
			)
		);
		animations->push_back	( animation );
	}

	m_animations.insert			( std::make_pair( type, animations ) );
}
void parse_table	(luabind::object const &table, LPCSTR identifier, luabind::object &result)
{
	VERIFY2			(table.type() == LUA_TTABLE, "invalid loophole description passed");
	result			= table[identifier];
	VERIFY2			(result.type() != LUA_TNIL, make_string("cannot read table value %s", identifier));
	VERIFY2			(result.type() == LUA_TTABLE, make_string("cannot read table value %s", identifier));
}
示例#4
0
xr_string member_to_string			(luabind::object const& e, LPCSTR function_signature)
{
#if !defined(LUABIND_NO_ERROR_CHECKING3)
    using namespace luabind;
	lua_State* L = e.lua_state();
	LUABIND_CHECK_STACK(L);

	if (e.type() == LUA_TFUNCTION)
	{
		e.pushvalue();
		detail::stack_pop p(L, 1);

		{
			if (lua_getupvalue(L, -1, 3) == 0) return to_string(e);
			detail::stack_pop p2(L, 1);
			if (lua_touserdata(L, -1) != reinterpret_cast<void*>(0x1337)) return to_string(e);
		}

#ifdef BOOST_NO_STRINGSTREAM
		std::strstream s;
#else
		std::stringstream s;
#endif
		{
			lua_getupvalue(L, -1, 2);
			detail::stack_pop p2(L, 1);
		}

		{
			lua_getupvalue(L, -1, 1);
			detail::stack_pop p2(L, 1);
			detail::method_rep* m = static_cast<detail::method_rep*>(lua_touserdata(L, -1));

			for (std::vector<detail::overload_rep>::const_iterator i = m->overloads().begin();
				i != m->overloads().end(); ++i)
			{
				xr_string str;
				i->get_signature(L, str);
				if (i != m->overloads().begin())
					s << "\n";
				s << function_signature << process_signature(str) << ";";
			}
		}
#ifdef BOOST_NO_STRINGSTREAM
		s << std::ends;
#endif
		return s.str().c_str();
	}

    return to_string(e);
#else
    return "";
#endif
}
// TODO Move arguments from constructor to here?
void timer::start(luabind::object& f)
{
  thread = player->create_lua_thread();
#ifndef NDEBUG
  int old_top = lua_gettop(thread);
#endif
  //function.push(thread);
  f.push(f.interpreter());
  lua_xmove(f.interpreter(), thread, 1);
  assert(old_top + 1 == lua_gettop(thread));

  timer_thread = boost::thread(&timer::count_down, this);
}
示例#6
0
void set_voxel_physics_boxes(const luabind::object &node_o,
		const luabind::object &buffer_o, sp_<VoxelRegistry> voxel_reg)
{
	lua_State *L = node_o.interpreter();

	GET_TOLUA_STUFF(node, 1, Node);
	TRY_GET_TOLUA_STUFF(buf, 2, const VectorBuffer);

	log_d(MODULE, "set_voxel_physics_boxes(): node=%p", node);
	log_d(MODULE, "set_voxel_physics_boxes(): buf=%p", buf);

	ss_ data;
	if(buf == nullptr)
		data = lua_tocppstring(L, 2);
	else
		data.assign((const char*)&buf->GetBuffer()[0], buf->GetBuffer().Size());

	lua_getfield(L, LUA_REGISTRYINDEX, "__buildat_app");
	app::App *buildat_app = (app::App*)lua_touserdata(L, -1);
	lua_pop(L, 1);

	up_<SetPhysicsBoxesTask> task(new SetPhysicsBoxesTask(
			node, data, voxel_reg
			));

	auto *thread_pool = buildat_app->get_thread_pool();

	thread_pool->add_task(std::move(task));
}
示例#7
0
文件: menu.cpp 项目: akivajp/levana
  luabind::object menu::build(luabind::object setting)
  {
    using namespace luabind;
    lua_State *L = setting.interpreter();
    const char *title = NULL;
    object m;

    if (!setting || luabind::type(setting) != LUA_TTABLE) { return m; }

    iterator i(setting), end;
    if (setting["title"]) { title = object_cast<const char *>(setting["title"]); }
    else if (setting["t"]) { title = object_cast<const char *>(setting["t"]); }
    else if (setting["label"]) { title = object_cast<const char *>(setting["label"]); }
    else if (type(*i) == LUA_TSTRING)
    {
      title = object_cast<const char *>(*i);
      i++;
    }

    m = globals(L)["lev"]["gui"]["menu"](title);
    for (; i != end; i++)
    {
      if (type(*i) == LUA_TTABLE)
      {
        m["append"](m, *i);
      }
    }
    return m;
  }
position LuaQuestScript::getPosition(const luabind::object &potentialPosition) {
    using namespace luabind;

    if (!potentialPosition.is_valid()) {
        throw std::logic_error("no position found");
    }

    try {
        return object_cast<position>(potentialPosition);
    } catch (cast_failed &e) {
    }

    auto positionType = type(potentialPosition);

    if (positionType == LUA_TTABLE) {
        try {
            int16_t x = object_cast<int16_t>(potentialPosition[1]);
            int16_t y = object_cast<int16_t>(potentialPosition[2]);
            int16_t z = object_cast<int16_t>(potentialPosition[3]);
            return position(x, y, z);
        } catch (cast_failed &e) {
        }
    }

    throw std::logic_error("no position found");
}
示例#9
0
文件: scheduler.cpp 项目: Greyze/xd
void xd::lua::scheduler::start(luabind::object func)
{
	// create a new thread
	lua_State *thread = lua_newthread(m_vm.lua_state());
	// push the function in the new thread, because the function
	// the thread calls must be called in its local environment
	func.push(thread);
	// construct a callable object from it, the value is copied from stack
	luabind::object thread_func(luabind::from_stack(thread, -1));
	// remove the original value from the stack
	lua_pop(thread, 1);
	// set the current thread
	m_thread_stack->threads.push(thread);
	m_current_thread = thread;
	// start the thread
	luabind::object result = luabind::resume_function<luabind::object>(thread_func);
	// if the thread was yielded from lua side, and the return value is a callable function
	int type = luabind::type(result);
	if (type == LUA_TFUNCTION || type == LUA_TTABLE || type == LUA_TUSERDATA) {
		yield(xd::create<detail::callback_task_lua>(result));
	}
	// reset current thread
	m_thread_stack->threads.pop();
	if (m_thread_stack->threads.empty())
		m_current_thread = nullptr;
	else
		m_current_thread = m_thread_stack->threads.top();
}
void LuaProxy::Graphics::__setSimpleSpriteOverride(const std::string & name, const luabind::object & overrideImg, lua_State * L)
{
    HDC mainHdc, maskHdc;
    if (name.find("hardcoded-") == 0)
    {
        // If non of the above applies, then try with the hardcoded ones:
        HardcodedGraphicsItem::GetHDCByName(name, &mainHdc, &maskHdc);
    }
    if (mainHdc == nullptr && maskHdc == nullptr) {
        luaL_error(L, "Failed to get hardcoded image!");
        return;
    }
    
    SMBXMaskedImage* img = SMBXMaskedImage::get(mainHdc, maskHdc);
    if (!overrideImg.is_valid())
    {
        img->UnsetOverride();
        return;
    }

    boost::optional<SMBXMaskedImage*> maskImg = luabind::object_cast_nothrow<SMBXMaskedImage*>(overrideImg);
    if (maskImg != boost::none) {
        img->SetOverride(*maskImg);
        return;
    }

    boost::optional<LuaProxy::Graphics::LuaImageResource*> rgbaImg = luabind::object_cast_nothrow<LuaProxy::Graphics::LuaImageResource*>(overrideImg);
    if (rgbaImg != boost::none) {
        if (*rgbaImg != nullptr && (*rgbaImg)->img) {
            img->SetOverride((*rgbaImg)->img);
        }
        return;
    }
    luaL_error(L, "Invalid input for sprite override!");
}
Fvector parse_fvector (luabind::object const &table, LPCSTR identifier)
{
	VERIFY2			(table.type() == LUA_TTABLE, "invalid loophole description passed");
	luabind::object	result = table[identifier];
	VERIFY2			(result.type() != LUA_TNIL, make_string("cannot read vector value %s", identifier));
	return			(luabind::object_cast<Fvector>(result));
}
示例#12
0
static ComponentTypeId
ComponentFactory_registerComponentType(
    ComponentFactory* self,
    const std::string& name,
    luabind::object cls
) {
    lua_State* L = cls.interpreter();
    auto type = luabind::type(cls);
    if (type != LUA_TUSERDATA) {
        std::string typeName(
            lua_typename(L, type)
        );
        throw std::runtime_error("Argument 2 must be class object, but is: " + typeName);
    }
    ComponentTypeId typeId = self->registerComponentType(
        name,
        [cls] (const StorageContainer& storage) {
            luabind::object classTable = cls;
            luabind::object obj = classTable();
            auto component = std::unique_ptr<Component>(
                luabind::object_cast<Component*>(obj, luabind::adopt(luabind::result))
            );
            component->load(storage);
            return component;
        }
    );
    cls["TYPE_ID"] = typeId;
    return typeId;
}
示例#13
0
void setGeometryIndices( Geometry * pGeom, const luabind::object & indexTable )
{
	if ( indexTable.is_valid() && indexTable.type() == LUA_TTABLE )
	{
		if ( !pGeom->m_indexBuffer ) pGeom->m_indexBuffer.reset( new vector<unsigned short> );
		unsigned int uiNewIndices = 0;
		pGeom->m_indexBuffer->clear();
		for( luabind::object::array_iterator iter = indexTable.abegin();
			iter != indexTable.aend();
			iter++, uiNewIndices++ )
		{
			boost::optional<unsigned short> oindex = luabind::object_cast_nothrow<unsigned short>( *iter );
			if ( oindex )
				pGeom->m_indexBuffer->push_back( *oindex.get() );
		}
		pGeom->m_indexCount = uiNewIndices;
	}
}
示例#14
0
//------------------------------------------------------------------------------
bool ofxLua::checkType(int type, luabind::object& object) {
	
	if(!object.is_valid())
		return false;

	if(luabind::type(object) == type)
		return true;
	return false;
}
示例#15
0
bool ReadScriptDescriptor::_CheckDataType(int32 type, luabind::object& obj_check) {
	int32 object_type = luabind::type(obj_check);

	if (obj_check.is_valid() == false)
		return false;

	// When this type is passed to the function, we don't care what type the object is as long
	// as it was seen to be something
	if (type == LUA_TNIL) {
		return true;
	}

	// Simple type comparison is all that is needed for all non-numeric types
	else if (type == object_type) {
		return true;
	}

	// Because Lua only has a "number" type, we have to do perform a special cast
	// to examine integer versus floating point types
	else if (object_type == LUA_TNUMBER) {
		if (type == INTEGER_TYPE) {
			try {
				luabind::object_cast<int32>(obj_check);
				return true;
			}
			catch (...) {
				return false;
			}
		}
		else if (type == UINTEGER_TYPE) {
			try {
				luabind::object_cast<uint32>(obj_check);
				return true;
			}
			catch (...) {
				return false;
			}
		}
		else if (type == FLOAT_TYPE) {
			try {
				luabind::object_cast<float>(obj_check);
				return true;
			}
			catch (...) {
				return false;
			}
		}
		else {
			return false;
		}
	}

	else {
		return false;
	}
} // bool ReadScriptDescriptor::_CheckDataType(int32 type, luabind::object& obj_check)
示例#16
0
void WaypointList::addFromList(const luabind::object &list) {
    if (list.is_valid()) {
        if (luabind::type(list) == LUA_TTABLE) {
            for (luabind::iterator it(list), end; it != end; ++it) {
                position pos = luabind::object_cast<position>(*it);
                positions.push_back(pos);
            }
        }
    }
}
示例#17
0
void LVL_Npc::lua_setSequence(luabind::object frames)
{
    int ltype = luabind::type(frames);
    if(luabind::type(frames) != LUA_TTABLE)
    {
        luaL_error(frames.interpreter(), "setSequence exptected int-array, got %s", lua_typename(frames.interpreter(), ltype));
        return;
    }
    animator.setSequence(luabind_utils::convArrayTo<int>(frames));
}
示例#18
0
void setGeometryTexture1( Geometry * pGeom, const luabind::object & texture1Table )
{
	if ( texture1Table.is_valid() && texture1Table.type() == LUA_TTABLE )
	{
		if ( !pGeom->m_texture1Buffer ) pGeom->m_texture1Buffer.reset( new vector<float> );
		pGeom->m_texture1Buffer->clear();
		for( luabind::object::array_iterator iter = texture1Table.abegin();
			iter != texture1Table.aend();
			iter++ )
		{
			boost::optional<Point2> opt = luabind::object_cast_nothrow<Point2>( *iter );
			if ( opt )
			{
				Point2 * pt = opt.get();
				pGeom->m_texture1Buffer->push_back( pt->x );
				pGeom->m_texture1Buffer->push_back( pt->y );
			}
		}
	}
}
示例#19
0
void clear_voxel_geometry(const luabind::object &node_o)
{
	lua_State *L = node_o.interpreter();

	GET_TOLUA_STUFF(node, 1, Node);

	log_d(MODULE, "clear_voxel_geometry(): node=%p", node);

	CustomGeometry *cg = node->GetComponent<CustomGeometry>();
	if(cg)
		node->RemoveComponent(cg);
}
示例#20
0
void setGeometryColor( Geometry * pGeom, const luabind::object & colorTable )
{
	if ( colorTable.is_valid() && colorTable.type() == LUA_TTABLE )
	{
		if ( !pGeom->m_colorBuffer ) pGeom->m_colorBuffer.reset( new vector<float> );
		pGeom->m_colorBuffer->clear();
		for( luabind::object::array_iterator iter = colorTable.abegin();
			iter != colorTable.aend();
			iter++ )
		{
			boost::optional<ColorA> opt = luabind::object_cast_nothrow<ColorA>( *iter );
			if ( opt )
			{
				ColorA * pt = opt.get();
				pGeom->m_colorBuffer->push_back( pt->r );
				pGeom->m_colorBuffer->push_back( pt->g );
				pGeom->m_colorBuffer->push_back( pt->b );
				pGeom->m_colorBuffer->push_back( pt->a );
			}
		}
	}
}
示例#21
0
//
// Local Functions
//
void setGeometryVertices( Geometry * pGeom, const luabind::object & vertexTable )
{
	if ( vertexTable.is_valid() && vertexTable.type() == LUA_TTABLE )
	{
		if ( !pGeom->m_vertexBuffer ) pGeom->m_vertexBuffer.reset( new vector<float> );
		pGeom->m_vertexBuffer->clear();
		unsigned uiNewVertices = 0;
		for( luabind::object::array_iterator iter = vertexTable.abegin();
			iter != vertexTable.aend();
			iter++, uiNewVertices++ )
		{
			boost::optional<Point3> opt = luabind::object_cast_nothrow<Point3>( *iter );
			if ( opt )
			{
				Point3 * pt = opt.get();
				pGeom->m_vertexBuffer->push_back( pt->x );
				pGeom->m_vertexBuffer->push_back( pt->y );
				pGeom->m_vertexBuffer->push_back( pt->z );
			}
		}
		pGeom->m_vertexCount = uiNewVertices;
	}
}
示例#22
0
bool ReadScriptDescriptor::RunScriptObject(const luabind::object& object) {

	// Don't log in that case because we might want to run invalid (empty) objects
	// to simplify the caller code.
	if (!object.is_valid())
		return true;

	try {
	    ScriptCallFunction<void>(object);
	} catch(luabind::error e) {
		PRINT_ERROR << "Error while loading script object." << std::endl;
		ScriptManager->HandleLuaError(e);
		return false;
	}
	return true;
}
float parse_float	(
					 luabind::object const &table,
					 LPCSTR identifier,
					 float const &min_threshold = flt_min,
					 float const &max_threshold = flt_max
					 )
{
	VERIFY2			(table.type() == LUA_TTABLE, "invalid loophole description passed");
	luabind::object	lua_result = table[identifier];
	VERIFY2			(lua_result.type() != LUA_TNIL, make_string("cannot read number value %s", identifier));
	VERIFY2			(lua_result.type() == LUA_TNUMBER, make_string("cannot read number value %s", identifier));
	float			result = luabind::object_cast<float>(lua_result);
	VERIFY2			(result >= min_threshold, make_string("invalid read number value %s", identifier));
	VERIFY2			(result <= max_threshold, make_string("invalid number value %s", identifier));
	return			(result);
}
void WaypointList::addFromList(const luabind::object &list) {
    if (list.is_valid()) {
        if (luabind::type(list) == LUA_TTABLE) {
            for (luabind::iterator it(list), end; it != end; ++it) {
                try {
                    position pos = luabind::object_cast<position>(*it);
                    positions.push_back(pos);
                } catch (luabind::cast_failed &e) {
                    std::string script = World::get()->getCurrentScript()->getFileName();
                    std::string err = "Invalid type in parameter list of WaypointList:addFromList in " + script + ":\n";
                    err += "Expected type position\n";
                    Logger::writeError("scripts", err);
                }
            }
        }
    }
}
示例#25
0
void clear_voxel_physics_boxes(const luabind::object &node_o)
{
	lua_State *L = node_o.interpreter();

	GET_TOLUA_STUFF(node, 1, Node);

	log_d(MODULE, "clear_voxel_physics_boxes(): node=%p", node);

	RigidBody *body = node->GetComponent<RigidBody>();
	if(body)
		node->RemoveComponent(body);

	PODVector<CollisionShape*> previous_shapes;
	node->GetComponents<CollisionShape>(previous_shapes);
	for(size_t i = 0; i < previous_shapes.Size(); i++)
		node->RemoveComponent(previous_shapes[i]);
}
示例#26
0
bool NPL::NPLHelper::StringToLuaObject(const char* input, int nLen, luabind::object& output, lua_State* pState)
{
	NPLLex lex;
	LexState* ls = lex.SetInput(input, nLen);
	ls->nestlevel = 0;

	try
	{
		NPLParser::next(ls);  /* read first token */

		if (ls->t.token == '{')
		{
			if (pState == 0)
				pState = output.interpreter();
			luabind::object tabGlobal = luabind::globals(pState);
			luabind::object_index_proxy tabProxy = tabGlobal["__tmp"];

			if (DeserializePureDataBlock(ls, tabProxy))
			{
				NPLParser::testnext(ls, ';');
				if (ls->t.token == NPLLex::TK_EOS)
				{
					output = tabProxy;
					// this is not necessary, the next call will overwrite this. 
					// tabGlobal["__tmp"] = luabind::detail::nil_type();
					return true;
				}
			}
		}
	}
	catch (const char* err)
	{
		OUTPUT_LOG("error: %s in NPLHelper::StringToLuaObject()\n", err);
		return false;
	}
	catch (...)
	{
		OUTPUT_LOG("error: unknown error in NPLHelper::StringToLuaObject()\n");
		return false;
	}
	return false;
}
void LuaProxy::Graphics::__setSpriteOverride(const std::string& t, int index, const luabind::object& overrideImg, lua_State* L)
{
    SMBXMaskedImage* img = getMaskedImage(t, index);
    if (img == nullptr)
    {
        luaL_error(L, "Graphics.sprite.%s[%d] does not exist", t.c_str(), index);
        return;
    }

    if (!overrideImg.is_valid())
    {
        img->UnsetOverride();
        return;
    }

	//I found that 'object_cast_nothrow' sometimes casts between incompatible types without returning 'boost::none'
	//switch order to avoid invalid casting from LIR to SMBXMI
	boost::optional<LuaProxy::Graphics::LuaImageResource*> rgbaImg = luabind::object_cast_nothrow<LuaProxy::Graphics::LuaImageResource*>(overrideImg);
	if (rgbaImg != boost::none) {

		if (*rgbaImg != nullptr && (*rgbaImg)->img) {
			img->SetOverride((*rgbaImg)->img);
		}
		return;
	}


	boost::optional<SMBXMaskedImage*> maskImg = luabind::object_cast_nothrow<SMBXMaskedImage*>(overrideImg);
	if (maskImg != boost::none) {
		img->SetOverride(*maskImg);
		return;
	}

	

    

    

    luaL_error(L, "Cannot set Graphics.sprite.%s[%d], invalid input type", t.c_str(), index);
}
示例#28
0
void set_8bit_voxel_geometry(const luabind::object &node_o,
		int w, int h, int d, const luabind::object &buffer_o,
		sp_<VoxelRegistry> voxel_reg, sp_<AtlasRegistry> atlas_reg)
{
	lua_State *L = node_o.interpreter();

	GET_TOLUA_STUFF(node, 1, Node);
	TRY_GET_TOLUA_STUFF(buf, 5, const VectorBuffer);

	log_d(MODULE, "set_8bit_voxel_geometry(): node=%p", node);
	log_d(MODULE, "set_8bit_voxel_geometry(): buf=%p", buf);

	ss_ data;
	if(buf == nullptr)
		data = lua_tocppstring(L, 5);
	else
		data.assign((const char*)&buf->GetBuffer()[0], buf->GetBuffer().Size());

	if((int)data.size() != w * h * d){
		throw Exception(ss_()+"set_8bit_voxel_geometry(): Data size does not match"
				" with dimensions ("+cs(data.size())+" vs. "+cs(w*h*d)+")");
	}

	lua_getfield(L, LUA_REGISTRYINDEX, "__buildat_app");
	app::App *buildat_app = (app::App*)lua_touserdata(L, -1);
	lua_pop(L, 1);
	Context *context = buildat_app->get_scene()->GetContext();

	CustomGeometry *cg = node->GetOrCreateComponent<CustomGeometry>(LOCAL);

	interface::mesh::set_8bit_voxel_geometry(cg, context, w, h, d, data,
			voxel_reg.get(), atlas_reg.get());

	cg->SetOccluder(true);
	cg->SetCastShadows(true);
}
示例#29
0
文件: menu.cpp 项目: akivajp/levana
  luabind::object menubar::build(luabind::object setting)
  {
    using namespace luabind;
    lua_State *L = setting.interpreter();
    object mb;

    if (!setting || type(setting) != LUA_TTABLE) { return mb; }
    mb = globals(L)["lev"]["gui"]["menubar"]();

    for (iterator i(setting), end; i != end; i++)
    {
      object m, name;
      const char *label1 = NULL, *label2 = NULL;

      iterator j(*i);
      if (j == end || type(*j) != LUA_TSTRING) { continue; }
      label1 = object_cast<const char *>(*j++);
      if (j != end && type(*j) == LUA_TSTRING)
      {
        label2 = object_cast<const char *>(*j++);
      }
      if (j != end && type(*j) == LUA_TSTRING) { name = *j++; }
      else if ((*i)["id"]) { name = (*i)["id"]; }
      else if ((*i)["name"]) { name = (*i)["name"]; }
      m = globals(L)["lev"]["gui"]["menu"](label2);
      for (; j != end; j++)
      {
        if (type(*j) == LUA_TTABLE)
        {
          m["append"](m, *j);
        }
      }
      mb["append"](mb, m, label1, name);
    }
    return mb;
  }
示例#30
0
	SynchronizedRunBuffer::SynchronizedRunBuffer(luabind::object const& delegate) :
		_init(false) {
		_state = delegate.interpreter();
	}