Something went wrong. Try again.
Massive multiplayer game aiming to create a collaborative musical environment.
Something went wrong. Try again.
BoomBox Codebase External CEGUI-0.6.2 ScriptingModules CEGUILua LuaScriptModule src CEGUILuaFunctor.cpp
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458/*********************************************************************** filename: CEGUILuaFunctor.cpp created: Thu Jan 26 2006 author: Tomas Lindquist Olsen <tomas@famolsen.dk>
purpose: Implementation for LuaFunctor class*************************************************************************//*************************************************************************** * Copyright (C) 2004 - 2008 Paul D Turner & The CEGUI Development Team * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. ***************************************************************************/#include "CEGUILuaFunctor.h"#include "CEGUILogger.h"#include "CEGUIExceptions.h"#include "CEGUIPropertyHelper.h"#include "CEGUILua.h"
// include Lua libs and tolua++extern "C" {#include "lua.h"#include "lualib.h"#include "lauxlib.h"}
#include "tolua++.h"
// Start of CEGUI namespace sectionnamespace CEGUI{
/************************************************************************* Constructor*************************************************************************/LuaFunctor::LuaFunctor(lua_State* state, int func, int selfIndex) : L(state), index(func), self(selfIndex), needs_lookup(false), d_errFuncIndex(LUA_NOREF), d_ourErrFuncIndex(false){ // TODO: This would perhaps be better done another way, to avoid the // TODO: interdependence. LuaScriptModule* sm = static_cast<LuaScriptModule*>(System::getSingleton().getScriptingModule());
if (sm) { d_errFuncName = sm->getActivePCallErrorHandlerString(); d_errFuncIndex = sm->getActivePCallErrorHandlerReference(); }}
/************************************************************************* Constructor*************************************************************************/LuaFunctor::LuaFunctor(lua_State* state, const String& func, int selfIndex) : L(state), index(LUA_NOREF), self(selfIndex), needs_lookup(true), function_name(func), d_errFuncIndex(LUA_NOREF), d_ourErrFuncIndex(false){ // TODO: This would perhaps be better done another way, to avoid the // TODO: interdependence. LuaScriptModule* sm = static_cast<LuaScriptModule*>(System::getSingleton().getScriptingModule());
if (sm) { d_errFuncName = sm->getActivePCallErrorHandlerString(); d_errFuncIndex = sm->getActivePCallErrorHandlerReference(); }}
/************************************************************************* Constructor*************************************************************************/LuaFunctor::LuaFunctor(const LuaFunctor& cp) : L(cp.L), index(cp.index), self(cp.self), needs_lookup(cp.needs_lookup), function_name(cp.function_name), d_errFuncName(cp.d_errFuncName), d_errFuncIndex(cp.d_errFuncIndex), d_ourErrFuncIndex(cp.d_ourErrFuncIndex){}
/************************************************************************* Destructor*************************************************************************/LuaFunctor::~LuaFunctor(){ if (self != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, self);
if (index != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, index);
if (d_ourErrFuncIndex && (d_errFuncIndex != LUA_NOREF) && !d_errFuncName.empty()) luaL_unref(L, LUA_REGISTRYINDEX, d_errFuncIndex);}
/************************************************************************* Call operator*************************************************************************/bool LuaFunctor::operator()(const EventArgs& args) const{ // named error handler needs binding? if ((d_errFuncIndex == LUA_NOREF) && !d_errFuncName.empty()) { pushNamedFunction(L, d_errFuncName); d_errFuncIndex = luaL_ref(L, LUA_REGISTRYINDEX); d_ourErrFuncIndex = true; }
// is this a late binding? if (needs_lookup) { pushNamedFunction(L, function_name); // reference function index = luaL_ref(L, LUA_REGISTRYINDEX); needs_lookup = false; CEGUI_LOGINSANE("Late binding of callback '"+function_name+"' performed"); function_name.clear(); } // if (needs_lookup)
// put error handler on stack if we're using such a thing int err_idx = 0; if (d_errFuncIndex != LUA_NOREF) { lua_rawgeti(L, LUA_REGISTRYINDEX, d_errFuncIndex); err_idx = lua_gettop(L); }
ScriptWindowHelper* helper = 0; //Set a global for this window if(args.d_hasWindow) { WindowEventArgs& we = (WindowEventArgs&)args; helper = new ScriptWindowHelper(we.window); tolua_pushusertype(L,(void*)helper,"CEGUI::ScriptWindowHelper"); lua_setglobal(L,"this"); }
// retrieve function lua_rawgeti(L, LUA_REGISTRYINDEX, index);
// possibly self as well? int nargs = 1; if (self != LUA_NOREF) { lua_rawgeti(L, LUA_REGISTRYINDEX, self); ++nargs; }
// push EventArgs parameter tolua_pushusertype(L, (void*)&args, "const CEGUI::EventArgs");
// call it int error = lua_pcall(L, nargs, 1, err_idx);
// handle errors if (error) { String errStr(lua_tostring(L, -1)); lua_pop(L, 1); if(helper) { delete helper; helper = 0; } throw ScriptException("Unable to call Lua event handler:\n\n"+errStr+"\n"); } // if (error)
// retrieve result bool ret = lua_isboolean(L, -1) ? lua_toboolean(L, -1 ) : true; lua_pop(L, 1);
if(helper) { delete helper; helper = 0; }
return ret;}
/************************************************************************* Pushes a named function on the stack*************************************************************************/void LuaFunctor::pushNamedFunction(lua_State* L, const String& handler_name){ int top = lua_gettop(L);
// do we have any dots in the handler name? if so we grab the function as a table field String::size_type i = handler_name.find_first_of((utf32)'.'); if (i!=String::npos) { // split the rest of the string up in parts seperated by '.' // TODO: count the dots and size the vector accordingly from the beginning. std::vector<String> parts; String::size_type start = 0; do { parts.push_back(handler_name.substr(start,i-start)); start = i+1; i = handler_name.find_first_of((utf32)'.',start); } while(i!=String::npos);
// add last part parts.push_back(handler_name.substr(start));
// first part is the global lua_getglobal(L, parts[0].c_str()); if (!lua_istable(L,-1)) { lua_settop(L,top); throw ScriptException("Unable to get the Lua event handler: '"+handler_name+"' as first part is not a table"); }
// if there is more than two parts, we have more tables to go through std::vector<String>::size_type visz = parts.size(); if (visz-- > 2) // avoid subtracting one later on { // go through all the remaining parts to (hopefully) have a valid Lua function in the end std::vector<String>::size_type vi = 1; while (vi<visz) { // push key, and get the next table lua_pushstring(L,parts[vi].c_str()); lua_gettable(L,-2); if (!lua_istable(L,-1)) { lua_settop(L,top); throw ScriptException("Unable to get the Lua event handler: '"+handler_name+"' as part #"+PropertyHelper::uintToString(uint(vi+1))+" ("+parts[vi]+") is not a table"); } // get rid of the last table and move on lua_remove(L,-2); vi++; } }
// now we are ready to get the function to call ... phew :) lua_pushstring(L,parts[visz].c_str()); lua_gettable(L,-2); lua_remove(L,-2); // get rid of the table } // just a regular global function else { lua_getglobal(L, handler_name.c_str()); }
// is it a function if (!lua_isfunction(L,-1)) { lua_settop(L,top); throw ScriptException("The Lua event handler: '"+handler_name+"' does not represent a Lua function"); }}
//----------------------------------------------------------------------------//LuaFunctor::LuaFunctor(lua_State* state, const int func, const int selfIndex, const String& error_handler) : // State initialisation L(state), index(func), self(selfIndex), needs_lookup(false), d_errFuncName(error_handler), d_errFuncIndex(LUA_NOREF), d_ourErrFuncIndex(false){}
//----------------------------------------------------------------------------//LuaFunctor::LuaFunctor(lua_State* state, const String& func, const int selfIndex, const String& error_handler) : // State initialisation L(state), index(LUA_NOREF), self(selfIndex), needs_lookup(true), function_name(func), d_errFuncName(error_handler), d_errFuncIndex(LUA_NOREF), d_ourErrFuncIndex(false){}
//----------------------------------------------------------------------------//LuaFunctor::LuaFunctor(lua_State* state, const int func, const int selfIndex, const int error_handler) : // State initialisation L(state), index(func), self(selfIndex), needs_lookup(false), d_errFuncIndex(error_handler), d_ourErrFuncIndex(false){}
//----------------------------------------------------------------------------//LuaFunctor::LuaFunctor(lua_State* state, const String& func, const int selfIndex, const int error_handler) : // State initialisation L(state), index(LUA_NOREF), self(selfIndex), needs_lookup(true), function_name(func), d_errFuncIndex(error_handler), d_ourErrFuncIndex(false){}
//----------------------------------------------------------------------------//Event::Connection LuaFunctor::SubscribeEvent(EventSet* self, const String& event_name, const int funcIndex, const int selfIndex, const int error_handler, lua_State* L){ // deal with error handler function int err_idx = LUA_NOREF; String err_str; if (error_handler != LUA_NOREF) { int err_handler_type = lua_type(L,-1); switch (err_handler_type) { case LUA_TFUNCTION: // reference function err_idx = luaL_ref(L, LUA_REGISTRYINDEX); break; case LUA_TSTRING: err_str = lua_tostring(L, -1); lua_pop(L, 1); break; default: luaL_error(L, "bad error handler function passed to subscribe " "function. must be a real function, or a string for " "late binding"); break; }
}
// should we pass a self to the callback? int thisIndex = LUA_NOREF; if (selfIndex != LUA_NOREF) { // reference self thisIndex = luaL_ref(L, LUA_REGISTRYINDEX); }
// do the real subscription int type = lua_type(L,-1); Event::Connection con; if (type == LUA_TFUNCTION) { // reference function int index = luaL_ref(L, LUA_REGISTRYINDEX);
if (err_idx != LUA_NOREF) { LuaFunctor functor(L, index, thisIndex, err_idx); con = self->subscribeEvent(String(event_name), Event::Subscriber(functor)); functor.invalidateLuaRefs(); } else if (!err_str.empty()) { LuaFunctor functor(L, index, thisIndex, err_str); con = self->subscribeEvent(String(event_name), Event::Subscriber(functor)); functor.invalidateLuaRefs(); } else { LuaFunctor functor(L, index, thisIndex); con = self->subscribeEvent(String(event_name), Event::Subscriber(functor)); functor.invalidateLuaRefs(); } } else if (type == LUA_TSTRING) { const char* str = lua_tostring(L, -1);
if (err_idx != LUA_NOREF) { LuaFunctor functor(L, String(str), thisIndex, err_idx); con = self->subscribeEvent(String(event_name), Event::Subscriber(functor)); functor.invalidateLuaRefs(); } else if (!err_str.empty()) { LuaFunctor functor(L, String(str), thisIndex, err_str); con = self->subscribeEvent(String(event_name), Event::Subscriber(functor)); functor.invalidateLuaRefs(); } else { LuaFunctor functor(L, String(str), thisIndex); con = self->subscribeEvent(String(event_name), Event::Subscriber(functor)); functor.invalidateLuaRefs(); } } else { luaL_error(L, "bad function passed to subscribe function. must be a " "real function, or a string for late binding"); }
// return the event connection return con;}
//----------------------------------------------------------------------------//void LuaFunctor::invalidateLuaRefs(){ index = LUA_NOREF; self = LUA_NOREF; d_errFuncIndex = LUA_NOREF;}
//----------------------------------------------------------------------------//
} // namespace CEGUI